@babel/plugin-proposal-decorators 7.0.0 → 7.1.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +1 -1
- package/lib/index.js +9 -6
- package/lib/transformer.js +227 -1
- package/package.json +8 -3
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2014-2018 Sebastian McKenzie
|
|
3
|
+
Copyright (c) 2014-2018 Sebastian McKenzie and other contributors
|
|
4
4
|
|
|
5
5
|
Permission is hereby granted, free of charge, to any person obtaining
|
|
6
6
|
a copy of this software and associated documentation files (the
|
package/lib/index.js
CHANGED
|
@@ -34,19 +34,22 @@ function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { de
|
|
|
34
34
|
var _default = (0, _helperPluginUtils().declare)((api, options) => {
|
|
35
35
|
api.assertVersion(7);
|
|
36
36
|
const {
|
|
37
|
-
legacy = false
|
|
38
|
-
decoratorsBeforeExport
|
|
37
|
+
legacy = false
|
|
39
38
|
} = options;
|
|
40
39
|
|
|
41
40
|
if (typeof legacy !== "boolean") {
|
|
42
41
|
throw new Error("'legacy' must be a boolean.");
|
|
43
42
|
}
|
|
44
43
|
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
}
|
|
44
|
+
const {
|
|
45
|
+
decoratorsBeforeExport
|
|
46
|
+
} = options;
|
|
48
47
|
|
|
49
|
-
if (decoratorsBeforeExport
|
|
48
|
+
if (decoratorsBeforeExport === undefined) {
|
|
49
|
+
if (!legacy) {
|
|
50
|
+
throw new Error("The decorators plugin requires a 'decoratorsBeforeExport' option," + " whose value must be a boolean. If you want to use the legacy" + " decorators semantics, you can set the 'legacy: true' option.");
|
|
51
|
+
}
|
|
52
|
+
} else {
|
|
50
53
|
if (legacy) {
|
|
51
54
|
throw new Error("'decoratorsBeforeExport' can't be used with legacy decorators.");
|
|
52
55
|
}
|
package/lib/transformer.js
CHANGED
|
@@ -4,5 +4,231 @@ Object.defineProperty(exports, "__esModule", {
|
|
|
4
4
|
value: true
|
|
5
5
|
});
|
|
6
6
|
exports.default = void 0;
|
|
7
|
-
|
|
7
|
+
|
|
8
|
+
function _core() {
|
|
9
|
+
const data = require("@babel/core");
|
|
10
|
+
|
|
11
|
+
_core = function () {
|
|
12
|
+
return data;
|
|
13
|
+
};
|
|
14
|
+
|
|
15
|
+
return data;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function _helperSplitExportDeclaration() {
|
|
19
|
+
const data = _interopRequireDefault(require("@babel/helper-split-export-declaration"));
|
|
20
|
+
|
|
21
|
+
_helperSplitExportDeclaration = function () {
|
|
22
|
+
return data;
|
|
23
|
+
};
|
|
24
|
+
|
|
25
|
+
return data;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
function _helperReplaceSupers() {
|
|
29
|
+
const data = _interopRequireDefault(require("@babel/helper-replace-supers"));
|
|
30
|
+
|
|
31
|
+
_helperReplaceSupers = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
|
39
|
+
|
|
40
|
+
function prop(key, value) {
|
|
41
|
+
if (!value) return null;
|
|
42
|
+
return _core().types.objectProperty(_core().types.identifier(key), value);
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function value(body, params = [], async, generator) {
|
|
46
|
+
const method = _core().types.objectMethod("method", _core().types.identifier("value"), params, body);
|
|
47
|
+
|
|
48
|
+
method.async = !!async;
|
|
49
|
+
method.generator = !!generator;
|
|
50
|
+
return method;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
function hasDecorators({
|
|
54
|
+
node
|
|
55
|
+
}) {
|
|
56
|
+
if (node.decorators && node.decorators.length > 0) return true;
|
|
57
|
+
const body = node.body.body;
|
|
58
|
+
|
|
59
|
+
for (let i = 0; i < body.length; i++) {
|
|
60
|
+
const method = body[i];
|
|
61
|
+
|
|
62
|
+
if (method.decorators && method.decorators.length > 0) {
|
|
63
|
+
return true;
|
|
64
|
+
}
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
|
|
70
|
+
function takeDecorators({
|
|
71
|
+
node
|
|
72
|
+
}) {
|
|
73
|
+
let result;
|
|
74
|
+
|
|
75
|
+
if (node.decorators && node.decorators.length > 0) {
|
|
76
|
+
result = _core().types.arrayExpression(node.decorators.map(decorator => decorator.expression));
|
|
77
|
+
}
|
|
78
|
+
|
|
79
|
+
node.decorators = undefined;
|
|
80
|
+
return result;
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
function getKey(node) {
|
|
84
|
+
if (node.computed) {
|
|
85
|
+
return node.key;
|
|
86
|
+
} else if (_core().types.isIdentifier(node.key)) {
|
|
87
|
+
return _core().types.stringLiteral(node.key.name);
|
|
88
|
+
} else {
|
|
89
|
+
return _core().types.stringLiteral(String(node.key.value));
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
function getSingleElementDefinition(path, superRef, classRef, file) {
|
|
94
|
+
const {
|
|
95
|
+
node,
|
|
96
|
+
scope
|
|
97
|
+
} = path;
|
|
98
|
+
const isMethod = path.isClassMethod();
|
|
99
|
+
|
|
100
|
+
if (path.isPrivate()) {
|
|
101
|
+
throw path.buildCodeFrameError(`Private ${isMethod ? "methods" : "fields"} in decorated classes are not supported yet.`);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
new (_helperReplaceSupers().default)({
|
|
105
|
+
methodPath: path,
|
|
106
|
+
methodNode: node,
|
|
107
|
+
objectRef: classRef,
|
|
108
|
+
isStatic: node.static,
|
|
109
|
+
superRef,
|
|
110
|
+
scope,
|
|
111
|
+
file
|
|
112
|
+
}, true).replace();
|
|
113
|
+
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);
|
|
114
|
+
return _core().types.objectExpression(properties);
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
function getElementsDefinitions(path, fId, file) {
|
|
118
|
+
const elements = [];
|
|
119
|
+
|
|
120
|
+
for (const p of path.get("body.body")) {
|
|
121
|
+
if (!p.isClassMethod({
|
|
122
|
+
kind: "constructor"
|
|
123
|
+
})) {
|
|
124
|
+
elements.push(getSingleElementDefinition(p, path.node.superClass, fId, file));
|
|
125
|
+
p.remove();
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
return _core().types.arrayExpression(elements);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
function getConstructorPath(path) {
|
|
133
|
+
return path.get("body.body").find(path => path.isClassMethod({
|
|
134
|
+
kind: "constructor"
|
|
135
|
+
}));
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
const bareSupersVisitor = {
|
|
139
|
+
CallExpression(path, {
|
|
140
|
+
initializeInstanceElements
|
|
141
|
+
}) {
|
|
142
|
+
if (path.get("callee").isSuper()) {
|
|
143
|
+
path.insertAfter(_core().types.cloneNode(initializeInstanceElements));
|
|
144
|
+
path.skip();
|
|
145
|
+
}
|
|
146
|
+
},
|
|
147
|
+
|
|
148
|
+
Function(path) {
|
|
149
|
+
if (!path.isArrowFunctionExpression()) path.skip();
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
};
|
|
153
|
+
|
|
154
|
+
function insertInitializeInstanceElements(path, initializeInstanceId) {
|
|
155
|
+
const isBase = !path.node.superClass;
|
|
156
|
+
|
|
157
|
+
const initializeInstanceElements = _core().types.callExpression(initializeInstanceId, [_core().types.thisExpression()]);
|
|
158
|
+
|
|
159
|
+
const constructorPath = getConstructorPath(path);
|
|
160
|
+
|
|
161
|
+
if (constructorPath) {
|
|
162
|
+
if (isBase) {
|
|
163
|
+
constructorPath.get("body").unshiftContainer("body", [_core().types.expressionStatement(initializeInstanceElements)]);
|
|
164
|
+
} else {
|
|
165
|
+
constructorPath.traverse(bareSupersVisitor, {
|
|
166
|
+
initializeInstanceElements
|
|
167
|
+
});
|
|
168
|
+
}
|
|
169
|
+
} else {
|
|
170
|
+
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)]));
|
|
171
|
+
path.node.body.body.push(constructor);
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
function transformClass(path, file) {
|
|
176
|
+
const isDeclaration = path.node.id && path.isDeclaration();
|
|
177
|
+
const isStrict = path.isInStrictMode();
|
|
178
|
+
const {
|
|
179
|
+
superClass
|
|
180
|
+
} = path.node;
|
|
181
|
+
path.node.type = "ClassDeclaration";
|
|
182
|
+
if (!path.node.id) path.node.id = path.scope.generateUidIdentifier("class");
|
|
183
|
+
const initializeId = path.scope.generateUidIdentifier("initialize");
|
|
184
|
+
const superId = superClass && path.scope.generateUidIdentifierBasedOnNode(path.node.superClass, "super");
|
|
185
|
+
if (superClass) path.node.superClass = superId;
|
|
186
|
+
const classDecorators = takeDecorators(path);
|
|
187
|
+
const definitions = getElementsDefinitions(path, path.node.id, file);
|
|
188
|
+
insertInitializeInstanceElements(path, initializeId);
|
|
189
|
+
const expr = _core().template.expression.ast`
|
|
190
|
+
${addDecorateHelper(file)}(
|
|
191
|
+
${classDecorators || _core().types.nullLiteral()},
|
|
192
|
+
function (${initializeId}, ${superClass ? superId : null}) {
|
|
193
|
+
${path.node}
|
|
194
|
+
return { F: ${_core().types.cloneNode(path.node.id)}, d: ${definitions} };
|
|
195
|
+
},
|
|
196
|
+
${superClass}
|
|
197
|
+
)
|
|
198
|
+
`;
|
|
199
|
+
|
|
200
|
+
if (!isStrict) {
|
|
201
|
+
expr.arguments[1].body.directives.push(_core().types.directive(_core().types.directiveLiteral("use strict")));
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
return isDeclaration ? _core().template.ast`let ${path.node.id} = ${expr}` : expr;
|
|
205
|
+
}
|
|
206
|
+
|
|
207
|
+
function addDecorateHelper(file) {
|
|
208
|
+
try {
|
|
209
|
+
return file.addHelper("decorate");
|
|
210
|
+
} catch (err) {
|
|
211
|
+
if (err.code === "BABEL_HELPER_UNKNOWN") {
|
|
212
|
+
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.";
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
throw err;
|
|
216
|
+
}
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
var _default = {
|
|
220
|
+
ExportDefaultDeclaration(path) {
|
|
221
|
+
let decl = path.get("declaration");
|
|
222
|
+
if (!decl.isClassDeclaration() || !hasDecorators(decl)) return;
|
|
223
|
+
if (decl.node.id) decl = (0, _helperSplitExportDeclaration().default)(path);
|
|
224
|
+
decl.replaceWith(transformClass(decl, this.file));
|
|
225
|
+
},
|
|
226
|
+
|
|
227
|
+
Class(path) {
|
|
228
|
+
if (hasDecorators(path)) {
|
|
229
|
+
path.replaceWith(transformClass(path, this.file));
|
|
230
|
+
}
|
|
231
|
+
}
|
|
232
|
+
|
|
233
|
+
};
|
|
8
234
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,8 +1,11 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@babel/plugin-proposal-decorators",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.1.6",
|
|
4
4
|
"author": "Logan Smyth <loganfsmyth@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
|
+
"publishConfig": {
|
|
7
|
+
"access": "public"
|
|
8
|
+
},
|
|
6
9
|
"description": "Compile class and object decorators to ES5",
|
|
7
10
|
"repository": "https://github.com/babel/babel/tree/master/packages/babel-plugin-proposal-decorators",
|
|
8
11
|
"main": "lib/index.js",
|
|
@@ -13,13 +16,15 @@
|
|
|
13
16
|
],
|
|
14
17
|
"dependencies": {
|
|
15
18
|
"@babel/helper-plugin-utils": "^7.0.0",
|
|
16
|
-
"@babel/
|
|
19
|
+
"@babel/helper-replace-supers": "^7.1.0",
|
|
20
|
+
"@babel/helper-split-export-declaration": "^7.0.0",
|
|
21
|
+
"@babel/plugin-syntax-decorators": "^7.1.0"
|
|
17
22
|
},
|
|
18
23
|
"peerDependencies": {
|
|
19
24
|
"@babel/core": "^7.0.0-0"
|
|
20
25
|
},
|
|
21
26
|
"devDependencies": {
|
|
22
|
-
"@babel/core": "^7.
|
|
27
|
+
"@babel/core": "^7.1.6",
|
|
23
28
|
"@babel/helper-plugin-test-runner": "^7.0.0"
|
|
24
29
|
}
|
|
25
30
|
}
|