Skip to content

Commit

Permalink
Refactor: ban Array#push() (#792)
Browse files Browse the repository at this point in the history
  • Loading branch information
emmercm authored Oct 27, 2023
1 parent d034726 commit c83d577
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 15 deletions.
5 changes: 4 additions & 1 deletion .eslintrc
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,10 @@
"unicorn/empty-brace-spaces": "error",

// ***** Arrays *****
"no-restricted-syntax": ["error", {
"selector": "CallExpression[callee.property.name='push'] > SpreadElement",
"message": "Array#push(...Array) can cause 'call stack size exceeded' runtime errors when pushing many values, prefer 'Array = [...Array, ...Array]'"
}],
"unicorn/no-new-array": "error",
// TypeScript doesn't do a good job of reporting indexed values as potentially undefined, such as `[1,2,3][999]`
"unicorn/prefer-at": "error",
Expand Down Expand Up @@ -144,7 +148,6 @@
// ***** airbnb-base, airbnb-typescript/base *****
"no-await-in-loop": "off",
"no-bitwise": "off",
"no-restricted-syntax": "off",

// ***** plugin:jest/recommended *****
// A lot of test files define their own expect functions
Expand Down
11 changes: 7 additions & 4 deletions src/igir.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ export default class Igir {
}

const datsToWrittenFiles = new Map<DAT, File[]>();
const romOutputDirs: string[] = [];
const movedRomsToDelete: File[] = [];
let romOutputDirs: string[] = [];
let movedRomsToDelete: File[] = [];
const datsStatuses: DATStatus[] = [];

// Process every DAT
Expand All @@ -106,12 +106,15 @@ export default class Igir {
indexedRoms,
patches,
);
romOutputDirs.push(...this.getCandidateOutputDirs(filteredDat, parentsToCandidates));
romOutputDirs = [
...romOutputDirs,
...this.getCandidateOutputDirs(filteredDat, parentsToCandidates),
];

// Write the output files
const movedRoms = await new CandidateWriter(this.options, progressBar)
.write(filteredDat, parentsToCandidates);
movedRomsToDelete.push(...movedRoms);
movedRomsToDelete = [...movedRomsToDelete, ...movedRoms];
const writtenRoms = [...parentsToCandidates.values()]
.flat()
.flatMap((releaseCandidate) => releaseCandidate
Expand Down
4 changes: 2 additions & 2 deletions src/modules/candidatePatchGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ export default class CandidatePatchGenerator extends Module {
// ReleaseCandidate for each Game, so remember which Games we've seen for this Parent
const seenGames = new Set<Game>();

const parentsAndReleaseCandidates: [Parent, ReleaseCandidate[]][] = [
let parentsAndReleaseCandidates: [Parent, ReleaseCandidate[]][] = [
[parent, releaseCandidates],
];

Expand All @@ -99,7 +99,7 @@ export default class CandidatePatchGenerator extends Module {
seenGames.add(releaseCandidate.getGame());

if (patchedParents) {
parentsAndReleaseCandidates.push(...patchedParents);
parentsAndReleaseCandidates = [...parentsAndReleaseCandidates, ...patchedParents];
}
}

Expand Down
8 changes: 3 additions & 5 deletions src/modules/directoryCleaner.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,10 +119,8 @@ export default class DirectoryCleaner extends Module {
}

// Otherwise, recurse and look for empty subdirectories
const emptyDirs: string[] = [];
await Promise.all(subDirs.map(async (subDir) => {
emptyDirs.push(...await this.getEmptyDirs(subDir));
}));
return emptyDirs;
return (await Promise.all(
subDirs.map(async (subDir) => this.getEmptyDirs(subDir)),
)).flat();
}
}
6 changes: 3 additions & 3 deletions src/polyfill/fsPoly.ts
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ export default class FsPoly {
}

static async walk(pathLike: PathLike, callback?: FsWalkCallback): Promise<string[]> {
const output = [];
let output: string[] = [];

let files: string[];
try {
Expand All @@ -314,12 +314,12 @@ export default class FsPoly {
const fullPath = path.join(pathLike.toString(), file);
if (await this.isDirectory(fullPath)) {
const subDirFiles = await this.walk(fullPath);
output.push(...subDirFiles);
output = [...output, ...subDirFiles];
if (callback) {
callback(subDirFiles.length - 1);
}
} else {
output.push(fullPath);
output = [...output, fullPath];
}
}

Expand Down

0 comments on commit c83d577

Please sign in to comment.