@babel/plugin-proposal-decorators 7.0.0-beta.43 → 7.0.0-beta.47

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/README.md CHANGED
@@ -62,11 +62,33 @@ Add the following line to your .babelrc file:
62
62
  }
63
63
  ```
64
64
 
65
- #### NOTE: Order of Plugins Matters!
65
+ ### Via CLI
66
+
67
+ ```sh
68
+ babel --plugins @babel/plugin-proposal-decorators script.js
69
+ ```
70
+
71
+ ### Via Node API
72
+
73
+ ```javascript
74
+ require("@babel/core").transform("code", {
75
+ plugins: ["@babel/plugin-proposal-decorators"]
76
+ });
77
+ ```
78
+
79
+ ## Options
80
+
81
+ ### `legacy`
82
+
83
+ `boolean`, defaults to `false`.
84
+
85
+ Use the legacy (stage 1) decorators syntax and behavior.
86
+
87
+ #### NOTE: Compatibility with `@babel/plugin-proposal-class-properties`
66
88
 
67
89
  If you are including your plugins manually and using `@babel/plugin-proposal-class-properties`, make sure that `@babel/plugin-proposal-decorators` comes *before* `@babel/plugin-proposal-class-properties`.
68
90
 
69
- Currently, `@babel/plugin-proposal-class-properties` must be used in `loose` mode to support the `@babel/plugin-proposal-decorators`. To use `@babel/plugin-proposal-class-properties` in spec mode with decorators, wait for the next major version of decorators (Stage 2).
91
+ When using the `legacy: true` mode, `@babel/plugin-proposal-class-properties` must be used in `loose` mode to support the `@babel/plugin-proposal-decorators`.
70
92
 
71
93
  Wrong:
72
94
 
@@ -85,23 +107,18 @@ Right:
85
107
  {
86
108
  "plugins": [
87
109
  "@babel/plugin-proposal-decorators",
88
- ["@babel/plugin-proposal-class-properties", { "loose" : true }]
110
+ "@babel/plugin-proposal-class-properties"
89
111
  ]
90
112
  }
91
113
  ```
92
114
 
93
- ### Via CLI
94
-
95
- ```sh
96
- babel --plugins @babel/plugin-proposal-decorators script.js
97
- ```
98
-
99
- ### Via Node API
100
-
101
- ```javascript
102
- require("@babel/core").transform("code", {
103
- plugins: ["@babel/plugin-proposal-decorators"]
104
- });
115
+ ```json
116
+ {
117
+ "plugins": [
118
+ ["@babel/plugin-proposal-decorators", { "legacy": true }],
119
+ ["@babel/plugin-proposal-class-properties", { "loose" : true }]
120
+ ]
121
+ }
105
122
  ```
106
123
 
107
124
  ## References
package/lib/index.js CHANGED
@@ -8,7 +8,7 @@ exports.default = void 0;
8
8
  function _helperPluginUtils() {
9
9
  const data = require("@babel/helper-plugin-utils");
10
10
 
11
- _helperPluginUtils = function () {
11
+ _helperPluginUtils = function _helperPluginUtils() {
12
12
  return data;
13
13
  };
14
14
 
@@ -18,173 +18,35 @@ function _helperPluginUtils() {
18
18
  function _pluginSyntaxDecorators() {
19
19
  const data = _interopRequireDefault(require("@babel/plugin-syntax-decorators"));
20
20
 
21
- _pluginSyntaxDecorators = function () {
21
+ _pluginSyntaxDecorators = function _pluginSyntaxDecorators() {
22
22
  return data;
23
23
  };
24
24
 
25
25
  return data;
26
26
  }
27
27
 
28
- function _core() {
29
- const data = require("@babel/core");
28
+ var _transformer = _interopRequireDefault(require("./transformer"));
30
29
 
31
- _core = function () {
32
- return data;
33
- };
34
-
35
- return data;
36
- }
30
+ var _transformerLegacy = _interopRequireDefault(require("./transformer-legacy"));
37
31
 
38
32
  function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
39
33
 
40
- const buildClassDecorator = (0, _core().template)(`
41
- DECORATOR(CLASS_REF = INNER) || CLASS_REF;
42
- `);
43
- const buildClassPrototype = (0, _core().template)(`
44
- CLASS_REF.prototype;
45
- `);
46
- const buildGetDescriptor = (0, _core().template)(`
47
- Object.getOwnPropertyDescriptor(TARGET, PROPERTY);
48
- `);
49
- const buildGetObjectInitializer = (0, _core().template)(`
50
- (TEMP = Object.getOwnPropertyDescriptor(TARGET, PROPERTY), (TEMP = TEMP ? TEMP.value : undefined), {
51
- enumerable: true,
52
- configurable: true,
53
- writable: true,
54
- initializer: function(){
55
- return TEMP;
56
- }
57
- })
58
- `);
59
-
60
- var _default = (0, _helperPluginUtils().declare)(api => {
34
+ var _default = (0, _helperPluginUtils().declare)((api, options) => {
61
35
  api.assertVersion(7);
62
- const WARNING_CALLS = new WeakSet();
63
-
64
- function applyEnsureOrdering(path) {
65
- const decorators = (path.isClass() ? [path].concat(path.get("body.body")) : path.get("properties")).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []);
66
- const identDecorators = decorators.filter(decorator => !_core().types.isIdentifier(decorator.expression));
67
- if (identDecorators.length === 0) return;
68
- return _core().types.sequenceExpression(identDecorators.map(decorator => {
69
- const expression = decorator.expression;
70
- const id = decorator.expression = path.scope.generateDeclaredUidIdentifier("dec");
71
- return _core().types.assignmentExpression("=", id, expression);
72
- }).concat([path.node]));
73
- }
74
-
75
- function applyClassDecorators(classPath) {
76
- if (!hasClassDecorators(classPath.node)) return;
77
- const decorators = classPath.node.decorators || [];
78
- classPath.node.decorators = null;
79
- const name = classPath.scope.generateDeclaredUidIdentifier("class");
80
- return decorators.map(dec => dec.expression).reverse().reduce(function (acc, decorator) {
81
- return buildClassDecorator({
82
- CLASS_REF: _core().types.cloneNode(name),
83
- DECORATOR: _core().types.cloneNode(decorator),
84
- INNER: acc
85
- }).expression;
86
- }, classPath.node);
87
- }
88
-
89
- function hasClassDecorators(classNode) {
90
- return !!(classNode.decorators && classNode.decorators.length);
91
- }
92
-
93
- function applyMethodDecorators(path, state) {
94
- if (!hasMethodDecorators(path.node.body.body)) return;
95
- return applyTargetDecorators(path, state, path.node.body.body);
96
- }
36
+ const _options$legacy = options.legacy,
37
+ legacy = _options$legacy === void 0 ? false : _options$legacy;
97
38
 
98
- function hasMethodDecorators(body) {
99
- return body.some(node => node.decorators && node.decorators.length);
39
+ if (typeof legacy !== "boolean") {
40
+ throw new Error("'legacy' must be a boolean.");
100
41
  }
101
42
 
102
- function applyObjectDecorators(path, state) {
103
- if (!hasMethodDecorators(path.node.properties)) return;
104
- return applyTargetDecorators(path, state, path.node.properties);
105
- }
106
-
107
- function applyTargetDecorators(path, state, decoratedProps) {
108
- const name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? "class" : "obj");
109
- const exprs = decoratedProps.reduce(function (acc, node) {
110
- const decorators = node.decorators || [];
111
- node.decorators = null;
112
- if (decorators.length === 0) return acc;
113
-
114
- if (node.computed) {
115
- throw path.buildCodeFrameError("Computed method/property decorators are not yet supported.");
116
- }
117
-
118
- const property = _core().types.isLiteral(node.key) ? node.key : _core().types.stringLiteral(node.key.name);
119
- const target = path.isClass() && !node.static ? buildClassPrototype({
120
- CLASS_REF: name
121
- }).expression : name;
122
-
123
- if (_core().types.isClassProperty(node, {
124
- static: false
125
- })) {
126
- const descriptor = path.scope.generateDeclaredUidIdentifier("descriptor");
127
- const initializer = node.value ? _core().types.functionExpression(null, [], _core().types.blockStatement([_core().types.returnStatement(node.value)])) : _core().types.nullLiteral();
128
- node.value = _core().types.callExpression(state.addHelper("initializerWarningHelper"), [descriptor, _core().types.thisExpression()]);
129
- WARNING_CALLS.add(node.value);
130
- acc = acc.concat([_core().types.assignmentExpression("=", descriptor, _core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.expression))), _core().types.objectExpression([_core().types.objectProperty(_core().types.identifier("enumerable"), _core().types.booleanLiteral(true)), _core().types.objectProperty(_core().types.identifier("initializer"), initializer)])]))]);
131
- } else {
132
- acc = acc.concat(_core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.expression))), _core().types.isObjectProperty(node) || _core().types.isClassProperty(node, {
133
- static: true
134
- }) ? buildGetObjectInitializer({
135
- TEMP: path.scope.generateDeclaredUidIdentifier("init"),
136
- TARGET: _core().types.cloneNode(target),
137
- PROPERTY: _core().types.cloneNode(property)
138
- }).expression : buildGetDescriptor({
139
- TARGET: _core().types.cloneNode(target),
140
- PROPERTY: _core().types.cloneNode(property)
141
- }).expression, _core().types.cloneNode(target)]));
142
- }
143
-
144
- return acc;
145
- }, []);
146
- return _core().types.sequenceExpression([_core().types.assignmentExpression("=", _core().types.cloneNode(name), path.node), _core().types.sequenceExpression(exprs), _core().types.cloneNode(name)]);
43
+ if (legacy !== true) {
44
+ throw new Error("The new decorators proposal is not supported yet." + ' You must pass the `"legacy": true` option to' + " @babel/plugin-proposal-decorators");
147
45
  }
148
46
 
149
47
  return {
150
48
  inherits: _pluginSyntaxDecorators().default,
151
- visitor: {
152
- ClassDeclaration(path) {
153
- const {
154
- node
155
- } = path;
156
-
157
- if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) {
158
- return;
159
- }
160
-
161
- const ref = node.id ? _core().types.cloneNode(node.id) : path.scope.generateUidIdentifier("class");
162
-
163
- const letDeclaration = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(ref, _core().types.toExpression(node))]);
164
-
165
- if (path.parentPath.isExportDefaultDeclaration()) {
166
- path.parentPath.replaceWithMultiple([letDeclaration, _core().types.exportNamedDeclaration(null, [_core().types.exportSpecifier(_core().types.cloneNode(ref), _core().types.identifier("default"))])]);
167
- } else {
168
- path.replaceWith(letDeclaration);
169
- }
170
- },
171
-
172
- ClassExpression(path, state) {
173
- const decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path, state) || applyMethodDecorators(path, state);
174
- if (decoratedClass) path.replaceWith(decoratedClass);
175
- },
176
-
177
- ObjectExpression(path, state) {
178
- const decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
179
- if (decoratedObject) path.replaceWith(decoratedObject);
180
- },
181
-
182
- AssignmentExpression(path, state) {
183
- if (!WARNING_CALLS.has(path.node.right)) return;
184
- path.replaceWith(_core().types.callExpression(state.addHelper("initializerDefineProperty"), [_core().types.cloneNode(path.get("left.object").node), _core().types.stringLiteral(path.get("left.property").node.name), _core().types.cloneNode(path.get("right.arguments")[0].node), _core().types.cloneNode(path.get("right.arguments")[1].node)]));
185
- }
186
-
187
- }
49
+ visitor: legacy ? _transformerLegacy.default : _transformer.default
188
50
  };
189
51
  });
190
52
 
@@ -0,0 +1,159 @@
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 _core() {
12
+ return data;
13
+ };
14
+
15
+ return data;
16
+ }
17
+
18
+ const buildClassDecorator = (0, _core().template)(`
19
+ DECORATOR(CLASS_REF = INNER) || CLASS_REF;
20
+ `);
21
+ const buildClassPrototype = (0, _core().template)(`
22
+ CLASS_REF.prototype;
23
+ `);
24
+ const buildGetDescriptor = (0, _core().template)(`
25
+ Object.getOwnPropertyDescriptor(TARGET, PROPERTY);
26
+ `);
27
+ const buildGetObjectInitializer = (0, _core().template)(`
28
+ (TEMP = Object.getOwnPropertyDescriptor(TARGET, PROPERTY), (TEMP = TEMP ? TEMP.value : undefined), {
29
+ enumerable: true,
30
+ configurable: true,
31
+ writable: true,
32
+ initializer: function(){
33
+ return TEMP;
34
+ }
35
+ })
36
+ `);
37
+ const WARNING_CALLS = new WeakSet();
38
+
39
+ function applyEnsureOrdering(path) {
40
+ const decorators = (path.isClass() ? [path].concat(path.get("body.body")) : path.get("properties")).reduce((acc, prop) => acc.concat(prop.node.decorators || []), []);
41
+ const identDecorators = decorators.filter(decorator => !_core().types.isIdentifier(decorator.callee));
42
+ if (identDecorators.length === 0) return;
43
+ return _core().types.sequenceExpression(identDecorators.map(decorator => {
44
+ const callee = decorator.callee;
45
+ const id = decorator.callee = path.scope.generateDeclaredUidIdentifier("dec");
46
+ return _core().types.assignmentExpression("=", id, callee);
47
+ }).concat([path.node]));
48
+ }
49
+
50
+ function applyClassDecorators(classPath) {
51
+ if (!hasClassDecorators(classPath.node)) return;
52
+ const decorators = classPath.node.decorators || [];
53
+ classPath.node.decorators = null;
54
+ const name = classPath.scope.generateDeclaredUidIdentifier("class");
55
+ return decorators.map(dec => dec.callee).reverse().reduce(function (acc, decorator) {
56
+ return buildClassDecorator({
57
+ CLASS_REF: _core().types.cloneNode(name),
58
+ DECORATOR: _core().types.cloneNode(decorator),
59
+ INNER: acc
60
+ }).expression;
61
+ }, classPath.node);
62
+ }
63
+
64
+ function hasClassDecorators(classNode) {
65
+ return !!(classNode.decorators && classNode.decorators.length);
66
+ }
67
+
68
+ function applyMethodDecorators(path, state) {
69
+ if (!hasMethodDecorators(path.node.body.body)) return;
70
+ return applyTargetDecorators(path, state, path.node.body.body);
71
+ }
72
+
73
+ function hasMethodDecorators(body) {
74
+ return body.some(node => node.decorators && node.decorators.length);
75
+ }
76
+
77
+ function applyObjectDecorators(path, state) {
78
+ if (!hasMethodDecorators(path.node.properties)) return;
79
+ return applyTargetDecorators(path, state, path.node.properties);
80
+ }
81
+
82
+ function applyTargetDecorators(path, state, decoratedProps) {
83
+ const name = path.scope.generateDeclaredUidIdentifier(path.isClass() ? "class" : "obj");
84
+ const exprs = decoratedProps.reduce(function (acc, node) {
85
+ const decorators = node.decorators || [];
86
+ node.decorators = null;
87
+ if (decorators.length === 0) return acc;
88
+
89
+ if (node.computed) {
90
+ throw path.buildCodeFrameError("Computed method/property decorators are not yet supported.");
91
+ }
92
+
93
+ const property = _core().types.isLiteral(node.key) ? node.key : _core().types.stringLiteral(node.key.name);
94
+ const target = path.isClass() && !node.static ? buildClassPrototype({
95
+ CLASS_REF: name
96
+ }).expression : name;
97
+
98
+ if (_core().types.isClassProperty(node, {
99
+ static: false
100
+ })) {
101
+ const descriptor = path.scope.generateDeclaredUidIdentifier("descriptor");
102
+ const initializer = node.value ? _core().types.functionExpression(null, [], _core().types.blockStatement([_core().types.returnStatement(node.value)])) : _core().types.nullLiteral();
103
+ node.value = _core().types.callExpression(state.addHelper("initializerWarningHelper"), [descriptor, _core().types.thisExpression()]);
104
+ WARNING_CALLS.add(node.value);
105
+ acc = acc.concat([_core().types.assignmentExpression("=", descriptor, _core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.objectExpression([_core().types.objectProperty(_core().types.identifier("enumerable"), _core().types.booleanLiteral(true)), _core().types.objectProperty(_core().types.identifier("initializer"), initializer)])]))]);
106
+ } else {
107
+ acc = acc.concat(_core().types.callExpression(state.addHelper("applyDecoratedDescriptor"), [_core().types.cloneNode(target), _core().types.cloneNode(property), _core().types.arrayExpression(decorators.map(dec => _core().types.cloneNode(dec.callee))), _core().types.isObjectProperty(node) || _core().types.isClassProperty(node, {
108
+ static: true
109
+ }) ? buildGetObjectInitializer({
110
+ TEMP: path.scope.generateDeclaredUidIdentifier("init"),
111
+ TARGET: _core().types.cloneNode(target),
112
+ PROPERTY: _core().types.cloneNode(property)
113
+ }).expression : buildGetDescriptor({
114
+ TARGET: _core().types.cloneNode(target),
115
+ PROPERTY: _core().types.cloneNode(property)
116
+ }).expression, _core().types.cloneNode(target)]));
117
+ }
118
+
119
+ return acc;
120
+ }, []);
121
+ return _core().types.sequenceExpression([_core().types.assignmentExpression("=", _core().types.cloneNode(name), path.node), _core().types.sequenceExpression(exprs), _core().types.cloneNode(name)]);
122
+ }
123
+
124
+ var _default = {
125
+ ClassDeclaration(path) {
126
+ const node = path.node;
127
+
128
+ if (!hasClassDecorators(node) && !hasMethodDecorators(node.body.body)) {
129
+ return;
130
+ }
131
+
132
+ const ref = node.id ? _core().types.cloneNode(node.id) : path.scope.generateUidIdentifier("class");
133
+
134
+ const letDeclaration = _core().types.variableDeclaration("let", [_core().types.variableDeclarator(ref, _core().types.toExpression(node))]);
135
+
136
+ if (path.parentPath.isExportDefaultDeclaration()) {
137
+ path.parentPath.replaceWithMultiple([letDeclaration, _core().types.exportNamedDeclaration(null, [_core().types.exportSpecifier(_core().types.cloneNode(ref), _core().types.identifier("default"))])]);
138
+ } else {
139
+ path.replaceWith(letDeclaration);
140
+ }
141
+ },
142
+
143
+ ClassExpression(path, state) {
144
+ const decoratedClass = applyEnsureOrdering(path) || applyClassDecorators(path, state) || applyMethodDecorators(path, state);
145
+ if (decoratedClass) path.replaceWith(decoratedClass);
146
+ },
147
+
148
+ ObjectExpression(path, state) {
149
+ const decoratedObject = applyEnsureOrdering(path) || applyObjectDecorators(path, state);
150
+ if (decoratedObject) path.replaceWith(decoratedObject);
151
+ },
152
+
153
+ AssignmentExpression(path, state) {
154
+ if (!WARNING_CALLS.has(path.node.right)) return;
155
+ path.replaceWith(_core().types.callExpression(state.addHelper("initializerDefineProperty"), [_core().types.cloneNode(path.get("left.object").node), _core().types.stringLiteral(path.get("left.property").node.name), _core().types.cloneNode(path.get("right.arguments")[0].node), _core().types.cloneNode(path.get("right.arguments")[1].node)]));
156
+ }
157
+
158
+ };
159
+ exports.default = _default;
@@ -0,0 +1,8 @@
1
+ "use strict";
2
+
3
+ Object.defineProperty(exports, "__esModule", {
4
+ value: true
5
+ });
6
+ exports.default = void 0;
7
+ var _default = {};
8
+ exports.default = _default;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/plugin-proposal-decorators",
3
- "version": "7.0.0-beta.43",
3
+ "version": "7.0.0-beta.47",
4
4
  "author": "Logan Smyth <loganfsmyth@gmail.com>",
5
5
  "license": "MIT",
6
6
  "description": "Compile class and object decorators to ES5",
@@ -12,14 +12,14 @@
12
12
  "decorators"
13
13
  ],
14
14
  "dependencies": {
15
- "@babel/helper-plugin-utils": "7.0.0-beta.43",
16
- "@babel/plugin-syntax-decorators": "7.0.0-beta.43"
15
+ "@babel/helper-plugin-utils": "7.0.0-beta.47",
16
+ "@babel/plugin-syntax-decorators": "7.0.0-beta.47"
17
17
  },
18
18
  "peerDependencies": {
19
- "@babel/core": "7.0.0-beta.43"
19
+ "@babel/core": "7.0.0-beta.47"
20
20
  },
21
21
  "devDependencies": {
22
- "@babel/core": "7.0.0-beta.43",
23
- "@babel/helper-plugin-test-runner": "7.0.0-beta.43"
22
+ "@babel/core": "7.0.0-beta.47",
23
+ "@babel/helper-plugin-test-runner": "7.0.0-beta.47"
24
24
  }
25
25
  }