| 1 |
// TODO(Babel 8): Remove this file |
| 2 |
|
| 3 |
import { types as t, template } from "@babel/core"; |
| 4 |
import type { File } from "@babel/core"; |
| 5 |
import type { NodePath } from "@babel/traverse"; |
| 6 |
import ReplaceSupers from "@babel/helper-replace-supers"; |
| 7 |
import nameFunction from "@babel/helper-function-name"; |
| 8 |
|
| 9 |
type Decorable = Extract<t.Node, { decorators?: t.Decorator[] | null }>; |
| 10 |
|
| 11 |
export function hasOwnDecorators(node: t.Node) { |
| 12 |
// @ts-expect-error(flow->ts) TODO: maybe we could add t.isDecoratable to make ts happy |
| 13 |
return !!(node.decorators && node.decorators.length); |
| 14 |
} |
| 15 |
|
| 16 |
export function hasDecorators(node: t.Class) { |
| 17 |
return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators); |
| 18 |
} |
| 19 |
|
| 20 |
function prop(key: string, value?: t.Expression) { |
| 21 |
if (!value) return null; |
| 22 |
return t.objectProperty(t.identifier(key), value); |
| 23 |
} |
| 24 |
|
| 25 |
function method(key: string, body: t.Statement[]) { |
| 26 |
return t.objectMethod( |
| 27 |
"method", |
| 28 |
t.identifier(key), |
| 29 |
[], |
| 30 |
t.blockStatement(body), |
| 31 |
); |
| 32 |
} |
| 33 |
|
| 34 |
function takeDecorators(node: Decorable) { |
| 35 |
let result: t.ArrayExpression | undefined; |
| 36 |
if (node.decorators && node.decorators.length > 0) { |
| 37 |
result = t.arrayExpression( |
| 38 |
node.decorators.map(decorator => decorator.expression), |
| 39 |
); |
| 40 |
} |
| 41 |
node.decorators = undefined; |
| 42 |
return result; |
| 43 |
} |
| 44 |
|
| 45 |
type AcceptedElement = Exclude<ClassElement, t.TSIndexSignature>; |
| 46 |
type SupportedElement = Exclude< |
| 47 |
AcceptedElement, |
| 48 |
| t.ClassPrivateMethod |
| 49 |
| t.ClassPrivateProperty |
| 50 |
| t.ClassAccessorProperty |
| 51 |
| t.StaticBlock |
| 52 |
>; |
| 53 |
|
| 54 |
function getKey(node: SupportedElement) { |
| 55 |
if (node.computed) { |
| 56 |
return node.key; |
| 57 |
} else if (t.isIdentifier(node.key)) { |
| 58 |
return t.stringLiteral(node.key.name); |
| 59 |
} else { |
| 60 |
return t.stringLiteral( |
| 61 |
String( |
| 62 |
// A non-identifier non-computed key |
| 63 |
(node.key as t.StringLiteral | t.NumericLiteral | t.BigIntLiteral) |
| 64 |
.value, |
| 65 |
), |
| 66 |
); |
| 67 |
} |
| 68 |
} |
| 69 |
|
| 70 |
function extractElementDescriptor( |
| 71 |
file: File, |
| 72 |
classRef: t.Identifier, |
| 73 |
superRef: t.Identifier, |
| 74 |
path: NodePath<AcceptedElement>, |
| 75 |
) { |
| 76 |
const isMethod = path.isClassMethod(); |
| 77 |
if (path.isPrivate()) { |
| 78 |
throw path.buildCodeFrameError( |
| 79 |
`Private ${ |
| 80 |
isMethod ? "methods" : "fields" |
| 81 |
} in decorated classes are not supported yet.`, |
| 82 |
); |
| 83 |
} |
| 84 |
if (path.node.type === "ClassAccessorProperty") { |
| 85 |
throw path.buildCodeFrameError( |
| 86 |
`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`, |
| 87 |
); |
| 88 |
} |
| 89 |
if (path.node.type === "StaticBlock") { |
| 90 |
throw path.buildCodeFrameError( |
| 91 |
`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`, |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
const { node, scope } = path as NodePath<SupportedElement>; |
| 96 |
|
| 97 |
if (!path.isTSDeclareMethod()) { |
| 98 |
new ReplaceSupers({ |
| 99 |
methodPath: path as NodePath< |
| 100 |
Exclude<SupportedElement, t.TSDeclareMethod> |
| 101 |
>, |
| 102 |
objectRef: classRef, |
| 103 |
superRef, |
| 104 |
file, |
| 105 |
refToPreserve: classRef, |
| 106 |
}).replace(); |
| 107 |
} |
| 108 |
|
| 109 |
const properties: t.ObjectExpression["properties"] = [ |
| 110 |
prop("kind", t.stringLiteral(t.isClassMethod(node) ? node.kind : "field")), |
| 111 |
prop("decorators", takeDecorators(node as Decorable)), |
| 112 |
prop("static", node.static && t.booleanLiteral(true)), |
| 113 |
prop("key", getKey(node)), |
| 114 |
].filter(Boolean); |
| 115 |
|
| 116 |
if (t.isClassMethod(node)) { |
| 117 |
const id = node.computed |
| 118 |
? null |
| 119 |
: (node.key as |
| 120 |
| t.Identifier |
| 121 |
| t.StringLiteral |
| 122 |
| t.NumericLiteral |
| 123 |
| t.BigIntLiteral); |
| 124 |
const transformed = t.toExpression(node); |
| 125 |
properties.push( |
| 126 |
prop( |
| 127 |
"value", |
| 128 |
nameFunction({ node: transformed, id, scope }) || transformed, |
| 129 |
), |
| 130 |
); |
| 131 |
} else if (t.isClassProperty(node) && node.value) { |
| 132 |
properties.push( |
| 133 |
method("value", template.statements.ast`return ${node.value}`), |
| 134 |
); |
| 135 |
} else { |
| 136 |
properties.push(prop("value", scope.buildUndefinedNode())); |
| 137 |
} |
| 138 |
|
| 139 |
path.remove(); |
| 140 |
|
| 141 |
return t.objectExpression(properties); |
| 142 |
} |
| 143 |
|
| 144 |
function addDecorateHelper(file: File) { |
| 145 |
return file.addHelper("decorate"); |
| 146 |
} |
| 147 |
|
| 148 |
type ClassElement = t.Class["body"]["body"][number]; |
| 149 |
type ClassElementPath = NodePath<ClassElement>; |
| 150 |
|
| 151 |
export function buildDecoratedClass( |
| 152 |
ref: t.Identifier, |
| 153 |
path: NodePath<t.Class>, |
| 154 |
elements: ClassElementPath[], |
| 155 |
file: File, |
| 156 |
) { |
| 157 |
const { node, scope } = path; |
| 158 |
const initializeId = scope.generateUidIdentifier("initialize"); |
| 159 |
const isDeclaration = node.id && path.isDeclaration(); |
| 160 |
const isStrict = path.isInStrictMode(); |
| 161 |
const { superClass } = node; |
| 162 |
|
| 163 |
node.type = "ClassDeclaration"; |
| 164 |
if (!node.id) node.id = t.cloneNode(ref); |
| 165 |
|
| 166 |
let superId: t.Identifier; |
| 167 |
if (superClass) { |
| 168 |
superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super"); |
| 169 |
node.superClass = superId; |
| 170 |
} |
| 171 |
|
| 172 |
const classDecorators = takeDecorators(node); |
| 173 |
const definitions = t.arrayExpression( |
| 174 |
elements |
| 175 |
.filter( |
| 176 |
element => |
| 177 |
// @ts-expect-error Ignore TypeScript's abstract methods (see #10514) |
| 178 |
!element.node.abstract && element.node.type !== "TSIndexSignature", |
| 179 |
) |
| 180 |
.map(path => |
| 181 |
extractElementDescriptor( |
| 182 |
file, |
| 183 |
node.id, |
| 184 |
superId, |
| 185 |
// @ts-expect-error TS can not exclude TSIndexSignature |
| 186 |
path, |
| 187 |
), |
| 188 |
), |
| 189 |
); |
| 190 |
|
| 191 |
const wrapperCall = template.expression.ast` |
| 192 |
${addDecorateHelper(file)}( |
| 193 |
${classDecorators || t.nullLiteral()}, |
| 194 |
function (${initializeId}, ${superClass ? t.cloneNode(superId) : null}) { |
| 195 |
${node} |
| 196 |
return { F: ${t.cloneNode(node.id)}, d: ${definitions} }; |
| 197 |
}, |
| 198 |
${superClass} |
| 199 |
) |
| 200 |
` as t.CallExpression & { arguments: [unknown, t.FunctionExpression] }; |
| 201 |
|
| 202 |
if (!isStrict) { |
| 203 |
wrapperCall.arguments[1].body.directives.push( |
| 204 |
t.directive(t.directiveLiteral("use strict")), |
| 205 |
); |
| 206 |
} |
| 207 |
|
| 208 |
let replacement: t.Node = wrapperCall; |
| 209 |
let classPathDesc = "arguments.1.body.body.0"; |
| 210 |
if (isDeclaration) { |
| 211 |
replacement = template.statement.ast`let ${ref} = ${wrapperCall}`; |
| 212 |
classPathDesc = "declarations.0.init." + classPathDesc; |
| 213 |
} |
| 214 |
|
| 215 |
return { |
| 216 |
instanceNodes: [template.statement.ast`${t.cloneNode(initializeId)}(this)`], |
| 217 |
wrapClass(path: NodePath<t.Class>) { |
| 218 |
path.replaceWith(replacement); |
| 219 |
return path.get(classPathDesc) as NodePath; |
| 220 |
}, |
| 221 |
}; |
| 222 |
} |