| 1 |
import { template, traverse, types as t } from "@babel/core"; |
| 2 |
import type { File } from "@babel/core"; |
| 3 |
import type { NodePath, Scope, Visitor, Binding } from "@babel/traverse"; |
| 4 |
import environmentVisitor from "@babel/helper-environment-visitor"; |
| 5 |
|
| 6 |
const findBareSupers = traverse.visitors.merge<NodePath<t.CallExpression>[]>([ |
| 7 |
{ |
| 8 |
Super(path) { |
| 9 |
const { node, parentPath } = path; |
| 10 |
if (parentPath.isCallExpression({ callee: node })) { |
| 11 |
this.push(parentPath); |
| 12 |
} |
| 13 |
}, |
| 14 |
}, |
| 15 |
environmentVisitor, |
| 16 |
]); |
| 17 |
|
| 18 |
const referenceVisitor: Visitor<{ scope: Scope }> = { |
| 19 |
"TSTypeAnnotation|TypeAnnotation"( |
| 20 |
path: NodePath<t.TSTypeAnnotation | t.TypeAnnotation>, |
| 21 |
) { |
| 22 |
path.skip(); |
| 23 |
}, |
| 24 |
|
| 25 |
ReferencedIdentifier(path: NodePath<t.Identifier>, { scope }) { |
| 26 |
if (scope.hasOwnBinding(path.node.name)) { |
| 27 |
scope.rename(path.node.name); |
| 28 |
path.skip(); |
| 29 |
} |
| 30 |
}, |
| 31 |
}; |
| 32 |
|
| 33 |
type HandleClassTDZState = { |
| 34 |
classBinding: Binding; |
| 35 |
file: File; |
| 36 |
}; |
| 37 |
|
| 38 |
function handleClassTDZ( |
| 39 |
path: NodePath<t.Identifier>, |
| 40 |
state: HandleClassTDZState, |
| 41 |
) { |
| 42 |
if ( |
| 43 |
state.classBinding && |
| 44 |
state.classBinding === path.scope.getBinding(path.node.name) |
| 45 |
) { |
| 46 |
const classNameTDZError = state.file.addHelper("classNameTDZError"); |
| 47 |
const throwNode = t.callExpression(classNameTDZError, [ |
| 48 |
t.stringLiteral(path.node.name), |
| 49 |
]); |
| 50 |
|
| 51 |
path.replaceWith(t.sequenceExpression([throwNode, path.node])); |
| 52 |
path.skip(); |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
const classFieldDefinitionEvaluationTDZVisitor: Visitor<HandleClassTDZState> = { |
| 57 |
ReferencedIdentifier: handleClassTDZ, |
| 58 |
}; |
| 59 |
|
| 60 |
interface RenamerState { |
| 61 |
scope: Scope; |
| 62 |
} |
| 63 |
|
| 64 |
export function injectInitialization( |
| 65 |
path: NodePath<t.Class>, |
| 66 |
constructor: NodePath<t.ClassMethod> | undefined, |
| 67 |
nodes: t.Statement[], |
| 68 |
renamer?: (visitor: Visitor<RenamerState>, state: RenamerState) => void, |
| 69 |
) { |
| 70 |
if (!nodes.length) return; |
| 71 |
|
| 72 |
const isDerived = !!path.node.superClass; |
| 73 |
|
| 74 |
if (!constructor) { |
| 75 |
const newConstructor = t.classMethod( |
| 76 |
"constructor", |
| 77 |
t.identifier("constructor"), |
| 78 |
[], |
| 79 |
t.blockStatement([]), |
| 80 |
); |
| 81 |
|
| 82 |
if (isDerived) { |
| 83 |
newConstructor.params = [t.restElement(t.identifier("args"))]; |
| 84 |
newConstructor.body.body.push(template.statement.ast`super(...args)`); |
| 85 |
} |
| 86 |
|
| 87 |
[constructor] = path |
| 88 |
.get("body") |
| 89 |
.unshiftContainer("body", newConstructor) as NodePath<t.ClassMethod>[]; |
| 90 |
} |
| 91 |
|
| 92 |
if (renamer) { |
| 93 |
renamer(referenceVisitor, { scope: constructor.scope }); |
| 94 |
} |
| 95 |
|
| 96 |
if (isDerived) { |
| 97 |
const bareSupers: NodePath<t.CallExpression>[] = []; |
| 98 |
constructor.traverse(findBareSupers, bareSupers); |
| 99 |
let isFirst = true; |
| 100 |
for (const bareSuper of bareSupers) { |
| 101 |
if (isFirst) { |
| 102 |
bareSuper.insertAfter(nodes); |
| 103 |
isFirst = false; |
| 104 |
} else { |
| 105 |
bareSuper.insertAfter(nodes.map(n => t.cloneNode(n))); |
| 106 |
} |
| 107 |
} |
| 108 |
} else { |
| 109 |
constructor.get("body").unshiftContainer("body", nodes); |
| 110 |
} |
| 111 |
} |
| 112 |
|
| 113 |
export function extractComputedKeys( |
| 114 |
path: NodePath<t.Class>, |
| 115 |
computedPaths: NodePath<t.ClassProperty | t.ClassMethod>[], |
| 116 |
file: File, |
| 117 |
) { |
| 118 |
const declarations: t.ExpressionStatement[] = []; |
| 119 |
const state = { |
| 120 |
classBinding: path.node.id && path.scope.getBinding(path.node.id.name), |
| 121 |
file, |
| 122 |
}; |
| 123 |
for (const computedPath of computedPaths) { |
| 124 |
const computedKey = computedPath.get("key"); |
| 125 |
if (computedKey.isReferencedIdentifier()) { |
| 126 |
handleClassTDZ(computedKey, state); |
| 127 |
} else { |
| 128 |
computedKey.traverse(classFieldDefinitionEvaluationTDZVisitor, state); |
| 129 |
} |
| 130 |
|
| 131 |
const computedNode = computedPath.node; |
| 132 |
// Make sure computed property names are only evaluated once (upon class definition) |
| 133 |
// and in the right order in combination with static properties |
| 134 |
if (!computedKey.isConstantExpression()) { |
| 135 |
const ident = path.scope.generateUidIdentifierBasedOnNode( |
| 136 |
computedNode.key, |
| 137 |
); |
| 138 |
// Declaring in the same block scope |
| 139 |
// Ref: https://github.com/babel/babel/pull/10029/files#diff-fbbdd83e7a9c998721c1484529c2ce92 |
| 140 |
path.scope.push({ |
| 141 |
id: ident, |
| 142 |
kind: "let", |
| 143 |
}); |
| 144 |
declarations.push( |
| 145 |
t.expressionStatement( |
| 146 |
t.assignmentExpression("=", t.cloneNode(ident), computedNode.key), |
| 147 |
), |
| 148 |
); |
| 149 |
computedNode.key = t.cloneNode(ident); |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
return declarations; |
| 154 |
} |