forked from babel/babel
-
Notifications
You must be signed in to change notification settings - Fork 3
/
index.ts
56 lines (52 loc) 路 1.52 KB
/
index.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
import type { NodePath, Visitor } from "@babel/traverse";
import type * as t from "@babel/types";
// TODO (Babel 8): Don't export this function.
export function skipAllButComputedKey(
path: NodePath<t.Method | t.ClassProperty>,
) {
path.skip();
if (path.node.computed) {
// requeue the computed key
path.context.maybeQueue(path.get("key"));
}
}
export function requeueComputedKeyAndDecorators(
path: NodePath<t.Method | t.Property>,
) {
const { context, node } = path;
//@ts-ignore ClassPrivateProperty does not have computed
if (node.computed) {
// requeue the computed key
context.maybeQueue(path.get("key"));
}
if (node.decorators) {
for (const decorator of path.get("decorators")) {
// requeue the decorators
context.maybeQueue(decorator);
}
}
}
// 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
const visitor: Visitor = {
FunctionParent(path) {
if (path.isArrowFunctionExpression()) {
// arrows are not skipped because they inherit the context.
return;
} else {
path.skip();
if (path.isMethod()) {
requeueComputedKeyAndDecorators(path);
}
}
},
Property(path) {
if (path.isObjectProperty()) {
return;
}
path.skip();
requeueComputedKeyAndDecorators(path);
},
};
export default visitor;