@babel/plugin-proposal-decorators 7.1.6 → 7.3.0
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 +34 -7
- package/package.json +4 -5
- package/lib/transformer.js +0 -234
package/LICENSE
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
MIT License
|
|
2
2
|
|
|
3
|
-
Copyright (c) 2014-
|
|
3
|
+
Copyright (c) 2014-present 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
|
@@ -25,7 +25,15 @@ function _pluginSyntaxDecorators() {
|
|
|
25
25
|
return data;
|
|
26
26
|
}
|
|
27
27
|
|
|
28
|
-
|
|
28
|
+
function _helperCreateClassFeaturesPlugin() {
|
|
29
|
+
const data = require("@babel/helper-create-class-features-plugin");
|
|
30
|
+
|
|
31
|
+
_helperCreateClassFeaturesPlugin = function () {
|
|
32
|
+
return data;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
return data;
|
|
36
|
+
}
|
|
29
37
|
|
|
30
38
|
var _transformerLegacy = _interopRequireDefault(require("./transformer-legacy"));
|
|
31
39
|
|
|
@@ -59,17 +67,36 @@ var _default = (0, _helperPluginUtils().declare)((api, options) => {
|
|
|
59
67
|
}
|
|
60
68
|
}
|
|
61
69
|
|
|
62
|
-
|
|
63
|
-
|
|
70
|
+
if (legacy) {
|
|
71
|
+
return {
|
|
72
|
+
name: "proposal-decorators",
|
|
73
|
+
inherits: _pluginSyntaxDecorators().default,
|
|
74
|
+
|
|
75
|
+
manipulateOptions({
|
|
76
|
+
generatorOpts
|
|
77
|
+
}) {
|
|
78
|
+
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
|
|
79
|
+
},
|
|
80
|
+
|
|
81
|
+
visitor: _transformerLegacy.default
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
return (0, _helperCreateClassFeaturesPlugin().createClassFeaturePlugin)({
|
|
86
|
+
name: "proposal-decorators",
|
|
87
|
+
feature: _helperCreateClassFeaturesPlugin().FEATURES.decorators,
|
|
64
88
|
|
|
65
89
|
manipulateOptions({
|
|
66
|
-
generatorOpts
|
|
90
|
+
generatorOpts,
|
|
91
|
+
parserOpts
|
|
67
92
|
}) {
|
|
93
|
+
parserOpts.plugins.push(["decorators", {
|
|
94
|
+
decoratorsBeforeExport
|
|
95
|
+
}]);
|
|
68
96
|
generatorOpts.decoratorsBeforeExport = decoratorsBeforeExport;
|
|
69
|
-
}
|
|
97
|
+
}
|
|
70
98
|
|
|
71
|
-
|
|
72
|
-
};
|
|
99
|
+
});
|
|
73
100
|
});
|
|
74
101
|
|
|
75
102
|
exports.default = _default;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@babel/plugin-proposal-decorators",
|
|
3
|
-
"version": "7.
|
|
3
|
+
"version": "7.3.0",
|
|
4
4
|
"author": "Logan Smyth <loganfsmyth@gmail.com>",
|
|
5
5
|
"license": "MIT",
|
|
6
6
|
"publishConfig": {
|
|
@@ -15,16 +15,15 @@
|
|
|
15
15
|
"decorators"
|
|
16
16
|
],
|
|
17
17
|
"dependencies": {
|
|
18
|
+
"@babel/helper-create-class-features-plugin": "^7.3.0",
|
|
18
19
|
"@babel/helper-plugin-utils": "^7.0.0",
|
|
19
|
-
"@babel/
|
|
20
|
-
"@babel/helper-split-export-declaration": "^7.0.0",
|
|
21
|
-
"@babel/plugin-syntax-decorators": "^7.1.0"
|
|
20
|
+
"@babel/plugin-syntax-decorators": "^7.2.0"
|
|
22
21
|
},
|
|
23
22
|
"peerDependencies": {
|
|
24
23
|
"@babel/core": "^7.0.0-0"
|
|
25
24
|
},
|
|
26
25
|
"devDependencies": {
|
|
27
|
-
"@babel/core": "^7.
|
|
26
|
+
"@babel/core": "^7.2.2",
|
|
28
27
|
"@babel/helper-plugin-test-runner": "^7.0.0"
|
|
29
28
|
}
|
|
30
29
|
}
|
package/lib/transformer.js
DELETED
|
@@ -1,234 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
|
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
|
4
|
-
value: true
|
|
5
|
-
});
|
|
6
|
-
exports.default = void 0;
|
|
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
|
-
};
|
|
234
|
-
exports.default = _default;
|