Skip to content

Commit

Permalink
[Babel 8] Remove some Scope methods (babel#16705)
Browse files Browse the repository at this point in the history
Co-authored-by: Nicolò Ribaudo <[email protected]>
  • Loading branch information
liuxingbaoyu and nicolo-ribaudo authored Sep 7, 2024
1 parent ca62240 commit 7467c9d
Show file tree
Hide file tree
Showing 4 changed files with 222 additions and 138 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ function generateUid(scope: Scope, denyList: Set<string>) {
let uid;
let i = 1;
do {
uid = scope._generateUid(name, i);
uid = `_${name}`;
if (i > 1) uid += i;
i++;
} while (denyList.has(uid));
return uid;
Expand Down
43 changes: 40 additions & 3 deletions packages/babel-plugin-transform-destructuring/src/util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { types as t } from "@babel/core";
import { types as t, template } from "@babel/core";
import type { File, Scope, NodePath } from "@babel/core";

function isPureVoid(node: t.Node) {
Expand Down Expand Up @@ -186,14 +186,51 @@ export class DestructuringTransformer {
}
}

toArray(node: t.Expression, count?: boolean | number) {
toArray(node: t.Expression, count?: false | number) {
if (
this.iterableIsArray ||
(t.isIdentifier(node) && this.arrayRefSet.has(node.name))
) {
return node;
} else {
return this.scope.toArray(node, count, this.arrayLikeIsIterable);
const { scope, arrayLikeIsIterable } = this;

if (t.isIdentifier(node)) {
const binding = scope.getBinding(node.name);
if (binding?.constant && binding.path.isGenericType("Array")) {
return node;
}
}

if (t.isArrayExpression(node)) {
return node;
}

if (t.isIdentifier(node, { name: "arguments" })) {
return template.expression.ast`
Array.prototype.slice.call(${node})
`;
}

let helperName;
const args = [node];
if (typeof count === "number") {
args.push(t.numericLiteral(count));

// Used in array-rest to create an array from a subset of an iterable.
helperName = "slicedToArray";
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
} else {
// Used in array-rest to create an array
helperName = "toArray";
}

if (arrayLikeIsIterable) {
args.unshift(scope.path.hub.addHelper(helperName));
helperName = "maybeArrayLike";
}

return t.callExpression(scope.path.hub.addHelper(helperName), args);
}
}

Expand Down
31 changes: 29 additions & 2 deletions packages/babel-plugin-transform-spread/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { declare } from "@babel/helper-plugin-utils";
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers";
import { types as t } from "@babel/core";
import { types as t, template } from "@babel/core";
import type { File, NodePath, Scope } from "@babel/core";

type ListElement = t.SpreadElement | t.Expression;
Expand All @@ -27,7 +27,34 @@ export default declare((api, options: Options) => {
) {
return spread.argument;
} else {
return scope.toArray(spread.argument, true, arrayLikeIsIterable);
const node = spread.argument;

if (t.isIdentifier(node)) {
const binding = scope.getBinding(node.name);
if (binding?.constant && binding.path.isGenericType("Array")) {
return node;
}
}

if (t.isArrayExpression(node)) {
return node;
}

if (t.isIdentifier(node, { name: "arguments" })) {
return template.expression.ast`
Array.prototype.slice.call(${node})
`;
}

const args = [node];
let helperName = "toConsumableArray";

if (arrayLikeIsIterable) {
args.unshift(scope.path.hub.addHelper(helperName));
helperName = "maybeArrayLike";
}

return t.callExpression(scope.path.hub.addHelper(helperName), args);
}
}

Expand Down
Loading

0 comments on commit 7467c9d

Please sign in to comment.