Skip to content

Commit

Permalink
Review
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Nov 29, 2021
1 parent 9d191ac commit d27f33d
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
3 changes: 3 additions & 0 deletions packages/babel-helper-environment-visitor/package.json
Expand Up @@ -13,6 +13,9 @@
"access": "public"
},
"main": "./lib/index.js",
"exports": {
".": "./lib/index.js"
},
"dependencies": {
"@babel/types": "workspace:^"
},
Expand Down
17 changes: 5 additions & 12 deletions packages/babel-helper-environment-visitor/src/index.ts
Expand Up @@ -4,10 +4,9 @@ import type * as t from "@babel/types";

// TODO (Babel 8): Don't export this function.
export function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty | t.ClassPrivateProperty>,
path: NodePath<t.Method | t.ClassProperty>,
) {
// If the path isn't computed, just skip everything.
// @ts-expect-error todo(flow->ts) check node type before cheking the property
if (!path.node.computed) {
path.skip();
return;
Expand All @@ -21,24 +20,18 @@ export function skipAllButComputedKey(
}
}

// Methods are handled by the Method visitor; arrows are not skipped because they inherit the context.
const skipKey = process.env.BABEL_8_BREAKING
? "StaticBlock|ClassPrivateProperty|TypeAnnotation"
: `${staticBlock ? "StaticBlock|" : ""}ClassPrivateProperty|TypeAnnotation`;
? "StaticBlock|ClassPrivateProperty|TypeAnnotation|FunctionDeclaration|FunctionExpression"
: (staticBlock ? "StaticBlock|" : "") +
"ClassPrivateProperty|TypeAnnotation|FunctionDeclaration|FunctionExpression";

// environmentVisitor should be used when traversing the whole class and not for specific class elements/methods.
// For perf reasons, the environmentVisitor might be traversed with `{ noScope: true }`, which means `path.scope` is undefined.
// Avoid using `path.scope` here
export default {
[skipKey]: path => path.skip(),

Function(path: NodePath<t.Function>) {
// Methods will be handled by the Method visit
if (path.isMethod()) return;
// Arrow functions inherit their parent's environment
if (path.isArrowFunctionExpression()) return;
path.skip();
},

"Method|ClassProperty"(path: NodePath<t.Method | t.ClassProperty>) {
skipAllButComputedKey(path);
},
Expand Down

0 comments on commit d27f33d

Please sign in to comment.