@babel/plugin-proposal-decorators 7.17.0 → 7.17.9

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.
@@ -11,6 +11,8 @@ var _pluginSyntaxDecorators = require("@babel/plugin-syntax-decorators");
11
11
 
12
12
  var _helperReplaceSupers = require("@babel/helper-replace-supers");
13
13
 
14
+ var _helperSplitExportDeclaration = require("@babel/helper-split-export-declaration");
15
+
14
16
  function incrementId(id, idx = id.length - 1) {
15
17
  if (idx === -1) {
16
18
  id.unshift(65);
@@ -161,8 +163,13 @@ function isDecoratorInfo(info) {
161
163
  return "decorators" in info;
162
164
  }
163
165
 
166
+ function filteredOrderedDecoratorInfo(info) {
167
+ const filtered = info.filter(isDecoratorInfo);
168
+ return [...filtered.filter(el => el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...filtered.filter(el => !el.isStatic && el.kind >= ACCESSOR && el.kind <= SETTER), ...filtered.filter(el => el.isStatic && el.kind === FIELD), ...filtered.filter(el => !el.isStatic && el.kind === FIELD)];
169
+ }
170
+
164
171
  function generateDecorationExprs(info) {
165
- return _core.types.arrayExpression(info.filter(isDecoratorInfo).map(el => {
172
+ return _core.types.arrayExpression(filteredOrderedDecoratorInfo(info).map(el => {
166
173
  const decs = el.decorators.length > 1 ? _core.types.arrayExpression(el.decorators) : el.decorators[0];
167
174
  const kind = el.isStatic ? el.kind + STATIC : el.kind;
168
175
  const decInfo = [decs, _core.types.numericLiteral(kind), el.name];
@@ -181,23 +188,25 @@ function generateDecorationExprs(info) {
181
188
  }
182
189
 
183
190
  function extractElementLocalAssignments(decorationInfo) {
184
- const locals = [];
191
+ const localIds = [];
185
192
 
186
- for (const el of decorationInfo) {
187
- if ("locals" in el && el.locals) {
188
- if (Array.isArray(el.locals)) {
189
- locals.push(...el.locals);
190
- } else {
191
- locals.push(el.locals);
192
- }
193
+ for (const el of filteredOrderedDecoratorInfo(decorationInfo)) {
194
+ const {
195
+ locals
196
+ } = el;
197
+
198
+ if (Array.isArray(locals)) {
199
+ localIds.push(...locals);
200
+ } else if (locals !== undefined) {
201
+ localIds.push(locals);
193
202
  }
194
203
  }
195
204
 
196
- return locals;
205
+ return localIds;
197
206
  }
198
207
 
199
208
  function addCallAccessorsFor(element, key, getId, setId) {
200
- element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(getId), [_core.types.thisExpression()]))])));
209
+ element.insertAfter(_core.types.classPrivateMethod("get", _core.types.cloneNode(key), [], _core.types.blockStatement([_core.types.returnStatement(_core.types.callExpression(_core.types.cloneNode(getId), [_core.types.thisExpression()]))])));
201
210
  element.insertAfter(_core.types.classPrivateMethod("set", _core.types.cloneNode(key), [_core.types.identifier("v")], _core.types.blockStatement([_core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(setId), [_core.types.thisExpression(), _core.types.identifier("v")]))])));
202
211
  }
203
212
 
@@ -270,16 +279,29 @@ function transformClass(path, state, constantSuper) {
270
279
  let constructorPath;
271
280
  let requiresProtoInit = false;
272
281
  let requiresStaticInit = false;
273
- let hasComputedProps = false;
274
282
  const decoratedPrivateMethods = new Set();
275
283
  let protoInitLocal, staticInitLocal, classInitLocal, classLocal;
284
+ const assignments = [];
285
+ const scopeParent = path.scope.parent;
286
+
287
+ const memoiseExpression = (expression, hint) => {
288
+ const localEvaluatedId = scopeParent.generateDeclaredUidIdentifier(hint);
289
+ assignments.push(_core.types.assignmentExpression("=", localEvaluatedId, expression));
290
+ return _core.types.cloneNode(localEvaluatedId);
291
+ };
276
292
 
277
293
  if (classDecorators) {
278
- classInitLocal = path.scope.parent.generateDeclaredUidIdentifier("initClass");
294
+ classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass");
279
295
  const [localId, classPath] = replaceClassWithVar(path);
280
296
  path = classPath;
281
297
  classLocal = localId;
282
298
  path.node.decorators = null;
299
+
300
+ for (const classDecorator of classDecorators) {
301
+ if (!scopeParent.isStatic(classDecorator.expression)) {
302
+ classDecorator.expression = memoiseExpression(classDecorator.expression, "dec");
303
+ }
304
+ }
283
305
  } else {
284
306
  if (!path.node.id) {
285
307
  path.node.id = path.scope.generateUidIdentifier("Class");
@@ -294,19 +316,39 @@ function transformClass(path, state, constantSuper) {
294
316
  continue;
295
317
  }
296
318
 
297
- let {
298
- key
299
- } = element.node;
300
- const kind = getElementKind(element);
319
+ const {
320
+ node
321
+ } = element;
301
322
  const decorators = element.get("decorators");
302
- const isPrivate = key.type === "PrivateName";
323
+ const hasDecorators = Array.isArray(decorators) && decorators.length > 0;
324
+
325
+ if (hasDecorators) {
326
+ for (const decoratorPath of decorators) {
327
+ if (!scopeParent.isStatic(decoratorPath.node.expression)) {
328
+ decoratorPath.node.expression = memoiseExpression(decoratorPath.node.expression, "dec");
329
+ }
330
+ }
331
+ }
332
+
303
333
  const isComputed = "computed" in element.node && element.node.computed === true;
334
+
335
+ if (isComputed) {
336
+ if (!scopeParent.isStatic(node.key)) {
337
+ node.key = memoiseExpression(node.key, "computedKey");
338
+ }
339
+ }
340
+
341
+ const kind = getElementKind(element);
342
+ const {
343
+ key
344
+ } = node;
345
+ const isPrivate = key.type === "PrivateName";
304
346
  const isStatic = !!element.node.static;
305
347
  let name = "computedKey";
306
348
 
307
349
  if (isPrivate) {
308
350
  name = key.id.name;
309
- } else if (key.type === "Identifier") {
351
+ } else if (!isComputed && key.type === "Identifier") {
310
352
  name = key.name;
311
353
  }
312
354
 
@@ -316,19 +358,7 @@ function transformClass(path, state, constantSuper) {
316
358
  constructorPath = element;
317
359
  }
318
360
 
319
- if (isComputed) {
320
- const keyPath = element.get("key");
321
- const localComputedNameId = keyPath.scope.parent.generateDeclaredUidIdentifier(name);
322
- keyPath.replaceWith(localComputedNameId);
323
- elementDecoratorInfo.push({
324
- localComputedNameId: _core.types.cloneNode(localComputedNameId),
325
- keyNode: _core.types.cloneNode(key)
326
- });
327
- key = localComputedNameId;
328
- hasComputedProps = true;
329
- }
330
-
331
- if (Array.isArray(decorators) && decorators.length > 0) {
361
+ if (hasDecorators) {
332
362
  let locals;
333
363
  let privateMethods;
334
364
 
@@ -437,43 +467,14 @@ function transformClass(path, state, constantSuper) {
437
467
  }
438
468
  }
439
469
 
440
- if (hasComputedProps) {
441
- const assignments = [];
442
-
443
- for (const info of elementDecoratorInfo) {
444
- if (isDecoratorInfo(info)) {
445
- const {
446
- decorators
447
- } = info;
448
- const newDecorators = [];
449
-
450
- for (const decorator of decorators) {
451
- const localComputedNameId = path.scope.parent.generateDeclaredUidIdentifier("dec");
452
- assignments.push(_core.types.assignmentExpression("=", localComputedNameId, decorator));
453
- newDecorators.push(_core.types.cloneNode(localComputedNameId));
454
- }
455
-
456
- info.decorators = newDecorators;
457
- } else {
458
- assignments.push(_core.types.assignmentExpression("=", info.localComputedNameId, info.keyNode));
459
- }
460
- }
461
-
462
- path.insertBefore(assignments);
463
- }
464
-
465
470
  const elementDecorations = generateDecorationExprs(elementDecoratorInfo);
466
471
 
467
472
  const classDecorations = _core.types.arrayExpression((classDecorators || []).map(d => d.expression));
468
473
 
469
474
  const locals = extractElementLocalAssignments(elementDecoratorInfo);
470
475
 
471
- if (classDecorators) {
472
- locals.push(classLocal, classInitLocal);
473
- }
474
-
475
476
  if (requiresProtoInit) {
476
- protoInitLocal = path.scope.parent.generateDeclaredUidIdentifier("initProto");
477
+ protoInitLocal = scopeParent.generateDeclaredUidIdentifier("initProto");
477
478
  locals.push(protoInitLocal);
478
479
 
479
480
  const protoInitCall = _core.types.callExpression(_core.types.cloneNode(protoInitLocal), [_core.types.thisExpression()]);
@@ -514,7 +515,7 @@ function transformClass(path, state, constantSuper) {
514
515
  }
515
516
 
516
517
  if (requiresStaticInit) {
517
- staticInitLocal = path.scope.parent.generateDeclaredUidIdentifier("initStatic");
518
+ staticInitLocal = scopeParent.generateDeclaredUidIdentifier("initStatic");
518
519
  locals.push(staticInitLocal);
519
520
  }
520
521
 
@@ -540,6 +541,7 @@ function transformClass(path, state, constantSuper) {
540
541
  const originalClass = path.node;
541
542
 
542
543
  if (classDecorators) {
544
+ locals.push(classLocal, classInitLocal);
543
545
  const statics = [];
544
546
  let staticBlocks = [];
545
547
  path.get("body.body").forEach(element => {
@@ -599,6 +601,7 @@ function transformClass(path, state, constantSuper) {
599
601
  }
600
602
 
601
603
  originalClass.body.body.unshift(_core.types.staticBlock([_core.types.expressionStatement(_core.types.assignmentExpression("=", _core.types.arrayPattern(locals), _core.types.callExpression(state.addHelper("applyDecs"), [_core.types.thisExpression(), elementDecorations, classDecorations]))), requiresStaticInit && _core.types.expressionStatement(_core.types.callExpression(_core.types.cloneNode(staticInitLocal), [_core.types.thisExpression()]))].filter(Boolean)));
604
+ path.insertBefore(assignments.map(expr => _core.types.expressionStatement(expr)));
602
605
  path.scope.crawl();
603
606
  return path;
604
607
  }
@@ -618,22 +621,22 @@ function _default({
618
621
  name: "proposal-decorators",
619
622
  inherits: _pluginSyntaxDecorators.default,
620
623
  visitor: {
621
- ClassDeclaration(path, state) {
622
- if (VISITED.has(path)) return;
623
- const newPath = transformClass(path, state, constantSuper);
624
+ "ExportNamedDeclaration|ExportDefaultDeclaration"(path) {
625
+ var _declaration$decorato;
624
626
 
625
- if (newPath) {
626
- VISITED.add(newPath);
627
+ const {
628
+ declaration
629
+ } = path.node;
630
+
631
+ if ((declaration == null ? void 0 : declaration.type) === "ClassDeclaration" && ((_declaration$decorato = declaration.decorators) == null ? void 0 : _declaration$decorato.length) > 0) {
632
+ (0, _helperSplitExportDeclaration.default)(path);
627
633
  }
628
634
  },
629
635
 
630
- ClassExpression(path, state) {
636
+ Class(path, state) {
631
637
  if (VISITED.has(path)) return;
632
638
  const newPath = transformClass(path, state, constantSuper);
633
-
634
- if (newPath) {
635
- VISITED.add(newPath);
636
- }
639
+ if (newPath) VISITED.add(newPath);
637
640
  }
638
641
 
639
642
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@babel/plugin-proposal-decorators",
3
- "version": "7.17.0",
3
+ "version": "7.17.9",
4
4
  "author": "The Babel Team (https://babel.dev/team)",
5
5
  "license": "MIT",
6
6
  "publishConfig": {
@@ -20,9 +20,10 @@
20
20
  "decorators"
21
21
  ],
22
22
  "dependencies": {
23
- "@babel/helper-create-class-features-plugin": "^7.17.0",
23
+ "@babel/helper-create-class-features-plugin": "^7.17.9",
24
24
  "@babel/helper-plugin-utils": "^7.16.7",
25
25
  "@babel/helper-replace-supers": "^7.16.7",
26
+ "@babel/helper-split-export-declaration": "^7.16.7",
26
27
  "@babel/plugin-syntax-decorators": "^7.17.0",
27
28
  "charcodes": "^0.2.0"
28
29
  },
@@ -30,9 +31,9 @@
30
31
  "@babel/core": "^7.0.0-0"
31
32
  },
32
33
  "devDependencies": {
33
- "@babel/core": "^7.17.0",
34
+ "@babel/core": "^7.17.9",
34
35
  "@babel/helper-plugin-test-runner": "^7.16.7",
35
- "@babel/traverse": "^7.17.0",
36
+ "@babel/traverse": "^7.17.9",
36
37
  "babel-plugin-polyfill-es-shims": "^0.6.0",
37
38
  "object.getownpropertydescriptors": "^2.1.1"
38
39
  },