Skip to content

Commit

Permalink
feat: export requeueComputedKeyAndDecorator
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Mar 24, 2022
1 parent 5e395f7 commit b35e893
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 24 deletions.
11 changes: 6 additions & 5 deletions packages/babel-helper-environment-visitor/src/index.ts
Expand Up @@ -12,10 +12,9 @@ export function skipAllButComputedKey(
}
}

function skipAndrequeueComputedKeysAndDecorators(
export function requeueComputedKeyAndDecorator(
path: NodePath<t.Method | t.Property>,
) {
path.skip();
const { context } = path;
//@ts-ignore ClassPrivateProperty does not have computed
if (path.node.computed) {
Expand All @@ -38,15 +37,17 @@ export default {
if (path.isArrowFunctionExpression()) {
// arrows are not skipped because they inherit the context.
return;
} else if (path.isMethod()) {
skipAndrequeueComputedKeysAndDecorators(path);
} else {
path.skip();
if (path.isMethod()) {
requeueComputedKeyAndDecorator(path);
}
}
},
"ClassProperty|ClassPrivateProperty"(
path: NodePath<t.ClassProperty | t.ClassPrivateProperty>,
) {
skipAndrequeueComputedKeysAndDecorators(path);
path.skip();
requeueComputedKeyAndDecorator(path);
},
} as Visitor<unknown>;
24 changes: 5 additions & 19 deletions packages/babel-traverse/src/scope/lib/renamer.ts
Expand Up @@ -8,7 +8,7 @@ import {
variableDeclarator,
} from "@babel/types";
import type { Visitor } from "../../types";
import type NodePath from "../../path";
import { requeueComputedKeyAndDecorator } from "@babel/helper-environment-visitor";

const renameVisitor: Visitor<Renamer> = {
ReferencedIdentifier({ node }, state) {
Expand All @@ -24,7 +24,10 @@ const renameVisitor: Visitor<Renamer> = {
state.binding.identifier,
)
) {
skipAndrequeueComputedKeysAndDecorators(path);
path.skip();
if (path.isMethod()) {
requeueComputedKeyAndDecorator(path);
}
}
},

Expand Down Expand Up @@ -146,20 +149,3 @@ export default class Renamer {
}
}
}

function skipAndrequeueComputedKeysAndDecorators(path: NodePath) {
path.skip();
if (path.isMethod()) {
const { context } = path;
if (path.node.computed) {
// requeue the computed key
context.maybeQueue(path.get("key"));
}
if (path.node.decorators) {
for (const decorator of path.get("decorators")) {
// requeue the decorators
context.maybeQueue(decorator);
}
}
}
}

0 comments on commit b35e893

Please sign in to comment.