1 | "use strict";
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.buildDecoratedClass = buildDecoratedClass;
|
7 | exports.hasDecorators = hasDecorators;
|
8 | exports.hasOwnDecorators = hasOwnDecorators;
|
9 | var _core = require("@babel/core");
|
10 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
11 | function hasOwnDecorators(node) {
|
12 | var _node$decorators;
|
13 | return !!((_node$decorators = node.decorators) != null && _node$decorators.length);
|
14 | }
|
15 | function hasDecorators(node) {
|
16 | return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
|
17 | }
|
18 | function prop(key, value) {
|
19 | if (!value) return null;
|
20 | return _core.types.objectProperty(_core.types.identifier(key), value);
|
21 | }
|
22 | function method(key, body) {
|
23 | return _core.types.objectMethod("method", _core.types.identifier(key), [], _core.types.blockStatement(body));
|
24 | }
|
25 | function takeDecorators(node) {
|
26 | let result;
|
27 | if (node.decorators && node.decorators.length > 0) {
|
28 | result = _core.types.arrayExpression(node.decorators.map(decorator => decorator.expression));
|
29 | }
|
30 | node.decorators = undefined;
|
31 | return result;
|
32 | }
|
33 | function getKey(node) {
|
34 | if (node.computed) {
|
35 | return node.key;
|
36 | } else if (_core.types.isIdentifier(node.key)) {
|
37 | return _core.types.stringLiteral(node.key.name);
|
38 | } else {
|
39 | return _core.types.stringLiteral(String(node.key.value));
|
40 | }
|
41 | }
|
42 | function extractElementDescriptor(file, classRef, superRef, path) {
|
43 | const isMethod = path.isClassMethod();
|
44 | if (path.isPrivate()) {
|
45 | throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
|
46 | }
|
47 | if (path.node.type === "ClassAccessorProperty") {
|
48 | throw path.buildCodeFrameError(`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
|
49 | }
|
50 | if (path.node.type === "StaticBlock") {
|
51 | throw path.buildCodeFrameError(`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
|
52 | }
|
53 | const {
|
54 | node,
|
55 | scope
|
56 | } = path;
|
57 | if (!path.isTSDeclareMethod()) {
|
58 | new _helperReplaceSupers.default({
|
59 | methodPath: path,
|
60 | objectRef: classRef,
|
61 | superRef,
|
62 | file,
|
63 | refToPreserve: classRef
|
64 | }).replace();
|
65 | }
|
66 | const properties = [prop("kind", _core.types.stringLiteral(_core.types.isClassMethod(node) ? node.kind : "field")), prop("decorators", takeDecorators(node)), prop("static", node.static && _core.types.booleanLiteral(true)), prop("key", getKey(node))].filter(Boolean);
|
67 | if (isMethod) {
|
68 | {
|
69 | var _path$ensureFunctionN;
|
70 | (_path$ensureFunctionN = path.ensureFunctionName) != null ? _path$ensureFunctionN : path.ensureFunctionName = require("@babel/traverse").NodePath.prototype.ensureFunctionName;
|
71 | }
|
72 | path.ensureFunctionName(false);
|
73 | properties.push(prop("value", _core.types.toExpression(path.node)));
|
74 | } else if (_core.types.isClassProperty(node) && node.value) {
|
75 | properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
|
76 | } else {
|
77 | properties.push(prop("value", scope.buildUndefinedNode()));
|
78 | }
|
79 | path.remove();
|
80 | return _core.types.objectExpression(properties);
|
81 | }
|
82 | function addDecorateHelper(file) {
|
83 | return file.addHelper("decorate");
|
84 | }
|
85 | function buildDecoratedClass(ref, path, elements, file) {
|
86 | const {
|
87 | node,
|
88 | scope
|
89 | } = path;
|
90 | const initializeId = scope.generateUidIdentifier("initialize");
|
91 | const isDeclaration = node.id && path.isDeclaration();
|
92 | const isStrict = path.isInStrictMode();
|
93 | const {
|
94 | superClass
|
95 | } = node;
|
96 | node.type = "ClassDeclaration";
|
97 | if (!node.id) node.id = _core.types.cloneNode(ref);
|
98 | let superId;
|
99 | if (superClass) {
|
100 | superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
|
101 | node.superClass = superId;
|
102 | }
|
103 | const classDecorators = takeDecorators(node);
|
104 | const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
|
105 | const wrapperCall = _core.template.expression.ast`
|
106 | ${addDecorateHelper(file)}(
|
107 | ${classDecorators || _core.types.nullLiteral()},
|
108 | function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
|
109 | ${node}
|
110 | return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
|
111 | },
|
112 | ${superClass}
|
113 | )
|
114 | `;
|
115 | if (!isStrict) {
|
116 | wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
|
117 | }
|
118 | let replacement = wrapperCall;
|
119 | let classPathDesc = "arguments.1.body.body.0";
|
120 | if (isDeclaration) {
|
121 | replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
|
122 | classPathDesc = "declarations.0.init." + classPathDesc;
|
123 | }
|
124 | return {
|
125 | instanceNodes: [_core.template.statement.ast`
|
126 | ${_core.types.cloneNode(initializeId)}(this)
|
127 | `],
|
128 | wrapClass(path) {
|
129 | path.replaceWith(replacement);
|
130 | return path.get(classPathDesc);
|
131 | }
|
132 | };
|
133 | }
|
134 |
|
135 |
|