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(classRef, superRef, path) {
|
---|
55 | const {
|
---|
56 | node,
|
---|
57 | scope
|
---|
58 | } = path;
|
---|
59 | const isMethod = path.isClassMethod();
|
---|
60 |
|
---|
61 | if (path.isPrivate()) {
|
---|
62 | throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
|
---|
63 | }
|
---|
64 |
|
---|
65 | new _helperReplaceSupers.default({
|
---|
66 | methodPath: path,
|
---|
67 | objectRef: classRef,
|
---|
68 | superRef,
|
---|
69 | file: this,
|
---|
70 | refToPreserve: classRef
|
---|
71 | }).replace();
|
---|
72 | 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);
|
---|
73 |
|
---|
74 | if (_core.types.isClassMethod(node)) {
|
---|
75 | const id = node.computed ? null : node.key;
|
---|
76 |
|
---|
77 | _core.types.toExpression(node);
|
---|
78 |
|
---|
79 | properties.push(prop("value", (0, _helperFunctionName.default)({
|
---|
80 | node,
|
---|
81 | id,
|
---|
82 | scope
|
---|
83 | }) || node));
|
---|
84 | } else if (_core.types.isClassProperty(node) && node.value) {
|
---|
85 | properties.push(method("value", _core.template.statements.ast`return ${node.value}`));
|
---|
86 | } else {
|
---|
87 | properties.push(prop("value", scope.buildUndefinedNode()));
|
---|
88 | }
|
---|
89 |
|
---|
90 | path.remove();
|
---|
91 | return _core.types.objectExpression(properties);
|
---|
92 | }
|
---|
93 |
|
---|
94 | function addDecorateHelper(file) {
|
---|
95 | try {
|
---|
96 | return file.addHelper("decorate");
|
---|
97 | } catch (err) {
|
---|
98 | if (err.code === "BABEL_HELPER_UNKNOWN") {
|
---|
99 | 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.";
|
---|
100 | }
|
---|
101 |
|
---|
102 | throw err;
|
---|
103 | }
|
---|
104 | }
|
---|
105 |
|
---|
106 | function buildDecoratedClass(ref, path, elements, file) {
|
---|
107 | const {
|
---|
108 | node,
|
---|
109 | scope
|
---|
110 | } = path;
|
---|
111 | const initializeId = scope.generateUidIdentifier("initialize");
|
---|
112 | const isDeclaration = node.id && path.isDeclaration();
|
---|
113 | const isStrict = path.isInStrictMode();
|
---|
114 | const {
|
---|
115 | superClass
|
---|
116 | } = node;
|
---|
117 | node.type = "ClassDeclaration";
|
---|
118 | if (!node.id) node.id = _core.types.cloneNode(ref);
|
---|
119 | let superId;
|
---|
120 |
|
---|
121 | if (superClass) {
|
---|
122 | superId = scope.generateUidIdentifierBasedOnNode(node.superClass, "super");
|
---|
123 | node.superClass = superId;
|
---|
124 | }
|
---|
125 |
|
---|
126 | const classDecorators = takeDecorators(node);
|
---|
127 |
|
---|
128 | const definitions = _core.types.arrayExpression(elements.filter(element => !element.node.abstract).map(extractElementDescriptor.bind(file, node.id, superId)));
|
---|
129 |
|
---|
130 | const wrapperCall = _core.template.expression.ast`
|
---|
131 | ${addDecorateHelper(file)}(
|
---|
132 | ${classDecorators || _core.types.nullLiteral()},
|
---|
133 | function (${initializeId}, ${superClass ? _core.types.cloneNode(superId) : null}) {
|
---|
134 | ${node}
|
---|
135 | return { F: ${_core.types.cloneNode(node.id)}, d: ${definitions} };
|
---|
136 | },
|
---|
137 | ${superClass}
|
---|
138 | )
|
---|
139 | `;
|
---|
140 |
|
---|
141 | if (!isStrict) {
|
---|
142 | wrapperCall.arguments[1].body.directives.push(_core.types.directive(_core.types.directiveLiteral("use strict")));
|
---|
143 | }
|
---|
144 |
|
---|
145 | let replacement = wrapperCall;
|
---|
146 | let classPathDesc = "arguments.1.body.body.0";
|
---|
147 |
|
---|
148 | if (isDeclaration) {
|
---|
149 | replacement = _core.template.statement.ast`let ${ref} = ${wrapperCall}`;
|
---|
150 | classPathDesc = "declarations.0.init." + classPathDesc;
|
---|
151 | }
|
---|
152 |
|
---|
153 | return {
|
---|
154 | instanceNodes: [_core.template.statement.ast`${_core.types.cloneNode(initializeId)}(this)`],
|
---|
155 |
|
---|
156 | wrapClass(path) {
|
---|
157 | path.replaceWith(replacement);
|
---|
158 | return path.get(classPathDesc);
|
---|
159 | }
|
---|
160 |
|
---|
161 | };
|
---|
162 | } |
---|