Skip to content

Commit 16d3220

Browse files
Add 2022-03 decorators version (stage 3) (#14836)
* Copy `applyDecs`->`applyDecs2203` helper, and `2021-12`->`2022-03` tests * Avoid conflicting fn names in helpers * Add `2022-03` decorators version * Rename `isPrivate`/`isStatic`->`private`/`static` * Disallow `@(...)()` in 2022-03 decorators This commits add a new `allowCallParenthesized` option to the decorators parser plugin: when set to `false`, `@(...)()`-style decorators are allowed to match the stage 3 proposal presented in March 2022. It will default to `false` in Babel 8 (we might just remove the option) * Disallow `decoratorsBeforeExport` option with 2022-03 decorators This aligns with the Babel 8 behavior * Remove `.initializer` fallback for accessor properties * Remove `.initializer` fallback for accessor properties * Expose `.access` for public class elements * Remove `getMetadata`/`setMetadata` * Make the parser error recoverable * Print necessary parentheses in generator
1 parent 85ce832 commit 16d3220

325 files changed

Lines changed: 7789 additions & 77 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

packages/babel-generator/src/generators/expressions.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -131,14 +131,13 @@ function isDecoratorMemberExpression(
131131
function shouldParenthesizeDecoratorExpression(
132132
node: t.Expression | t.Super | t.V8IntrinsicIdentifier,
133133
) {
134-
if (node.type === "CallExpression") {
135-
node = node.callee;
136-
}
137134
if (node.type === "ParenthesizedExpression") {
138135
// We didn't check extra?.parenthesized here because we don't track decorators in needsParen
139136
return false;
140137
}
141-
return !isDecoratorMemberExpression(node);
138+
return !isDecoratorMemberExpression(
139+
node.type === "CallExpression" ? node.callee : node,
140+
);
142141
}
143142

144143
export function Decorator(this: Printer, node: t.Decorator) {

packages/babel-generator/test/fixtures/decorators/decorator-parenthesized-expression-createParenthesizedExpression/input.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ class C extends class {} {
99
@(this.dec)
1010
@(super.dec)
1111
@(new DecFactory)
12-
@(decs[three])()
1312
p;
1413
}
1514

@@ -20,6 +19,11 @@ class C extends class {} {
2019
p;
2120
}
2221

22+
class ShouldAddParens {
23+
@(decs[three])()
24+
p;
25+
}
26+
2327
class WillPreserveParens {
2428
@(decs)
2529
@(decs.one)

packages/babel-generator/test/fixtures/decorators/decorator-parenthesized-expression-createParenthesizedExpression/output.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ class C extends class {} {
1010
@(this.dec)
1111
@(super.dec)
1212
@(new DecFactory())
13-
@(decs[three])()
1413
p;
1514
}
1615

@@ -21,6 +20,11 @@ class C extends class {} {
2120
p;
2221
}
2322

23+
class ShouldAddParens {
24+
@((decs[three])())
25+
p;
26+
}
27+
2428
class WillPreserveParens {
2529
@(decs)
2630
@(decs.one)

packages/babel-helpers/src/helpers-generated.ts

Lines changed: 5 additions & 1 deletion
Large diffs are not rendered by default.

packages/babel-helpers/src/helpers/applyDecs.js

Lines changed: 46 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
/* @minVersion 7.17.8 */
22

3+
/**
4+
* NOTE: This is an old version of the helper, used for 2021-12 decorators.
5+
* Updates should be done in applyDecs2203.js.
6+
*/
7+
38
/**
49
Enums are used in this file, but not assigned to vars to avoid non-hoistable values
510
@@ -18,16 +23,16 @@
1823
CLASS = 10; // only used in assertValidReturnValue
1924
*/
2025

21-
function createMetadataMethodsForProperty(
26+
function old_createMetadataMethodsForProperty(
2227
metadataMap,
2328
kind,
2429
property,
2530
decoratorFinishedRef
2631
) {
2732
return {
2833
getMetadata: function (key) {
29-
assertNotFinished(decoratorFinishedRef, "getMetadata");
30-
assertMetadataKey(key);
34+
old_assertNotFinished(decoratorFinishedRef, "getMetadata");
35+
old_assertMetadataKey(key);
3136

3237
var metadataForKey = metadataMap[key];
3338

@@ -48,8 +53,8 @@ function createMetadataMethodsForProperty(
4853
}
4954
},
5055
setMetadata: function (key, value) {
51-
assertNotFinished(decoratorFinishedRef, "setMetadata");
52-
assertMetadataKey(key);
56+
old_assertNotFinished(decoratorFinishedRef, "setMetadata");
57+
old_assertMetadataKey(key);
5358

5459
var metadataForKey = metadataMap[key];
5560

@@ -80,7 +85,7 @@ function createMetadataMethodsForProperty(
8085
};
8186
}
8287

83-
function convertMetadataMapToFinal(obj, metadataMap) {
88+
function old_convertMetadataMapToFinal(obj, metadataMap) {
8489
var parentMetadataMap = obj[Symbol.metadata || Symbol.for("Symbol.metadata")];
8590
var metadataKeys = Object.getOwnPropertySymbols(metadataMap);
8691

@@ -123,15 +128,15 @@ function convertMetadataMapToFinal(obj, metadataMap) {
123128
obj[Symbol.metadata || Symbol.for("Symbol.metadata")] = metadataMap;
124129
}
125130

126-
function createAddInitializerMethod(initializers, decoratorFinishedRef) {
131+
function old_createAddInitializerMethod(initializers, decoratorFinishedRef) {
127132
return function addInitializer(initializer) {
128-
assertNotFinished(decoratorFinishedRef, "addInitializer");
129-
assertCallable(initializer, "An initializer");
133+
old_assertNotFinished(decoratorFinishedRef, "addInitializer");
134+
old_assertCallable(initializer, "An initializer");
130135
initializers.push(initializer);
131136
};
132137
}
133138

134-
function memberDec(
139+
function old_memberDec(
135140
dec,
136141
name,
137142
desc,
@@ -171,7 +176,7 @@ function memberDec(
171176
var decoratorFinishedRef = { v: false };
172177

173178
if (kind !== 0 /* FIELD */) {
174-
ctx.addInitializer = createAddInitializerMethod(
179+
ctx.addInitializer = old_createAddInitializerMethod(
175180
initializers,
176181
decoratorFinishedRef
177182
);
@@ -218,7 +223,7 @@ function memberDec(
218223
value,
219224
Object.assign(
220225
ctx,
221-
createMetadataMethodsForProperty(
226+
old_createMetadataMethodsForProperty(
222227
metadataMap,
223228
metadataKind,
224229
metadataName,
@@ -231,27 +236,27 @@ function memberDec(
231236
}
232237
}
233238

234-
function assertNotFinished(decoratorFinishedRef, fnName) {
239+
function old_assertNotFinished(decoratorFinishedRef, fnName) {
235240
if (decoratorFinishedRef.v) {
236241
throw new Error(
237242
"attempted to call " + fnName + " after decoration was finished"
238243
);
239244
}
240245
}
241246

242-
function assertMetadataKey(key) {
247+
function old_assertMetadataKey(key) {
243248
if (typeof key !== "symbol") {
244249
throw new TypeError("Metadata keys must be symbols, received: " + key);
245250
}
246251
}
247252

248-
function assertCallable(fn, hint) {
253+
function old_assertCallable(fn, hint) {
249254
if (typeof fn !== "function") {
250255
throw new TypeError(hint + " must be a function");
251256
}
252257
}
253258

254-
function assertValidReturnValue(kind, value) {
259+
function old_assertValidReturnValue(kind, value) {
255260
var type = typeof value;
256261

257262
if (kind === 1 /* ACCESSOR */) {
@@ -261,16 +266,16 @@ function assertValidReturnValue(kind, value) {
261266
);
262267
}
263268
if (value.get !== undefined) {
264-
assertCallable(value.get, "accessor.get");
269+
old_assertCallable(value.get, "accessor.get");
265270
}
266271
if (value.set !== undefined) {
267-
assertCallable(value.set, "accessor.set");
272+
old_assertCallable(value.set, "accessor.set");
268273
}
269274
if (value.init !== undefined) {
270-
assertCallable(value.init, "accessor.init");
275+
old_assertCallable(value.init, "accessor.init");
271276
}
272277
if (value.initializer !== undefined) {
273-
assertCallable(value.initializer, "accessor.initializer");
278+
old_assertCallable(value.initializer, "accessor.initializer");
274279
}
275280
} else if (type !== "function") {
276281
var hint;
@@ -285,7 +290,7 @@ function assertValidReturnValue(kind, value) {
285290
}
286291
}
287292

288-
function getInit(desc) {
293+
function old_getInit(desc) {
289294
var initializer;
290295
if (
291296
(initializer = desc.init) == null &&
@@ -297,7 +302,7 @@ function getInit(desc) {
297302
return initializer;
298303
}
299304

300-
function applyMemberDec(
305+
function old_applyMemberDec(
301306
ret,
302307
base,
303308
decInfo,
@@ -351,7 +356,7 @@ function applyMemberDec(
351356
var newValue, get, set;
352357

353358
if (typeof decs === "function") {
354-
newValue = memberDec(
359+
newValue = old_memberDec(
355360
decs,
356361
name,
357362
desc,
@@ -364,12 +369,12 @@ function applyMemberDec(
364369
);
365370

366371
if (newValue !== void 0) {
367-
assertValidReturnValue(kind, newValue);
372+
old_assertValidReturnValue(kind, newValue);
368373

369374
if (kind === 0 /* FIELD */) {
370375
initializer = newValue;
371376
} else if (kind === 1 /* ACCESSOR */) {
372-
initializer = getInit(newValue);
377+
initializer = old_getInit(newValue);
373378
get = newValue.get || value.get;
374379
set = newValue.set || value.set;
375380

@@ -382,7 +387,7 @@ function applyMemberDec(
382387
for (var i = decs.length - 1; i >= 0; i--) {
383388
var dec = decs[i];
384389

385-
newValue = memberDec(
390+
newValue = old_memberDec(
386391
dec,
387392
name,
388393
desc,
@@ -395,13 +400,13 @@ function applyMemberDec(
395400
);
396401

397402
if (newValue !== void 0) {
398-
assertValidReturnValue(kind, newValue);
403+
old_assertValidReturnValue(kind, newValue);
399404
var newInit;
400405

401406
if (kind === 0 /* FIELD */) {
402407
newInit = newValue;
403408
} else if (kind === 1 /* ACCESSOR */) {
404-
newInit = getInit(newValue);
409+
newInit = old_getInit(newValue);
405410
get = newValue.get || value.get;
406411
set = newValue.set || value.set;
407412

@@ -485,7 +490,7 @@ function applyMemberDec(
485490
}
486491
}
487492

488-
function applyMemberDecs(
493+
function old_applyMemberDecs(
489494
ret,
490495
Class,
491496
protoMetadataMap,
@@ -555,7 +560,7 @@ function applyMemberDecs(
555560
}
556561
}
557562

558-
applyMemberDec(
563+
old_applyMemberDec(
559564
ret,
560565
base,
561566
decInfo,
@@ -568,11 +573,11 @@ function applyMemberDecs(
568573
);
569574
}
570575

571-
pushInitializers(ret, protoInitializers);
572-
pushInitializers(ret, staticInitializers);
576+
old_pushInitializers(ret, protoInitializers);
577+
old_pushInitializers(ret, staticInitializers);
573578
}
574579

575-
function pushInitializers(ret, initializers) {
580+
function old_pushInitializers(ret, initializers) {
576581
if (initializers) {
577582
ret.push(function (instance) {
578583
for (var i = 0; i < initializers.length; i++) {
@@ -583,7 +588,7 @@ function pushInitializers(ret, initializers) {
583588
}
584589
}
585590

586-
function applyClassDecs(ret, targetClass, metadataMap, classDecs) {
591+
function old_applyClassDecs(ret, targetClass, metadataMap, classDecs) {
587592
if (classDecs.length > 0) {
588593
var initializers = [];
589594
var newClass = targetClass;
@@ -597,12 +602,12 @@ function applyClassDecs(ret, targetClass, metadataMap, classDecs) {
597602
{
598603
kind: "class",
599604
name: name,
600-
addInitializer: createAddInitializerMethod(
605+
addInitializer: old_createAddInitializerMethod(
601606
initializers,
602607
decoratorFinishedRef
603608
),
604609
},
605-
createMetadataMethodsForProperty(
610+
old_createMetadataMethodsForProperty(
606611
metadataMap,
607612
0 /* CONSTRUCTOR */,
608613
name,
@@ -615,7 +620,7 @@ function applyClassDecs(ret, targetClass, metadataMap, classDecs) {
615620
}
616621

617622
if (nextNewClass !== undefined) {
618-
assertValidReturnValue(10 /* CLASS */, nextNewClass);
623+
old_assertValidReturnValue(10 /* CLASS */, nextNewClass);
619624
newClass = nextNewClass;
620625
}
621626
}
@@ -779,19 +784,19 @@ export default function applyDecs(targetClass, memberDecs, classDecs) {
779784

780785
var protoMetadataMap = {};
781786

782-
applyMemberDecs(
787+
old_applyMemberDecs(
783788
ret,
784789
targetClass,
785790
protoMetadataMap,
786791
staticMetadataMap,
787792
memberDecs
788793
);
789794

790-
convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap);
795+
old_convertMetadataMapToFinal(targetClass.prototype, protoMetadataMap);
791796

792-
applyClassDecs(ret, targetClass, staticMetadataMap, classDecs);
797+
old_applyClassDecs(ret, targetClass, staticMetadataMap, classDecs);
793798

794-
convertMetadataMapToFinal(targetClass, staticMetadataMap);
799+
old_convertMetadataMapToFinal(targetClass, staticMetadataMap);
795800

796801
return ret;
797802
}

0 commit comments

Comments
 (0)