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 |
|
10 | var _core = require("@babel/core");
|
11 |
|
12 | var _helperReplaceSupers = require("@babel/helper-replace-supers");
|
13 |
|
14 | var _helperFunctionName = require("@babel/helper-function-name");
|
15 |
|
16 | function hasOwnDecorators(node) {
|
17 | return !!(node.decorators && node.decorators.length);
|
18 | }
|
19 |
|
20 | function hasDecorators(node) {
|
21 | return hasOwnDecorators(node) || node.body.body.some(hasOwnDecorators);
|
22 | }
|
23 |
|
24 | function prop(key, value) {
|
25 | if (!value) return null;
|
26 | return _core.types.objectProperty(_core.types.identifier(key), value);
|
27 | }
|
28 |
|
29 | function method(key, body) {
|
30 | return _core.types.objectMethod("method", _core.types.identifier(key), [], _core.types.blockStatement(body));
|
31 | }
|
32 |
|
33 | function takeDecorators(node) {
|
34 | let result;
|
35 |
|
36 | if (node.decorators && node.decorators.length > 0) {
|
37 | result = _core.types.arrayExpression(node.decorators.map(decorator => decorator.expression));
|
38 | }
|
39 |
|
40 | node.decorators = undefined;
|
41 | return result;
|
42 | }
|
43 |
|
44 | function getKey(node) {
|
45 | if (node.computed) {
|
46 | return node.key;
|
47 | } else if (_core.types.isIdentifier(node.key)) {
|
48 | return _core.types.stringLiteral(node.key.name);
|
49 | } else {
|
50 | return _core.types.stringLiteral(String(node.key.value));
|
51 | }
|
52 | }
|
53 |
|
54 | function extractElementDescriptor(file, classRef, superRef, path) {
|
55 | const isMethod = path.isClassMethod();
|
56 |
|
57 | if (path.isPrivate()) {
|
58 | throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
|
59 | }
|
60 |
|
61 | if (path.node.type === "ClassAccessorProperty") {
|
62 | throw path.buildCodeFrameError(`Accessor properties are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
|
63 | }
|
64 |
|
65 | if (path.node.type === "StaticBlock") {
|
66 | throw path.buildCodeFrameError(`Static blocks are not supported in 2018-09 decorator transform, please specify { "version": "2021-12" } instead.`);
|
67 | }
|
68 |
|
69 | const {
|
70 | node,
|
71 | scope
|
72 | } = path;
|
73 | new _helperReplaceSupers.default({
|
74 | methodPath: path,
|
75 | objectRef: classRef,
|
76 | superRef,
|
77 | file,
|
78 | refToPreserve: classRef
|
79 | }).replace();
|
80 | 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);
|
81 |
|
82 | if (_core.types.isClassMethod(node)) {
|
83 | const id = node.computed ? null : node.key;
|
84 |
|
85 | const transformed = _core.types.toExpression(node);
|
86 |
|
87 | properties.push(prop("value", (0, _helperFunctionName.default)({
|
88 | node: transformed,
|
89 | id,
|
90 | scope
|
91 | }) || transformed));
|
92 | } else if (_core.types.isClassProperty(node) && node.value) {
|
93 | properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
|
94 | } else {
|
95 | properties.push(prop("value", scope.buildUndefinedNode()));
|
96 | }
|
97 |
|
98 | path.remove();
|
99 | return _core.types.objectExpression(properties);
|
100 | }
|
101 |
|
102 | function addDecorateHelper(file) {
|
103 | try {
|
104 | return file.addHelper("decorate");
|
105 | } catch (err) {
|
106 | if (err.code === "BABEL_HELPER_UNKNOWN") {
|
107 | err.message += "\n '@babel/plugin-transform-decorators' in non-legacy mode" + " requires '@babel/core' version ^7.0.2 and you appear to be using" + " an older version.";
|
108 | }
|
109 |
|
110 | throw err;
|
111 | }
|
112 | }
|
113 |
|
114 | function buildDecoratedClass(ref, path, elements, file) {
|
115 | const {
|
116 | node,
|
117 | scope
|
118 | } = path;
|
119 | const initializeId = scope.generateUidIdentifier("initialize");
|
120 | const isDeclaration = node.id && path.isDeclaration();
|
121 | const isStrict = path.isInStrictMode();
|
122 | const {
|
123 | superClass
|
124 | } = node;
|
125 | node.type = "ClassDeclaration";
|
126 | if (!node.id) node.id = _core.types.cloneNode(ref);
|
127 | let superId;
|
128 |
|
129 | if (superClass) {
|
130 | superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
|
131 | node.superClass = superId;
|
132 | }
|
133 |
|
134 | const classDecorators = takeDecorators(node);
|
135 |
|
136 | const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract && element.node.type !== "TSIndexSignature").map(path => extractElementDescriptor(file, node.id, superId, path)));
|
137 |
|
138 | const wrapperCall = _core.template.expression.ast`
|
139 | ${addDecorateHelper(file)}(
|
140 | ${classDecorators || _core.types.nullLiteral()},
|
141 | function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
|
142 | ${node}
|
143 | return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
|
144 | },
|
145 | ${superClass}
|
146 | )
|
147 | `;
|
148 |
|
149 | if (!isStrict) {
|
150 | wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
|
151 | }
|
152 |
|
153 | let replacement = wrapperCall;
|
154 | let classPathDesc = "arguments.1.body.body.0";
|
155 |
|
156 | if (isDeclaration) {
|
157 | replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
|
158 | classPathDesc = "declarations.0.init." + classPathDesc;
|
159 | }
|
160 |
|
161 | return {
|
162 | instanceNodes: [_core.template.statement.ast`${_core.types.cloneNode(initializeId)}(this)`],
|
163 |
|
164 | wrapClass(path) {
|
165 | path.replaceWith(replacement);
|
166 | return path.get(classPathDesc);
|
167 | }
|
168 |
|
169 | };
|
170 | } |
\ | No newline at end of file |