1 |
- {"version":3,"names":["_core","require","_helperReplaceSupers","hasOwnDecorators","node","_node$decorators","decorators","length","hasDecorators","body","some","prop","key","value","t","objectProperty","identifier","method","objectMethod","blockStatement","takeDecorators","result","arrayExpression","map","decorator","expression","undefined","getKey","computed","isIdentifier","stringLiteral","name","String","extractElementDescriptor","file","classRef","superRef","path","isMethod","isClassMethod","isPrivate","buildCodeFrameError","type","scope","isTSDeclareMethod","ReplaceSupers","methodPath","objectRef","refToPreserve","replace","properties","kind","static","booleanLiteral","filter","Boolean","_path$ensureFunctionN","ensureFunctionName","NodePath","prototype","push","toExpression","isClassProperty","template","statements","ast","buildUndefinedNode","remove","objectExpression","addDecorateHelper","addHelper","buildDecoratedClass","ref","elements","initializeId","generateUidIdentifier","isDeclaration","id","isStrict","isInStrictMode","superClass","cloneNode","superId","generateUidIdentifierBasedOnNode","classDecorators","definitions","element","abstract","wrapperCall","nullLiteral","arguments","directives","directive","directiveLiteral","replacement","classPathDesc","statement","instanceNodes","wrapClass","replaceWith","get"],"sources":["../src/decorators-2018-09.ts"],"sourcesContent":["// TODO(Babel 8): Remove this file\n\nimport { types as t, template } from \"@babel/core\";\nimport type { File, NodePath } from \"@babel/core\";\nimport ReplaceSupers from \"@babel/helper-replace-supers\";\n\ntype Decoratable = Extract<t.Node, { decorators?: t.Decorator[] | null }>;\n\nexport function hasOwnDecorators(node: t.Class | t.ClassBody[\"body\"][number]) {\n // @ts-expect-error: 'decorators' not in TSIndexSignature\n return !!node.decorators?.length;\n}\n\nexport function hasDecorators(node: t.Class) {\n return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);\n}\n\nfunction prop(key: string, value?: t.Expression) {\n if (!value) return null;\n return t.objectProperty(t.identifier(key), value);\n}\n\nfunction method(key: string, body: t.Statement[]) {\n return t.objectMethod(\n \"method\",\n t.identifier(key),\n [],\n t.blockStatement(body),\n );\n}\n\nfunction takeDecorators(node: Decoratable) {\n let result: t.ArrayExpression | undefined;\n if (node.decorators && node.decorators.length > 0) {\n result = t.arrayExpression(\n node.decorators.map(decorator => decorator.expression),\n );\n }\n node.decorators = undefined;\n return result;\n}\n\ntype AcceptedElement = Exclude<ClassElement, t.TSIndexSignature>;\ntype SupportedElement = Exclude<\n AcceptedElement,\n | t.ClassPrivateMethod\n | t.ClassPrivateProperty\n | t.ClassAccessorProperty\n | t.StaticBlock\n>;\n\nfunction getKey(node: SupportedElement) {\n if (node.computed) {\n return node.key;\n } else if (t.isIdentifier(node.key)) {\n return t.stringLiteral(node.key.name);\n } else {\n return t.stringLiteral(\n String(\n // A non-identifier non-computed key\n (node.key as t.StringLiteral | t.NumericLiteral | t.BigIntLiteral)\n .value,\n ),\n );\n }\n}\n\nfunction extractElementDescriptor(\n file: File,\n classRef: t.Identifier,\n superRef: t.Identifier,\n path: NodePath<AcceptedElement>,\n) {\n const isMethod = path.isClassMethod();\n if (path.isPrivate()) {\n throw path.buildCodeFrameError(\n `Private ${\n isMethod ? \"methods\" : \"fields\"\n } in decorated classes are not supported yet.`,\n );\n }\n if (path.node.type === \"ClassAccessorProperty\") {\n throw path.buildCodeFrameError(\n `Accessor properties are not supported in 2018-09 decorator transform, please specify { \"version\": \"2021-12\" } instead.`,\n );\n }\n if (path.node.type === \"StaticBlock\") {\n throw path.buildCodeFrameError(\n `Static blocks are not supported in 2018-09 decorator transform, please specify { \"version\": \"2021-12\" } instead.`,\n );\n }\n\n const { node, scope } = path as NodePath<SupportedElement>;\n\n if (!path.isTSDeclareMethod()) {\n new ReplaceSupers({\n methodPath: path as NodePath<\n Exclude<SupportedElement, t.TSDeclareMethod>\n >,\n objectRef: classRef,\n superRef,\n file,\n refToPreserve: classRef,\n }).replace();\n }\n\n const properties: t.ObjectExpression[\"properties\"] = [\n prop(\"kind\", t.stringLiteral(t.isClassMethod(node) ? node.kind : \"field\")),\n prop(\"decorators\", takeDecorators(node as Decoratable)),\n prop(\"static\", node.static && t.booleanLiteral(true)),\n prop(\"key\", getKey(node)),\n ].filter(Boolean);\n\n if (isMethod) {\n if (!process.env.BABEL_8_BREAKING && !USE_ESM && !IS_STANDALONE) {\n // polyfill when being run by an older Babel version\n path.ensureFunctionName ??=\n // eslint-disable-next-line no-restricted-globals\n require(\"@babel/traverse\").NodePath.prototype.ensureFunctionName;\n }\n // @ts-expect-error path is a ClassMethod, that technically\n // is not supported as it does not have an .id property\n // This plugin will however then transform the ClassMethod\n // to a function expression, so it's fine.\n path.ensureFunctionName(false);\n\n properties.push(prop(\"value\", t.toExpression(path.node)));\n } else if (t.isClassProperty(node) && node.value) {\n properties.push(\n method(\"value\", template.statements.ast`return ${node.value}`),\n );\n } else {\n properties.push(prop(\"value\", scope.buildUndefinedNode()));\n }\n\n path.remove();\n\n return t.objectExpression(properties);\n}\n\nfunction addDecorateHelper(file: File) {\n return file.addHelper(\"decorate\");\n}\n\ntype ClassElement = t.Class[\"body\"][\"body\"][number];\ntype ClassElementPath = NodePath<ClassElement>;\n\nexport function buildDecoratedClass(\n ref: t.Identifier,\n path: NodePath<t.Class>,\n elements: ClassElementPath[],\n file: File,\n) {\n const { node, scope } = path;\n const initializeId = scope.generateUidIdentifier(\"initialize\");\n const isDeclaration = node.id && path.isDeclaration();\n const isStrict = path.isInStrictMode();\n const { superClass } = node;\n\n node.type = \"ClassDeclaration\";\n if (!node.id) node.id = t.cloneNode(ref);\n\n let superId: t.Identifier;\n if (superClass) {\n superId = scope.generateUidIdentifierBasedOnNode(node.superClass, \"super\");\n node.superClass = superId;\n }\n\n const classDecorators = takeDecorators(node);\n const definitions = t.arrayExpression(\n elements\n .filter(\n element =>\n // @ts-expect-error Ignore TypeScript's abstract methods (see #10514)\n !element.node.abstract && element.node.type !== \"TSIndexSignature\",\n )\n .map(path =>\n extractElementDescriptor(\n file,\n node.id,\n superId,\n // @ts-expect-error TS can not exclude TSIndexSignature\n path,\n ),\n ),\n );\n\n const wrapperCall = template.expression.ast`\n ${addDecorateHelper(file)}(\n ${classDecorators || t.nullLiteral()},\n function (${initializeId}, ${superClass ? t.cloneNode(superId) : null}) {\n ${node}\n return { F: ${t.cloneNode(node.id)}, d: ${definitions} };\n },\n ${superClass}\n )\n ` as t.CallExpression & { arguments: [unknown, t.FunctionExpression] };\n\n if (!isStrict) {\n wrapperCall.arguments[1].body.directives.push(\n t.directive(t.directiveLiteral(\"use strict\")),\n );\n }\n\n let replacement: t.Node = wrapperCall;\n let classPathDesc = \"arguments.1.body.body.0\";\n if (isDeclaration) {\n replacement = template.statement.ast`let ${ref} = ${wrapperCall}`;\n classPathDesc = \"declarations.0.init.\" + classPathDesc;\n }\n\n return {\n instanceNodes: [\n template.statement.ast`\n ${t.cloneNode(initializeId)}(this)\n ` as t.ExpressionStatement,\n ],\n wrapClass(path: NodePath<t.Class>) {\n path.replaceWith(replacement);\n return path.get(classPathDesc) as NodePath;\n },\n };\n}\n"],"mappings":";;;;;;;;AAEA,IAAAA,KAAA,GAAAC,OAAA;AAEA,IAAAC,oBAAA,GAAAD,OAAA;AAIO,SAASE,gBAAgBA,CAACC,IAA2C,EAAE;EAAA,IAAAC,gBAAA;EAE5E,OAAO,CAAC,GAAAA,gBAAA,GAACD,IAAI,CAACE,UAAU,aAAfD,gBAAA,CAAiBE,MAAM;AAClC;AAEO,SAASC,aAAaA,CAACJ,IAAa,EAAE;EAC3C,OAAOD,gBAAgB,CAACC,IAAI,CAAC,IAAIA,IAAI,CAACK,IAAI,CAACA,IAAI,CAACC,IAAI,CAACP,gBAAgB,CAAC;AACxE;AAEA,SAASQ,IAAIA,CAACC,GAAW,EAAEC,KAAoB,EAAE;EAC/C,IAAI,CAACA,KAAK,EAAE,OAAO,IAAI;EACvB,OAAOC,WAAC,CAACC,cAAc,CAACD,WAAC,CAACE,UAAU,CAACJ,GAAG,CAAC,EAAEC,KAAK,CAAC;AACnD;AAEA,SAASI,MAAMA,CAACL,GAAW,EAAEH,IAAmB,EAAE;EAChD,OAAOK,WAAC,CAACI,YAAY,CACnB,QAAQ,EACRJ,WAAC,CAACE,UAAU,CAACJ,GAAG,CAAC,E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