Skip to content

Commit 7467c9d

Browse files
[Babel 8] Remove some Scope methods (#16705)
Co-authored-by: Nicolò Ribaudo <[email protected]>
1 parent ca62240 commit 7467c9d

File tree

4 files changed

+222
-138
lines changed
  • packages
    • babel-plugin-transform-class-static-block/src
    • babel-plugin-transform-destructuring/src
    • babel-plugin-transform-spread/src
    • babel-traverse/src/scope

4 files changed

+222
-138
lines changed

packages/babel-plugin-transform-class-static-block/src/index.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@ function generateUid(scope: Scope, denyList: Set<string>) {
1818
let uid;
1919
let i = 1;
2020
do {
21-
uid = scope._generateUid(name, i);
21+
uid = `_${name}`;
22+
if (i > 1) uid += i;
2223
i++;
2324
} while (denyList.has(uid));
2425
return uid;

packages/babel-plugin-transform-destructuring/src/util.ts

Lines changed: 40 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { types as t } from "@babel/core";
1+
import { types as t, template } from "@babel/core";
22
import type { File, Scope, NodePath } from "@babel/core";
33

44
function isPureVoid(node: t.Node) {
@@ -186,14 +186,51 @@ export class DestructuringTransformer {
186186
}
187187
}
188188

189-
toArray(node: t.Expression, count?: boolean | number) {
189+
toArray(node: t.Expression, count?: false | number) {
190190
if (
191191
this.iterableIsArray ||
192192
(t.isIdentifier(node) && this.arrayRefSet.has(node.name))
193193
) {
194194
return node;
195195
} else {
196-
return this.scope.toArray(node, count, this.arrayLikeIsIterable);
196+
const { scope, arrayLikeIsIterable } = this;
197+
198+
if (t.isIdentifier(node)) {
199+
const binding = scope.getBinding(node.name);
200+
if (binding?.constant && binding.path.isGenericType("Array")) {
201+
return node;
202+
}
203+
}
204+
205+
if (t.isArrayExpression(node)) {
206+
return node;
207+
}
208+
209+
if (t.isIdentifier(node, { name: "arguments" })) {
210+
return template.expression.ast`
211+
Array.prototype.slice.call(${node})
212+
`;
213+
}
214+
215+
let helperName;
216+
const args = [node];
217+
if (typeof count === "number") {
218+
args.push(t.numericLiteral(count));
219+
220+
// Used in array-rest to create an array from a subset of an iterable.
221+
helperName = "slicedToArray";
222+
// TODO if (this.hub.isLoose("es6.forOf")) helperName += "-loose";
223+
} else {
224+
// Used in array-rest to create an array
225+
helperName = "toArray";
226+
}
227+
228+
if (arrayLikeIsIterable) {
229+
args.unshift(scope.path.hub.addHelper(helperName));
230+
helperName = "maybeArrayLike";
231+
}
232+
233+
return t.callExpression(scope.path.hub.addHelper(helperName), args);
197234
}
198235
}
199236

packages/babel-plugin-transform-spread/src/index.ts

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { declare } from "@babel/helper-plugin-utils";
22
import { skipTransparentExprWrappers } from "@babel/helper-skip-transparent-expression-wrappers";
3-
import { types as t } from "@babel/core";
3+
import { types as t, template } from "@babel/core";
44
import type { File, NodePath, Scope } from "@babel/core";
55

66
type ListElement = t.SpreadElement | t.Expression;
@@ -27,7 +27,34 @@ export default declare((api, options: Options) => {
2727
) {
2828
return spread.argument;
2929
} else {
30-
return scope.toArray(spread.argument, true, arrayLikeIsIterable);
30+
const node = spread.argument;
31+
32+
if (t.isIdentifier(node)) {
33+
const binding = scope.getBinding(node.name);
34+
if (binding?.constant && binding.path.isGenericType("Array")) {
35+
return node;
36+
}
37+
}
38+
39+
if (t.isArrayExpression(node)) {
40+
return node;
41+
}
42+
43+
if (t.isIdentifier(node, { name: "arguments" })) {
44+
return template.expression.ast`
45+
Array.prototype.slice.call(${node})
46+
`;
47+
}
48+
49+
const args = [node];
50+
let helperName = "toConsumableArray";
51+
52+
if (arrayLikeIsIterable) {
53+
args.unshift(scope.path.hub.addHelper(helperName));
54+
helperName = "maybeArrayLike";
55+
}
56+
57+
return t.callExpression(scope.path.hub.addHelper(helperName), args);
3158
}
3259
}
3360

0 commit comments

Comments
 (0)