|
| 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 | +}; |
0 commit comments