Skip to content

Commit 36a5ac4

Browse files
Fix handling of this&co in computed keys in arrows transform (#14005)
1 parent 8936501 commit 36a5ac4

File tree

20 files changed

+286
-117
lines changed

20 files changed

+286
-117
lines changed

packages/babel-helper-create-class-features-plugin/package.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
],
2020
"dependencies": {
2121
"@babel/helper-annotate-as-pure": "workspace:^",
22+
"@babel/helper-environment-visitor": "workspace:^",
2223
"@babel/helper-function-name": "workspace:^",
2324
"@babel/helper-member-expression-to-functions": "workspace:^",
2425
"@babel/helper-optimise-call-expression": "workspace:^",

packages/babel-helper-create-class-features-plugin/src/fields.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
import { template, traverse, types as t } from "@babel/core";
22
import type { File } from "@babel/core";
33
import type { NodePath, Visitor, Scope } from "@babel/traverse";
4-
import ReplaceSupers, {
5-
environmentVisitor,
6-
} from "@babel/helper-replace-supers";
4+
import ReplaceSupers from "@babel/helper-replace-supers";
5+
import environmentVisitor from "@babel/helper-environment-visitor";
76
import memberExpressionToFunctions from "@babel/helper-member-expression-to-functions";
87
import type {
98
Handler,
@@ -851,7 +850,11 @@ function buildPrivateMethodDeclaration(
851850
);
852851
}
853852

854-
const thisContextVisitor = traverse.visitors.merge([
853+
const thisContextVisitor = traverse.visitors.merge<{
854+
classRef: t.Identifier;
855+
needsClassRef: boolean;
856+
innerBinding: t.Identifier;
857+
}>([
855858
{
856859
ThisExpression(path, state) {
857860
state.needsClassRef = true;

packages/babel-helper-create-class-features-plugin/src/misc.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
import { template, traverse, types as t } from "@babel/core";
22
import type { File } from "@babel/core";
33
import type { NodePath, Scope, Visitor, Binding } from "@babel/traverse";
4-
import { environmentVisitor } from "@babel/helper-replace-supers";
4+
import environmentVisitor from "@babel/helper-environment-visitor";
55

6-
const findBareSupers = traverse.visitors.merge([
6+
const findBareSupers = traverse.visitors.merge<NodePath<t.CallExpression>[]>([
77
{
88
Super(path: NodePath<t.Super>) {
99
const { node, parentPath } = path;
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
src
2+
test
3+
*.log
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
# @babel/helper-environment-visitor
2+
3+
> Helper visitor to only visit nodes in the current 'this' context
4+
5+
See our website [@babel/helper-environment-visitor](https://babeljs.io/docs/en/babel-helper-environment-visitor) for more information.
6+
7+
## Install
8+
9+
Using npm:
10+
11+
```sh
12+
npm install --save-dev @babel/helper-environment-visitor
13+
```
14+
15+
or using yarn:
16+
17+
```sh
18+
yarn add @babel/helper-environment-visitor --dev
19+
```
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
{
2+
"name": "@babel/helper-environment-visitor",
3+
"version": "7.16.0",
4+
"description": "Helper visitor to only visit nodes in the current 'this' context",
5+
"repository": {
6+
"type": "git",
7+
"url": "https://github.com/babel/babel.git",
8+
"directory": "packages/babel-helper-environment-visitor"
9+
},
10+
"homepage": "https://babel.dev/docs/en/next/babel-helper-environment-visitor",
11+
"license": "MIT",
12+
"publishConfig": {
13+
"access": "public"
14+
},
15+
"main": "./lib/index.js",
16+
"exports": {
17+
".": "./lib/index.js"
18+
},
19+
"dependencies": {
20+
"@babel/types": "workspace:^"
21+
},
22+
"devDependencies": {
23+
"@babel/traverse": "workspace:^"
24+
},
25+
"engines": {
26+
"node": ">=6.9.0"
27+
},
28+
"author": "The Babel Team (https://babel.dev/team)"
29+
}
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import type { NodePath } from "@babel/traverse";
2+
import { VISITOR_KEYS, staticBlock } from "@babel/types";
3+
import type * as t from "@babel/types";
4+
5+
// TODO (Babel 8): Don't export this function.
6+
export function skipAllButComputedKey(
7+
path: NodePath<t.Method | t.ClassProperty>,
8+
) {
9+
// If the path isn't computed, just skip everything.
10+
if (!path.node.computed) {
11+
path.skip();
12+
return;
13+
}
14+
15+
// So it's got a computed key. Make sure to skip every other key the
16+
// traversal would visit.
17+
const keys = VISITOR_KEYS[path.type];
18+
for (const key of keys) {
19+
if (key !== "key") path.skipKey(key);
20+
}
21+
}
22+
23+
// Methods are handled by the Method visitor; arrows are not skipped because they inherit the context.
24+
const skipKey = process.env.BABEL_8_BREAKING
25+
? "StaticBlock|ClassPrivateProperty|TypeAnnotation|FunctionDeclaration|FunctionExpression"
26+
: (staticBlock ? "StaticBlock|" : "") +
27+
"ClassPrivateProperty|TypeAnnotation|FunctionDeclaration|FunctionExpression";
28+
29+
// environmentVisitor should be used when traversing the whole class and not for specific class elements/methods.
30+
// For perf reasons, the environmentVisitor might be traversed with `{ noScope: true }`, which means `path.scope` is undefined.
31+
// Avoid using `path.scope` here
32+
export default {
33+
[skipKey]: path => path.skip(),
34+
35+
"Method|ClassProperty"(path: NodePath<t.Method | t.ClassProperty>) {
36+
skipAllButComputedKey(path);
37+
},
38+
};

packages/babel-helper-member-expression-to-functions/src/index.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -538,7 +538,7 @@ export interface HandlerState<State = {}> extends Handler<State> {
538538
handle(
539539
this: HandlerState<State> & State,
540540
member: Member,
541-
noDocumentAll: boolean,
541+
noDocumentAll?: boolean,
542542
): void;
543543
memoiser: AssignmentMemoiser;
544544
}

packages/babel-helper-module-transforms/package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,8 @@
1515
},
1616
"main": "./lib/index.js",
1717
"dependencies": {
18+
"@babel/helper-environment-visitor": "workspace:^",
1819
"@babel/helper-module-imports": "workspace:^",
19-
"@babel/helper-replace-supers": "workspace:^",
2020
"@babel/helper-simple-access": "workspace:^",
2121
"@babel/helper-split-export-declaration": "workspace:^",
2222
"@babel/helper-validator-identifier": "workspace:^",

packages/babel-helper-module-transforms/src/rewrite-this.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { environmentVisitor } from "@babel/helper-replace-supers";
1+
import environmentVisitor from "@babel/helper-environment-visitor";
22
import traverse from "@babel/traverse";
33
import { numericLiteral, unaryExpression } from "@babel/types";
44
import type * as t from "@babel/types";

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy