transformer.js
6.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
"use strict";
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.default = void 0;
function _core() {
const data = require("@babel/core");
_core = function () {
return data;
};
return data;
}
function _helperSplitExportDeclaration() {
const data = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
_helperSplitExportDeclaration = function () {
return data;
};
return data;
}
function _helperReplaceSupers() {
const data = _interopRequireDefault(require("@babel/helper-replace-supers"));
_helperReplaceSupers = function () {
return data;
};
return data;
}
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function prop(key, value) {
if (!value) return null;
return _core().types.objectProperty(_core().types.identifier(key), value);
}
function value(body, params = [], async, generator) {
const method = _core().types.objectMethod("method", _core().types.identifier("value"), params, body);
method.async = !!async;
method.generator = !!generator;
return method;
}
function hasDecorators({
node
}) {
if (node.decorators && node.decorators.length > 0) return true;
const body = node.body.body;
for (let i = 0; i < body.length; i++) {
const method = body[i];
if (method.decorators && method.decorators.length > 0) {
return true;
}
}
return false;
}
function takeDecorators({
node
}) {
let result;
if (node.decorators && node.decorators.length > 0) {
result = _core().types.arrayExpression(node.decorators.map(decorator => decorator.expression));
}
node.decorators = undefined;
return result;
}
function getKey(node) {
if (node.computed) {
return node.key;
} else if (_core().types.isIdentifier(node.key)) {
return _core().types.stringLiteral(node.key.name);
} else {
return _core().types.stringLiteral(String(node.key.value));
}
}
function getSingleElementDefinition(path, superRef, classRef, file) {
const {
node,
scope
} = path;
const isMethod = path.isClassMethod();
if (path.isPrivate()) {
throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
}
new (_helperReplaceSupers().default)({
methodPath: path,
methodNode: node,
objectRef: classRef,
isStatic: node.static,
superRef,
scope,
file
}, true).replace();
const properties = [prop("kind", _core().types.stringLiteral(isMethod ? node.kind : "field")), prop("decorators", takeDecorators(path)), prop("static", node.static && _core().types.booleanLiteral(true)), prop("key", getKey(node)), isMethod ? value(node.body, node.params, node.async, node.generator) : node.value ? value(_core().template.ast`{ return ${node.value} }`) : prop("value", scope.buildUndefinedNode())].filter(Boolean);
return _core().types.objectExpression(properties);
}
function getElementsDefinitions(path, fId, file) {
const elements = [];
for (const p of path.get("body.body")) {
if (!p.isClassMethod({
kind: "constructor"
})) {
elements.push(getSingleElementDefinition(p, path.node.superClass, fId, file));
p.remove();
}
}
return _core().types.arrayExpression(elements);
}
function getConstructorPath(path) {
return path.get("body.body").find(path => path.isClassMethod({
kind: "constructor"
}));
}
const bareSupersVisitor = {
CallExpression(path, {
initializeInstanceElements
}) {
if (path.get("callee").isSuper()) {
path.insertAfter(_core().types.cloneNode(initializeInstanceElements));
}
},
Function(path) {
if (!path.isArrowFunctionExpression()) path.skip();
}
};
function insertInitializeInstanceElements(path, initializeInstanceId) {
const isBase = !path.node.superClass;
const initializeInstanceElements = _core().types.callExpression(initializeInstanceId, [_core().types.thisExpression()]);
const constructorPath = getConstructorPath(path);
if (constructorPath) {
if (isBase) {
constructorPath.get("body").unshiftContainer("body", [_core().types.expressionStatement(initializeInstanceElements)]);
} else {
constructorPath.traverse(bareSupersVisitor, {
initializeInstanceElements
});
}
} else {
const constructor = isBase ? _core().types.classMethod("constructor", _core().types.identifier("constructor"), [], _core().types.blockStatement([_core().types.expressionStatement(initializeInstanceElements)])) : _core().types.classMethod("constructor", _core().types.identifier("constructor"), [_core().types.restElement(_core().types.identifier("args"))], _core().types.blockStatement([_core().types.expressionStatement(_core().types.callExpression(_core().types.Super(), [_core().types.spreadElement(_core().types.identifier("args"))])), _core().types.expressionStatement(initializeInstanceElements)]));
path.node.body.body.push(constructor);
}
}
function transformClass(path, file) {
const isDeclaration = path.node.id && path.isDeclaration();
const isStrict = path.isInStrictMode();
const {
superClass
} = path.node;
path.node.type = "ClassDeclaration";
if (!path.node.id) path.node.id = path.scope.generateUidIdentifier("class");
const initializeId = path.scope.generateUidIdentifier("initialize");
const superId = superClass && path.scope.generateUidIdentifierBasedOnNode(path.node.superClass, "super");
if (superClass) path.node.superClass = superId;
const classDecorators = takeDecorators(path);
const definitions = getElementsDefinitions(path, path.node.id, file);
insertInitializeInstanceElements(path, initializeId);
const expr = _core().template.expression.ast`
${addDecorateHelper(file)}(
${classDecorators || _core().types.nullLiteral()},
function (${initializeId}, ${superClass ? superId : null}) {
${path.node}
return { F: ${_core().types.cloneNode(path.node.id)}, d: ${definitions} };
},
${superClass}
)
`;
if (!isStrict) {
expr.arguments[1].body.directives.push(_core().types.directive(_core().types.directiveLiteral("use strict")));
}
return isDeclaration ? _core().template.ast`let ${path.node.id} = ${expr}` : expr;
}
function addDecorateHelper(file) {
try {
return file.addHelper("decorate");
} catch (err) {
if (err.code === "BABEL_HELPER_UNKNOWN") {
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.";
}
throw err;
}
}
var _default = {
ExportDefaultDeclaration(path) {
let decl = path.get("declaration");
if (!decl.isClassDeclaration() || !hasDecorators(decl)) return;
if (decl.node.id) decl = (0, _helperSplitExportDeclaration().default)(path);
decl.replaceWith(transformClass(decl, this.file));
},
Class(path) {
if (hasDecorators(path)) {
path.replaceWith(transformClass(path, this.file));
}
}
};
exports.default = _default;