Skip to content

Commit

Permalink
Use spread push
Browse files Browse the repository at this point in the history
  • Loading branch information
fedeci committed Aug 5, 2021
1 parent fcfed84 commit 49e3e71
Show file tree
Hide file tree
Showing 15 changed files with 40 additions and 50 deletions.
2 changes: 1 addition & 1 deletion packages/babel-cli/src/babel-external-helpers.ts
Expand Up @@ -11,7 +11,7 @@ function collect(
const values = value.split(",");

if (previousValue) {
Array.prototype.push.apply(previousValue, values);
previousValue.push(...values);
return previousValue;
}
return values;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-cli/src/babel/options.ts
Expand Up @@ -192,7 +192,7 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
let filenames = commander.args.reduce(function (globbed, input) {
let files = glob.sync(input);
if (!files.length) files = [input];
Array.prototype.push.apply(globbed, files);
globbed.push(...files);
return globbed;
}, []);

Expand Down Expand Up @@ -355,7 +355,7 @@ function collect(
const values = value.split(",");

if (previousValue) {
Array.prototype.push.apply(previousValue, values);
previousValue.push(...values);
return previousValue;
}
return values;
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-helper-define-map/src/index.js
Expand Up @@ -42,9 +42,8 @@ export function push(
if (node.decorators) {
const decorators = (map.decorators =
map.decorators || t.arrayExpression([]));
Array.prototype.push.apply(
decorators.elements,
node.decorators.map(dec => dec.expression).reverse(),
decorators.elements.push(
...node.decorators.map(dec => dec.expression).reverse(),
);
}

Expand Down
2 changes: 1 addition & 1 deletion packages/babel-node/src/_babel-node.js
Expand Up @@ -22,7 +22,7 @@ function collect(value, previousValue): Array<string> {
const values = value.split(",");

if (previousValue) {
Array.prototype.push.apply(previousValue, values);
previousValue.push(...values);
return previousValue;
}
return values;
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-node/src/babel-node.js
Expand Up @@ -69,7 +69,7 @@ getV8Flags(async function (err, v8Flags) {

// append arguments passed after --
if (argSeparator > -1) {
Array.prototype.push.apply(args, userArgs);
args.push(...userArgs);
}

try {
Expand Down
Expand Up @@ -47,7 +47,7 @@ export default declare(api => {
}

// push the rest of the original loop body onto our new body
Array.prototype.push.apply(block.body, node.body.body);
block.body.push(...node.body.body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
Expand Down
13 changes: 6 additions & 7 deletions packages/babel-plugin-transform-classes/src/transformClass.ts
Expand Up @@ -136,10 +136,8 @@ export default function transformClass(

if (classState.userConstructor) {
const { constructorBody, userConstructor, construct } = classState;
Array.prototype.push.apply(
constructorBody.body,
userConstructor.body.body,
);

constructorBody.body.push(...userConstructor.body.body);
t.inherits(construct, userConstructor);
t.inherits(constructorBody, userConstructor.body);
}
Expand Down Expand Up @@ -713,9 +711,10 @@ export default function transformClass(
);
}

Array.prototype.push.apply(
body,
classState.staticPropBody.map(fn => fn(t.cloneNode(classState.classRef))),
body.push(
...classState.staticPropBody.map(fn =>
fn(t.cloneNode(classState.classRef)),
),
);

const isStrict = path.isInStrictMode();
Expand Down
Expand Up @@ -24,7 +24,7 @@ export default function transformWithoutHelper(loose, path, state) {
}

// push the rest of the original loop body onto our new body
Array.prototype.push.apply(block.body, node.body.body);
block.body.push(...node.body.body);

t.inherits(loop, node);
t.inherits(loop.body, node.body);
Expand Down
10 changes: 4 additions & 6 deletions packages/babel-plugin-transform-modules-systemjs/src/index.js
Expand Up @@ -540,9 +540,8 @@ export default declare((api, options) => {
}
}

Array.prototype.push.apply(
setterBody,
constructExportCall(
setterBody.push(
...constructExportCall(
path,
t.identifier(exportIdent),
exportNames,
Expand Down Expand Up @@ -590,9 +589,8 @@ export default declare((api, options) => {
}

if (exportNames.length) {
Array.prototype.push.apply(
beforeBody,
constructExportCall(
beforeBody.push(
...constructExportCall(
path,
t.identifier(exportIdent),
exportNames,
Expand Down
5 changes: 1 addition & 4 deletions packages/babel-plugin-transform-parameters/src/rest.js
Expand Up @@ -305,10 +305,7 @@ export default function convertFunctionRest(path) {
return true;
}

Array.prototype.push.apply(
state.references,
state.candidates.map(({ path }) => path),
);
state.references.push(...state.candidates.map(({ path }) => path));

const start = t.numericLiteral(paramsCount);
const key = scope.generateUidIdentifier("key");
Expand Down
33 changes: 15 additions & 18 deletions packages/babel-traverse/src/path/family.ts
Expand Up @@ -47,7 +47,7 @@ function addCompletionRecords(
context: CompletionContext,
): Completion[] {
if (path) {
Array.prototype.push.apply(records, _getCompletionRecords(path, context));
records.push(..._getCompletionRecords(path, context));
}
return records;
}
Expand Down Expand Up @@ -75,9 +75,9 @@ function completionRecordForSwitch(
if (normalCompletions.length) {
lastNormalCompletions = normalCompletions;
}
Array.prototype.push.apply(records, breakCompletions);
records.push(...breakCompletions);
}
Array.prototype.push.apply(records, lastNormalCompletions);
records.push(...lastNormalCompletions);
return records;
}

Expand Down Expand Up @@ -157,11 +157,11 @@ function getStatementListCompletion(
// When we have seen normal completions from the last statement
// it is safe to stop populating break and mark normal completions as break
normalCompletionToBreak(lastNormalCompletions);
Array.prototype.push.apply(completions, lastNormalCompletions);
completions.push(...lastNormalCompletions);
// Declarations have empty completion record, however they can not be nested
// directly in return statement, i.e. `return (var a = 1)` is invalid.
if (lastNormalCompletions.some(c => c.path.isDeclaration())) {
Array.prototype.push.apply(completions, statementCompletions);
completions.push(...statementCompletions);
replaceBreakStatementInBreakCompletion(
statementCompletions,
/* reachable */ true,
Expand All @@ -172,7 +172,7 @@ function getStatementListCompletion(
/* reachable */ false,
);
} else {
Array.prototype.push.apply(completions, statementCompletions);
completions.push(...statementCompletions);
if (!context.shouldPopulateBreak) {
replaceBreakStatementInBreakCompletion(
statementCompletions,
Expand All @@ -183,11 +183,10 @@ function getStatementListCompletion(
break;
}
if (i === paths.length - 1) {
Array.prototype.push.apply(completions, statementCompletions);
completions.push(...statementCompletions);
} else {
Array.prototype.push.apply(
completions,
statementCompletions.filter(c => c.type === BREAK_COMPLETION),
completions.push(
...statementCompletions.filter(c => c.type === BREAK_COMPLETION),
);
lastNormalCompletions = statementCompletions.filter(
c => c.type === NORMAL_COMPLETION,
Expand All @@ -205,7 +204,7 @@ function getStatementListCompletion(
(pathCompletions.length === 1 &&
!pathCompletions[0].path.isVariableDeclaration())
) {
Array.prototype.push.apply(completions, pathCompletions);
completions.push(...pathCompletions);
break;
}
}
Expand All @@ -230,10 +229,9 @@ function _getCompletionRecords(
// @ts-expect-error(flow->ts): todo
records = addCompletionRecords(path.get("body"), records, context);
} else if (path.isProgram() || path.isBlockStatement()) {
Array.prototype.push.apply(
records,
records.push(
// @ts-expect-error(flow->ts): todo
getStatementListCompletion(path.get("body"), context),
...getStatementListCompletion(path.get("body"), context),
);
} else if (path.isFunction()) {
return _getCompletionRecords(path.get("body"), context);
Expand All @@ -245,9 +243,8 @@ function _getCompletionRecords(
} else if (path.isSwitchStatement()) {
records = completionRecordForSwitch(path.get("cases"), records, context);
} else if (path.isSwitchCase()) {
Array.prototype.push.apply(
records,
getStatementListCompletion(path.get("consequent"), {
records.push(
...getStatementListCompletion(path.get("consequent"), {
canHaveBreak: true,
shouldPopulateBreak: false,
inCaseClause: true,
Expand Down Expand Up @@ -543,7 +540,7 @@ export function getBindingIdentifierPaths(
const key = keys[i];
const child = id.get(key);
if (Array.isArray(child)) {
Array.prototype.push.apply(search, child);
search.push(...child);
} else if (child.node) {
search.push(child);
}
Expand Down
Expand Up @@ -84,7 +84,7 @@ function getTypeAnnotationBindingConstantViolations(binding, path, name) {
}*/

// add back on function constant violations since we can't track calls
Array.prototype.push.apply(constantViolations, functionConstantViolations);
constantViolations.push(...functionConstantViolations);

// push on inferred types of violated paths
for (const violation of constantViolations) {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-types/src/comments/addComments.ts
Expand Up @@ -16,7 +16,7 @@ export default function addComments<T extends t.Node>(
if (type === "leading") {
node[key] = comments.concat(node[key]);
} else {
Array.prototype.push.apply(node[key], comments);
node[key].push(...comments);
}
} else {
node[key] = comments;
Expand Down
Expand Up @@ -17,7 +17,7 @@ function getQualifiedName(node: t.GenericTypeAnnotation["id"]) {
* Dedupe type annotations.
*/
export default function removeTypeDuplicates(
nodes: ReadonlyArray<t.FlowType | false | null | undefined>,
nodes: (t.FlowType | false | null | undefined)[],
): t.FlowType[] {
const generics = {};
const bases = {};
Expand Down Expand Up @@ -48,7 +48,7 @@ export default function removeTypeDuplicates(

if (isUnionTypeAnnotation(node)) {
if (typeGroups.indexOf(node.types) < 0) {
Array.prototype.push.apply(nodes, node.types);
nodes.push(...node.types);
typeGroups.push(node.types);
}
continue;
Expand Down
Expand Up @@ -41,7 +41,7 @@ export default function removeTypeDuplicates(

if (isTSUnionType(node)) {
if (typeGroups.indexOf(node.types) < 0) {
Array.prototype.push.apply(nodes, node.types);
nodes.push(...node.types);
typeGroups.push(node.types);
}
continue;
Expand Down

0 comments on commit 49e3e71

Please sign in to comment.