diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index b0006a32171d..0d92c3f65de6 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -15,7 +15,7 @@ function helper(minVersion, source) { export default Object.freeze({ applyDecs: helper( "7.17.0", - 'function createMetadataMethodsForProperty(metadataMap,kind,property){return{getMetadata:function(key){if("symbol"!=typeof key)throw new TypeError("Metadata keys must be symbols, received: "+key);var metadataForKey=metadataMap[key];if(void 0!==metadataForKey)if(1===kind){var pub=metadataForKey.public;if(void 0!==pub)return pub[property]}else if(2===kind){var priv=metadataForKey.private;if(void 0!==priv)return priv.get(property)}else if(Object.hasOwnProperty.call(metadataForKey,"constructor"))return metadataForKey.constructor},setMetadata:function(key,value){if("symbol"!=typeof key)throw new TypeError("Metadata keys must be symbols, received: "+key);var metadataForKey=metadataMap[key];if(void 0===metadataForKey&&(metadataForKey=metadataMap[key]={}),1===kind){var pub=metadataForKey.public;void 0===pub&&(pub=metadataForKey.public={}),pub[property]=value}else if(2===kind){var priv=metadataForKey.priv;void 0===priv&&(priv=metadataForKey.private=new Map),priv.set(property,value)}else metadataForKey.constructor=value}}}function convertMetadataMapToFinal(obj,metadataMap){var parentMetadataMap=obj[Symbol.metadata||Symbol.for("Symbol.metadata")],metadataKeys=Object.getOwnPropertySymbols(metadataMap);if(0!==metadataKeys.length){for(var i=0;i=0;i--){var newInit;if(void 0!==(newValue=(0,decs[i])(value,ctx)))assertValidReturnValue(kind,newValue),0===kind?newInit=newValue:1===kind?(newInit=newValue.initialize,get=newValue.get||value.get,set=newValue.set||value.set,value={get:get,set:set}):value=newValue,void 0!==newInit&&(void 0===initializer?initializer=newInit:"function"==typeof initializer?initializer=[initializer,newInit]:initializer.push(newInit))}if(0===kind||1===kind){if(void 0===initializer)initializer=function(instance,init){return init};else if("function"!=typeof initializer){var ownInitializers=initializer;initializer=function(instance,init){for(var value=init,i=0;i3,isStatic=kind>=5;if(isStatic?(base=Class,metadataMap=staticMetadataMap,0!==(kind-=5)&&(initializers=staticInitializers=staticInitializers||[])):(base=Class.prototype,metadataMap=protoMetadataMap,0!==kind&&(initializers=protoInitializers=protoInitializers||[])),0!==kind&&!isPrivate){var existingNonFields=isStatic?existingStaticNonFields:existingProtoNonFields,existingKind=existingNonFields.get(name)||0;if(!0===existingKind||3===existingKind&&4!==kind||4===existingKind&&3!==kind)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+name);!existingKind&&kind>2?existingNonFields.set(name,kind):existingNonFields.set(name,!0)}applyMemberDec(ret,base,decInfo,name,kind,isStatic,isPrivate,metadataMap,initializers)}}pushInitializers(ret,protoInitializers),pushInitializers(ret,staticInitializers)}function pushInitializers(ret,initializers){initializers&&(initializers.length>0?(initializers=initializers.slice(),ret.push((function(instance){for(var i=0;i0){for(var initializers=[],newClass=targetClass,name=targetClass.name,ctx=Object.assign({kind:"class",name:name,addInitializer:createAddInitializerMethod(initializers)},createMetadataMethodsForProperty(metadataMap,0,name)),i=classDecs.length-1;i>=0;i--){var nextNewClass=classDecs[i](newClass,ctx);void 0!==nextNewClass&&(assertValidReturnValue(10,nextNewClass),newClass=nextNewClass)}ret.push(newClass),initializers.length>0?ret.push((function(){for(var i=0;i=0;i--){var newInit,dec=decs[i];if(ctx=memberDecCtx(name,desc,metadataMap,initializers,kind,isStatic,isPrivate,decorationState={finished:!1}),newValue=dec(value,ctx),decorationState.finished=!0,void 0!==newValue)assertValidReturnValue(kind,newValue),0===kind?newInit=newValue:1===kind?(newInit=newValue.init,get=newValue.get||value.get,set=newValue.set||value.set,value={get:get,set:set}):value=newValue,void 0!==newInit&&(void 0===initializer?initializer=newInit:"function"==typeof initializer?initializer=[initializer,newInit]:initializer.push(newInit))}if(0===kind||1===kind){if(void 0===initializer)initializer=function(instance,init){return init};else if("function"!=typeof initializer){var ownInitializers=initializer;initializer=function(instance,init){for(var value=init,i=0;i3,isStatic=kind>=5;if(isStatic?(base=Class,metadataMap=staticMetadataMap,0!==(kind-=5)&&(initializers=staticInitializers=staticInitializers||[])):(base=Class.prototype,metadataMap=protoMetadataMap,0!==kind&&(initializers=protoInitializers=protoInitializers||[])),0!==kind&&!isPrivate){var existingNonFields=isStatic?existingStaticNonFields:existingProtoNonFields,existingKind=existingNonFields.get(name)||0;if(!0===existingKind||3===existingKind&&4!==kind||4===existingKind&&3!==kind)throw new Error("Attempted to decorate a public method/accessor that has the same name as a previously decorated public method/accessor. This is not currently supported by the decorators plugin. Property name was: "+name);!existingKind&&kind>2?existingNonFields.set(name,kind):existingNonFields.set(name,!0)}applyMemberDec(ret,base,decInfo,name,kind,isStatic,isPrivate,metadataMap,initializers)}}pushInitializers(ret,protoInitializers),pushInitializers(ret,staticInitializers)}function pushInitializers(ret,initializers){initializers&&(initializers.length>0?(initializers=initializers.slice(),ret.push((function(instance){for(var i=0;i0){for(var initializers=[],newClass=targetClass,name=targetClass.name,i=classDecs.length-1;i>=0;i--){var decorationState={finished:!1},ctx=Object.assign({kind:"class",name:name,addInitializer:createAddInitializerMethod(initializers,decorationState)},createMetadataMethodsForProperty(metadataMap,0,name,decorationState)),nextNewClass=classDecs[i](newClass,ctx);void 0!==nextNewClass&&(assertValidReturnValue(10,nextNewClass),newClass=nextNewClass),decorationState.finished=!0}ret.push(newClass),initializers.length>0?ret.push((function(){for(var i=0;i= 0; i--) { var dec = decs[i]; + decorationState = { finished: false }; + ctx = memberDecCtx( + name, + desc, + metadataMap, + initializers, + kind, + isStatic, + isPrivate, + decorationState + ); newValue = dec(value, ctx); + decorationState.finished = true; + if (newValue !== void 0) { assertValidReturnValue(kind, newValue); var newInit; @@ -340,7 +389,7 @@ function applyMemberDec( if (kind === 0 /* FIELD */) { newInit = newValue; } else if (kind === 1 /* ACCESSOR */) { - newInit = newValue.initialize; + newInit = newValue.init; get = newValue.get || value.get; set = newValue.set || value.set; @@ -537,23 +586,35 @@ function applyClassDecs(ret, targetClass, metadataMap, classDecs) { if (classDecs.length > 0) { var initializers = []; var newClass = targetClass; - var name = targetClass.name; - var ctx = Object.assign( - { - kind: "class", - name: name, - addInitializer: createAddInitializerMethod(initializers), - }, - createMetadataMethodsForProperty(metadataMap, 0 /* CONSTRUCTOR */, name) - ); for (var i = classDecs.length - 1; i >= 0; i--) { + var decorationState = { finished: false }; + + var ctx = Object.assign( + { + kind: "class", + name: name, + addInitializer: createAddInitializerMethod( + initializers, + decorationState + ), + }, + createMetadataMethodsForProperty( + metadataMap, + 0 /* CONSTRUCTOR */, + name, + decorationState + ) + ); + var nextNewClass = classDecs[i](newClass, ctx); if (nextNewClass !== undefined) { assertValidReturnValue(10 /* CLASS */, nextNewClass); newClass = nextNewClass; } + + decorationState.finished = true; } ret.push(newClass); diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts index 2f4676921163..8170b5a0ff12 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2021-12.ts @@ -311,11 +311,28 @@ function isDecoratorInfo( return "decorators" in info; } +function filteredOrderedDecoratorInfo( + info: (DecoratorInfo | ComputedPropInfo)[], +): DecoratorInfo[] { + const filtered = info.filter(isDecoratorInfo); + + 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), + ]; +} + function generateDecorationExprs( info: (DecoratorInfo | ComputedPropInfo)[], ): t.ArrayExpression { return t.arrayExpression( - info.filter(isDecoratorInfo).map(el => { + filteredOrderedDecoratorInfo(info).map(el => { const decs = el.decorators.length > 1 ? t.arrayExpression(el.decorators) @@ -341,19 +358,19 @@ function generateDecorationExprs( function extractElementLocalAssignments( decorationInfo: (DecoratorInfo | ComputedPropInfo)[], ) { - const locals: t.Identifier[] = []; + const localIds: t.Identifier[] = []; - for (const el of decorationInfo) { - if ("locals" in el && el.locals) { - if (Array.isArray(el.locals)) { - locals.push(...el.locals); - } else { - locals.push(el.locals); - } + for (const el of filteredOrderedDecoratorInfo(decorationInfo)) { + const { locals } = el; + + if (Array.isArray(locals)) { + localIds.push(...locals); + } else if (locals !== undefined) { + localIds.push(locals); } } - return locals; + return localIds; } function addCallAccessorsFor( diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js index 7f57c355fafc..3abec2906a37 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/private/exec.js @@ -12,7 +12,7 @@ function dec({ get, set }, context) { set.call(this, v + 1); }, - initialize(v) { + init(v) { return v ? v : 1; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js index b2febe59b23a..9a4a201d815c 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/public/exec.js @@ -12,7 +12,7 @@ function dec({ get, set }, context) { set.call(this, v + 1); }, - initialize(v) { + init(v) { return v ? v : 1; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js index 8da351a43219..e3a5fb5df934 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-private/exec.js @@ -12,7 +12,7 @@ function dec({ get, set }, context) { set.call(this, v + 1); }, - initialize(v) { + init(v) { return v ? v : 1; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js index 38bc72041da3..19859f83fc5a 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-accessors--to-es2015/static-public/exec.js @@ -12,7 +12,7 @@ function dec({ get, set }, context) { set.call(this, v + 1); }, - initialize(v) { + init(v) { return v ? v : 1; } } diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js index 8ac906f1823a..72e253d55578 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/exec.js @@ -17,7 +17,7 @@ class Foo { expect(elements).toHaveLength(2); expect(elements[0].context.name).toBe("a"); -expect(elements[0].val).toBe(undefined); +expect(elements[0].val()).toBe(1); expect(elements[1].context.name).toBe("a"); -expect(elements[1].val()).toBe(1); +expect(elements[1].val).toBe(undefined); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js index 704f0d4c5dd9..cf6cfd0d2c3d 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys--to-es2015/method-and-field/output.js @@ -13,4 +13,4 @@ class Foo { } -[_init_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 0, "a"], [dec, 2, "a"]], []); +[_init_a, _initProto] = babelHelpers.applyDecs(Foo, [[dec, 2, "a"], [dec, 0, "a"]], []); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js index 6c1bdff1940b..ec133be95e04 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-duplicated-keys/method-and-field/output.js @@ -4,7 +4,7 @@ const dec = () => {}; class Foo { static { - [_init_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 0, "a"], [dec, 2, "a"]], []); + [_init_a, _initProto] = babelHelpers.applyDecs(this, [[dec, 2, "a"], [dec, 0, "a"]], []); } a = (_initProto(this), _init_a(this, 123)); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-addInitializer/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-addInitializer/exec.js new file mode 100644 index 000000000000..ae9f6e6b0641 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-addInitializer/exec.js @@ -0,0 +1,14 @@ +let addInitializer; + +function decMethod(_, context) { + ({ addInitializer } = context); + addInitializer(() => null); +} + +class C { + @decMethod m() {} +} + +expect(() => { + addInitializer(() => null); +}).toThrow('attempted to call addInitializer after decoration was finished') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-get-metadata/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-get-metadata/exec.js new file mode 100644 index 000000000000..ce3d127a07ad --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-get-metadata/exec.js @@ -0,0 +1,15 @@ +let getMetadata; +let metadataSymbol = Symbol(); + +function decMethod(_, context) { + ({ getMetadata } = context); + getMetadata(metadataSymbol); +} + +class C { + @decMethod m() {} +} + +expect(() => { + getMetadata(metadataSymbol); +}).toThrow('attempted to call getMetadata after decoration was finished') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-set-metadata/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-set-metadata/exec.js new file mode 100644 index 000000000000..f554a5970134 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-context-set-metadata/exec.js @@ -0,0 +1,15 @@ +let setMetadata; +let metadataSymbol = Symbol(); + +function decMethod(_, context) { + ({ setMetadata } = context); + setMetadata(metadataSymbol, 'value'); +} + +class C { + @decMethod m() {} +} + +expect(() => { + setMetadata(metadataSymbol, 'value'); +}).toThrow('attempted to call setMetadata after decoration was finished') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-proto-context-addInitializer/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-proto-context-addInitializer/exec.js deleted file mode 100644 index def1d6e45e95..000000000000 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-proto-context-addInitializer/exec.js +++ /dev/null @@ -1,21 +0,0 @@ -let addInitializer, i = 0; -const logs = []; - -function decCallProtoAddInitializer() { - addInitializer(() => logs.push(i++)); -} - -function decMethod(_, context) { - ({ addInitializer } = context); - addInitializer(() => logs.push(i++)); -} - -@decCallProtoAddInitializer -class C { - @decMethod m() {} - @decCallProtoAddInitializer static n() {} -} - -new C; - -expect(logs).toEqual([0, 1]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-static-context-addInitializer/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-static-context-addInitializer/exec.js deleted file mode 100644 index 1a0d7aff9e6d..000000000000 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc--to-es2015/leaked-static-context-addInitializer/exec.js +++ /dev/null @@ -1,21 +0,0 @@ -let addInitializer, i = 0; -const logs = []; - -function decCallStaticAddInitializer() { - addInitializer(() => logs.push(i++)); -} - -function decStaticMethod(_, context) { - ({ addInitializer } = context); - addInitializer(() => logs.push(i++)); -} - -@decCallStaticAddInitializer -class C { - @decStaticMethod static m() {} - @decCallStaticAddInitializer n() {} -} - -new C; - -expect(logs).toEqual([0, 1]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js index ab809be8665c..107c410c18ad 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-misc/all-decorators/output.js @@ -8,22 +8,22 @@ new class extends babelHelpers.identity { static { class Class { static { - [_init_a, _init_d, _init_e, _call_f, _call_g, _call_g2, _init_h, _get_h, _set_h, _init_i, _init_m, _init_n, _call_o, _call_p, _call_q, _init_r, _get_r, _set_r, _initProto, _initStatic, _Class, _initClass] = babelHelpers.applyDecs(this, [[dec, 0, "a"], [dec, 2, "b"], [dec, 3, "c"], [dec, 4, "c"], [dec, 1, "d"], [dec, 0, "e", function () { - return this.#e; + [_init_m, _call_o, _call_p, _call_q, _init_r, _get_r, _set_r, _init_d, _call_f, _call_g, _call_g2, _init_h, _get_h, _set_h, _init_i, _init_n, _init_a, _init_e, _initProto, _initStatic, _Class, _initClass] = babelHelpers.applyDecs(this, [[dec, 7, "j"], [dec, 8, "k"], [dec, 9, "l"], [dec, 6, "m"], [dec, 7, "o", function () {}], [dec, 8, "p", function () {}], [dec, 9, "q", function (v) {}], [dec, 6, "r", function () { + return this.#D; }, function (value) { - this.#e = value; - }], [dec, 2, "f", function () {}], [dec, 3, "g", function () {}], [dec, 4, "g", function (v) {}], [dec, 1, "h", function () { + this.#D = value; + }], [dec, 2, "b"], [dec, 3, "c"], [dec, 4, "c"], [dec, 1, "d"], [dec, 2, "f", function () {}], [dec, 3, "g", function () {}], [dec, 4, "g", function (v) {}], [dec, 1, "h", function () { return this.#B; }, function (value) { this.#B = value; - }], [dec, 5, "i"], [dec, 7, "j"], [dec, 8, "k"], [dec, 9, "l"], [dec, 6, "m"], [dec, 5, "n", function () { + }], [dec, 5, "i"], [dec, 5, "n", function () { return this.#n; }, function (value) { this.#n = value; - }], [dec, 7, "o", function () {}], [dec, 8, "p", function () {}], [dec, 9, "q", function (v) {}], [dec, 6, "r", function () { - return this.#D; + }], [dec, 0, "a"], [dec, 0, "e", function () { + return this.#e; }, function (value) { - this.#D = value; + this.#e = value; }]], [dec]); _initStatic(this); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/accessor-initializers/exec.js index 7d63f7f32353..892ced55301d 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/accessor-initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/accessor-initializers/exec.js @@ -17,7 +17,7 @@ function logAccessorDecoratorRun(a, b, c, d) { push(b); addInitializer(function () { push(c); }); return { - initialize: () => push(d) + init: () => push(d) }; }; } @@ -25,22 +25,24 @@ function logAccessorDecoratorRun(a, b, c, d) { @logClassDecoratorRun(0, 19, 29) @logClassDecoratorRun(1, 18, 28) class A { - @logAccessorDecoratorRun(2, 11) - @logAccessorDecoratorRun(3, 10) + @logAccessorDecoratorRun(2, 15, 31, 35) + @logAccessorDecoratorRun(3, 14, 30, 34) accessor a; - @logAccessorDecoratorRun(4, 13, 21, 25) - @logAccessorDecoratorRun(5, 12, 20, 24) + @logAccessorDecoratorRun(4, 11, 21, 25) + @logAccessorDecoratorRun(5, 10, 20, 24) static accessor b; - @logAccessorDecoratorRun(6, 15, 23, 27) - @logAccessorDecoratorRun(7, 14, 22, 26) + @logAccessorDecoratorRun(6, 13, 23, 27) + @logAccessorDecoratorRun(7, 12, 22, 26) static accessor #c; - @logAccessorDecoratorRun(8, 17) - @logAccessorDecoratorRun(9, 16) + @logAccessorDecoratorRun(8, 17, 33, 37) + @logAccessorDecoratorRun(9, 16, 32, 36) accessor #d; } -var nums = Array.from({ length: 30 }, (_, i) => i); +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/decorators/exec.js index 795f3a9101dd..f44656aaee7c 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/decorators/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/decorators/exec.js @@ -10,16 +10,16 @@ function logDecoratorRun(a, b) { @logDecoratorRun(0, 19) @logDecoratorRun(1, 18) class A { - @logDecoratorRun(2, 11) - @logDecoratorRun(3, 10) + @logDecoratorRun(2, 15) + @logDecoratorRun(3, 14) a; - @logDecoratorRun(4, 13) - @logDecoratorRun(5, 12) + @logDecoratorRun(4, 11) + @logDecoratorRun(5, 10) static b; - @logDecoratorRun(6, 15) - @logDecoratorRun(7, 14) + @logDecoratorRun(6, 13) + @logDecoratorRun(7, 12) static #c; @logDecoratorRun(8, 17) diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/initializers/exec.js index 0adac4dc6c94..35e07dd009a1 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering--to-es2015/initializers/exec.js @@ -14,38 +14,40 @@ function logDecoratorRun(a, b, c) { @logDecoratorRun(0, 35, 45) @logDecoratorRun(1, 34, 44) class A { - @logDecoratorRun(2, 19) - @logDecoratorRun(3, 18) + @logDecoratorRun(2, 27, 47) + @logDecoratorRun(3, 26, 46) a() {}; - @logDecoratorRun(4, 21, 37) - @logDecoratorRun(5, 20, 36) + @logDecoratorRun(4, 19, 37) + @logDecoratorRun(5, 18, 36) static b() {}; - @logDecoratorRun(6, 23, 39) - @logDecoratorRun(7, 22, 38) + @logDecoratorRun(6, 21, 39) + @logDecoratorRun(7, 20, 38) static #c() {}; - @logDecoratorRun(8, 25) - @logDecoratorRun(9, 24) + @logDecoratorRun(8, 29, 49) + @logDecoratorRun(9, 28, 48) #d() {}; - @logDecoratorRun(10, 27) - @logDecoratorRun(11, 26) + @logDecoratorRun(10, 31, 51) + @logDecoratorRun(11, 30, 50) accessor e; - @logDecoratorRun(12, 29, 41) - @logDecoratorRun(13, 28, 40) + @logDecoratorRun(12, 23, 41) + @logDecoratorRun(13, 22, 40) static accessor f; - @logDecoratorRun(14, 31, 43) - @logDecoratorRun(15, 30, 42) + @logDecoratorRun(14, 25, 43) + @logDecoratorRun(15, 24, 42) static accessor #g; - @logDecoratorRun(16, 33) - @logDecoratorRun(17, 32) + @logDecoratorRun(16, 33, 53) + @logDecoratorRun(17, 32, 52) accessor #h; } -var nums = Array.from({ length: 46 }, (_, i) => i); +new A(); + +var nums = Array.from({ length: 54 }, (_, i) => i); expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/accessor-initializers/exec.js new file mode 100644 index 000000000000..892ced55301d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/accessor-initializers/exec.js @@ -0,0 +1,48 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logClassDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { push(c); }); + return el; + }; +} + +function logAccessorDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { push(c); }); + return { + init: () => push(d) + }; + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + @logAccessorDecoratorRun(2, 15, 31, 35) + @logAccessorDecoratorRun(3, 14, 30, 34) + accessor a; + + @logAccessorDecoratorRun(4, 11, 21, 25) + @logAccessorDecoratorRun(5, 10, 20, 24) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 27) + @logAccessorDecoratorRun(7, 12, 22, 26) + static accessor #c; + + @logAccessorDecoratorRun(8, 17, 33, 37) + @logAccessorDecoratorRun(9, 16, 32, 36) + accessor #d; +} + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/decorators/exec.js index b241220635c7..7a51e8b93b7e 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/decorators/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/decorators/exec.js @@ -10,16 +10,16 @@ function logDecoratorRun(a, b) { @logDecoratorRun(0, 21) @logDecoratorRun(1, 20) class A { - @logDecoratorRun(2, 13) - @logDecoratorRun(3, 12) + @logDecoratorRun(2, 17) + @logDecoratorRun(3, 16) [push(4)]; - @logDecoratorRun(5, 15) - @logDecoratorRun(6, 14) + @logDecoratorRun(5, 13) + @logDecoratorRun(6, 12) static [push(7)]; - @logDecoratorRun(8, 17) - @logDecoratorRun(9, 16) + @logDecoratorRun(8, 15) + @logDecoratorRun(9, 14) static #c; @logDecoratorRun(10, 19) diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/initializers/exec.js index 0adac4dc6c94..35e07dd009a1 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/initializers/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-ordering/initializers/exec.js @@ -14,38 +14,40 @@ function logDecoratorRun(a, b, c) { @logDecoratorRun(0, 35, 45) @logDecoratorRun(1, 34, 44) class A { - @logDecoratorRun(2, 19) - @logDecoratorRun(3, 18) + @logDecoratorRun(2, 27, 47) + @logDecoratorRun(3, 26, 46) a() {}; - @logDecoratorRun(4, 21, 37) - @logDecoratorRun(5, 20, 36) + @logDecoratorRun(4, 19, 37) + @logDecoratorRun(5, 18, 36) static b() {}; - @logDecoratorRun(6, 23, 39) - @logDecoratorRun(7, 22, 38) + @logDecoratorRun(6, 21, 39) + @logDecoratorRun(7, 20, 38) static #c() {}; - @logDecoratorRun(8, 25) - @logDecoratorRun(9, 24) + @logDecoratorRun(8, 29, 49) + @logDecoratorRun(9, 28, 48) #d() {}; - @logDecoratorRun(10, 27) - @logDecoratorRun(11, 26) + @logDecoratorRun(10, 31, 51) + @logDecoratorRun(11, 30, 50) accessor e; - @logDecoratorRun(12, 29, 41) - @logDecoratorRun(13, 28, 40) + @logDecoratorRun(12, 23, 41) + @logDecoratorRun(13, 22, 40) static accessor f; - @logDecoratorRun(14, 31, 43) - @logDecoratorRun(15, 30, 42) + @logDecoratorRun(14, 25, 43) + @logDecoratorRun(15, 24, 42) static accessor #g; - @logDecoratorRun(16, 33) - @logDecoratorRun(17, 32) + @logDecoratorRun(16, 33, 53) + @logDecoratorRun(17, 32, 52) accessor #h; } -var nums = Array.from({ length: 46 }, (_, i) => i); +new A(); + +var nums = Array.from({ length: 54 }, (_, i) => i); expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js index 167c235d3de2..cd977fce13e0 100644 --- a/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2021-12-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js @@ -3,18 +3,18 @@ const returnsNull = () => null; const returnsFalse = () => false; const returnsFunction = () => () => {} const returnsGetSet = () => ({ set(v) {}, get() {} }); -const returnsInitialize = () => ({ initialize() {} }); +const returnsInit = () => ({ init() {} }); const returnsGetFalse = () => ({ get: false }); const returnsSetFalse = () => ({ set: false }); -const returnsInitializeFalse = () => ({ initialize: false }); +const returnsInitFalse = () => ({ init: false }); -expect(() => class { @returnsNull accessor a }).toThrow("accessor decorators must return an object with get, set, or initializer properties or void 0") -expect(() => class { @returnsFalse accessor a }).toThrow("accessor decorators must return an object with get, set, or initializer properties or void 0") -expect(() => class { @returnsFunction accessor a }).toThrow("accessor decorators must return an object with get, set, or initializer properties or void 0") +expect(() => class { @returnsNull accessor a }).toThrow("accessor decorators must return an object with get, set, or init properties or void 0") +expect(() => class { @returnsFalse accessor a }).toThrow("accessor decorators must return an object with get, set, or init properties or void 0") +expect(() => class { @returnsFunction accessor a }).toThrow("accessor decorators must return an object with get, set, or init properties or void 0") expect(() => class { @returnsGetFalse accessor a }).toThrow("accessor.get must be a function"); expect(() => class { @returnsSetFalse accessor a }).toThrow("accessor.set must be a function"); -expect(() => class { @returnsInitializeFalse accessor a }).toThrow("accessor.initialize must be a function"); +expect(() => class { @returnsInitFalse accessor a }).toThrow("accessor.init must be a function"); expect(() => class { @returnsGetSet accessor a }).not.toThrow(); -expect(() => class { @returnsInitialize accessor a }).not.toThrow(); +expect(() => class { @returnsInit accessor a }).not.toThrow(); expect(() => class { @returnsUndefined accessor a }).not.toThrow();