diff --git a/packages/babel-helpers/src/helpers-generated.ts b/packages/babel-helpers/src/helpers-generated.ts index 6524b3653d75..85b5c056487d 100644 --- a/packages/babel-helpers/src/helpers-generated.ts +++ b/packages/babel-helpers/src/helpers-generated.ts @@ -35,7 +35,11 @@ export default Object.freeze({ ), applyDecs2301: helper( "7.21.0", - 'import checkInRHS from"checkInRHS";function createAddInitializerMethod(initializers,decoratorFinishedRef){return function(initializer){assertNotFinished(decoratorFinishedRef,"addInitializer"),assertCallable(initializer,"An initializer"),initializers.push(initializer)}}function assertInstanceIfPrivate(has,target){if(!has(target))throw new TypeError("Attempted to access private element on non-instance")}function memberDec(dec,name,desc,initializers,kind,isStatic,isPrivate,value,hasPrivateBrand){var kindStr;switch(kind){case 1:kindStr="accessor";break;case 2:kindStr="method";break;case 3:kindStr="getter";break;case 4:kindStr="setter";break;default:kindStr="field"}var get,set,ctx={kind:kindStr,name:isPrivate?"#"+name:name,static:isStatic,private:isPrivate},decoratorFinishedRef={v:!1};if(0!==kind&&(ctx.addInitializer=createAddInitializerMethod(initializers,decoratorFinishedRef)),isPrivate||0!==kind&&2!==kind)if(2===kind)get=function(target){return assertInstanceIfPrivate(hasPrivateBrand,target),desc.value};else{var t=0===kind||1===kind;(t||3===kind)&&(get=isPrivate?function(target){return assertInstanceIfPrivate(hasPrivateBrand,target),desc.get.call(target)}:function(target){return desc.get.call(target)}),(t||4===kind)&&(set=isPrivate?function(target,value){assertInstanceIfPrivate(hasPrivateBrand,target),desc.set.call(target,value)}:function(target,value){desc.set.call(target,value)})}else get=function(target){return target[name]},0===kind&&(set=function(target,v){target[name]=v});var has=isPrivate?hasPrivateBrand.bind():function(target){return name in target};ctx.access=get&&set?{get:get,set:set,has:has}:get?{get:get,has:has}:{set:set,has:has};try{return dec(value,ctx)}finally{decoratorFinishedRef.v=!0}}function assertNotFinished(decoratorFinishedRef,fnName){if(decoratorFinishedRef.v)throw new Error("attempted to call "+fnName+" after decoration was finished")}function assertCallable(fn,hint){if("function"!=typeof fn)throw new TypeError(hint+" must be a function")}function assertValidReturnValue(kind,value){var type=typeof value;if(1===kind){if("object"!==type||null===value)throw new TypeError("accessor decorators must return an object with get, set, or init properties or void 0");void 0!==value.get&&assertCallable(value.get,"accessor.get"),void 0!==value.set&&assertCallable(value.set,"accessor.set"),void 0!==value.init&&assertCallable(value.init,"accessor.init")}else if("function"!==type){var hint;throw hint=0===kind?"field":10===kind?"class":"method",new TypeError(hint+" decorators must return a function or void 0")}}function curryThis1(fn){return function(){return fn(this)}}function curryThis2(fn){return function(value){fn(this,value)}}function applyMemberDec(ret,base,decInfo,name,kind,isStatic,isPrivate,initializers,hasPrivateBrand){var desc,init,value,newValue,get,set,decs=decInfo[0];if(isPrivate?desc=0===kind||1===kind?{get:curryThis1(decInfo[3]),set:curryThis2(decInfo[4])}:3===kind?{get:decInfo[3]}:4===kind?{set:decInfo[3]}:{value:decInfo[3]}:0!==kind&&(desc=Object.getOwnPropertyDescriptor(base,name)),1===kind?value={get:desc.get,set:desc.set}:2===kind?value=desc.value:3===kind?value=desc.get:4===kind&&(value=desc.set),"function"==typeof decs)void 0!==(newValue=memberDec(decs,name,desc,initializers,kind,isStatic,isPrivate,value,hasPrivateBrand))&&(assertValidReturnValue(kind,newValue),0===kind?init=newValue:1===kind?(init=newValue.init,get=newValue.get||value.get,set=newValue.set||value.set,value={get:get,set:set}):value=newValue);else for(var i=decs.length-1;i>=0;i--){var newInit;if(void 0!==(newValue=memberDec(decs[i],name,desc,initializers,kind,isStatic,isPrivate,value,hasPrivateBrand)))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===init?init=newInit:"function"==typeof init?init=[init,newInit]:init.push(newInit))}if(0===kind||1===kind){if(void 0===init)init=function(instance,init){return init};else if("function"!=typeof init){var ownInitializers=init;init=function(instance,init){for(var value=init,i=0;i3,isStatic=kind>=5,hasPrivateBrand=instanceBrand;if(isStatic?(base=Class,0!==(kind-=5)&&(initializers=staticInitializers=staticInitializers||[]),isPrivate&&!staticBrand&&(staticBrand=function(_){return checkInRHS(_)===Class}),hasPrivateBrand=staticBrand):(base=Class.prototype,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,initializers,hasPrivateBrand)}}return pushInitializers(ret,protoInitializers),pushInitializers(ret,staticInitializers),ret}function pushInitializers(ret,initializers){initializers&&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 decoratorFinishedRef={v:!1};try{var nextNewClass=classDecs[i](newClass,{kind:"class",name:name,addInitializer:createAddInitializerMethod(initializers,decoratorFinishedRef)})}finally{decoratorFinishedRef.v=!0}void 0!==nextNewClass&&(assertValidReturnValue(10,nextNewClass),newClass=nextNewClass)}return[newClass,function(){for(var i=0;i=0;i--){var newInit;if(void 0!==(newValue=memberDec(decs[i],name,desc,initializers,kind,isStatic,isPrivate,value,hasPrivateBrand)))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===init?init=newInit:"function"==typeof init?init=[init,newInit]:init.push(newInit))}if(0===kind||1===kind){if(void 0===init)init=function(instance,init){return init};else if("function"!=typeof init){var ownInitializers=init;init=function(instance,init){for(var value=init,i=0;i3,isStatic=kind>=5,hasPrivateBrand=instanceBrand;if(isStatic?(base=Class,0!==(kind-=5)&&(initializers=staticInitializers=staticInitializers||[]),isPrivate&&!staticBrand&&(staticBrand=function(_){return checkInRHS(_)===Class}),hasPrivateBrand=staticBrand):(base=Class.prototype,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,initializers,hasPrivateBrand)}}return pushInitializers(ret,protoInitializers),pushInitializers(ret,staticInitializers),ret}function pushInitializers(ret,initializers){initializers&&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 decoratorFinishedRef={v:!1};try{var nextNewClass=classDecs[i](newClass,{kind:"class",name:name,addInitializer:createAddInitializerMethod(initializers,decoratorFinishedRef)})}finally{decoratorFinishedRef.v=!0}void 0!==nextNewClass&&(assertValidReturnValue(10,nextNewClass),newClass=nextNewClass)}return[newClass,function(){for(var i=0;i=0;i-=inc){var newInit;if(void 0!==(newValue=memberDec(decs[i],decoratorsHaveThis?decs[i-1]:void 0,name,desc,initializers,kind,isStatic,isPrivate,value,hasPrivateBrand)))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===init?init=newInit:"function"==typeof init?init=[init,newInit]:init.push(newInit))}if(0===kind||1===kind){if(void 0===init)init=function(instance,init){return init};else if("function"!=typeof init){var ownInitializers=init;init=function(instance,init){for(var value=init,i=ownInitializers.length-1;i>=0;i--)value=ownInitializers[i].call(instance,value);return value}}else{var originalInitializer=init;init=function(instance,init){return originalInitializer.call(instance,init)}}ret.push(init)}0!==kind&&(1===kind?(desc.get=value.get,desc.set=value.set):2===kind?desc.value=value:3===kind?desc.get=value:4===kind&&(desc.set=value),isPrivate?1===kind?(ret.push((function(instance,args){return value.get.call(instance,args)})),ret.push((function(instance,args){return value.set.call(instance,args)}))):2===kind?ret.push(value):ret.push((function(instance,args){return value.call(instance,args)})):Object.defineProperty(base,name,desc))}function applyMemberDecs(Class,decInfos,instanceBrand){for(var protoInitializers,staticInitializers,staticBrand,ret=[],existingProtoNonFields=new Map,existingStaticNonFields=new Map,i=0;i3,decoratorsHaveThis=16&kind,isStatic=!!(8&kind),hasPrivateBrand=instanceBrand;if(kind&=7,isStatic?(base=Class,0!==kind&&(initializers=staticInitializers=staticInitializers||[]),isPrivate&&!staticBrand&&(staticBrand=function(_){return checkInRHS(_)===Class}),hasPrivateBrand=staticBrand):(base=Class.prototype,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);existingNonFields.set(name,!(!existingKind&&kind>2)||kind)}applyMemberDec(ret,base,decInfo,decoratorsHaveThis,name,kind,isStatic,isPrivate,initializers,hasPrivateBrand)}}return pushInitializers(ret,protoInitializers),pushInitializers(ret,staticInitializers),ret}function pushInitializers(ret,initializers){initializers&&ret.push((function(instance){for(var i=0;i=0;i-=inc){var decoratorFinishedRef={v:!1};try{var nextNewClass=classDecs[i].call(decoratorsHaveThis?classDecs[i-1]:void 0,newClass,{kind:"class",name:name,addInitializer:createAddInitializerMethod(initializers,decoratorFinishedRef)})}finally{decoratorFinishedRef.v=!0}void 0!==nextNewClass&&(assertValidReturnValue(5,nextNewClass),newClass=nextNewClass)}return[newClass,function(){for(var i=0;i= 0; i--) { - var dec = decs[i]; + var newValue, get, set; + + if (typeof decs === "function") { newValue = memberDec( - dec, + decs, name, desc, initializers, @@ -300,12 +274,11 @@ function applyMemberDec( if (newValue !== void 0) { assertValidReturnValue(kind, newValue); - var newInit; if (kind === 0 /* FIELD */) { - newInit = newValue; + init = newValue; } else if (kind === 1 /* ACCESSOR */) { - newInit = newValue.init; + init = newValue.init; get = newValue.get || value.get; set = newValue.set || value.set; @@ -313,367 +286,414 @@ function applyMemberDec( } else { value = newValue; } + } + } else { + for (var i = decs.length - 1; i >= 0; i--) { + var dec = decs[i]; + + newValue = memberDec( + dec, + name, + desc, + initializers, + kind, + isStatic, + isPrivate, + value, + hasPrivateBrand + ); - if (newInit !== void 0) { - if (init === void 0) { - init = newInit; - } else if (typeof init === "function") { - init = [init, newInit]; + if (newValue !== void 0) { + assertValidReturnValue(kind, newValue); + var newInit; + + if (kind === 0 /* FIELD */) { + newInit = newValue; + } else if (kind === 1 /* ACCESSOR */) { + newInit = newValue.init; + get = newValue.get || value.get; + set = newValue.set || value.set; + + value = { get: get, set: set }; } else { - init.push(newInit); + value = newValue; + } + + if (newInit !== void 0) { + if (init === void 0) { + init = newInit; + } else if (typeof init === "function") { + init = [init, newInit]; + } else { + init.push(newInit); + } } } } } - } - - if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) { - if (init === void 0) { - // If the initializer was void 0, sub in a dummy initializer - init = function (instance, init) { - return init; - }; - } else if (typeof init !== "function") { - var ownInitializers = init; - init = function (instance, init) { - var value = init; + if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) { + if (init === void 0) { + // If the initializer was void 0, sub in a dummy initializer + init = function (instance, init) { + return init; + }; + } else if (typeof init !== "function") { + var ownInitializers = init; - for (var i = 0; i < ownInitializers.length; i++) { - value = ownInitializers[i].call(instance, value); - } + init = function (instance, init) { + var value = init; - return value; - }; - } else { - var originalInitializer = init; + for (var i = 0; i < ownInitializers.length; i++) { + value = ownInitializers[i].call(instance, value); + } - init = function (instance, init) { - return originalInitializer.call(instance, init); - }; - } + return value; + }; + } else { + var originalInitializer = init; - ret.push(init); - } + init = function (instance, init) { + return originalInitializer.call(instance, init); + }; + } - if (kind !== 0 /* FIELD */) { - if (kind === 1 /* ACCESSOR */) { - desc.get = value.get; - desc.set = value.set; - } else if (kind === 2 /* METHOD */) { - desc.value = value; - } else if (kind === 3 /* GETTER */) { - desc.get = value; - } else if (kind === 4 /* SETTER */) { - desc.set = value; + ret.push(init); } - if (isPrivate) { + if (kind !== 0 /* FIELD */) { if (kind === 1 /* ACCESSOR */) { - ret.push(function (instance, args) { - return value.get.call(instance, args); - }); - ret.push(function (instance, args) { - return value.set.call(instance, args); - }); + desc.get = value.get; + desc.set = value.set; } else if (kind === 2 /* METHOD */) { - ret.push(value); + desc.value = value; + } else if (kind === 3 /* GETTER */) { + desc.get = value; + } else if (kind === 4 /* SETTER */) { + desc.set = value; + } + + if (isPrivate) { + if (kind === 1 /* ACCESSOR */) { + ret.push(function (instance, args) { + return value.get.call(instance, args); + }); + ret.push(function (instance, args) { + return value.set.call(instance, args); + }); + } else if (kind === 2 /* METHOD */) { + ret.push(value); + } else { + ret.push(function (instance, args) { + return value.call(instance, args); + }); + } } else { - ret.push(function (instance, args) { - return value.call(instance, args); - }); + Object.defineProperty(base, name, desc); } - } else { - Object.defineProperty(base, name, desc); } } -} -function applyMemberDecs(Class, decInfos, instanceBrand) { - var ret = []; - var protoInitializers; - var staticInitializers; - var staticBrand; - - var existingProtoNonFields = new Map(); - var existingStaticNonFields = new Map(); - - for (var i = 0; i < decInfos.length; i++) { - var decInfo = decInfos[i]; - - // skip computed property names - if (!Array.isArray(decInfo)) continue; - - var kind = decInfo[1]; - var name = decInfo[2]; - var isPrivate = decInfo.length > 3; - - var isStatic = kind >= 5; /* STATIC */ - var base; - var initializers; - var hasPrivateBrand = instanceBrand; - - if (isStatic) { - base = Class; - kind = kind - 5 /* STATIC */; - // initialize staticInitializers when we see a non-field static member - if (kind !== 0 /* FIELD */) { - staticInitializers = staticInitializers || []; - initializers = staticInitializers; - } - if (isPrivate && !staticBrand) { - staticBrand = function (_) { - return checkInRHS(_) === Class; - }; - } - hasPrivateBrand = staticBrand; - } else { - base = Class.prototype; - // initialize protoInitializers when we see a non-field member - if (kind !== 0 /* FIELD */) { - protoInitializers = protoInitializers || []; - initializers = protoInitializers; + function applyMemberDecs(Class, decInfos, instanceBrand) { + var ret = []; + var protoInitializers; + var staticInitializers; + var staticBrand; + + var existingProtoNonFields = new Map(); + var existingStaticNonFields = new Map(); + + for (var i = 0; i < decInfos.length; i++) { + var decInfo = decInfos[i]; + + // skip computed property names + if (!Array.isArray(decInfo)) continue; + + var kind = decInfo[1]; + var name = decInfo[2]; + var isPrivate = decInfo.length > 3; + + var isStatic = kind >= 5; /* STATIC */ + var base; + var initializers; + var hasPrivateBrand = instanceBrand; + + if (isStatic) { + base = Class; + kind = kind - 5 /* STATIC */; + // initialize staticInitializers when we see a non-field static member + if (kind !== 0 /* FIELD */) { + staticInitializers = staticInitializers || []; + initializers = staticInitializers; + } + if (isPrivate && !staticBrand) { + staticBrand = function (_) { + return checkInRHS(_) === Class; + }; + } + hasPrivateBrand = staticBrand; + } else { + base = Class.prototype; + // initialize protoInitializers when we see a non-field member + if (kind !== 0 /* FIELD */) { + protoInitializers = protoInitializers || []; + initializers = protoInitializers; + } } - } - if (kind !== 0 /* FIELD */ && !isPrivate) { - var existingNonFields = isStatic - ? existingStaticNonFields - : existingProtoNonFields; - - var existingKind = existingNonFields.get(name) || 0; - - if ( - existingKind === true || - (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ || - (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */ - ) { - 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 - ); - } else if (!existingKind && kind > 2 /* METHOD */) { - existingNonFields.set(name, kind); - } else { - existingNonFields.set(name, true); + if (kind !== 0 /* FIELD */ && !isPrivate) { + var existingNonFields = isStatic + ? existingStaticNonFields + : existingProtoNonFields; + + var existingKind = existingNonFields.get(name) || 0; + + if ( + existingKind === true || + (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ || + (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */ + ) { + 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 + ); + } else if (!existingKind && kind > 2 /* METHOD */) { + existingNonFields.set(name, kind); + } else { + existingNonFields.set(name, true); + } } + + applyMemberDec( + ret, + base, + decInfo, + name, + kind, + isStatic, + isPrivate, + initializers, + hasPrivateBrand + ); } - applyMemberDec( - ret, - base, - decInfo, - name, - kind, - isStatic, - isPrivate, - initializers, - hasPrivateBrand - ); + pushInitializers(ret, protoInitializers); + pushInitializers(ret, staticInitializers); + return ret; } - pushInitializers(ret, protoInitializers); - pushInitializers(ret, staticInitializers); - return ret; -} - -function pushInitializers(ret, initializers) { - if (initializers) { - ret.push(function (instance) { - for (var i = 0; i < initializers.length; i++) { - initializers[i].call(instance); - } - return instance; - }); + function pushInitializers(ret, initializers) { + if (initializers) { + ret.push(function (instance) { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(instance); + } + return instance; + }); + } } -} -function applyClassDecs(targetClass, classDecs) { - if (classDecs.length > 0) { - var initializers = []; - var newClass = targetClass; - var name = targetClass.name; - - for (var i = classDecs.length - 1; i >= 0; i--) { - var decoratorFinishedRef = { v: false }; - - try { - var nextNewClass = classDecs[i](newClass, { - kind: "class", - name: name, - addInitializer: createAddInitializerMethod( - initializers, - decoratorFinishedRef - ), - }); - } finally { - decoratorFinishedRef.v = true; - } + function applyClassDecs(targetClass, classDecs) { + if (classDecs.length > 0) { + var initializers = []; + var newClass = targetClass; + var name = targetClass.name; + + for (var i = classDecs.length - 1; i >= 0; i--) { + var decoratorFinishedRef = { v: false }; + + try { + var nextNewClass = classDecs[i](newClass, { + kind: "class", + name: name, + addInitializer: createAddInitializerMethod( + initializers, + decoratorFinishedRef + ), + }); + } finally { + decoratorFinishedRef.v = true; + } - if (nextNewClass !== undefined) { - assertValidReturnValue(10 /* CLASS */, nextNewClass); - newClass = nextNewClass; + if (nextNewClass !== undefined) { + assertValidReturnValue(10 /* CLASS */, nextNewClass); + newClass = nextNewClass; + } } - } - return [ - newClass, - function () { - for (var i = 0; i < initializers.length; i++) { - initializers[i].call(newClass); - } - }, - ]; + return [ + newClass, + function () { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(newClass); + } + }, + ]; + } + // The transformer will not emit assignment when there are no class decorators, + // so we don't have to return an empty array here. } - // The transformer will not emit assignment when there are no class decorators, - // so we don't have to return an empty array here. -} -/** - Basic usage: + /** + Basic usage: - applyDecs( - Class, - [ - // member decorators + applyDecs( + Class, + [ + // member decorators + [ + dec, // dec or array of decs + 0, // kind of value being decorated + 'prop', // name of public prop on class containing the value being decorated, + '#p', // the name of the private property (if is private, void 0 otherwise), + ] + ], [ - dec, // dec or array of decs - 0, // kind of value being decorated - 'prop', // name of public prop on class containing the value being decorated, - '#p', // the name of the private property (if is private, void 0 otherwise), + // class decorators + dec1, dec2 ] - ], - [ - // class decorators - dec1, dec2 - ] - ) - ``` - - Fully transpiled example: - - ```js - @dec - class Class { - @dec - a = 123; + ) + ``` - @dec - #a = 123; + Fully transpiled example: + ```js @dec - @dec2 - accessor b = 123; + class Class { + @dec + a = 123; - @dec - accessor #b = 123; + @dec + #a = 123; - @dec - c() { console.log('c'); } + @dec + @dec2 + accessor b = 123; - @dec - #c() { console.log('privC'); } + @dec + accessor #b = 123; - @dec - get d() { console.log('d'); } + @dec + c() { console.log('c'); } - @dec - get #d() { console.log('privD'); } + @dec + #c() { console.log('privC'); } - @dec - set e(v) { console.log('e'); } + @dec + get d() { console.log('d'); } - @dec - set #e(v) { console.log('privE'); } - } + @dec + get #d() { console.log('privD'); } + @dec + set e(v) { console.log('e'); } - // becomes - let initializeInstance; - let initializeClass; + @dec + set #e(v) { console.log('privE'); } + } - let initA; - let initPrivA; - let initB; - let initPrivB, getPrivB, setPrivB; + // becomes + let initializeInstance; + let initializeClass; - let privC; - let privD; - let privE; + let initA; + let initPrivA; - let Class; - class _Class { - static { - let ret = applyDecs( - this, - [ - [dec, 0, 'a'], - [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v], - [[dec, dec2], 1, 'b'], - [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v], - [dec, 2, 'c'], - [dec, 2, 'c', () => console.log('privC')], - [dec, 3, 'd'], - [dec, 3, 'd', () => console.log('privD')], - [dec, 4, 'e'], - [dec, 4, 'e', () => console.log('privE')], - ], - [ - dec - ] - ) + let initB; + let initPrivB, getPrivB, setPrivB; - initA = ret[0]; + let privC; + let privD; + let privE; - initPrivA = ret[1]; + let Class; + class _Class { + static { + let ret = applyDecs( + this, + [ + [dec, 0, 'a'], + [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v], + [[dec, dec2], 1, 'b'], + [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v], + [dec, 2, 'c'], + [dec, 2, 'c', () => console.log('privC')], + [dec, 3, 'd'], + [dec, 3, 'd', () => console.log('privD')], + [dec, 4, 'e'], + [dec, 4, 'e', () => console.log('privE')], + ], + [ + dec + ] + ) - initB = ret[2]; + initA = ret[0]; - initPrivB = ret[3]; - getPrivB = ret[4]; - setPrivB = ret[5]; + initPrivA = ret[1]; - privC = ret[6]; + initB = ret[2]; - privD = ret[7]; + initPrivB = ret[3]; + getPrivB = ret[4]; + setPrivB = ret[5]; - privE = ret[8]; + privC = ret[6]; - initializeInstance = ret[9]; + privD = ret[7]; - Class = ret[10] + privE = ret[8]; - initializeClass = ret[11]; - } + initializeInstance = ret[9]; + + Class = ret[10] - a = (initializeInstance(this), initA(this, 123)); + initializeClass = ret[11]; + } - #a = initPrivA(this, 123); + a = (initializeInstance(this), initA(this, 123)); - #bData = initB(this, 123); - get b() { return this.#bData } - set b(v) { this.#bData = v } + #a = initPrivA(this, 123); - #privBData = initPrivB(this, 123); - get #b() { return getPrivB(this); } - set #b(v) { setPrivB(this, v); } + #bData = initB(this, 123); + get b() { return this.#bData } + set b(v) { this.#bData = v } - c() { console.log('c'); } + #privBData = initPrivB(this, 123); + get #b() { return getPrivB(this); } + set #b(v) { setPrivB(this, v); } - #c(...args) { return privC(this, ...args) } + c() { console.log('c'); } - get d() { console.log('d'); } + #c(...args) { return privC(this, ...args) } - get #d() { return privD(this); } + get d() { console.log('d'); } - set e(v) { console.log('e'); } + get #d() { return privD(this); } - set #e(v) { privE(this, v); } - } + set e(v) { console.log('e'); } + + set #e(v) { privE(this, v); } + } - initializeClass(Class); - */ + initializeClass(Class); + */ + return function applyDecs2301( + targetClass, + memberDecs, + classDecs, + instanceBrand + ) { + return { + e: applyMemberDecs(targetClass, memberDecs, instanceBrand), + // Lazily apply class decorations so that member init locals can be properly bound. + get c() { + return applyClassDecs(targetClass, classDecs); + }, + }; + }; +} export default function applyDecs2301( targetClass, @@ -681,11 +701,10 @@ export default function applyDecs2301( classDecs, instanceBrand ) { - return { - e: applyMemberDecs(targetClass, memberDecs, instanceBrand), - // Lazily apply class decorations so that member init locals can be properly bound. - get c() { - return applyClassDecs(targetClass, classDecs); - }, - }; + return (applyDecs2301 = applyDecs2301Factory())( + targetClass, + memberDecs, + classDecs, + instanceBrand + ); } diff --git a/packages/babel-helpers/src/helpers/applyDecs2305.js b/packages/babel-helpers/src/helpers/applyDecs2305.js new file mode 100644 index 000000000000..a8a48d159b49 --- /dev/null +++ b/packages/babel-helpers/src/helpers/applyDecs2305.js @@ -0,0 +1,681 @@ +/* @minVersion 7.21.0 */ + +import checkInRHS from "checkInRHS"; + +/** + Enums are used in this file, but not assigned to vars to avoid non-hoistable values + + CONSTRUCTOR = 0; + PUBLIC = 1; + PRIVATE = 2; + + FIELD = 0; + ACCESSOR = 1; + METHOD = 2; + GETTER = 3; + SETTER = 4; + CLASS = 5; // only used in assertValidReturnValue + + STATIC = 8; + + DECORATORS_HAVE_THIS = 16; +*/ + +function createAddInitializerMethod(initializers, decoratorFinishedRef) { + return function addInitializer(initializer) { + assertNotFinished(decoratorFinishedRef, "addInitializer"); + assertCallable(initializer, "An initializer"); + initializers.push(initializer); + }; +} + +function assertInstanceIfPrivate(has, target) { + if (!has(target)) { + throw new TypeError("Attempted to access private element on non-instance"); + } +} + +function memberDec( + dec, + thisArg, + name, + desc, + initializers, + kind, + isStatic, + isPrivate, + value, + hasPrivateBrand +) { + var kindStr; + + switch (kind) { + case 1 /* ACCESSOR */: + kindStr = "accessor"; + break; + case 2 /* METHOD */: + kindStr = "method"; + break; + case 3 /* GETTER */: + kindStr = "getter"; + break; + case 4 /* SETTER */: + kindStr = "setter"; + break; + default: + kindStr = "field"; + } + + var ctx = { + kind: kindStr, + name: isPrivate ? "#" + name : name, + static: isStatic, + private: isPrivate, + }; + + var decoratorFinishedRef = { v: false }; + + if (kind !== 0 /* FIELD */) { + ctx.addInitializer = createAddInitializerMethod( + initializers, + decoratorFinishedRef + ); + } + + var get, set; + if (!isPrivate && (kind === 0 /* FIELD */ || kind === 2) /* METHOD */) { + get = function (target) { + return target[name]; + }; + if (kind === 0 /* FIELD */) { + set = function (target, v) { + target[name] = v; + }; + } + } else if (kind === 2 /* METHOD */) { + // Assert: isPrivate is true. + get = function (target) { + assertInstanceIfPrivate(hasPrivateBrand, target); + return desc.value; + }; + } else { + // Assert: If kind === 0, then isPrivate is true. + var t = kind === 0 /* FIELD */ || kind === 1; /* ACCESSOR */ + if (t || kind === 3 /* GETTER */) { + if (isPrivate) { + get = function (target) { + assertInstanceIfPrivate(hasPrivateBrand, target); + return desc.get.call(target); + }; + } else { + get = function (target) { + return desc.get.call(target); + }; + } + } + if (t || kind === 4 /* SETTER */) { + if (isPrivate) { + set = function (target, value) { + assertInstanceIfPrivate(hasPrivateBrand, target); + desc.set.call(target, value); + }; + } else { + set = function (target, value) { + desc.set.call(target, value); + }; + } + } + } + var has = isPrivate + ? hasPrivateBrand.bind() + : function (target) { + return name in target; + }; + ctx.access = + get && set + ? { get: get, set: set, has: has } + : get + ? { get: get, has: has } + : { set: set, has: has }; + + try { + return dec.call(thisArg, value, ctx); + } finally { + decoratorFinishedRef.v = true; + } +} + +function assertNotFinished(decoratorFinishedRef, fnName) { + if (decoratorFinishedRef.v) { + throw new Error( + "attempted to call " + fnName + " after decoration was finished" + ); + } +} + +function assertCallable(fn, hint) { + if (typeof fn !== "function") { + throw new TypeError(hint + " must be a function"); + } +} + +function assertValidReturnValue(kind, value) { + var type = typeof value; + + if (kind === 1 /* ACCESSOR */) { + if (type !== "object" || value === null) { + throw new TypeError( + "accessor decorators must return an object with get, set, or init properties or void 0" + ); + } + if (value.get !== undefined) { + assertCallable(value.get, "accessor.get"); + } + if (value.set !== undefined) { + assertCallable(value.set, "accessor.set"); + } + if (value.init !== undefined) { + assertCallable(value.init, "accessor.init"); + } + } else if (type !== "function") { + var hint; + if (kind === 0 /* FIELD */) { + hint = "field"; + } else if (kind === 5 /* CLASS */) { + hint = "class"; + } else { + hint = "method"; + } + throw new TypeError(hint + " decorators must return a function or void 0"); + } +} + +function curryThis1(fn) { + return function () { + return fn(this); + }; +} +function curryThis2(fn) { + return function (value) { + fn(this, value); + }; +} + +function applyMemberDec( + ret, + base, + decInfo, + decoratorsHaveThis, + name, + kind, + isStatic, + isPrivate, + initializers, + hasPrivateBrand +) { + var decs = decInfo[0]; + + if (!decoratorsHaveThis && !Array.isArray(decs)) { + decs = [decs]; + } + + var desc, init, value; + + if (isPrivate) { + if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) { + desc = { + get: curryThis1(decInfo[3]), + set: curryThis2(decInfo[4]), + }; + } else { + if (kind === 3 /* GETTER */) { + desc = { + get: decInfo[3], + }; + } else if (kind === 4 /* SETTER */) { + desc = { + set: decInfo[3], + }; + } else { + desc = { + value: decInfo[3], + }; + } + } + } else if (kind !== 0 /* FIELD */) { + desc = Object.getOwnPropertyDescriptor(base, name); + } + + if (kind === 1 /* ACCESSOR */) { + value = { + get: desc.get, + set: desc.set, + }; + } else if (kind === 2 /* METHOD */) { + value = desc.value; + } else if (kind === 3 /* GETTER */) { + value = desc.get; + } else if (kind === 4 /* SETTER */) { + value = desc.set; + } + + var newValue, get, set; + + var inc = decoratorsHaveThis ? 2 : 1; + + for (var i = decs.length - 1; i >= 0; i -= inc) { + var dec = decs[i]; + + newValue = memberDec( + dec, + decoratorsHaveThis ? decs[i - 1] : undefined, + name, + desc, + initializers, + kind, + isStatic, + isPrivate, + value, + hasPrivateBrand + ); + + if (newValue !== void 0) { + assertValidReturnValue(kind, newValue); + var newInit; + + if (kind === 0 /* FIELD */) { + newInit = newValue; + } else if (kind === 1 /* ACCESSOR */) { + newInit = newValue.init; + get = newValue.get || value.get; + set = newValue.set || value.set; + + value = { get: get, set: set }; + } else { + value = newValue; + } + + if (newInit !== void 0) { + if (init === void 0) { + init = newInit; + } else if (typeof init === "function") { + init = [init, newInit]; + } else { + init.push(newInit); + } + } + } + } + + if (kind === 0 /* FIELD */ || kind === 1 /* ACCESSOR */) { + if (init === void 0) { + // If the initializer was void 0, sub in a dummy initializer + init = function (instance, init) { + return init; + }; + } else if (typeof init !== "function") { + var ownInitializers = init; + + init = function (instance, init) { + var value = init; + + for (var i = ownInitializers.length - 1; i >= 0; i--) { + value = ownInitializers[i].call(instance, value); + } + + return value; + }; + } else { + var originalInitializer = init; + + init = function (instance, init) { + return originalInitializer.call(instance, init); + }; + } + + ret.push(init); + } + + if (kind !== 0 /* FIELD */) { + if (kind === 1 /* ACCESSOR */) { + desc.get = value.get; + desc.set = value.set; + } else if (kind === 2 /* METHOD */) { + desc.value = value; + } else if (kind === 3 /* GETTER */) { + desc.get = value; + } else if (kind === 4 /* SETTER */) { + desc.set = value; + } + + if (isPrivate) { + if (kind === 1 /* ACCESSOR */) { + ret.push(function (instance, args) { + return value.get.call(instance, args); + }); + ret.push(function (instance, args) { + return value.set.call(instance, args); + }); + } else if (kind === 2 /* METHOD */) { + ret.push(value); + } else { + ret.push(function (instance, args) { + return value.call(instance, args); + }); + } + } else { + Object.defineProperty(base, name, desc); + } + } +} + +function applyMemberDecs(Class, decInfos, instanceBrand) { + var ret = []; + var protoInitializers; + var staticInitializers; + var staticBrand; + + var existingProtoNonFields = new Map(); + var existingStaticNonFields = new Map(); + + for (var i = 0; i < decInfos.length; i++) { + var decInfo = decInfos[i]; + + // skip computed property names + if (!Array.isArray(decInfo)) continue; + + var kind = decInfo[1]; + var name = decInfo[2]; + var isPrivate = decInfo.length > 3; + + var decoratorsHaveThis = kind & 16; /* DECORATORS_HAVE_THIS */ + var isStatic = !!(kind & 8); /* STATIC */ + var base; + var initializers; + var hasPrivateBrand = instanceBrand; + + kind &= 7 /* 0b111 */; + + if (isStatic) { + base = Class; + // initialize staticInitializers when we see a non-field static member + if (kind !== 0 /* FIELD */) { + staticInitializers = staticInitializers || []; + initializers = staticInitializers; + } + if (isPrivate && !staticBrand) { + staticBrand = function (_) { + return checkInRHS(_) === Class; + }; + } + hasPrivateBrand = staticBrand; + } else { + base = Class.prototype; + // initialize protoInitializers when we see a non-field member + if (kind !== 0 /* FIELD */) { + protoInitializers = protoInitializers || []; + initializers = protoInitializers; + } + } + + if (kind !== 0 /* FIELD */ && !isPrivate) { + var existingNonFields = isStatic + ? existingStaticNonFields + : existingProtoNonFields; + + var existingKind = existingNonFields.get(name) || 0; + + if ( + existingKind === true || + (existingKind === 3 /* GETTER */ && kind !== 4) /* SETTER */ || + (existingKind === 4 /* SETTER */ && kind !== 3) /* GETTER */ + ) { + 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 + ); + } + existingNonFields.set( + name, + !existingKind && kind > 2 /* METHOD */ ? kind : true + ); + } + + applyMemberDec( + ret, + base, + decInfo, + decoratorsHaveThis, + name, + kind, + isStatic, + isPrivate, + initializers, + hasPrivateBrand + ); + } + + pushInitializers(ret, protoInitializers); + pushInitializers(ret, staticInitializers); + return ret; +} + +function pushInitializers(ret, initializers) { + if (initializers) { + ret.push(function (instance) { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(instance); + } + return instance; + }); + } +} + +function applyClassDecs(targetClass, classDecs, decoratorsHaveThis) { + if (classDecs.length) { + var initializers = []; + var newClass = targetClass; + var name = targetClass.name; + + var inc = decoratorsHaveThis ? 2 : 1; + + for (var i = classDecs.length - 1; i >= 0; i -= inc) { + var decoratorFinishedRef = { v: false }; + + try { + var nextNewClass = classDecs[i].call( + decoratorsHaveThis ? classDecs[i - 1] : undefined, + newClass, + { + kind: "class", + name: name, + addInitializer: createAddInitializerMethod( + initializers, + decoratorFinishedRef + ), + } + ); + } finally { + decoratorFinishedRef.v = true; + } + + if (nextNewClass !== undefined) { + assertValidReturnValue(5 /* CLASS */, nextNewClass); + newClass = nextNewClass; + } + } + + return [ + newClass, + function () { + for (var i = 0; i < initializers.length; i++) { + initializers[i].call(newClass); + } + }, + ]; + } + // The transformer will not emit assignment when there are no class decorators, + // so we don't have to return an empty array here. +} + +/** + Basic usage: + + applyDecs( + Class, + [ + // member decorators + [ + decs, // dec, or array of decs, or array of this values and decs + 0, // kind of value being decorated + 'prop', // name of public prop on class containing the value being decorated, + '#p', // the name of the private property (if is private, void 0 otherwise), + ] + ], + [ + // class decorators + dec1, dec2 + ] + ) + ``` + + Fully transpiled example: + + ```js + @dec + class Class { + @dec + a = 123; + + @dec + #a = 123; + + @dec + @dec2 + accessor b = 123; + + @dec + accessor #b = 123; + + @dec + c() { console.log('c'); } + + @dec + #c() { console.log('privC'); } + + @dec + get d() { console.log('d'); } + + @dec + get #d() { console.log('privD'); } + + @dec + set e(v) { console.log('e'); } + + @dec + set #e(v) { console.log('privE'); } + } + + + // becomes + let initializeInstance; + let initializeClass; + + let initA; + let initPrivA; + + let initB; + let initPrivB, getPrivB, setPrivB; + + let privC; + let privD; + let privE; + + let Class; + class _Class { + static { + let ret = applyDecs( + this, + [ + [dec, 0, 'a'], + [dec, 0, 'a', (i) => i.#a, (i, v) => i.#a = v], + [[dec, dec2], 1, 'b'], + [dec, 1, 'b', (i) => i.#privBData, (i, v) => i.#privBData = v], + [dec, 2, 'c'], + [dec, 2, 'c', () => console.log('privC')], + [dec, 3, 'd'], + [dec, 3, 'd', () => console.log('privD')], + [dec, 4, 'e'], + [dec, 4, 'e', () => console.log('privE')], + ], + [ + dec + ] + ); + + initA = ret[0]; + + initPrivA = ret[1]; + + initB = ret[2]; + + initPrivB = ret[3]; + getPrivB = ret[4]; + setPrivB = ret[5]; + + privC = ret[6]; + + privD = ret[7]; + + privE = ret[8]; + + initializeInstance = ret[9]; + + Class = ret[10] + + initializeClass = ret[11]; + } + + a = (initializeInstance(this), initA(this, 123)); + + #a = initPrivA(this, 123); + + #bData = initB(this, 123); + get b() { return this.#bData } + set b(v) { this.#bData = v } + + #privBData = initPrivB(this, 123); + get #b() { return getPrivB(this); } + set #b(v) { setPrivB(this, v); } + + c() { console.log('c'); } + + #c(...args) { return privC(this, ...args) } + + get d() { console.log('d'); } + + get #d() { return privD(this); } + + set e(v) { console.log('e'); } + + set #e(v) { privE(this, v); } + } + + initializeClass(Class); + */ + +export default function applyDecs2305( + targetClass, + memberDecs, + classDecs, + classDecsHaveThis, + instanceBrand +) { + return { + e: applyMemberDecs(targetClass, memberDecs, instanceBrand), + // Lazily apply class decorations so that member init locals can be properly bound. + get c() { + return applyClassDecs(targetClass, classDecs, classDecsHaveThis); + }, + }; +} diff --git a/packages/babel-plugin-proposal-decorators/src/index.ts b/packages/babel-plugin-proposal-decorators/src/index.ts index f6c7d6730137..88305927b797 100644 --- a/packages/babel-plugin-proposal-decorators/src/index.ts +++ b/packages/babel-plugin-proposal-decorators/src/index.ts @@ -7,7 +7,7 @@ import { FEATURES, } from "@babel/helper-create-class-features-plugin"; import legacyVisitor from "./transformer-legacy"; -import transformer2022_03 from "./transformer-2023-01"; +import transformer2023_05 from "./transformer-2023-05"; import type { Options as SyntaxOptions } from "@babel/plugin-syntax-decorators"; interface Options extends SyntaxOptions { @@ -40,9 +40,10 @@ export default declare((api, options: Options) => { } else if ( version === "2021-12" || version === "2022-03" || - version === "2023-01" + version === "2023-01" || + version === "2023-05" ) { - return transformer2022_03(api, options, version); + return transformer2023_05(api, options, version); } else if (!process.env.BABEL_8_BREAKING) { api.assertVersion("^7.0.2"); return createClassFeaturePlugin({ diff --git a/packages/babel-plugin-proposal-decorators/src/transformer-2023-01.ts b/packages/babel-plugin-proposal-decorators/src/transformer-2023-05.ts similarity index 88% rename from packages/babel-plugin-proposal-decorators/src/transformer-2023-01.ts rename to packages/babel-plugin-proposal-decorators/src/transformer-2023-05.ts index c7b0d1424ebc..df457dd239a0 100644 --- a/packages/babel-plugin-proposal-decorators/src/transformer-2023-01.ts +++ b/packages/babel-plugin-proposal-decorators/src/transformer-2023-05.ts @@ -20,7 +20,7 @@ type ClassElement = | t.TSIndexSignature | t.StaticBlock; -type DecoratorVersionKind = "2023-01" | "2022-03" | "2021-12"; +type DecoratorVersionKind = "2023-05" | "2023-01" | "2022-03" | "2021-12"; function incrementId(id: number[], idx = id.length - 1): void { // If index is -1, id needs an additional character, unshift A @@ -165,16 +165,21 @@ function generateClassProperty( } function addProxyAccessorsFor( + className: t.Identifier, element: NodePath, originalKey: t.PrivateName | t.Expression, targetKey: t.PrivateName, + version: DecoratorVersionKind, isComputed = false, ): void { const { static: isStatic } = element.node; + const thisArg = + version === "2023-05" && isStatic ? className : t.thisExpression(); + const getterBody = t.blockStatement([ t.returnStatement( - t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.memberExpression(t.cloneNode(thisArg), t.cloneNode(targetKey)), ), ]); @@ -182,7 +187,7 @@ function addProxyAccessorsFor( t.expressionStatement( t.assignmentExpression( "=", - t.memberExpression(t.thisExpression(), t.cloneNode(targetKey)), + t.memberExpression(t.cloneNode(thisArg), t.cloneNode(targetKey)), t.identifier("v"), ), ), @@ -233,7 +238,7 @@ function extractProxyAccessorsFor( targetKey: t.PrivateName, version: DecoratorVersionKind, ): (t.FunctionExpression | t.ArrowFunctionExpression)[] { - if (version !== "2023-01") { + if (version !== "2023-05" && version !== "2023-01") { return [ template.expression.ast` function () { @@ -257,13 +262,16 @@ function extractProxyAccessorsFor( ]; } +// 3 bits reserved to this (0-7) const FIELD = 0; const ACCESSOR = 1; const METHOD = 2; const GETTER = 3; const SETTER = 4; -const STATIC = 5; +const STATIC_OLD_VERSION = 5; // Before 2023-05 +const STATIC = 8; // 1 << 3 +const DECORATORS_HAVE_THIS = 16; // 1 << 3 function getElementKind(element: NodePath): number { switch (element.node.type) { @@ -288,6 +296,7 @@ function getElementKind(element: NodePath): number { interface DecoratorInfo { // The expressions of the decorators themselves decorators: t.Expression[]; + decoratorsThis: t.Expression[]; // The kind of the decorated value, matches the kind value passed to applyDecs kind: number; @@ -337,21 +346,47 @@ function filteredOrderedDecoratorInfo( ]; } +function generateDecorationList( + decorators: t.Expression[], + decoratorsThis: (t.Expression | null)[], + version: DecoratorVersionKind, +) { + const decsCount = decorators.length; + const hasOneThis = decoratorsThis.some(Boolean); + const decs: t.Expression[] = []; + for (let i = 0; i < decsCount; i++) { + if (version === "2023-05" && hasOneThis) { + decs.push( + decoratorsThis[i] || t.unaryExpression("void", t.numericLiteral(0)), + ); + } + decs.push(decorators[i]); + } + + return { hasThis: hasOneThis, decs }; +} + function generateDecorationExprs( info: (DecoratorInfo | ComputedPropInfo)[], + version: DecoratorVersionKind, ): t.ArrayExpression { return t.arrayExpression( filteredOrderedDecoratorInfo(info).map(el => { - const decs = - el.decorators.length > 1 - ? t.arrayExpression(el.decorators) - : el.decorators[0]; + const { decs, hasThis } = generateDecorationList( + el.decorators, + el.decoratorsThis, + version, + ); - const kind = el.isStatic ? el.kind + STATIC : el.kind; + let flag = el.kind; + if (el.isStatic) { + flag += version === "2023-05" ? STATIC : STATIC_OLD_VERSION; + } + if (hasThis) flag += DECORATORS_HAVE_THIS; return t.arrayExpression([ - decs, - t.numericLiteral(kind), + decs.length === 1 ? decs[0] : t.arrayExpression(decs), + t.numericLiteral(flag), el.name, ...(el.privateMethods || []), ]); @@ -513,7 +548,14 @@ function transformClass( const newField = generateClassProperty(newId, valueNode, isStatic); const [newPath] = element.replaceWith(newField); - addProxyAccessorsFor(newPath, key, newId, computed); + addProxyAccessorsFor( + path.node.id, + newPath, + key, + newId, + version, + computed, + ); } } @@ -544,6 +586,29 @@ function transformClass( return t.cloneNode(localEvaluatedId); }; + const decoratorsThis = new Map(); + const maybeExtractDecorator = (decorator: t.Decorator) => { + const { expression } = decorator; + if (version === "2023-05" && t.isMemberExpression(expression)) { + let object; + if ( + t.isSuper(expression.object) || + t.isThisExpression(expression.object) + ) { + object = memoiseExpression(t.thisExpression(), "obj"); + } else if (!scopeParent.isStatic(expression.object)) { + object = memoiseExpression(expression.object, "obj"); + expression.object = object; + } else { + object = expression.object; + } + decoratorsThis.set(decorator, t.cloneNode(object)); + } + if (!scopeParent.isStatic(expression)) { + decorator.expression = memoiseExpression(expression, "dec"); + } + }; + if (classDecorators) { classInitLocal = scopeParent.generateDeclaredUidIdentifier("initClass"); @@ -554,12 +619,7 @@ function transformClass( path.node.decorators = null; for (const classDecorator of classDecorators) { - if (!scopeParent.isStatic(classDecorator.expression)) { - classDecorator.expression = memoiseExpression( - classDecorator.expression, - "dec", - ); - } + maybeExtractDecorator(classDecorator); } } else { if (!path.node.id) { @@ -584,12 +644,7 @@ function transformClass( if (hasDecorators) { for (const decoratorPath of decorators) { - if (!scopeParent.isStatic(decoratorPath.node.expression)) { - decoratorPath.node.expression = memoiseExpression( - decoratorPath.node.expression, - "dec", - ); - } + maybeExtractDecorator(decoratorPath.node); } } @@ -669,7 +724,14 @@ function transformClass( locals = [newFieldInitId, getId, setId]; } else { - addProxyAccessorsFor(newPath, key, newId, isComputed); + addProxyAccessorsFor( + path.node.id, + newPath, + key, + newId, + version, + isComputed, + ); locals = newFieldInitId; } } else if (kind === FIELD) { @@ -759,6 +821,7 @@ function transformClass( elementDecoratorInfo.push({ kind, decorators: decorators.map(d => d.node.expression), + decoratorsThis: decorators.map(d => decoratorsThis.get(d.node)), name: nameExpr, isStatic, privateMethods, @@ -790,10 +853,21 @@ function transformClass( } } - const elementDecorations = generateDecorationExprs(elementDecoratorInfo); - const classDecorations = t.arrayExpression( - (classDecorators || []).map(d => d.expression), + const elementDecorations = generateDecorationExprs( + elementDecoratorInfo, + version, ); + let classDecorationsFlag = 0; + let classDecorations: t.Expression[] = []; + if (classDecorators) { + const { hasThis, decs } = generateDecorationList( + classDecorators.map(el => el.expression), + classDecorators.map(dec => decoratorsThis.get(dec)), + version, + ); + classDecorationsFlag = hasThis ? 1 : 0; + classDecorations = decs; + } const elementLocals: t.Identifier[] = extractElementLocalAssignments(elementDecoratorInfo); @@ -1003,7 +1077,8 @@ function transformClass( elementLocals, classLocals, elementDecorations, - classDecorations, + t.arrayExpression(classDecorations), + t.numericLiteral(classDecorationsFlag), needsInstancePrivateBrandCheck ? lastInstancePrivateName : null, state, version, @@ -1034,6 +1109,7 @@ function createLocalsAssignment( classLocals: t.Identifier[], elementDecorations: t.ArrayExpression, classDecorations: t.ArrayExpression, + classDecorationsFlag: t.NumericLiteral, maybePrivateBranName: t.PrivateName | null, state: PluginPass, version: DecoratorVersionKind, @@ -1056,7 +1132,19 @@ function createLocalsAssignment( ); } else { // TODO(Babel 8): Only keep the if branch - if (version === "2023-01") { + if (version === "2023-05") { + if (maybePrivateBranName || classDecorationsFlag.value !== 0) { + args.push(classDecorationsFlag); + } + if (maybePrivateBranName) { + args.push( + template.expression.ast` + _ => ${t.cloneNode(maybePrivateBranName)} in _ + ` as t.ArrowFunctionExpression, + ); + } + rhs = t.callExpression(state.addHelper("applyDecs2305"), args); + } else if (version === "2023-01") { if (maybePrivateBranName) { args.push( template.expression.ast` @@ -1092,9 +1180,9 @@ function createLocalsAssignment( export default function ( { assertVersion, assumption }: PluginAPI, { loose }: Options, - version: "2023-01" | "2022-03" | "2021-12", + version: "2023-05" | "2023-01" | "2022-03" | "2021-12", ): PluginObject { - if (version === "2023-01") { + if (version === "2023-05" || version === "2023-01") { assertVersion("^7.21.0"); } else if (version === "2021-12") { assertVersion("^7.16.0"); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/exec.js new file mode 100644 index 000000000000..3b8e879c54b8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/exec.js @@ -0,0 +1,56 @@ +function dec({ get, set }, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + init(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; +const bContext = foo['#bContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(false); + +expect(aContext.access.get(foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +aContext.access.set(foo, 123); +expect(aContext.access.get(foo)).toBe(125); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.access.get(foo)).toBe(124); +bContext.access.set(foo, 123); +expect(bContext.access.get(foo)).toBe(125); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(true); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/input.js new file mode 100644 index 000000000000..aa152db91d94 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/output.js new file mode 100644 index 000000000000..e2db1b0bc267 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/private/output.js @@ -0,0 +1,39 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto; +const dec = () => {}; +var _A = /*#__PURE__*/new WeakMap(); +var _a = /*#__PURE__*/new WeakMap(); +var _B = /*#__PURE__*/new WeakMap(); +var _b = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b2, + set: _set_b2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a2, + set: _set_a2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: (_initProto(this), _init_a(this)) + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: _init_b(this, 123) + }); + } +} +function _set_a2(v) { + _set_a(this, v); +} +function _get_a2() { + return _get_a(this); +} +function _set_b2(v) { + _set_b(this, v); +} +function _get_b2() { + return _get_b(this); +} +[_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 1, "a", o => babelHelpers.classPrivateFieldGet(o, _A), (o, v) => babelHelpers.classPrivateFieldSet(o, _A, v)], [dec, 1, "b", o => babelHelpers.classPrivateFieldGet(o, _B), (o, v) => babelHelpers.classPrivateFieldSet(o, _B, v)]], [], 0, _ => _a.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/exec.js new file mode 100644 index 000000000000..834c6deaecfe --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/exec.js @@ -0,0 +1,80 @@ +function dec({ get, set }, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + init(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; +const cContext = foo['cContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(foo.a).toBe(2); +expect(aContext.access.get(foo)).toBe(2); +foo.a = 123; +expect(foo.a).toBe(125); +expect(aContext.access.get(foo)).toBe(125); +aContext.access.set(foo, 456); +expect(foo.a).toBe(458); +expect(aContext.access.get(foo)).toBe(458); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(foo.hasOwnProperty('a')).toBe(false); +expect(Foo.prototype.hasOwnProperty('a')).toBe(true); + +expect(foo.b).toBe(124); +foo.b = 123; +expect(foo.b).toBe(125); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(foo.hasOwnProperty('b')).toBe(false); +expect(Foo.prototype.hasOwnProperty('b')).toBe(true); + +expect(foo.c).toBe(457); +foo.c = 456; +expect(foo.c).toBe(458); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('accessor'); +expect(cContext.static).toBe(false); +expect(cContext.private).toBe(false); +expect(typeof cContext.addInitializer).toBe('function'); +expect(foo.hasOwnProperty('c')).toBe(false); +expect(Foo.prototype.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/input.js new file mode 100644 index 000000000000..bd8be5cbe225 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/output.js new file mode 100644 index 000000000000..2ed94c89dfff --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/public/output.js @@ -0,0 +1,41 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _initProto; +const dec = () => {}; +_computedKey = 'c'; +var _A = /*#__PURE__*/new WeakMap(); +var _B = /*#__PURE__*/new WeakMap(); +var _C = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: (_initProto(this), _init_a(this)) + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: _init_b(this, 123) + }); + babelHelpers.classPrivateFieldInitSpec(this, _C, { + writable: true, + value: _init_computedKey(this, 456) + }); + } + get a() { + return babelHelpers.classPrivateFieldGet(this, _A); + } + set a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); + } + get b() { + return babelHelpers.classPrivateFieldGet(this, _B); + } + set b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); + } + get [_computedKey]() { + return babelHelpers.classPrivateFieldGet(this, _C); + } + set [_computedKey](v) { + babelHelpers.classPrivateFieldSet(this, _C, v); + } +} +[_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 1, "a"], [dec, 1, "b"], [dec, 1, _computedKey]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..14008e61c117 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/exec.js @@ -0,0 +1,55 @@ +function dec({ get, set }, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + init(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} + +const aContext = Foo['#aContext']; +const bContext = Foo['#bContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(false); + +expect(aContext.access.get(Foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +aContext.access.set(Foo, 123); +expect(aContext.access.get(Foo)).toBe(125); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(aContext.access.get(Foo)).toBe(125); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.access.get(Foo)).toBe(124); +bContext.access.set(Foo, 123); +expect(bContext.access.get(Foo)).toBe(125); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(true); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/input.js new file mode 100644 index 000000000000..c30e61d463bc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/output.js new file mode 100644 index 000000000000..8d398c811a40 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-private/output.js @@ -0,0 +1,40 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +var _b = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b2, + set: _set_b2 + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a2, + set: _set_a2 + }); + } +} +function _set_a2(v) { + _set_a(this, v); +} +function _get_a2() { + return _get_a(this); +} +function _set_b2(v) { + _set_b(this, v); +} +function _get_b2() { + return _get_b(this); +} +(() => { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 9, "a", o => babelHelpers.classStaticPrivateFieldSpecGet(o, Foo, _A), (o, v) => babelHelpers.classStaticPrivateFieldSpecSet(o, Foo, _A, v)], [dec, 9, "b", o => babelHelpers.classStaticPrivateFieldSpecGet(o, Foo, _B), (o, v) => babelHelpers.classStaticPrivateFieldSpecSet(o, Foo, _B, v)]], []).e; + _initStatic(Foo); +})(); +var _A = { + writable: true, + value: _init_a(Foo) +}; +var _B = { + writable: true, + value: _init_b(Foo, 123) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public-this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public-this/exec.js new file mode 100644 index 000000000000..e981fac69393 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public-this/exec.js @@ -0,0 +1,12 @@ +// https://github.com/tc39/proposal-decorators/issues/468 + +class A { + static accessor x; + + @(() => {}) static accessor y; +} + +class B extends A {} + +expect(() => B.x).not.toThrow(); +expect(() => B.y).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..402618d18490 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/exec.js @@ -0,0 +1,75 @@ +function dec({ get, set }, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return { + get() { + return get.call(this) + 1; + }, + + set(v) { + set.call(this, v + 1); + }, + + init(v) { + return v ? v : 1; + } + } +} + +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; +const cContext = Foo['cContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(Foo.a).toBe(2); +expect(aContext.access.get(Foo)).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(125); +expect(aContext.access.get(Foo)).toBe(125); +aContext.access.set(Foo, 456); +expect(Foo.a).toBe(458); +expect(aContext.access.get(Foo)).toBe(458); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('accessor'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(124); +Foo.b = 123; +expect(Foo.b).toBe(125); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('accessor'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(457); +Foo.c = 456; +expect(Foo.c).toBe(458); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('accessor'); +expect(cContext.static).toBe(true); +expect(cContext.private).toBe(false); +expect(typeof cContext.addInitializer).toBe('function'); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/input.js new file mode 100644 index 000000000000..50f05c065848 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/output.js new file mode 100644 index 000000000000..f124a7035620 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/static-public/output.js @@ -0,0 +1,39 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + static get a() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); + } + static set a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); + } + static get b() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); + } + static set b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); + } + static get [_computedKey]() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _C); + } + static set [_computedKey](v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _C, v); + } +} +(() => { + [_init_a, _init_b, _init_computedKey, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 9, "a"], [dec, 9, "b"], [dec, 9, _computedKey]], []).e; + _initStatic(Foo); +})(); +var _A = { + writable: true, + value: _init_a(Foo) +}; +var _B = { + writable: true, + value: _init_b(Foo, 123) +}; +var _C = { + writable: true, + value: _init_computedKey(Foo, 456) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/exec.js new file mode 100644 index 000000000000..51715be9f173 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/exec.js @@ -0,0 +1,31 @@ +class Foo { + accessor #a; + + accessor #b = 123; + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } + + getB() { + return this.#b; + } + + setB(v) { + this.#b = v; + } +} + +let foo = new Foo(); + +expect(foo.getA()).toBe(undefined); +foo.setA(123) +expect(foo.getA()).toBe(123); + +expect(foo.getB()).toBe(123); +foo.setB(456) +expect(foo.getB()).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/input.js new file mode 100644 index 000000000000..07804cb8a125 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/input.js @@ -0,0 +1,6 @@ +const dec = () => {}; +class Foo { + accessor #a; + + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/output.js new file mode 100644 index 000000000000..84ce390b7799 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-private/output.js @@ -0,0 +1,37 @@ +const dec = () => {}; +var _A = /*#__PURE__*/new WeakMap(); +var _a = /*#__PURE__*/new WeakMap(); +var _B = /*#__PURE__*/new WeakMap(); +var _b = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _b, { + get: _get_b, + set: _set_b + }); + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: _set_a + }); + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: void 0 + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: 123 + }); + } +} +function _get_a() { + return babelHelpers.classPrivateFieldGet(this, _A); +} +function _set_a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); +} +function _get_b() { + return babelHelpers.classPrivateFieldGet(this, _B); +} +function _set_b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/exec.js new file mode 100644 index 000000000000..5ccd5dcce44d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/exec.js @@ -0,0 +1,27 @@ +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} + +let foo = new Foo(); + +expect(foo.a).toBe(undefined); +foo.a = 123; +expect(foo.a).toBe(123); +expect(foo.hasOwnProperty('a')).toBe(false); +expect(Foo.prototype.hasOwnProperty('a')).toBe(true); + +expect(foo.b).toBe(123); +foo.b = 456 +expect(foo.b).toBe(456); +expect(foo.hasOwnProperty('b')).toBe(false); +expect(Foo.prototype.hasOwnProperty('b')).toBe(true); + +expect(foo.c).toBe(456); +foo.c = 789 +expect(foo.c).toBe(789); +expect(foo.hasOwnProperty('c')).toBe(false); +expect(Foo.prototype.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/input.js new file mode 100644 index 000000000000..62dbd0c0381a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/output.js new file mode 100644 index 000000000000..070b4006e2e9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-public/output.js @@ -0,0 +1,38 @@ +const dec = () => {}; +var _A = /*#__PURE__*/new WeakMap(); +var _B = /*#__PURE__*/new WeakMap(); +var _C = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _A, { + writable: true, + value: void 0 + }); + babelHelpers.classPrivateFieldInitSpec(this, _B, { + writable: true, + value: 123 + }); + babelHelpers.classPrivateFieldInitSpec(this, _C, { + writable: true, + value: 456 + }); + } + get a() { + return babelHelpers.classPrivateFieldGet(this, _A); + } + set a(v) { + babelHelpers.classPrivateFieldSet(this, _A, v); + } + get b() { + return babelHelpers.classPrivateFieldGet(this, _B); + } + set b(v) { + babelHelpers.classPrivateFieldSet(this, _B, v); + } + get ['c']() { + return babelHelpers.classPrivateFieldGet(this, _C); + } + set ['c'](v) { + babelHelpers.classPrivateFieldSet(this, _C, v); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/exec.js new file mode 100644 index 000000000000..21defb3f24d8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/exec.js @@ -0,0 +1,29 @@ +class Foo { + static accessor #a; + + static accessor #b = 123; + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } + + static getB() { + return this.#b; + } + + static setB(v) { + this.#b = v; + } +} + +expect(Foo.getA()).toBe(undefined); +Foo.setA(123) +expect(Foo.getA()).toBe(123); + +expect(Foo.getB()).toBe(123); +Foo.setB(456) +expect(Foo.getB()).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/input.js new file mode 100644 index 000000000000..9e893b86b863 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/input.js @@ -0,0 +1,6 @@ +const dec = () => {}; +class Foo { + static accessor #a; + + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/output.js new file mode 100644 index 000000000000..a4d71f4cc5a1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-private/output.js @@ -0,0 +1,30 @@ +const dec = () => {}; +class Foo {} +function _get_a() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); +} +function _set_a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); +} +function _get_b() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); +} +function _set_b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); +} +var _b = { + get: _get_b, + set: _set_b +}; +var _a = { + get: _get_a, + set: _set_a +}; +var _A = { + writable: true, + value: void 0 +}; +var _B = { + writable: true, + value: 123 +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/exec.js new file mode 100644 index 000000000000..d22a4e710dd8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/exec.js @@ -0,0 +1,22 @@ +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} + +expect(Foo.a).toBe(undefined); +Foo.a = 123; +expect(Foo.a).toBe(123); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(123); +Foo.b = 456 +expect(Foo.b).toBe(456); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(456); +Foo.c = 789 +expect(Foo.c).toBe(789); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/input.js new file mode 100644 index 000000000000..eb7463cb9fc1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/output.js new file mode 100644 index 000000000000..ea1636ad8321 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors--to-es2015/undecorated-static-public/output.js @@ -0,0 +1,33 @@ +const dec = () => {}; +class Foo { + static get a() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _A); + } + static set a(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _A, v); + } + static get b() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _B); + } + static set b(v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _B, v); + } + static get ['c']() { + return babelHelpers.classStaticPrivateFieldSpecGet(Foo, Foo, _C); + } + static set ['c'](v) { + babelHelpers.classStaticPrivateFieldSpecSet(Foo, Foo, _C, v); + } +} +var _A = { + writable: true, + value: void 0 +}; +var _B = { + writable: true, + value: 123 +}; +var _C = { + writable: true, + value: 456 +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/input.js new file mode 100644 index 000000000000..aa152db91d94 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + accessor #a; + + @dec + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/output.js new file mode 100644 index 000000000000..158762fa3071 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/private/output.js @@ -0,0 +1,21 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto; +const dec = () => {}; +class Foo { + static { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 1, "a", o => o.#A, (o, v) => o.#A = v], [dec, 1, "b", o => o.#B, (o, v) => o.#B = v]], [], 0, _ => #a in _).e; + } + #A = (_initProto(this), _init_a(this)); + set #a(v) { + _set_a(this, v); + } + get #a() { + return _get_a(this); + } + #B = _init_b(this, 123); + set #b(v) { + _set_b(this, v); + } + get #b() { + return _get_b(this); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/input.js new file mode 100644 index 000000000000..bd8be5cbe225 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + accessor a; + + @dec + accessor b = 123; + + @dec + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/output.js new file mode 100644 index 000000000000..d1cc1e1f991e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/public/output.js @@ -0,0 +1,29 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _initProto; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + static { + [_init_a, _init_b, _init_computedKey, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 1, "a"], [dec, 1, "b"], [dec, 1, _computedKey]], []).e; + } + #A = (_initProto(this), _init_a(this)); + get a() { + return this.#A; + } + set a(v) { + this.#A = v; + } + #B = _init_b(this, 123); + get b() { + return this.#B; + } + set b(v) { + this.#B = v; + } + #C = _init_computedKey(this, 456); + get [_computedKey]() { + return this.#C; + } + set [_computedKey](v) { + this.#C = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/input.js new file mode 100644 index 000000000000..c30e61d463bc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + static accessor #a; + + @dec + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/output.js new file mode 100644 index 000000000000..8857eade379f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-private/output.js @@ -0,0 +1,22 @@ +var _init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic; +const dec = () => {}; +class Foo { + static { + [_init_a, _get_a, _set_a, _init_b, _get_b, _set_b, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 9, "a", o => o.#A, (o, v) => o.#A = v], [dec, 9, "b", o => o.#B, (o, v) => o.#B = v]], []).e; + _initStatic(this); + } + static #A = _init_a(this); + set #a(v) { + _set_a(this, v); + } + get #a() { + return _get_a(this); + } + static #B = _init_b(this, 123); + set #b(v) { + _set_b(this, v); + } + get #b() { + return _get_b(this); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/exec.js new file mode 100644 index 000000000000..e981fac69393 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/exec.js @@ -0,0 +1,12 @@ +// https://github.com/tc39/proposal-decorators/issues/468 + +class A { + static accessor x; + + @(() => {}) static accessor y; +} + +class B extends A {} + +expect(() => B.x).not.toThrow(); +expect(() => B.y).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public-inherited/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/input.js new file mode 100644 index 000000000000..50f05c065848 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + static accessor a; + + @dec + static accessor b = 123; + + @dec + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/output.js new file mode 100644 index 000000000000..f05e361a640b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/static-public/output.js @@ -0,0 +1,30 @@ +var _init_a, _init_b, _computedKey, _init_computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + static { + [_init_a, _init_b, _init_computedKey, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 9, "a"], [dec, 9, "b"], [dec, 9, _computedKey]], []).e; + _initStatic(this); + } + static #A = _init_a(this); + static get a() { + return Foo.#A; + } + static set a(v) { + Foo.#A = v; + } + static #B = _init_b(this, 123); + static get b() { + return Foo.#B; + } + static set b(v) { + Foo.#B = v; + } + static #C = _init_computedKey(this, 456); + static get [_computedKey]() { + return Foo.#C; + } + static set [_computedKey](v) { + Foo.#C = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/input.js new file mode 100644 index 000000000000..07804cb8a125 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/input.js @@ -0,0 +1,6 @@ +const dec = () => {}; +class Foo { + accessor #a; + + accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/output.js new file mode 100644 index 000000000000..50efd9ecf14f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-private/output.js @@ -0,0 +1,17 @@ +const dec = () => {}; +class Foo { + #A; + get #a() { + return this.#A; + } + set #a(v) { + this.#A = v; + } + #B = 123; + get #b() { + return this.#B; + } + set #b(v) { + this.#B = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/input.js new file mode 100644 index 000000000000..62dbd0c0381a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + accessor a; + + accessor b = 123; + + accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/output.js new file mode 100644 index 000000000000..0082fd52b442 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-public/output.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + #A; + get a() { + return this.#A; + } + set a(v) { + this.#A = v; + } + #B = 123; + get b() { + return this.#B; + } + set b(v) { + this.#B = v; + } + #C = 456; + get ['c']() { + return this.#C; + } + set ['c'](v) { + this.#C = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/input.js new file mode 100644 index 000000000000..9e893b86b863 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/input.js @@ -0,0 +1,6 @@ +const dec = () => {}; +class Foo { + static accessor #a; + + static accessor #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/output.js new file mode 100644 index 000000000000..7c864b31413f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-private/output.js @@ -0,0 +1,17 @@ +const dec = () => {}; +class Foo { + static #A; + static get #a() { + return Foo.#A; + } + static set #a(v) { + Foo.#A = v; + } + static #B = 123; + static get #b() { + return Foo.#B; + } + static set #b(v) { + Foo.#B = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/input.js new file mode 100644 index 000000000000..eb7463cb9fc1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + static accessor a; + + static accessor b = 123; + + static accessor ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/output.js new file mode 100644 index 000000000000..dc2012f8012f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-accessors/undecorated-static-public/output.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + static #A; + static get a() { + return Foo.#A; + } + static set a(v) { + Foo.#A = v; + } + static #B = 123; + static get b() { + return Foo.#B; + } + static set b(v) { + Foo.#B = v; + } + static #C = 456; + static get ['c']() { + return Foo.#C; + } + static set ['c'](v) { + Foo.#C = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/options.json new file mode 100644 index 000000000000..369462b1233d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]], + "assumptions": { + "constantSuper": true + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/input.js new file mode 100644 index 000000000000..6bc1a7dbbc4b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +@dec +class Foo extends Bar { + constructor() { + let foo = super(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/output.js new file mode 100644 index 000000000000..267564aaae78 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-nested-constructor-expression/output.js @@ -0,0 +1,14 @@ +var _initClass; +const dec = () => {}; +let _Foo; +class Foo extends Bar { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + constructor() { + let foo = super(); + } + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/input.js new file mode 100644 index 000000000000..b32ea0bd378f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +class Foo extends Bar { + @dec + get #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/output.js new file mode 100644 index 000000000000..b69e2ce5e1bb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-accessor/output.js @@ -0,0 +1,16 @@ +var _call_x, _initProto; +const dec = () => {}; +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "x", function () { + return Bar.prototype.foo.call(this); + }]], [], 0, _ => #x in _).e; + } + constructor(...args) { + super(...args); + _initProto(this); + } + get #x() { + return _call_x(this); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/input.js new file mode 100644 index 000000000000..36f9bd37dfa7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +class Foo extends Bar { + @dec + #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/output.js new file mode 100644 index 000000000000..e41db00e1d7b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-assumption-constantSuper/super-in-private-method/output.js @@ -0,0 +1,14 @@ +var _call_x, _initProto; +const dec = () => {}; +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "x", function () { + return Bar.prototype.foo.call(this); + }]], [], 0, _ => #x in _).e; + } + constructor(...args) { + super(...args); + _initProto(this); + } + #x = _call_x; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/exec.js new file mode 100644 index 000000000000..9d5c4bbf3d15 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/exec.js @@ -0,0 +1,14 @@ +var value; +const classDec = (Class) => { + value = (new Class).m; + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m; +} + +expect(value).toBe(42); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/input.js new file mode 100644 index 000000000000..8689cf13f9e5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/input.js @@ -0,0 +1,12 @@ +var value; +const classDec = (Class) => { + value = (new Class).p; + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/output.js new file mode 100644 index 000000000000..fa8ae7a79338 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-fields/output.js @@ -0,0 +1,18 @@ +var _initClass, _init_m; +var value; +const classDec = Class => { + value = new Class().p; + return Class; +}; +const memberDec = () => () => 42; +let _C; +class C { + constructor() { + babelHelpers.defineProperty(this, "m", _init_m(this)); + } +} +({ + e: [_init_m], + c: [_C, _initClass] +} = babelHelpers.applyDecs2305(C, [[memberDec, 0, "m"]], [classDec])); +_initClass(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/exec.js new file mode 100644 index 000000000000..a85569beafbc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/exec.js @@ -0,0 +1,14 @@ +var value; +const classDec = (Class) => { + value = (new Class).m(); + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m() {}; +} + +expect(value).toBe(42); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/input.js new file mode 100644 index 000000000000..7d8094583001 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/input.js @@ -0,0 +1,12 @@ +var value; +const classDec = (Class) => { + value = (new Class).m(); + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m() {}; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/output.js new file mode 100644 index 000000000000..87662878affd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/decorator-access-modified-methods/output.js @@ -0,0 +1,19 @@ +var _initClass, _initProto; +var value; +const classDec = Class => { + value = new Class().m(); + return Class; +}; +const memberDec = () => () => 42; +let _C; +class C { + constructor(...args) { + _initProto(this); + } + m() {} +} +({ + e: [_initProto], + c: [_C, _initClass] +} = babelHelpers.applyDecs2305(C, [[memberDec, 2, "m"]], [classDec])); +_initClass(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/input.js new file mode 100644 index 000000000000..853ac0e6a43b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +const A = @dec class A { static {} } +const B = @dec class C { static {} } +const D = @dec class { static {} } +const E = (@dec class { static {} }, 123); +const F = [@dec class G { static {} }, @dec class { static {} }]; +const H = @dec class extends I { static {} }; +const J = @dec class K extends L { static {} }; + +function classFactory() { + return @dec class { static {} } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/output.js new file mode 100644 index 000000000000..f4b047570ac4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions-static-blocks/output.js @@ -0,0 +1,67 @@ +var _initClass, _A, _temp, _initClass2, _C, _temp2, _initClass3, _D, _temp3, _initClass4, _decorated_class, _temp4, _class2, _initClass5, _G, _temp5, _initClass6, _decorated_class2, _temp6, _class3, _initClass7, _H, _temp7, _initClass8, _K, _temp8; +const dec = () => {}; +const A = (new (_temp = class extends babelHelpers.identity { + constructor() { + super(_A), (() => {})(), _initClass(); + } +}, (() => { + class A {} + [_A, _initClass] = babelHelpers.applyDecs2305(A, [], [dec]).c; +})(), _temp)(), _A); +const B = (new (_temp2 = class extends babelHelpers.identity { + constructor() { + super(_C), (() => {})(), _initClass2(); + } +}, (() => { + class C {} + [_C, _initClass2] = babelHelpers.applyDecs2305(C, [], [dec]).c; +})(), _temp2)(), _C); +const D = (new (_temp3 = class extends babelHelpers.identity { + constructor() { + super(_D), (() => {})(), _initClass3(); + } +}, (() => { + class D {} + [_D, _initClass3] = babelHelpers.applyDecs2305(D, [], [dec]).c; +})(), _temp3)(), _D); +const E = ((new (_temp4 = class extends babelHelpers.identity { + constructor() { + super(_decorated_class), (() => {})(), _initClass4(); + } +}, (_class2 = class {}, [_decorated_class, _initClass4] = babelHelpers.applyDecs2305(_class2, [], [dec]).c), _temp4)(), _decorated_class), 123); +const F = [(new (_temp5 = class extends babelHelpers.identity { + constructor() { + super(_G), (() => {})(), _initClass5(); + } +}, (() => { + class G {} + [_G, _initClass5] = babelHelpers.applyDecs2305(G, [], [dec]).c; +})(), _temp5)(), _G), (new (_temp6 = class extends babelHelpers.identity { + constructor() { + super(_decorated_class2), (() => {})(), _initClass6(); + } +}, (_class3 = class {}, [_decorated_class2, _initClass6] = babelHelpers.applyDecs2305(_class3, [], [dec]).c), _temp6)(), _decorated_class2)]; +const H = (new (_temp7 = class extends babelHelpers.identity { + constructor() { + super(_H), (() => {})(), _initClass7(); + } +}, (() => { + class H extends I {} + [_H, _initClass7] = babelHelpers.applyDecs2305(H, [], [dec]).c; +})(), _temp7)(), _H); +const J = (new (_temp8 = class extends babelHelpers.identity { + constructor() { + super(_K), (() => {})(), _initClass8(); + } +}, (() => { + class K extends L {} + [_K, _initClass8] = babelHelpers.applyDecs2305(K, [], [dec]).c; +})(), _temp8)(), _K); +function classFactory() { + var _initClass9, _decorated_class3, _temp9, _class5; + return new (_temp9 = class extends babelHelpers.identity { + constructor() { + super(_decorated_class3), (() => {})(), _initClass9(); + } + }, (_class5 = class {}, [_decorated_class3, _initClass9] = babelHelpers.applyDecs2305(_class5, [], [dec]).c), _temp9)(), _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/input.js new file mode 100644 index 000000000000..77060bea50ff --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +const A = @dec class A {} +const B = @dec class C {} +const D = @dec class {} +const E = (@dec class {}, 123); +const F = [@dec class G {}, @dec class {}]; +const H = @dec class extends I {}; +const J = @dec class K extends L {}; + +function classFactory() { + return @dec class {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/output.js new file mode 100644 index 000000000000..39492606f0e3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/expressions/output.js @@ -0,0 +1,13 @@ +var _initClass, _A, _class, _initClass2, _C, _class2, _initClass3, _D, _class3, _initClass4, _decorated_class, _class4, _initClass5, _G, _class5, _initClass6, _decorated_class2, _class6, _initClass7, _H, _class7, _initClass8, _K, _class8; +const dec = () => {}; +const A = ((_class = class A {}, [_A, _initClass] = babelHelpers.applyDecs2305(_class, [], [dec]).c, _initClass()), _A); +const B = ((_class2 = class C {}, [_C, _initClass2] = babelHelpers.applyDecs2305(_class2, [], [dec]).c, _initClass2()), _C); +const D = ((_class3 = class D {}, [_D, _initClass3] = babelHelpers.applyDecs2305(_class3, [], [dec]).c, _initClass3()), _D); +const E = (((_class4 = class {}, [_decorated_class, _initClass4] = babelHelpers.applyDecs2305(_class4, [], [dec]).c, _initClass4()), _decorated_class), 123); +const F = [((_class5 = class G {}, [_G, _initClass5] = babelHelpers.applyDecs2305(_class5, [], [dec]).c, _initClass5()), _G), ((_class6 = class {}, [_decorated_class2, _initClass6] = babelHelpers.applyDecs2305(_class6, [], [dec]).c, _initClass6()), _decorated_class2)]; +const H = ((_class7 = class H extends I {}, [_H, _initClass7] = babelHelpers.applyDecs2305(_class7, [], [dec]).c, _initClass7()), _H); +const J = ((_class8 = class K extends L {}, [_K, _initClass8] = babelHelpers.applyDecs2305(_class8, [], [dec]).c, _initClass8()), _K); +function classFactory() { + var _initClass9, _decorated_class3, _class9; + return (_class9 = class {}, [_decorated_class3, _initClass9] = babelHelpers.applyDecs2305(_class9, [], [dec]).c, _initClass9()), _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/exec.js new file mode 100644 index 000000000000..889e8c102cf4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/exec.js @@ -0,0 +1,18 @@ +let count = 0; + +function dec1(Klass) { + expect(++count).toBe(1); + expect(Klass.name).toBe('Bar'); +} + +@dec1 +class Bar {} + +function dec2(Klass) { + expect(++count).toBe(2); + expect(Klass.name).toBe('Foo'); + expect(Object.getPrototypeOf(Klass)).toBe(Bar); +} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/input.js new file mode 100644 index 000000000000..96a13523506c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/input.js @@ -0,0 +1,7 @@ +const dec1 = () => {}; +const dec2 = () => {}; +@dec1 +class Bar {} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/output.js new file mode 100644 index 000000000000..209979bb08ca --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/inheritance/output.js @@ -0,0 +1,11 @@ +var _initClass, _initClass2; +const dec1 = () => {}; +const dec2 = () => {}; +let _Bar; +class Bar {} +[_Bar, _initClass] = babelHelpers.applyDecs2305(Bar, [], [dec1]).c; +_initClass(); +let _Foo; +class Foo extends _Bar {} +[_Foo, _initClass2] = babelHelpers.applyDecs2305(Foo, [], [dec2]).c; +_initClass2(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/exec.js new file mode 100644 index 000000000000..aefc3ba5b88c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/exec.js @@ -0,0 +1,41 @@ +function dec1(Foo, { addInitializer }) { + expect(Foo.field).toBe(undefined); + addInitializer(() => { + Foo.initField = 123; + }); +} + +@dec1 +class Foo { + static { + expect(this.initField).toBe(undefined); + } + + static field = 123; +} + +expect(Foo.initField).toBe(123); +expect(Foo.field).toBe(123); + +function dec2(Bar, { addInitializer }) { + expect(Bar.field).toBe(123); + expect(Bar.otherField).toBe(undefined); + expect(Bar.initField).toBe(123); + addInitializer(() => { + Bar.initField = 456; + }); +} + +@dec2 +class Bar extends Foo { + static { + expect(this.initField).toBe(123); + this.otherField = 456; + } + + static field = 456; +} + +expect(Bar.initField).toBe(456); +expect(Bar.field).toBe(456); +expect(Bar.otherField).toBe(456); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/input.js new file mode 100644 index 000000000000..87c785d39a93 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +@dec +class Foo { + static field = 123; +} + +@dec +class Bar extends Foo { + static { + this.otherField = 456; + } + + static field = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/output.js new file mode 100644 index 000000000000..1e98b9b425e5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/initializers/output.js @@ -0,0 +1,22 @@ +var _initClass, _temp, _initClass2, _temp2; +const dec = () => {}; +let _Foo; +new (_temp = class extends babelHelpers.identity { + constructor() { + (super(_Foo), babelHelpers.defineProperty(this, "field", 123)), _initClass(); + } +}, (() => { + class Foo {} + [_Foo, _initClass] = babelHelpers.applyDecs2305(Foo, [], [dec]).c; +})(), _temp)(); +let _Bar; +new (_temp2 = class extends babelHelpers.identity { + constructor() { + (super(_Bar), babelHelpers.defineProperty(this, "field", ((() => { + this.otherField = 456; + })(), 123))), _initClass2(); + } +}, (() => { + class Bar extends _Foo {} + [_Bar, _initClass2] = babelHelpers.applyDecs2305(Bar, [], [dec]).c; +})(), _temp2)(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js new file mode 100644 index 000000000000..2f4e342e3eee --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/exec.js @@ -0,0 +1,33 @@ +let hasX, hasM, OriginalFoo; + +class Bar {} + +function dec(Foo) { + OriginalFoo = Foo; + return Bar; +} + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} + +expect(hasX(Bar)).toBe(true); +expect(hasM(Bar)).toBe(true); +expect(hasX(OriginalFoo)).toBe(false); +expect(hasM(OriginalFoo)).toBe(false); + +expect(Bar.hasOwnProperty("x")).toBe(true); +expect(OriginalFoo.hasOwnProperty("x")).toBe(false); + +expect(Bar.hasOwnProperty("m")).toBe(false); +expect(OriginalFoo.hasOwnProperty("m")).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/input.js new file mode 100644 index 000000000000..e8c62db59046 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/input.js @@ -0,0 +1,16 @@ +const dec = () => {}; +let hasX, hasM; + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/output.js new file mode 100644 index 000000000000..e6413e032983 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-installed-on-correct-class/output.js @@ -0,0 +1,21 @@ +var _initClass, _x, _m, _temp; +const dec = () => {}; +let hasX, hasM; +let _Foo; +new (_x = /*#__PURE__*/new WeakMap(), _m = /*#__PURE__*/new WeakSet(), (_temp = class extends babelHelpers.identity { + constructor() { + (super(_Foo), babelHelpers.classPrivateMethodInitSpec(this, _m), babelHelpers.classPrivateFieldInitSpec(this, _x, { + writable: true, + value: void 0 + }), babelHelpers.defineProperty(this, "x", void 0)), (() => { + hasX = o => _x.has(babelHelpers.checkInRHS(o)); + hasM = o => _m.has(babelHelpers.checkInRHS(o)); + })(), _initClass(); + } +}, (() => { + class Foo { + static m() {} + } + [_Foo, _initClass] = babelHelpers.applyDecs2305(Foo, [], [dec]).c; +})(), _temp))(); +function _m2() {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/exec.js new file mode 100644 index 000000000000..6b55df43edc8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/exec.js @@ -0,0 +1,19 @@ + +class Bar {} + +let _this, _this2, _this3; + +@(() => Bar) +class Foo { + static { + _this = this; + } + static field = (_this2 = this); + static { + _this3 = this; + } +} + +expect(_this).toBe(Bar); +expect(_this2).toBe(Bar); +expect(_this3).toBe(Bar); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/input.js new file mode 100644 index 000000000000..1b8d46cf3ac0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +@dec +class Foo { + static { + this + } + static field = this; + static { + this + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/output.js new file mode 100644 index 000000000000..4c7f4fae492d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-static-this/output.js @@ -0,0 +1,15 @@ +var _initClass, _temp; +const dec = () => {}; +let _Foo; +new (_temp = class extends babelHelpers.identity { + constructor() { + (super(_Foo), babelHelpers.defineProperty(this, "field", ((() => { + this; + })(), this))), (() => { + this; + })(), _initClass(); + } +}, (() => { + class Foo {} + [_Foo, _initClass] = babelHelpers.applyDecs2305(Foo, [], [dec]).c; +})(), _temp)(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/exec.js new file mode 100644 index 000000000000..739e33a05be5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/exec.js @@ -0,0 +1,17 @@ +let replaced; + +function dec(Klass) { + replaced = class extends Klass {}; + + return replaced; +} + +const Foo = @dec class Bar { + static bar = new Bar(); +}; + +const foo = new Foo(); + +expect(Foo).toBe(replaced); +expect(Foo.bar).toBeInstanceOf(replaced); +expect(foo).toBeInstanceOf(replaced); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/input.js new file mode 100644 index 000000000000..0cd5e45110b7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +const Foo = @dec class Bar { + bar = new Bar(); +}; + +const foo = new Foo(); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/output.js new file mode 100644 index 000000000000..6f91424929d4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement-with-expr/output.js @@ -0,0 +1,8 @@ +var _initClass, _Bar, _class; +const dec = () => {}; +const Foo = ((_class = class Bar { + constructor() { + babelHelpers.defineProperty(this, "bar", new _Bar()); + } +}, [_Bar, _initClass] = babelHelpers.applyDecs2305(_class, [], [dec]).c, _initClass()), _Bar); +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/exec.js new file mode 100644 index 000000000000..c8dc49c0d85e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/exec.js @@ -0,0 +1,19 @@ + +let replaced; + +function dec(Klass) { + replaced = class extends Klass {}; + + return replaced; +} + +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); + +expect(Foo).toBe(replaced); +expect(Foo.foo).toBeInstanceOf(replaced); +expect(foo).toBeInstanceOf(replaced); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/input.js new file mode 100644 index 000000000000..8f0a4dfe092f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/output.js new file mode 100644 index 000000000000..636e8ad68440 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes--to-es2015/replacement/output.js @@ -0,0 +1,12 @@ +var _initClass, _temp; +const dec = () => {}; +let _Foo; +new (_temp = class extends babelHelpers.identity { + constructor() { + (super(_Foo), babelHelpers.defineProperty(this, "foo", new _Foo())), _initClass(); + } +}, (() => { + class Foo {} + [_Foo, _initClass] = babelHelpers.applyDecs2305(Foo, [], [dec]).c; +})(), _temp)(); +const foo = new _Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/exec.js new file mode 100644 index 000000000000..9d5c4bbf3d15 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/exec.js @@ -0,0 +1,14 @@ +var value; +const classDec = (Class) => { + value = (new Class).m; + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m; +} + +expect(value).toBe(42); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/input.js new file mode 100644 index 000000000000..8689cf13f9e5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/input.js @@ -0,0 +1,12 @@ +var value; +const classDec = (Class) => { + value = (new Class).p; + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/output.js new file mode 100644 index 000000000000..f280a7062001 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-fields/output.js @@ -0,0 +1,20 @@ +var _initClass, _init_m; +var value; +const classDec = Class => { + value = new Class().p; + return Class; +}; +const memberDec = () => () => 42; +let _C; +class C { + static { + ({ + e: [_init_m], + c: [_C, _initClass] + } = babelHelpers.applyDecs2305(this, [[memberDec, 0, "m"]], [classDec])); + } + m = _init_m(this); + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/exec.js new file mode 100644 index 000000000000..a85569beafbc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/exec.js @@ -0,0 +1,14 @@ +var value; +const classDec = (Class) => { + value = (new Class).m(); + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m() {}; +} + +expect(value).toBe(42); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/input.js new file mode 100644 index 000000000000..7d8094583001 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/input.js @@ -0,0 +1,12 @@ +var value; +const classDec = (Class) => { + value = (new Class).m(); + return Class +}; + +const memberDec = () => () => 42; + +@classDec +class C { + @memberDec m() {}; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/output.js new file mode 100644 index 000000000000..79e6ff7facb3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/decorator-access-modified-methods/output.js @@ -0,0 +1,23 @@ +var _initClass, _initProto; +var value; +const classDec = Class => { + value = new Class().m(); + return Class; +}; +const memberDec = () => () => 42; +let _C; +class C { + static { + ({ + e: [_initProto], + c: [_C, _initClass] + } = babelHelpers.applyDecs2305(this, [[memberDec, 2, "m"]], [classDec])); + } + constructor(...args) { + _initProto(this); + } + m() {} + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/input.js new file mode 100644 index 000000000000..853ac0e6a43b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +const A = @dec class A { static {} } +const B = @dec class C { static {} } +const D = @dec class { static {} } +const E = (@dec class { static {} }, 123); +const F = [@dec class G { static {} }, @dec class { static {} }]; +const H = @dec class extends I { static {} }; +const J = @dec class K extends L { static {} }; + +function classFactory() { + return @dec class { static {} } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/output.js new file mode 100644 index 000000000000..ec19f58026e5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions-static-blocks/output.js @@ -0,0 +1,112 @@ +var _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K; +const dec = () => {}; +const A = (new class extends babelHelpers.identity { + static { + class A { + static { + [_A, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_A), (() => {})(), _initClass(); + } +}(), _A); +const B = (new class extends babelHelpers.identity { + static { + class C { + static { + [_C, _initClass2] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_C), (() => {})(), _initClass2(); + } +}(), _C); +const D = (new class extends babelHelpers.identity { + static { + class D { + static { + [_D, _initClass3] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_D), (() => {})(), _initClass3(); + } +}(), _D); +const E = ((new class extends babelHelpers.identity { + static { + (class { + static { + [_decorated_class, _initClass4] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + }); + } + constructor() { + super(_decorated_class), (() => {})(), _initClass4(); + } +}(), _decorated_class), 123); +const F = [(new class extends babelHelpers.identity { + static { + class G { + static { + [_G, _initClass5] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_G), (() => {})(), _initClass5(); + } +}(), _G), (new class extends babelHelpers.identity { + static { + (class { + static { + [_decorated_class2, _initClass6] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + }); + } + constructor() { + super(_decorated_class2), (() => {})(), _initClass6(); + } +}(), _decorated_class2)]; +const H = (new class extends babelHelpers.identity { + static { + class H extends I { + static { + [_H, _initClass7] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_H), (() => {})(), _initClass7(); + } +}(), _H); +const J = (new class extends babelHelpers.identity { + static { + class K extends L { + static { + [_K, _initClass8] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + constructor() { + super(_K), (() => {})(), _initClass8(); + } +}(), _K); +function classFactory() { + var _initClass9, _decorated_class3; + return new class extends babelHelpers.identity { + static { + (class { + static { + [_decorated_class3, _initClass9] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + }); + } + constructor() { + super(_decorated_class3), (() => {})(), _initClass9(); + } + }(), _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/input.js new file mode 100644 index 000000000000..77060bea50ff --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +const A = @dec class A {} +const B = @dec class C {} +const D = @dec class {} +const E = (@dec class {}, 123); +const F = [@dec class G {}, @dec class {}]; +const H = @dec class extends I {}; +const J = @dec class K extends L {}; + +function classFactory() { + return @dec class {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/output.js new file mode 100644 index 000000000000..9391b1e2597f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/expressions/output.js @@ -0,0 +1,76 @@ +var _initClass, _A, _initClass2, _C, _initClass3, _D, _initClass4, _decorated_class, _initClass5, _G, _initClass6, _decorated_class2, _initClass7, _H, _initClass8, _K; +const dec = () => {}; +const A = (class A { + static { + [_A, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass(); + } +}, _A); +const B = (class C { + static { + [_C, _initClass2] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass2(); + } +}, _C); +const D = (class D { + static { + [_D, _initClass3] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass3(); + } +}, _D); +const E = ((class { + static { + [_decorated_class, _initClass4] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass4(); + } +}, _decorated_class), 123); +const F = [(class G { + static { + [_G, _initClass5] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass5(); + } +}, _G), (class { + static { + [_decorated_class2, _initClass6] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass6(); + } +}, _decorated_class2)]; +const H = (class H extends I { + static { + [_H, _initClass7] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass7(); + } +}, _H); +const J = (class K extends L { + static { + [_K, _initClass8] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass8(); + } +}, _K); +function classFactory() { + var _initClass9, _decorated_class3; + return class { + static { + [_decorated_class3, _initClass9] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static { + _initClass9(); + } + }, _decorated_class3; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/input.js new file mode 100644 index 000000000000..0657f6e2b364 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/input.js @@ -0,0 +1,6 @@ +const dec = () => {}; +@dec1 +class Bar {} + +@dec2 +class Foo extends Bar {} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/output.js new file mode 100644 index 000000000000..5ee04e699f16 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/inheritance/output.js @@ -0,0 +1,22 @@ +var _initClass, _dec, _initClass2, _dec2; +const dec = () => {}; +let _Bar; +_dec = dec1; +class Bar { + static { + [_Bar, _initClass] = babelHelpers.applyDecs2305(this, [], [_dec]).c; + } + static { + _initClass(); + } +} +let _Foo; +_dec2 = dec2; +class Foo extends _Bar { + static { + [_Foo, _initClass2] = babelHelpers.applyDecs2305(this, [], [_dec2]).c; + } + static { + _initClass2(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/input.js new file mode 100644 index 000000000000..87c785d39a93 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +@dec +class Foo { + static field = 123; +} + +@dec +class Bar extends Foo { + static { + this.otherField = 456; + } + + static field = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/output.js new file mode 100644 index 000000000000..23906884f271 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/initializers/output.js @@ -0,0 +1,32 @@ +var _initClass, _initClass2; +const dec = () => {}; +let _Foo; +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + field = 123; + constructor() { + super(_Foo), _initClass(); + } +}(); +let _Bar; +new class extends babelHelpers.identity { + static { + class Bar extends _Foo { + static { + [_Bar, _initClass2] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + field = ((() => { + this.otherField = 456; + })(), 123); + constructor() { + super(_Bar), _initClass2(); + } +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/input.js new file mode 100644 index 000000000000..e8c62db59046 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/input.js @@ -0,0 +1,16 @@ +const dec = () => {}; +let hasX, hasM; + +@dec +class Foo { + static #x; + static #m() {} + + static x; + static m() {} + + static { + hasX = o => #x in o; + hasM = o => #m in o; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/output.js new file mode 100644 index 000000000000..7216dda60846 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-installed-on-correct-class/output.js @@ -0,0 +1,23 @@ +var _initClass; +const dec = () => {}; +let hasX, hasM; +let _Foo; +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + static m() {} + } + } + #x; + #m() {} + x; + constructor() { + super(_Foo), (() => { + hasX = o => #x in o; + hasM = o => #m in o; + })(), _initClass(); + } +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/input.js new file mode 100644 index 000000000000..1b8d46cf3ac0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +@dec +class Foo { + static { + this + } + static field = this; + static { + this + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/output.js new file mode 100644 index 000000000000..66c8ef5979ee --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-static-this/output.js @@ -0,0 +1,20 @@ +var _initClass; +const dec = () => {}; +let _Foo; +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + field = ((() => { + this; + })(), this); + constructor() { + super(_Foo), (() => { + this; + })(), _initClass(); + } +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/input.js new file mode 100644 index 000000000000..0cd5e45110b7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +const Foo = @dec class Bar { + bar = new Bar(); +}; + +const foo = new Foo(); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/output.js new file mode 100644 index 000000000000..73da860bd0ed --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement-with-expr/output.js @@ -0,0 +1,12 @@ +var _initClass, _Bar; +const dec = () => {}; +const Foo = (class Bar { + static { + [_Bar, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + bar = new _Bar(); + static { + _initClass(); + } +}, _Bar); +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/input.js new file mode 100644 index 000000000000..8f0a4dfe092f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +@dec +class Foo { + static foo = new Foo(); +} + +const foo = new Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/output.js new file mode 100644 index 000000000000..fdced4d4f3b7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-classes/replacement/output.js @@ -0,0 +1,17 @@ +var _initClass; +const dec = () => {}; +let _Foo; +new class extends babelHelpers.identity { + static { + class Foo { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + } + } + foo = new _Foo(); + constructor() { + super(_Foo), _initClass(); + } +}(); +const foo = new _Foo(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js new file mode 100644 index 000000000000..49fad189f4e2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/exec.js @@ -0,0 +1,33 @@ +var i = 0; + +function getKey() { + return (i++).toString(); +} + +let elements = []; + +function dec(fn, context) { + elements.push({ fn, context }); +} + +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} + +expect(elements).toHaveLength(2); + +expect(elements[0].context.name).toBe("0"); +expect(elements[0].fn()).toBe(1); + +expect(elements[1].context.name).toBe("1"); +expect(elements[1].fn()).toBe(2); + +expect(i).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/input.js new file mode 100644 index 000000000000..fd4743f81bc7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/output.js new file mode 100644 index 000000000000..44378e06bd5f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-ast/output.js @@ -0,0 +1,16 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = getKey(); +_computedKey2 = getKey(); +class Foo { + constructor(...args) { + _initProto(this); + } + [_computedKey]() { + return 1; + } + [_computedKey2]() { + return 2; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, _computedKey], [dec, 2, _computedKey2]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/exec.js new file mode 100644 index 000000000000..c97e2293ac96 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/exec.js @@ -0,0 +1,29 @@ +expect(() => { + var i = 0; + var j = 0; + + function getKeyI() { + return (i++).toString(); + } + function getKeyJ() { + return (j++).toString(); + } + + let elements = []; + + function dec(fn, context) { + elements.push({ fn, context }); + } + + class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } + } +}).toThrow("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: 0") diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/input.js new file mode 100644 index 000000000000..0f6e2ca31d39 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/output.js new file mode 100644 index 000000000000..e3a195f2a774 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/computed-keys-same-value/output.js @@ -0,0 +1,16 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = getKeyI(); +_computedKey2 = getKeyJ(); +class Foo { + constructor(...args) { + _initProto(this); + } + [_computedKey]() { + return 1; + } + [_computedKey2]() { + return 2; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, _computedKey], [dec, 2, _computedKey2]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/exec.js new file mode 100644 index 000000000000..72e253d55578 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/exec.js @@ -0,0 +1,23 @@ +let elements = []; + +function dec(val, context) { + elements.push({ val, context }); +} + +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} + +expect(elements).toHaveLength(2); + +expect(elements[0].context.name).toBe("a"); +expect(elements[0].val()).toBe(1); + +expect(elements[1].context.name).toBe("a"); +expect(elements[1].val).toBe(undefined); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/input.js new file mode 100644 index 000000000000..e2bfd2a7b415 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/input.js @@ -0,0 +1,10 @@ +const dec = () => {}; +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/output.js new file mode 100644 index 000000000000..70726746ac71 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/method-and-field/output.js @@ -0,0 +1,11 @@ +var _init_a, _initProto; +const dec = () => {}; +class Foo { + constructor() { + babelHelpers.defineProperty(this, "a", (_initProto(this), _init_a(this, 123))); + } + a() { + return 1; + } +} +[_init_a, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, "a"], [dec, 0, "a"]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/exec.js new file mode 100644 index 000000000000..9e270b2a7488 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/exec.js @@ -0,0 +1,19 @@ +expect(() => { + let elements = []; + + function dec(val, context) { + elements.push({ val, context }); + } + + class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } + } +}).toThrow("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: a") diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/input.js new file mode 100644 index 000000000000..ab961fbc9dfb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/output.js new file mode 100644 index 000000000000..269d5980d4bf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/methods-with-same-key/output.js @@ -0,0 +1,14 @@ +var _initProto; +const dec = () => {}; +class Foo { + constructor(...args) { + _initProto(this); + } + a() { + return 1; + } + a() { + return 2; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, "a"], [dec, 2, "a"]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/input.js new file mode 100644 index 000000000000..fd4743f81bc7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + [getKey()]() { + return 1; + } + + @dec + [getKey()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/output.js new file mode 100644 index 000000000000..cfbba5d6cf27 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-ast/output.js @@ -0,0 +1,18 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = getKey(); +_computedKey2 = getKey(); +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, _computedKey], [dec, 2, _computedKey2]], []).e; + } + constructor(...args) { + _initProto(this); + } + [_computedKey]() { + return 1; + } + [_computedKey2]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/input.js new file mode 100644 index 000000000000..0f6e2ca31d39 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + [getKeyI()]() { + return 1; + } + + @dec + [getKeyJ()]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/output.js new file mode 100644 index 000000000000..1a58d243573e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/computed-keys-same-value/output.js @@ -0,0 +1,18 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = getKeyI(); +_computedKey2 = getKeyJ(); +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, _computedKey], [dec, 2, _computedKey2]], []).e; + } + constructor(...args) { + _initProto(this); + } + [_computedKey]() { + return 1; + } + [_computedKey2]() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/input.js new file mode 100644 index 000000000000..e2bfd2a7b415 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/input.js @@ -0,0 +1,10 @@ +const dec = () => {}; +class Foo { + @dec + a = 123; + + @dec + a() { + return 1; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/output.js new file mode 100644 index 000000000000..4bf204f4686a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/method-and-field/output.js @@ -0,0 +1,11 @@ +var _init_a, _initProto; +const dec = () => {}; +class Foo { + static { + [_init_a, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "a"], [dec, 0, "a"]], []).e; + } + a = (_initProto(this), _init_a(this, 123)); + a() { + return 1; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/input.js new file mode 100644 index 000000000000..ab961fbc9dfb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/input.js @@ -0,0 +1,12 @@ +const dec = () => {}; +class Foo { + @dec + a() { + return 1; + } + + @dec + a() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/output.js new file mode 100644 index 000000000000..b05c1ca3ee94 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/methods-with-same-key/output.js @@ -0,0 +1,16 @@ +var _initProto; +const dec = () => {}; +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "a"], [dec, 2, "a"]], []).e; + } + constructor(...args) { + _initProto(this); + } + a() { + return 1; + } + a() { + return 2; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-duplicated-keys/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/input.mjs new file mode 100644 index 000000000000..05db70faed30 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/input.mjs @@ -0,0 +1 @@ +export default @dec class A {} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/output.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/output.mjs new file mode 100644 index 000000000000..0269b8ef738d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-anonymous/output.mjs @@ -0,0 +1,12 @@ +var _initClass, _dec; +let _A; +_dec = dec; +class A { + static { + [_A, _initClass] = babelHelpers.applyDecs2305(this, [], [_dec]).c; + } + static { + _initClass(); + } +} +export { _A as default }; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/input.mjs new file mode 100644 index 000000000000..31cdac17444e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/input.mjs @@ -0,0 +1 @@ +export default @dec class {} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/output.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/output.mjs new file mode 100644 index 000000000000..dc9a83f0b5a9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/default-named/output.mjs @@ -0,0 +1,12 @@ +var _initClass, _dec; +let _default2; +_dec = dec; +class _default { + static { + [_default2, _initClass] = babelHelpers.applyDecs2305(this, [], [_dec]).c; + } + static { + _initClass(); + } +} +export { _default2 as default }; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/input.mjs new file mode 100644 index 000000000000..f72e0e9c2249 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/input.mjs @@ -0,0 +1,3 @@ +export class A { + @dec x; +} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/output.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/output.mjs new file mode 100644 index 000000000000..fc487029f496 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/member-decorator/output.mjs @@ -0,0 +1,8 @@ +var _dec, _init_x; +_dec = dec; +export class A { + static { + [_init_x] = babelHelpers.applyDecs2305(this, [[_dec, 0, "x"]], []).e; + } + x = _init_x(this); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/input.mjs new file mode 100644 index 000000000000..367f946714b0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/input.mjs @@ -0,0 +1 @@ +export @dec class A {} \ No newline at end of file diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/output.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/output.mjs new file mode 100644 index 000000000000..3eae3eec47d2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/named/output.mjs @@ -0,0 +1,12 @@ +var _initClass, _dec; +let _A; +_dec = dec; +class A { + static { + [_A, _initClass] = babelHelpers.applyDecs2305(this, [], [_dec]).c; + } + static { + _initClass(); + } +} +export { _A as A }; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/input.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/input.mjs new file mode 100644 index 000000000000..bc88730f95a8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/input.mjs @@ -0,0 +1,4 @@ +export class A {} + +class B {} +export { B }; \ No newline at end of file diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/output.mjs b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/output.mjs new file mode 100644 index 000000000000..9ac373c42b10 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/no-decorators/output.mjs @@ -0,0 +1,3 @@ +export class A {} +class B {} +export { B }; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-exported/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/exec.js new file mode 100644 index 000000000000..5d9715ee8ee7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/exec.js @@ -0,0 +1,44 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + #a; + + @dec + #b = 123; +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; +const bContext = foo['#bContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(false); + +expect(aContext.access.get(foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +aContext.access.set(foo, 123); +expect(aContext.access.get(foo)).toBe(123); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(aContext.access.get(foo)).toBe(123); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('field'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); + +expect(bContext.access.get(foo)).toBe(124); +bContext.access.set(foo, 123); +expect(bContext.access.get(foo)).toBe(123); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('field'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/input.js new file mode 100644 index 000000000000..2cd77e99b7e3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + #a; + + @dec + #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/output.js new file mode 100644 index 000000000000..5e014f1e0022 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/private/output.js @@ -0,0 +1,17 @@ +var _init_a, _init_b; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +var _b = /*#__PURE__*/new WeakMap(); +class Foo { + constructor() { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: _init_a(this) + }); + babelHelpers.classPrivateFieldInitSpec(this, _b, { + writable: true, + value: _init_b(this, 123) + }); + } +} +[_init_a, _init_b] = babelHelpers.applyDecs2305(Foo, [[dec, 0, "a", o => babelHelpers.classPrivateFieldGet(o, _a), (o, v) => babelHelpers.classPrivateFieldSet(o, _a, v)], [dec, 0, "b", o => babelHelpers.classPrivateFieldGet(o, _b), (o, v) => babelHelpers.classPrivateFieldSet(o, _b, v)]], [], 0, _ => _b.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/exec.js new file mode 100644 index 000000000000..df182db7ac21 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/exec.js @@ -0,0 +1,67 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; +const cContext = foo['cContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(foo.a).toBe(2); +expect(aContext.access.get(foo)).toBe(2); +foo.a = 123; +expect(foo.a).toBe(123); +expect(aContext.access.get(foo)).toBe(123); +aContext.access.set(foo, 456); +expect(foo.a).toBe(456); +expect(aContext.access.get(foo)).toBe(456); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('field'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(foo.hasOwnProperty('a')).toBe(true); +expect(Foo.prototype.hasOwnProperty('a')).toBe(false); + +expect(foo.b).toBe(124); +foo.b = 123; +expect(foo.b).toBe(123); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('field'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('undefined'); +expect(foo.hasOwnProperty('b')).toBe(true); +expect(Foo.prototype.hasOwnProperty('b')).toBe(false); + +expect(foo.c).toBe(457); +foo.c = 456; +expect(foo.c).toBe(456); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('field'); +expect(cContext.static).toBe(false); +expect(cContext.private).toBe(false); +expect(typeof cContext.addInitializer).toBe('undefined'); +expect(foo.hasOwnProperty('c')).toBe(true); +expect(Foo.prototype.hasOwnProperty('c')).toBe(false); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/input.js new file mode 100644 index 000000000000..16a36d324047 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/output.js new file mode 100644 index 000000000000..4b6f87585eb3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/public/output.js @@ -0,0 +1,11 @@ +var _init_a, _init_b, _computedKey, _init_computedKey; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + constructor() { + babelHelpers.defineProperty(this, "a", _init_a(this)); + babelHelpers.defineProperty(this, "b", _init_b(this, 123)); + babelHelpers.defineProperty(this, _computedKey, _init_computedKey(this, 456)); + } +} +[_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs2305(Foo, [[dec, 0, "a"], [dec, 0, "b"], [dec, 0, _computedKey]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..bafdfec712f5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/exec.js @@ -0,0 +1,42 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} + +const aContext = Foo['#aContext']; +const bContext = Foo['#bContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(false); + +expect(aContext.access.get(Foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +aContext.access.set(Foo, 123); +expect(aContext.access.get(Foo)).toBe(123); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(aContext.access.get(Foo)).toBe(123); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('field'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); + +expect(bContext.access.get(Foo)).toBe(124); +bContext.access.set(Foo, 123); +expect(bContext.access.get(Foo)).toBe(123); +expect(bContext.name).toBe('#b'); +expect(bContext.kind).toBe('field'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('undefined'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/input.js new file mode 100644 index 000000000000..bc6f56f17527 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/output.js new file mode 100644 index 000000000000..888a72a454f2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-private/output.js @@ -0,0 +1,12 @@ +var _init_a, _init_b; +const dec = () => {}; +class Foo {} +[_init_a, _init_b] = babelHelpers.applyDecs2305(Foo, [[dec, 8, "a", o => babelHelpers.classStaticPrivateFieldSpecGet(o, Foo, _a), (o, v) => babelHelpers.classStaticPrivateFieldSpecSet(o, Foo, _a, v)], [dec, 8, "b", o => babelHelpers.classStaticPrivateFieldSpecGet(o, Foo, _b), (o, v) => babelHelpers.classStaticPrivateFieldSpecSet(o, Foo, _b, v)]], []).e; +var _a = { + writable: true, + value: _init_a(Foo) +}; +var _b = { + writable: true, + value: _init_b(Foo, 123) +}; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..36113e23c702 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/exec.js @@ -0,0 +1,62 @@ +function dec(_v, context) { + return function (v) { + this[context.name + 'Context'] = context; + return (v || 1) + 1; + } +} + +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; +const cContext = Foo['cContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(Foo.a).toBe(2); +expect(aContext.access.get(Foo)).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(123); +expect(aContext.access.get(Foo)).toBe(123); +aContext.access.set(Foo, 456); +expect(Foo.a).toBe(456); +expect(aContext.access.get(Foo)).toBe(456); +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('field'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('undefined'); +expect(Foo.hasOwnProperty('a')).toBe(true); + +expect(Foo.b).toBe(124); +Foo.b = 123; +expect(Foo.b).toBe(123); +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('field'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('undefined'); +expect(Foo.hasOwnProperty('b')).toBe(true); + +expect(Foo.c).toBe(457); +Foo.c = 456; +expect(Foo.c).toBe(456); +expect(cContext.name).toBe('c'); +expect(cContext.kind).toBe('field'); +expect(cContext.static).toBe(true); +expect(cContext.private).toBe(false); +expect(typeof cContext.addInitializer).toBe('undefined'); +expect(Foo.hasOwnProperty('c')).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/input.js new file mode 100644 index 000000000000..e7e44bf1ec16 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/output.js new file mode 100644 index 000000000000..a708464cb337 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields--to-es2015/static-public/output.js @@ -0,0 +1,8 @@ +var _init_a, _init_b, _computedKey, _init_computedKey; +const dec = () => {}; +_computedKey = 'c'; +class Foo {} +[_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs2305(Foo, [[dec, 8, "a"], [dec, 8, "b"], [dec, 8, _computedKey]], []).e; +babelHelpers.defineProperty(Foo, "a", _init_a(Foo)); +babelHelpers.defineProperty(Foo, "b", _init_b(Foo, 123)); +babelHelpers.defineProperty(Foo, _computedKey, _init_computedKey(Foo, 456)); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/input.js new file mode 100644 index 000000000000..2cd77e99b7e3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + #a; + + @dec + #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/output.js new file mode 100644 index 000000000000..e118ca78ccec --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/private/output.js @@ -0,0 +1,9 @@ +var _init_a, _init_b; +const dec = () => {}; +class Foo { + static { + [_init_a, _init_b] = babelHelpers.applyDecs2305(this, [[dec, 0, "a", o => o.#a, (o, v) => o.#a = v], [dec, 0, "b", o => o.#b, (o, v) => o.#b = v]], [], 0, _ => #b in _).e; + } + #a = _init_a(this); + #b = _init_b(this, 123); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/input.js new file mode 100644 index 000000000000..16a36d324047 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + a; + + @dec + b = 123; + + @dec + ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/output.js new file mode 100644 index 000000000000..922c8f793e57 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/public/output.js @@ -0,0 +1,11 @@ +var _init_a, _init_b, _computedKey, _init_computedKey; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + static { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs2305(this, [[dec, 0, "a"], [dec, 0, "b"], [dec, 0, _computedKey]], []).e; + } + a = _init_a(this); + b = _init_b(this, 123); + [_computedKey] = _init_computedKey(this, 456); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/input.js new file mode 100644 index 000000000000..bc6f56f17527 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec + static #a; + + @dec + static #b = 123; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/output.js new file mode 100644 index 000000000000..4150e9947f1f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-private/output.js @@ -0,0 +1,9 @@ +var _init_a, _init_b; +const dec = () => {}; +class Foo { + static { + [_init_a, _init_b] = babelHelpers.applyDecs2305(this, [[dec, 8, "a", o => o.#a, (o, v) => o.#a = v], [dec, 8, "b", o => o.#b, (o, v) => o.#b = v]], []).e; + } + static #a = _init_a(this); + static #b = _init_b(this, 123); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/input.js new file mode 100644 index 000000000000..e7e44bf1ec16 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class Foo { + @dec + static a; + + @dec + static b = 123; + + @dec + static ['c'] = 456; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/output.js new file mode 100644 index 000000000000..ced0276ccf0a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-fields/static-public/output.js @@ -0,0 +1,11 @@ +var _init_a, _init_b, _computedKey, _init_computedKey; +const dec = () => {}; +_computedKey = 'c'; +class Foo { + static { + [_init_a, _init_b, _init_computedKey] = babelHelpers.applyDecs2305(this, [[dec, 8, "a"], [dec, 8, "b"], [dec, 8, _computedKey]], []).e; + } + static a = _init_a(this); + static b = _init_b(this, 123); + static [_computedKey] = _init_computedKey(this, 456); +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/exec.js new file mode 100644 index 000000000000..eab384f77836 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/exec.js @@ -0,0 +1,42 @@ +function dec(get, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(false); + +expect(aContext.access.get(foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +expect(foo.getA()).toBe(2); +foo.value = 123; +expect(aContext.access.get(foo)).toBe(124); +expect(foo.getA()).toBe(124); +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/input.js new file mode 100644 index 000000000000..28519502bf46 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/output.js new file mode 100644 index 000000000000..3d04c19d1698 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/private/output.js @@ -0,0 +1,22 @@ +var _call_a, _initProto; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: void 0 + }); + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + getA() { + return babelHelpers.classPrivateFieldGet(this, _a); + } +} +function _get_a() { + return _call_a(this); +} +[_call_a, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 3, "a", function () { + return this.value; +}]], [], 0, _ => _a.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/exec.js new file mode 100644 index 000000000000..1e2bbfd6f644 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/exec.js @@ -0,0 +1,56 @@ +function dec(get, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(foo.a).toBe(2); +expect(foo.b).toBe(2); +expect(aContext.access.get(foo)).toBe(2); +expect(bContext.access.get(foo)).toBe(2); +foo.value = 123; +expect(foo.a).toBe(124); +expect(foo.b).toBe(124); +expect(aContext.access.get(foo)).toBe(124); +expect(bContext.access.get(foo)).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('getter'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/input.js new file mode 100644 index 000000000000..392f9e03412f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/output.js new file mode 100644 index 000000000000..81bc3aa71767 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + get a() { + return this.value; + } + get [_computedKey]() { + return this.value; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 3, "a"], [dec, 3, _computedKey]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..082c93bd57ad --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/exec.js @@ -0,0 +1,41 @@ +function dec(get, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} + +const aContext = Foo['#aContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(false); + +expect(aContext.access.get(Foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +expect(Foo.getA()).toBe(2); +Foo.value = 123; +expect(aContext.access.get(Foo)).toBe(124); +expect(Foo.getA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..9d64c02982da --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..b12d94be3fb2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-private/output.js @@ -0,0 +1,21 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static getA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a); + } +} +function _get_a() { + return _call_a(this); +} +var _a = { + get: _get_a, + set: void 0 +}; +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 11, "a", function () { + return this.value; + }]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..aaebed1470da --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/exec.js @@ -0,0 +1,54 @@ +function dec(get, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return get.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(Foo.a).toBe(2); +expect(Foo.b).toBe(2); +expect(aContext.access.get(Foo)).toBe(2); +expect(bContext.access.get(Foo)).toBe(2); +Foo.value = 123; +expect(Foo.a).toBe(124); +expect(Foo.b).toBe(124); +expect(aContext.access.get(Foo)).toBe(124); +expect(bContext.access.get(Foo)).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('getter'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('getter'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..3bc53fb17cc5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..f3a99040dff7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters--to-es2015/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static get a() { + return this.value; + } + static get [_computedKey]() { + return this.value; + } +} +(() => { + [_initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 11, "a"], [dec, 11, _computedKey]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/exec.js new file mode 100644 index 000000000000..4fd73c4c5ca1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/exec.js @@ -0,0 +1,74 @@ +function dec(value, context) { + context.addInitializer(function() { + this[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} + +let foo = new Foo(); + +const a_getterContext = foo['#a_getterContext']; +const a_setterContext = foo['#a_setterContext']; + +expect(a_getterContext.access.has(foo)).toBe(true); +expect(a_getterContext.access.has({})).toBe(false); +expect(a_getterContext.access.has(Object.create(foo))).toBe(false); +expect(a_setterContext.access.has(foo)).toBe(true); +expect(a_setterContext.access.has({})).toBe(false); +expect(a_setterContext.access.has(Object.create(foo))).toBe(false); +expect(a_getterContext.access.has).not.toBe(a_setterContext.access.has); + +expect(a_getterContext.access.get(foo)).toBe(2); +expect(() => a_getterContext.access.get({})).toThrow(TypeError); +expect(foo.getA()).toBe(2); +a_setterContext.access.set(foo, 123); +expect(a_getterContext.access.get(foo)).toBe(125); +expect(() => a_setterContext.access.set({}, 456)).toThrow(TypeError); +expect(a_getterContext.access.get(foo)).toBe(125); +expect(foo.getA()).toBe(125); +foo.setA(456); +expect(a_getterContext.access.get(foo)).toBe(458); +expect(foo.getA()).toBe(458); + +expect(a_getterContext.name).toBe('#a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.static).toBe(false); +expect(a_getterContext.private).toBe(true); +expect(typeof a_getterContext.addInitializer).toBe('function'); + +expect(a_setterContext.name).toBe('#a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.static).toBe(false); +expect(a_setterContext.private).toBe(true); +expect(typeof a_setterContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/input.js new file mode 100644 index 000000000000..fdf137cc4a64 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/input.js @@ -0,0 +1,22 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/output.js new file mode 100644 index 000000000000..a8669c2bc544 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/private/output.js @@ -0,0 +1,30 @@ +var _call_a, _call_a2, _initProto; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: _get_a, + set: _set_a + }); + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + getA() { + return babelHelpers.classPrivateFieldGet(this, _a); + } + setA(v) { + babelHelpers.classPrivateFieldSet(this, _a, v); + } +} +function _get_a() { + return _call_a(this); +} +function _set_a(v) { + _call_a2(this, v); +} +[_call_a, _call_a2, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 3, "a", function () { + return this.value; +}], [dec, 4, "a", function (v) { + this.value = v; +}]], [], 0, _ => _a.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/exec.js new file mode 100644 index 000000000000..fa548d8d5c40 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/exec.js @@ -0,0 +1,103 @@ +function dec(value, context) { + context.addInitializer(function() { + this[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} + +let foo = new Foo(); + +const a_getterContext = foo['a_getterContext']; +const a_setterContext = foo['a_setterContext']; + +const b_getterContext = foo['b_getterContext']; +const b_setterContext = foo['b_setterContext']; + +expect(a_getterContext.access.has(foo)).toBe(true); +expect(a_getterContext.access.has({})).toBe(false); +expect(a_getterContext.access.has(Object.create(foo))).toBe(true); +expect(a_getterContext.access.has({ a: 1 })).toBe(true); +expect(a_getterContext.access.has(Object.create({ a: 1 }))).toBe(true); +expect(a_setterContext.access.has(foo)).toBe(true); +expect(a_setterContext.access.has({})).toBe(false); +expect(a_setterContext.access.has(Object.create(foo))).toBe(true); +expect(a_setterContext.access.has({ a: 1 })).toBe(true); +expect(a_setterContext.access.has(Object.create({ a: 1 }))).toBe(true); +expect(a_getterContext.access.has).not.toBe(a_setterContext.access.has); + +expect(foo.a).toBe(2); +expect(foo.b).toBe(2); +expect(a_getterContext.access.get(foo)).toBe(2); +expect(b_getterContext.access.get(foo)).toBe(2); +foo.a = 123; +expect(foo.a).toBe(125); +expect(foo.b).toBe(125); +expect(a_getterContext.access.get(foo)).toBe(125); +expect(b_getterContext.access.get(foo)).toBe(125); +foo.b = 456; +expect(foo.a).toBe(458); +expect(foo.b).toBe(458); +expect(a_getterContext.access.get(foo)).toBe(458); +expect(b_getterContext.access.get(foo)).toBe(458); +a_setterContext.access.set(foo, 789); +expect(foo.a).toBe(791); +expect(foo.b).toBe(791); +expect(a_getterContext.access.get(foo)).toBe(791); +expect(b_getterContext.access.get(foo)).toBe(791); + +expect(a_getterContext.name).toBe('a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.static).toBe(false); +expect(a_getterContext.private).toBe(false); +expect(typeof a_getterContext.addInitializer).toBe('function'); + +expect(a_setterContext.name).toBe('a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.static).toBe(false); +expect(a_setterContext.private).toBe(false); +expect(typeof a_setterContext.addInitializer).toBe('function'); + +expect(b_getterContext.name).toBe('b'); +expect(b_getterContext.kind).toBe('getter'); +expect(b_getterContext.static).toBe(false); +expect(b_getterContext.private).toBe(false); +expect(typeof b_getterContext.addInitializer).toBe('function'); + +expect(b_setterContext.name).toBe('b'); +expect(b_setterContext.kind).toBe('setter'); +expect(b_setterContext.static).toBe(false); +expect(b_setterContext.private).toBe(false); +expect(typeof b_setterContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/input.js new file mode 100644 index 000000000000..1af1569ba988 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/input.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/output.js new file mode 100644 index 000000000000..0a94d7d3a1af --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/public/output.js @@ -0,0 +1,23 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = 'b'; +_computedKey2 = 'b'; +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + get a() { + return this.value; + } + set a(v) { + this.value = v; + } + get [_computedKey]() { + return this.value; + } + set [_computedKey2](v) { + this.value = v; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 3, "a"], [dec, 4, "a"], [dec, 3, _computedKey], [dec, 4, _computedKey2]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..c7faf2394242 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/exec.js @@ -0,0 +1,72 @@ +function dec(value, context) { + context.addInitializer(function() { + this[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} + +const a_getterContext = Foo['#a_getterContext']; +const a_setterContext = Foo['#a_setterContext']; + +expect(a_getterContext.access.has(Foo)).toBe(true); +expect(a_getterContext.access.has({})).toBe(false); +expect(a_getterContext.access.has(Object.create(Foo))).toBe(false); +expect(a_setterContext.access.has(Foo)).toBe(true); +expect(a_setterContext.access.has({})).toBe(false); +expect(a_setterContext.access.has(Object.create(Foo))).toBe(false); +expect(a_getterContext.access.has).not.toBe(a_setterContext.access.has); + +expect(a_getterContext.access.get(Foo)).toBe(2); +expect(() => a_getterContext.access.get({})).toThrow(TypeError); +expect(Foo.getA()).toBe(2); +a_setterContext.access.set(Foo, 123); +expect(a_getterContext.access.get(Foo)).toBe(125); +expect(() => a_setterContext.access.set({}, 456)).toThrow(TypeError); +expect(a_getterContext.access.get(Foo)).toBe(125); +expect(Foo.getA()).toBe(125); +Foo.setA(456); +expect(a_getterContext.access.get(Foo)).toBe(458); +expect(Foo.getA()).toBe(458); + +expect(a_getterContext.name).toBe('#a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.static).toBe(true); +expect(a_getterContext.private).toBe(true); +expect(typeof a_getterContext.addInitializer).toBe('function'); + +expect(a_setterContext.name).toBe('#a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.static).toBe(true); +expect(a_setterContext.private).toBe(true); +expect(typeof a_setterContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..c832634c6b72 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/input.js @@ -0,0 +1,22 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..05a30c307f11 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-private/output.js @@ -0,0 +1,29 @@ +var _call_a, _call_a2, _initStatic; +const dec = () => {}; +class Foo { + static getA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a); + } + static setA(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _a, v); + } +} +function _get_a() { + return _call_a(this); +} +function _set_a(v) { + _call_a2(this, v); +} +var _a = { + get: _get_a, + set: _set_a +}; +(() => { + [_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 11, "a", function () { + return this.value; + }], [dec, 12, "a", function (v) { + this.value = v; + }]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..9b4f9539e2e4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/exec.js @@ -0,0 +1,102 @@ +function dec(value, context) { + context.addInitializer(function() { + this[context.name + '_' + context.kind + 'Context'] = context; + }); + + if (context.kind === 'getter') { + return function () { + return value.call(this) + 1; + } + } else { + return function (v) { + return value.call(this, v + 1); + } + } +} + +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} + +const a_getterContext = Foo['a_getterContext']; +const a_setterContext = Foo['a_setterContext']; + +const b_getterContext = Foo['b_getterContext']; +const b_setterContext = Foo['b_setterContext']; + +expect(a_getterContext.access.has(Foo)).toBe(true); +expect(a_getterContext.access.has({})).toBe(false); +expect(a_getterContext.access.has(Object.create(Foo))).toBe(true); +expect(a_getterContext.access.has({ a: 1 })).toBe(true); +expect(a_getterContext.access.has(Object.create({ a: 1 }))).toBe(true); +expect(a_setterContext.access.has(Foo)).toBe(true); +expect(a_setterContext.access.has({})).toBe(false); +expect(a_setterContext.access.has(Object.create(Foo))).toBe(true); +expect(a_setterContext.access.has({ a: 1 })).toBe(true); +expect(a_setterContext.access.has(Object.create({ a: 1 }))).toBe(true); +expect(a_getterContext.access.has).not.toBe(a_setterContext.access.has); + +expect(Foo.a).toBe(2); +expect(Foo.b).toBe(2); +expect(a_getterContext.access.get(Foo)).toBe(2); +expect(b_getterContext.access.get(Foo)).toBe(2); +Foo.a = 123; +expect(Foo.a).toBe(125); +expect(Foo.b).toBe(125); +expect(a_getterContext.access.get(Foo)).toBe(125); +expect(b_getterContext.access.get(Foo)).toBe(125); +Foo.b = 456; +expect(Foo.a).toBe(458); +expect(Foo.b).toBe(458); +expect(a_getterContext.access.get(Foo)).toBe(458); +expect(b_getterContext.access.get(Foo)).toBe(458); +a_setterContext.access.set(Foo, 789); +expect(Foo.a).toBe(791); +expect(Foo.b).toBe(791); +expect(a_getterContext.access.get(Foo)).toBe(791); +expect(b_getterContext.access.get(Foo)).toBe(791); + +expect(a_getterContext.name).toBe('a'); +expect(a_getterContext.kind).toBe('getter'); +expect(a_getterContext.static).toBe(true); +expect(a_getterContext.private).toBe(false); +expect(typeof a_getterContext.addInitializer).toBe('function'); + +expect(a_setterContext.name).toBe('a'); +expect(a_setterContext.kind).toBe('setter'); +expect(a_setterContext.static).toBe(true); +expect(a_setterContext.private).toBe(false); +expect(typeof a_setterContext.addInitializer).toBe('function'); + +expect(b_getterContext.name).toBe('b'); +expect(b_getterContext.kind).toBe('getter'); +expect(b_getterContext.static).toBe(true); +expect(b_getterContext.private).toBe(false); +expect(typeof b_getterContext.addInitializer).toBe('function'); + +expect(b_setterContext.name).toBe('b'); +expect(b_setterContext.kind).toBe('setter'); +expect(b_setterContext.static).toBe(true); +expect(b_setterContext.private).toBe(false); +expect(typeof b_setterContext.addInitializer).toBe('function'); + diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..bbad4af972ed --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/input.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..2ad222223db6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters--to-es2015/static-public/output.js @@ -0,0 +1,23 @@ +var _computedKey, _computedKey2, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +_computedKey2 = 'b'; +class Foo { + static get a() { + return this.value; + } + static set a(v) { + this.value = v; + } + static get [_computedKey]() { + return this.value; + } + static set [_computedKey2](v) { + this.value = v; + } +} +(() => { + [_initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 11, "a"], [dec, 12, "a"], [dec, 11, _computedKey], [dec, 12, _computedKey2]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/input.js new file mode 100644 index 000000000000..fdf137cc4a64 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/input.js @@ -0,0 +1,22 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + @dec + set #a(v) { + this.value = v; + } + + getA() { + return this.#a; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/output.js new file mode 100644 index 000000000000..b1df32728c7f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/private/output.js @@ -0,0 +1,27 @@ +var _call_a, _call_a2, _initProto; +const dec = () => {}; +class Foo { + static { + [_call_a, _call_a2, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "a", function () { + return this.value; + }], [dec, 4, "a", function (v) { + this.value = v; + }]], [], 0, _ => #a in _).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + get #a() { + return _call_a(this); + } + set #a(v) { + _call_a2(this, v); + } + getA() { + return this.#a; + } + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/input.js new file mode 100644 index 000000000000..1af1569ba988 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/input.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + set a(v) { + this.value = v; + } + + @dec + get ['b']() { + return this.value; + } + + @dec + set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/output.js new file mode 100644 index 000000000000..5e11ed318222 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/public/output.js @@ -0,0 +1,25 @@ +var _computedKey, _computedKey2, _initProto; +const dec = () => {}; +_computedKey = 'b'; +_computedKey2 = 'b'; +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "a"], [dec, 4, "a"], [dec, 3, _computedKey], [dec, 4, _computedKey2]], []).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + get a() { + return this.value; + } + set a(v) { + this.value = v; + } + get [_computedKey]() { + return this.value; + } + set [_computedKey2](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/input.js new file mode 100644 index 000000000000..c832634c6b72 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/input.js @@ -0,0 +1,22 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + @dec + static set #a(v) { + this.value = v; + } + + static getA() { + return this.#a; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/output.js new file mode 100644 index 000000000000..168ed643a964 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-private/output.js @@ -0,0 +1,25 @@ +var _call_a, _call_a2, _initStatic; +const dec = () => {}; +class Foo { + static { + [_call_a, _call_a2, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 11, "a", function () { + return this.value; + }], [dec, 12, "a", function (v) { + this.value = v; + }]], []).e; + _initStatic(this); + } + static value = 1; + static get #a() { + return _call_a(this); + } + static set #a(v) { + _call_a2(this, v); + } + static getA() { + return this.#a; + } + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/input.js new file mode 100644 index 000000000000..bbad4af972ed --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/input.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static set a(v) { + this.value = v; + } + + @dec + static get ['b']() { + return this.value; + } + + @dec + static set ['b'](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/output.js new file mode 100644 index 000000000000..ca36aed1af62 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters-and-setters/static-public/output.js @@ -0,0 +1,23 @@ +var _computedKey, _computedKey2, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +_computedKey2 = 'b'; +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs2305(this, [[dec, 11, "a"], [dec, 12, "a"], [dec, 11, _computedKey], [dec, 12, _computedKey2]], []).e; + _initStatic(this); + } + static value = 1; + static get a() { + return this.value; + } + static set a(v) { + this.value = v; + } + static get [_computedKey]() { + return this.value; + } + static set [_computedKey2](v) { + this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/input.js new file mode 100644 index 000000000000..28519502bf46 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get #a() { + return this.value; + } + + getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/output.js new file mode 100644 index 000000000000..5f696817f78d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/private/output.js @@ -0,0 +1,19 @@ +var _call_a, _initProto; +const dec = () => {}; +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "a", function () { + return this.value; + }]], [], 0, _ => #a in _).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + get #a() { + return _call_a(this); + } + getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/input.js new file mode 100644 index 000000000000..392f9e03412f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + get a() { + return this.value; + } + + @dec + get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/output.js new file mode 100644 index 000000000000..95bd7ee3235e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/public/output.js @@ -0,0 +1,18 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "a"], [dec, 3, _computedKey]], []).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + get a() { + return this.value; + } + get [_computedKey]() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/input.js new file mode 100644 index 000000000000..9d64c02982da --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get #a() { + return this.value; + } + + static getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/output.js new file mode 100644 index 000000000000..e409b9a19438 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-private/output.js @@ -0,0 +1,17 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 11, "a", function () { + return this.value; + }]], []).e; + _initStatic(this); + } + static value = 1; + static get #a() { + return _call_a(this); + } + static getA() { + return this.#a; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/input.js new file mode 100644 index 000000000000..3bc53fb17cc5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static get a() { + return this.value; + } + + @dec + static get ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/output.js new file mode 100644 index 000000000000..d924f9a613f3 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-getters/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs2305(this, [[dec, 11, "a"], [dec, 11, _computedKey]], []).e; + _initStatic(this); + } + static value = 1; + static get a() { + return this.value; + } + static get [_computedKey]() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private-with-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private-with-initializers/exec.js new file mode 100644 index 000000000000..493327c91c46 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private-with-initializers/exec.js @@ -0,0 +1,44 @@ +function dec(fn, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(false); + +// First call gets the method, second call calls the method with correct `this` +expect(aContext.access.get(foo).call(foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +expect(foo.callA()).toBe(2); +foo.value = 123; +expect(aContext.access.get(foo).call(foo)).toBe(124); +expect(foo.callA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('method'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/exec.js new file mode 100644 index 000000000000..a74cb2331eca --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/exec.js @@ -0,0 +1,24 @@ +function dec(fn, context) { + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} + +let foo = new Foo(); + +expect(foo.callA()).toBe(2); +foo.value = 123; +expect(foo.callA()).toBe(124); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/input.js new file mode 100644 index 000000000000..098bbbb687f2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/output.js new file mode 100644 index 000000000000..730d8deeefe1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/private/output.js @@ -0,0 +1,19 @@ +var _call_a, _initProto; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: _call_a + }); + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + callA() { + return babelHelpers.classPrivateFieldGet(this, _a).call(this); + } +} +[_call_a, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, "a", function () { + return this.value; +}]], [], 0, _ => _a.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public-with-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public-with-initializers/exec.js new file mode 100644 index 000000000000..d7f969ca466e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public-with-initializers/exec.js @@ -0,0 +1,59 @@ +function dec(fn, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(foo.a()).toBe(2); +expect(aContext.access.get(foo).call(foo)).toBe(2); +expect(foo.b()).toBe(2); +expect(bContext.access.get(foo).call(foo)).toBe(2); +foo.value = 123; +expect(aContext.access.get(foo).call(foo)).toBe(124); +expect(foo.a()).toBe(124); +expect(bContext.access.get(foo).call(foo)).toBe(124); +expect(foo.b()).toBe(124); + +expect(aContext.access.get({})).toBe(undefined); +expect(aContext.access.get({ a: 3 })).toBe(3); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('method'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('method'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/exec.js new file mode 100644 index 000000000000..b9ec2b92e328 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/exec.js @@ -0,0 +1,27 @@ +function dec(fn, context) { + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} + +let foo = new Foo(); + +expect(foo.a()).toBe(2); +expect(foo.b()).toBe(2); +foo.value = 123; +expect(foo.a()).toBe(124); +expect(foo.b()).toBe(124); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/input.js new file mode 100644 index 000000000000..3a7f4cfaae9c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/output.js new file mode 100644 index 000000000000..8a4e58184d16 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + a() { + return this.value; + } + [_computedKey]() { + return this.value; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 2, "a"], [dec, 2, _computedKey]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private-with-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private-with-initializers/exec.js new file mode 100644 index 000000000000..b94e75710e49 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private-with-initializers/exec.js @@ -0,0 +1,42 @@ +function dec(fn, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} + +const aContext = Foo['#aContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(false); + +// First call gets the method, second call calls the method with correct `this` +expect(aContext.access.get(Foo).call(Foo)).toBe(2); +expect(() => aContext.access.get({})).toThrow(TypeError); +expect(Foo.callA()).toBe(2); +Foo.value = 123; +expect(aContext.access.get(Foo).call(Foo)).toBe(124); +expect(Foo.callA()).toBe(124); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('method'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..47deb64ec304 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/exec.js @@ -0,0 +1,22 @@ +function dec(fn, context) { + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} + +expect(Foo.callA()).toBe(2); +Foo.value = 123; +expect(Foo.callA()).toBe(124); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/input.js new file mode 100644 index 000000000000..4a6ae5d4d7fd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/output.js new file mode 100644 index 000000000000..9d80d32f442f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-private/output.js @@ -0,0 +1,18 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static callA() { + return babelHelpers.classStaticPrivateFieldSpecGet(this, Foo, _a).call(this); + } +} +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 10, "a", function () { + return this.value; + }]], []).e; + _initStatic(Foo); +})(); +var _a = { + writable: true, + value: _call_a +}; +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public-with-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public-with-initializers/exec.js new file mode 100644 index 000000000000..910dae50598e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public-with-initializers/exec.js @@ -0,0 +1,54 @@ +function dec(fn, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(Foo.a()).toBe(2); +expect(aContext.access.get(Foo).call(Foo)).toBe(2); +expect(Foo.b()).toBe(2); +expect(bContext.access.get(Foo).call(Foo)).toBe(2); +Foo.value = 123; +expect(aContext.access.get(Foo).call(Foo)).toBe(124); +expect(Foo.a()).toBe(124); +expect(bContext.access.get(Foo).call(Foo)).toBe(124); +expect(Foo.b()).toBe(124); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('method'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('method'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..e5d03ee48021 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/exec.js @@ -0,0 +1,25 @@ +function dec(fn, context) { + return function () { + return fn.call(this) + 1; + } +} + +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} + +expect(Foo.a()).toBe(2); +expect(Foo.b()).toBe(2); +Foo.value = 123; +expect(Foo.a()).toBe(124); +expect(Foo.b()).toBe(124); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/input.js new file mode 100644 index 000000000000..5f81da5c0405 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/output.js new file mode 100644 index 000000000000..0aadaa343295 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods--to-es2015/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static a() { + return this.value; + } + static [_computedKey]() { + return this.value; + } +} +(() => { + [_initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 10, "a"], [dec, 10, _computedKey]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/input.js new file mode 100644 index 000000000000..098bbbb687f2 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + #a() { + return this.value; + } + + callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/output.js new file mode 100644 index 000000000000..1f2a2abb25d1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/private/output.js @@ -0,0 +1,17 @@ +var _call_a, _initProto; +const dec = () => {}; +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "a", function () { + return this.value; + }]], [], 0, _ => #a in _).e; + } + constructor(...args) { + _initProto(this); + } + #a = _call_a; + value = 1; + callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/input.js new file mode 100644 index 000000000000..3a7f4cfaae9c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + a() { + return this.value; + } + + @dec + ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/output.js new file mode 100644 index 000000000000..08e57bbaf923 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/public/output.js @@ -0,0 +1,18 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "a"], [dec, 2, _computedKey]], []).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + a() { + return this.value; + } + [_computedKey]() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/input.js new file mode 100644 index 000000000000..4a6ae5d4d7fd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static #a() { + return this.value; + } + + static callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/output.js new file mode 100644 index 000000000000..c1db8be452fc --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-private/output.js @@ -0,0 +1,15 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 10, "a", function () { + return this.value; + }]], []).e; + _initStatic(this); + } + static #a = _call_a; + static value = 1; + static callA() { + return this.#a(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/input.js new file mode 100644 index 000000000000..5f81da5c0405 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static a() { + return this.value; + } + + @dec + static ['b']() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/output.js new file mode 100644 index 000000000000..843617af4d3e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-methods/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs2305(this, [[dec, 10, "a"], [dec, 10, _computedKey]], []).e; + _initStatic(this); + } + static value = 1; + static a() { + return this.value; + } + static [_computedKey]() { + return this.value; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-method-decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-method-decorators/exec.js new file mode 100644 index 000000000000..11aae499cb0d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-method-decorators/exec.js @@ -0,0 +1,77 @@ +function dec() {} + +var i = 0; +var log = []; + +function push(x) { log.push(x); return x; } + +function decWithInitializer(_, { addInitializer }) { + addInitializer(() => push(i++) ); +} + +new @dec class C1 { + @dec static m() {} +} + +new @dec class C2 { + @dec static #m() {} +} + +new @dec class C3 { + @dec m() {} +} + +new @dec class C4 { + @dec #m() {} +} + +new @decWithInitializer class C5 { + @dec static m() {} +} + +new @decWithInitializer class C6 { + @dec static #m() {} +} + +new @decWithInitializer class C7 { + @dec m() {} +} + +new @decWithInitializer class C8 { + @dec #m() {} +} + +new @dec class C9 { + @decWithInitializer static m() {} +} + +new @dec class C10 { + @decWithInitializer static #m() {} +} + +new @dec class C11 { + @decWithInitializer m() {} +} + +new @dec class C12 { + @decWithInitializer #m() {} +} + +new @decWithInitializer class C13 { + @decWithInitializer static m() {} +} + +new @decWithInitializer class C14 { + @decWithInitializer static #m() {} +} + +new @decWithInitializer class C15 { + @decWithInitializer m() {} +} + +new @decWithInitializer class C16 { + @decWithInitializer #m() {} +} + +var nums = Array.from({ length: 16 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-property-decorators/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-property-decorators/exec.js new file mode 100644 index 000000000000..3993d925adec --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/class-and-property-decorators/exec.js @@ -0,0 +1,45 @@ +function dec() {} + +var i = 0; +var log = []; + +function push(x) { log.push(x); return x; } + +function decWithInitializer(_, { addInitializer }) { + addInitializer(() => push(i++) ); +} + +new @dec class C1 { + @dec static p +} + +new @dec class C2 { + @dec static #p +} + +new @dec class C3 { + @dec p +} + +new @dec class C4 { + @dec #p +} + +new @decWithInitializer class C5 { + @dec static p +} + +new @decWithInitializer class C6 { + @dec static #p +} + +new @decWithInitializer class C7 { + @dec p +} + +new @decWithInitializer class C8 { + @dec #p +} + +var nums = Array.from({ length: 4 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/decorator-evaluation-scope/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/decorator-evaluation-scope/exec.js new file mode 100644 index 000000000000..ec8beb3b8cbf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/decorator-evaluation-scope/exec.js @@ -0,0 +1,12 @@ +let receivedName; +function decFactory(name) { receivedName = name; return x => x } +class B { + static m() { + class C { + @decFactory(this.name) p; + } + } +} + +B.m(); +expect(receivedName).toBe("B"); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/exec.js new file mode 100644 index 000000000000..3d81b8cdf452 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/exec.js @@ -0,0 +1,28 @@ +let self, a, initCalled; + +function deco(_, context) { + context.addInitializer(() => { + initCalled = true; + }) +} + +class B { + constructor(s) { + a = s; + } +} + +class A extends B { + constructor() { + let a = 2; + self = super(a); + } + + @deco + method() {} +} + +let instance = new A(); +expect(self).toBe(instance); +expect(a).toBe(2); +expect(initCalled).toBe(true); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/input.js new file mode 100644 index 000000000000..68484c72749d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class A extends B { + constructor() { + let a = 2; + super(a); + foo(); + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/output.js new file mode 100644 index 000000000000..96026932df55 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initProto-existing-derived-constructor/output.js @@ -0,0 +1,12 @@ +var _dec, _initProto; +const dec = () => {}; +_dec = deco; +class A extends B { + constructor() { + let a = 2; + _initProto(super(a)); + foo(); + } + method() {} +} +[_initProto] = babelHelpers.applyDecs2305(A, [[_dec, 2, "method"]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-property-ignored/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-property-ignored/exec.js new file mode 100644 index 000000000000..2828e1c22408 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-property-ignored/exec.js @@ -0,0 +1,25 @@ +let init = false; +let initializer = false; + +function decorator() { + return { + get init() { + init = true; + return () => {}; + }, + get initializer() { + initializer = true; + return () => {}; + } + }; +} + +class A { + @decorator + accessor x; +} + +new A(); + +expect(init).toBe(true); +expect(initializer).toBe(false); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-timing/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-timing/exec.js new file mode 100644 index 000000000000..ac5920dcd16a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/initializer-timing/exec.js @@ -0,0 +1,34 @@ +function dec1(fn, context) { + context.addInitializer((instance) => { + expect(instance.value).toBe(undefined); + }); + + return fn; +} + +class Foo { + value = 1; + + @dec1 + foo() {} +} + +function dec2(fn, context) { + context.addInitializer((instance) => { + expect(instance.value).toBe(1); + }); + + return fn; +} + + +class Bar extends Foo { + constructor() { + super(); + + this.value = 2; + } + + @dec2 + bar() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer-throw/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer-throw/exec.js new file mode 100644 index 000000000000..ccf93163d0fa --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer-throw/exec.js @@ -0,0 +1,17 @@ +let addInitializer; + +function decMethod(_, context) { + ({ addInitializer } = context); + addInitializer(() => null); +} + +try { + class C { + @decMethod + m() {} + } +} finally {} + +expect(() => { + addInitializer(() => null); +}).toThrow('attempted to call addInitializer after decoration was finished') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer/exec.js new file mode 100644 index 000000000000..61cd2063364c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/leaked-context-addInitializer/exec.js @@ -0,0 +1,18 @@ +let addInitializer; + +function callCapturedFunc() { + addInitializer(() => null); +} + +function decMethod(_, context) { + ({ addInitializer } = context); + addInitializer(() => null); +} + +expect(() => { + class C { + @callCapturedFunc + @decMethod + m() {} + } +}).toThrow('attempted to call addInitializer after decoration was finished') diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/input.js new file mode 100644 index 000000000000..7e75b7766176 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/input.js @@ -0,0 +1,9 @@ +class A extends B { + m() { + @(super.dec1) + class C { + @(super.dec2) + m2() {} + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/output.js new file mode 100644 index 000000000000..b977fb657630 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/super-in-decorator/output.js @@ -0,0 +1,21 @@ +class A extends B { + m() { + var _initClass, _obj, _dec, _obj2, _dec2, _initProto; + let _C; + _obj = this; + _dec = super.dec1; + _obj2 = this; + _dec2 = super.dec2; + class C { + constructor(...args) { + _initProto(this); + } + m2() {} + } + ({ + e: [_initProto], + c: [_C, _initClass] + } = babelHelpers.applyDecs2305(C, [[[_obj2, _dec2], 18, "m2"]], [_obj, _dec], 1)); + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/exec.js new file mode 100644 index 000000000000..e418f72569e9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/exec.js @@ -0,0 +1,26 @@ +"use strict"; + +let _this = []; + +function dec() { + _this.push(this); +} + +let o1 = { dec }; +let o2 = { dec }; +let o3 = { o: { dec } }; + +@o1.dec +@dec +@o2.dec +class A { + @o2.dec + @o3.o.dec + x; + + @o2.dec + @dec + y; +} + +expect(_this).toEqual([o3.o, o2, undefined, o2, o2, undefined, o1]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/input.js new file mode 100644 index 000000000000..c95350691ddb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/input.js @@ -0,0 +1,12 @@ +@o1.dec +@dec +@o2.dec +class A { + @o2.dec + @o3.o.dec + x; + + @o2.dec + @dec + y; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/output.js new file mode 100644 index 000000000000..cbd9f3b7e277 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/this/output.js @@ -0,0 +1,25 @@ +var _initClass, _obj, _dec, _dec2, _obj2, _dec3, _obj3, _dec4, _obj4, _dec5, _init_x, _obj5, _dec6, _dec7, _init_y; +let _A; +_obj = o1; +_dec = _obj.dec; +_dec2 = dec; +_obj2 = o2; +_dec3 = _obj2.dec; +_obj3 = o2; +_dec4 = _obj3.dec; +_obj4 = o3.o; +_dec5 = _obj4.dec; +_obj5 = o2; +_dec6 = _obj5.dec; +_dec7 = dec; +class A { + constructor() { + babelHelpers.defineProperty(this, "x", _init_x(this)); + babelHelpers.defineProperty(this, "y", _init_y(this)); + } +} +({ + e: [_init_x, _init_y], + c: [_A, _initClass] +} = babelHelpers.applyDecs2305(A, [[[_obj3, _dec4, _obj4, _dec5], 16, "x"], [[_obj5, _dec6, void 0, _dec7], 16, "y"]], [_obj, _dec, void 0, _dec2, _obj2, _dec3], 1)); +_initClass(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/input.js new file mode 100644 index 000000000000..586564c5e219 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/input.js @@ -0,0 +1,23 @@ +const dec = () => {}; +@dec +@call() +@chain.expr() +@(arbitrary + expr) +@(array[expr]) +class Foo { + #a; + + @dec + @call() + @chain.expr() + @(arbitrary + expr) + @(array[expr]) + method() {} + + makeClass() { + return class Nested { + @(this.#a) + bar; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/output.js new file mode 100644 index 000000000000..7d5829ccd30b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc--to-es2015/valid-expression-formats/output.js @@ -0,0 +1,37 @@ +var _initClass, _dec, _dec2, _dec3, _obj, _dec4, _dec5, _dec6, _dec7, _obj2, _dec8, _initProto; +const dec = () => {}; +let _Foo; +_dec = call(); +_dec2 = chain.expr(); +_dec3 = arbitrary + expr; +_obj = array; +_dec4 = _obj[expr]; +_dec5 = call(); +_dec6 = chain.expr(); +_dec7 = arbitrary + expr; +_obj2 = array; +_dec8 = _obj2[expr]; +var _a = /*#__PURE__*/new WeakMap(); +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + writable: true, + value: void 0 + }); + _initProto(this); + } + method() {} + makeClass() { + var _dec9, _init_bar, _class; + return _dec9 = babelHelpers.classPrivateFieldGet(this, _a), (_class = class Nested { + constructor() { + babelHelpers.defineProperty(this, "bar", _init_bar(this)); + } + }, [_init_bar] = babelHelpers.applyDecs2305(_class, [[_dec9, 0, "bar"]], []).e, _class); + } +} +({ + e: [_initProto], + c: [_Foo, _initClass] +} = babelHelpers.applyDecs2305(Foo, [[[void 0, dec, void 0, _dec5, void 0, _dec6, void 0, _dec7, _obj2, _dec8], 18, "method"]], [void 0, dec, void 0, _dec, void 0, _dec2, void 0, _dec3, _obj, _dec4], 1)); +_initClass(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/input.js new file mode 100644 index 000000000000..6986908e606d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/input.js @@ -0,0 +1,27 @@ +const dec = () => {}; +@dec +class Class { + @dec a; + @dec b() {} + @dec get c() {} + @dec set c(v) {} + @dec accessor d; + + @dec #e; + @dec #f() {} + @dec get #g() {} + @dec set #g(v) {} + @dec accessor #h; + + @dec static i; + @dec static j() {} + @dec static get k() {} + @dec static set l(v) {} + @dec static accessor m; + + @dec static #n; + @dec static #o() {} + @dec static get #p() {} + @dec static set #q(v) {} + @dec static accessor #r; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/output.js new file mode 100644 index 000000000000..f72010c74a38 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/all-decorators/output.js @@ -0,0 +1,71 @@ +var _initClass, _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; +const dec = () => {}; +let _Class; +new class extends babelHelpers.identity { + static { + class Class { + static { + ({ + 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], + c: [_Class, _initClass] + } = babelHelpers.applyDecs2305(this, [[dec, 10, "j"], [dec, 11, "k"], [dec, 12, "l"], [dec, 9, "m"], [dec, 10, "o", function () {}], [dec, 11, "p", function () {}], [dec, 12, "q", function (v) {}], [dec, 9, "r", o => o.#D, (o, v) => o.#D = v], [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", o => o.#B, (o, v) => o.#B = v], [dec, 8, "i"], [dec, 8, "n", o => o.#n, (o, v) => o.#n = v], [dec, 0, "a"], [dec, 0, "e", o => o.#e, (o, v) => o.#e = v]], [dec], 0, _ => #e in _)); + _initStatic(this); + } + #f = _call_f; + a = (_initProto(this), _init_a(this)); + b() {} + get c() {} + set c(v) {} + #A = _init_d(this); + get d() { + return this.#A; + } + set d(v) { + this.#A = v; + } + #e = _init_e(this); + get #g() { + return _call_g(this); + } + set #g(v) { + _call_g2(this, v); + } + #B = _init_h(this); + set #h(v) { + _set_h(this, v); + } + get #h() { + return _get_h(this); + } + static j() {} + static get k() {} + static set l(v) {} + static get m() { + return Class.#C; + } + static set m(v) { + Class.#C = v; + } + set #r(v) { + _set_r(this, v); + } + get #r() { + return _get_r(this); + } + } + } + #o = _call_o; + i = _init_i(this); + #C = _init_m(this); + #n = _init_n(this); + get #p() { + return _call_p(this); + } + set #q(v) { + _call_q(this, v); + } + #D = _init_r(this); + constructor() { + super(_Class), _initClass(); + } +}(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/exec.js new file mode 100644 index 000000000000..ec8beb3b8cbf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/exec.js @@ -0,0 +1,12 @@ +let receivedName; +function decFactory(name) { receivedName = name; return x => x } +class B { + static m() { + class C { + @decFactory(this.name) p; + } + } +} + +B.m(); +expect(receivedName).toBe("B"); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/options.json new file mode 100644 index 000000000000..c70512252968 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/decorator-evaluation-scope/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-static-block" + ], + "minNodeVersion": "12.0.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/input.js new file mode 100644 index 000000000000..a8da039942d6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/input.js @@ -0,0 +1,24 @@ +const dec = () => {}; +class A extends B { + constructor() { + if (Math.random() > 0.5) { + super(true); + } else { + super(false); + } + } + + @deco + method() {} +} + +class C extends B { + constructor() { + try { + super(super(), null.x); + } catch {} + } + + @deco + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/output.js new file mode 100644 index 000000000000..6ad6fa2fe920 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor-multiple-super/output.js @@ -0,0 +1,28 @@ +var _dec, _initProto, _dec2, _initProto2; +const dec = () => {}; +_dec = deco; +class A extends B { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[_dec, 2, "method"]], []).e; + } + constructor() { + if (Math.random() > 0.5) { + _initProto(super(true)); + } else { + _initProto(super(false)); + } + } + method() {} +} +_dec2 = deco; +class C extends B { + static { + [_initProto2] = babelHelpers.applyDecs2305(this, [[_dec2, 2, "method"]], []).e; + } + constructor() { + try { + _initProto2(super(_initProto2(super()), null.x)); + } catch {} + } + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/input.js new file mode 100644 index 000000000000..b3c7b6f85e9b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class A extends B { + constructor() { + let a = 2; + super(a); + foo(); + } + + @dec + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/output.js new file mode 100644 index 000000000000..b28d62844d7c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initProto-existing-derived-constructor/output.js @@ -0,0 +1,13 @@ +var _initProto; +const dec = () => {}; +class A extends B { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "method"]], []).e; + } + constructor() { + let a = 2; + _initProto(super(a)); + foo(); + } + method() {} +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/exec.js new file mode 100644 index 000000000000..2828e1c22408 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/exec.js @@ -0,0 +1,25 @@ +let init = false; +let initializer = false; + +function decorator() { + return { + get init() { + init = true; + return () => {}; + }, + get initializer() { + initializer = true; + return () => {}; + } + }; +} + +class A { + @decorator + accessor x; +} + +new A(); + +expect(init).toBe(true); +expect(initializer).toBe(false); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/options.json new file mode 100644 index 000000000000..c70512252968 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/initializer-property-ignored/options.json @@ -0,0 +1,7 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-static-block" + ], + "minNodeVersion": "12.0.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/input.js new file mode 100644 index 000000000000..0e7ac6161824 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/input.js @@ -0,0 +1,11 @@ +const dec = () => {}; +class A { + #A = 1; + static B = class B extends A { + accessor a = 2; + + getA() { + return this.#A; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/output.js new file mode 100644 index 000000000000..ec3d19f84d2a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/private-keys-in-enclosing-class/output.js @@ -0,0 +1,16 @@ +const dec = () => {}; +class A { + #A = 1; + static B = class B extends A { + #B = 2; + get a() { + return this.#B; + } + set a(v) { + this.#B = v; + } + getA() { + return this.#A; + } + }; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/input.js new file mode 100644 index 000000000000..53848c42cb07 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + ([this.#x] = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-array-pattern/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/input.js new file mode 100644 index 000000000000..2ffabb75d901 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + for (this.#x of this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-for-of/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/input.js new file mode 100644 index 000000000000..a60de2bae344 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + ({ x: this.#x } = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-object-pattern/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/input.js new file mode 100644 index 000000000000..6b513a380627 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + ([...this.#x] = this.baz); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-rest/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/input.js new file mode 100644 index 000000000000..7d01837e1b51 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + this.#x++; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method-via-update/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/input.js new file mode 100644 index 000000000000..b6417214fec7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/input.js @@ -0,0 +1,8 @@ +const dec = () => {}; +class Foo { + @dec #x() {} + + bar() { + this.#x = 123; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/options.json new file mode 100644 index 000000000000..188eae592ec5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/setting-private-method/options.json @@ -0,0 +1,3 @@ +{ + "throws": "Decorated private methods are not updatable, but \"#x\" is updated via this expression." +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/input.js new file mode 100644 index 000000000000..7e75b7766176 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/input.js @@ -0,0 +1,9 @@ +class A extends B { + m() { + @(super.dec1) + class C { + @(super.dec2) + m2() {} + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/output.js new file mode 100644 index 000000000000..04a2be72ff77 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-decorator/output.js @@ -0,0 +1,25 @@ +class A extends B { + m() { + var _initClass, _obj, _dec, _obj2, _dec2, _initProto; + let _C; + _obj = this; + _dec = super.dec1; + _obj2 = this; + _dec2 = super.dec2; + class C { + static { + ({ + e: [_initProto], + c: [_C, _initClass] + } = babelHelpers.applyDecs2305(this, [[[_obj2, _dec2], 18, "m2"]], [_obj, _dec], 1)); + } + constructor(...args) { + _initProto(this); + } + m2() {} + static { + _initClass(); + } + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/input.js new file mode 100644 index 000000000000..6bc1a7dbbc4b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +@dec +class Foo extends Bar { + constructor() { + let foo = super(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/output.js new file mode 100644 index 000000000000..267564aaae78 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-nested-constructor-expression/output.js @@ -0,0 +1,14 @@ +var _initClass; +const dec = () => {}; +let _Foo; +class Foo extends Bar { + static { + [_Foo, _initClass] = babelHelpers.applyDecs2305(this, [], [dec]).c; + } + constructor() { + let foo = super(); + } + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/input.js new file mode 100644 index 000000000000..b32ea0bd378f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +class Foo extends Bar { + @dec + get #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/output.js new file mode 100644 index 000000000000..cc2ef8ee435f --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-accessor/output.js @@ -0,0 +1,16 @@ +var _call_x, _initProto; +const dec = () => {}; +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 3, "x", function () { + return babelHelpers.get(babelHelpers.getPrototypeOf(Foo), "foo", this).call(this); + }]], [], 0, _ => #x in _).e; + } + constructor(...args) { + super(...args); + _initProto(this); + } + get #x() { + return _call_x(this); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/input.js new file mode 100644 index 000000000000..36f9bd37dfa7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/input.js @@ -0,0 +1,7 @@ +const dec = () => {}; +class Foo extends Bar { + @dec + #x() { + return super.foo(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/output.js new file mode 100644 index 000000000000..1a370c02ff33 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/super-in-private-method/output.js @@ -0,0 +1,14 @@ +var _call_x, _initProto; +const dec = () => {}; +class Foo extends Bar { + static { + [_call_x, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 2, "x", function () { + return babelHelpers.get(babelHelpers.getPrototypeOf(Foo), "foo", this).call(this); + }]], [], 0, _ => #x in _).e; + } + constructor(...args) { + super(...args); + _initProto(this); + } + #x = _call_x; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/exec.js new file mode 100644 index 000000000000..e418f72569e9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/exec.js @@ -0,0 +1,26 @@ +"use strict"; + +let _this = []; + +function dec() { + _this.push(this); +} + +let o1 = { dec }; +let o2 = { dec }; +let o3 = { o: { dec } }; + +@o1.dec +@dec +@o2.dec +class A { + @o2.dec + @o3.o.dec + x; + + @o2.dec + @dec + y; +} + +expect(_this).toEqual([o3.o, o2, undefined, o2, o2, undefined, o1]); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/input.js new file mode 100644 index 000000000000..c95350691ddb --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/input.js @@ -0,0 +1,12 @@ +@o1.dec +@dec +@o2.dec +class A { + @o2.dec + @o3.o.dec + x; + + @o2.dec + @dec + y; +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/options.json new file mode 100644 index 000000000000..f97653393918 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/options.json @@ -0,0 +1,3 @@ +{ + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/output.js new file mode 100644 index 000000000000..cc5c62df7399 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/this/output.js @@ -0,0 +1,27 @@ +var _initClass, _obj, _dec, _dec2, _obj2, _dec3, _obj3, _dec4, _obj4, _dec5, _init_x, _obj5, _dec6, _dec7, _init_y; +let _A; +_obj = o1; +_dec = _obj.dec; +_dec2 = dec; +_obj2 = o2; +_dec3 = _obj2.dec; +_obj3 = o2; +_dec4 = _obj3.dec; +_obj4 = o3.o; +_dec5 = _obj4.dec; +_obj5 = o2; +_dec6 = _obj5.dec; +_dec7 = dec; +class A { + static { + ({ + e: [_init_x, _init_y], + c: [_A, _initClass] + } = babelHelpers.applyDecs2305(this, [[[_obj3, _dec4, _obj4, _dec5], 16, "x"], [[_obj5, _dec6, void 0, _dec7], 16, "y"]], [_obj, _dec, void 0, _dec2, _obj2, _dec3], 1)); + } + x = _init_x(this); + y = _init_y(this); + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/input.js new file mode 100644 index 000000000000..586564c5e219 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/input.js @@ -0,0 +1,23 @@ +const dec = () => {}; +@dec +@call() +@chain.expr() +@(arbitrary + expr) +@(array[expr]) +class Foo { + #a; + + @dec + @call() + @chain.expr() + @(arbitrary + expr) + @(array[expr]) + method() {} + + makeClass() { + return class Nested { + @(this.#a) + bar; + } + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/output.js new file mode 100644 index 000000000000..3bb5e2ef71f6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-misc/valid-expression-formats/output.js @@ -0,0 +1,38 @@ +var _initClass, _dec, _dec2, _dec3, _obj, _dec4, _dec5, _dec6, _dec7, _obj2, _dec8, _initProto; +const dec = () => {}; +let _Foo; +_dec = call(); +_dec2 = chain.expr(); +_dec3 = arbitrary + expr; +_obj = array; +_dec4 = _obj[expr]; +_dec5 = call(); +_dec6 = chain.expr(); +_dec7 = arbitrary + expr; +_obj2 = array; +_dec8 = _obj2[expr]; +class Foo { + static { + ({ + e: [_initProto], + c: [_Foo, _initClass] + } = babelHelpers.applyDecs2305(this, [[[void 0, dec, void 0, _dec5, void 0, _dec6, void 0, _dec7, _obj2, _dec8], 18, "method"]], [void 0, dec, void 0, _dec, void 0, _dec2, void 0, _dec3, _obj, _dec4], 1)); + } + constructor(...args) { + _initProto(this); + } + #a; + method() {} + makeClass() { + var _obj3, _dec9, _init_bar; + return _obj3 = this, _dec9 = this.#a, class Nested { + static { + [_init_bar] = babelHelpers.applyDecs2305(this, [[[_obj3, _dec9], 16, "bar"]], []).e; + } + bar = _init_bar(this); + }; + } + static { + _initClass(); + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-fields/exec.js new file mode 100644 index 000000000000..ad49bc03d1e7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-fields/exec.js @@ -0,0 +1,67 @@ +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), + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 26) + @logAccessorDecoratorRun(3, 10, 22, 27) + accessor a; + + @logFieldDecoratorRun(4, 15) + @logFieldDecoratorRun(5, 14) + b; + + @logFieldDecoratorRun(6, 17) + @logFieldDecoratorRun(7, 16) + #c; + + @logAccessorDecoratorRun(8, 13, 25, 28) + @logAccessorDecoratorRun(9, 12, 24, 29) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-setters/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-setters/exec.js new file mode 100644 index 000000000000..2d4e63303055 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-initializers-setters/exec.js @@ -0,0 +1,61 @@ +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, e, f) { + push(a); + return function ({ set }, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + set(v) { push(e); const result = set.call(this, v); push(f); return result; } + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 11, 13) +@logClassDecoratorRun(1, 10, 12) +class A { + @logAccessorDecoratorRun(2, 7, 15, 18, 22, 25) + @logAccessorDecoratorRun(3, 6, 14, 19, 23, 24) + accessor a; + + @logAccessorDecoratorRun(4, 9, 17, 20, 26, 29) + @logAccessorDecoratorRun(5, 8, 16, 21, 27, 28) + accessor #b; + + constructor() { + this.a = null; + this.#b = null; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-method-initializers/exec.js new file mode 100644 index 000000000000..405ad66b271b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-method-initializers/exec.js @@ -0,0 +1,75 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 30) + @logAccessorDecoratorRun(3, 10, 22, 31) + accessor a; + + @logMethodDecoratorRun(4, 13, 25, 35) + @logMethodDecoratorRun(5, 12, 24, 34) + b() {}; + + @logMethodDecoratorRun(6, 15, 27, 37) + @logMethodDecoratorRun(7, 14, 26, 36) + #c() {}; + + @logAccessorDecoratorRun(8, 17, 29, 32) + @logAccessorDecoratorRun(9, 16, 28, 33) + accessor #d; + + constructor() { + this.b(); + this.#c(); + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-accessor-initializers/exec.js new file mode 100644 index 000000000000..232218430bdf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-accessor-initializers/exec.js @@ -0,0 +1,51 @@ +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, 34) + @logAccessorDecoratorRun(3, 14, 30, 35) + accessor a; + + @logAccessorDecoratorRun(4, 11, 21, 24) + @logAccessorDecoratorRun(5, 10, 20, 25) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 26) + @logAccessorDecoratorRun(7, 12, 22, 27) + static accessor #c; + + @logAccessorDecoratorRun(8, 17, 33, 36) + @logAccessorDecoratorRun(9, 16, 32, 37) + accessor #d; +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-method-initializers/exec.js new file mode 100644 index 000000000000..2e6c4f3191f6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/accessor-static-method-initializers/exec.js @@ -0,0 +1,77 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + static { + A.b(), A.#c(); + } + + @logAccessorDecoratorRun(2, 15, 31, 34) + @logAccessorDecoratorRun(3, 14, 30, 35) + accessor a; + + @logMethodDecoratorRun(4, 11, 21, 25) + @logMethodDecoratorRun(5, 10, 20, 24) + static b() {}; + + @logMethodDecoratorRun(6, 13, 23, 27) + @logMethodDecoratorRun(7, 12, 22, 26) + static #c() {}; + + @logAccessorDecoratorRun(8, 17, 33, 36) + @logAccessorDecoratorRun(9, 16, 32, 37) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field-initializers-after-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field-initializers-after-methods/exec.js new file mode 100644 index 000000000000..46e71f7281db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field-initializers-after-methods/exec.js @@ -0,0 +1,27 @@ +var counter = 0; + +@(x => x) +class A { + foo = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBeUndefined(); + expect(this.bar).toBeUndefined(); + return "foo"; + })(); + + method() {} + + bar = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBe("foo"); + expect(this.bar).toBeUndefined(); + })(); +} + +expect(counter).toBe(0); + +new A(); + +expect(counter).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field/exec.js new file mode 100644 index 000000000000..7a51e8b93b7e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/field/exec.js @@ -0,0 +1,31 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logDecoratorRun(0, 21) +@logDecoratorRun(1, 20) +class A { + @logDecoratorRun(2, 17) + @logDecoratorRun(3, 16) + [push(4)]; + + @logDecoratorRun(5, 13) + @logDecoratorRun(6, 12) + static [push(7)]; + + @logDecoratorRun(8, 15) + @logDecoratorRun(9, 14) + static #c; + + @logDecoratorRun(10, 19) + @logDecoratorRun(11, 18) + #d; +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/initializers/exec.js new file mode 100644 index 000000000000..2a43cb651ee9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/initializers/exec.js @@ -0,0 +1,56 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { push(c); }); + return el; + }; +} + +@logDecoratorRun(0, 35, 45) +@logDecoratorRun(1, 34, 44) +class A { + @logDecoratorRun(2, 27, 47) + @logDecoratorRun(3, 26, 46) + a() {}; + + @logDecoratorRun(4, 19, 37) + @logDecoratorRun(5, 18, 36) + static b() {}; + + @logDecoratorRun(6, 21, 39) + @logDecoratorRun(7, 20, 38) + static #c() {}; + + @logDecoratorRun(8, 29, 49) + @logDecoratorRun(9, 28, 48) + #d() {}; + + @logDecoratorRun(10, 31, 51) + @logDecoratorRun(11, 30, 50) + accessor e; + + @logDecoratorRun(12, 23, 41) + @logDecoratorRun(13, 22, 40) + static accessor f; + + @logDecoratorRun(14, 25, 43) + @logDecoratorRun(15, 24, 42) + static accessor #g; + + @logDecoratorRun(16, 33, 53) + @logDecoratorRun(17, 32, 52) + accessor #h; +} + +var nums = Array.from({ length: 46 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 54 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-accessor-method-initializers/exec.js new file mode 100644 index 000000000000..c3e18ec7f687 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-accessor-method-initializers/exec.js @@ -0,0 +1,74 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + @logMethodDecoratorRun(2, 15, 31, 35) + @logMethodDecoratorRun(3, 14, 30, 34) + a() {} + + @logAccessorDecoratorRun(4, 11, 21, 24) + @logAccessorDecoratorRun(5, 10, 20, 25) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 26) + @logAccessorDecoratorRun(7, 12, 22, 27) + static accessor #c; + + @logMethodDecoratorRun(8, 17, 33, 37) + @logMethodDecoratorRun(9, 16, 32, 36) + #d() {} + + constructor() { + this.a(); + this.#d(); + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-field-initializers-after-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-field-initializers-after-methods/exec.js new file mode 100644 index 000000000000..0703f091cc8d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering--to-es2015/static-field-initializers-after-methods/exec.js @@ -0,0 +1,23 @@ +var counter = 0; + +@(x => x) +class A { + static foo = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBeUndefined(); + expect(this.bar).toBeUndefined(); + return "foo"; + })(); + + static method() {} + + static bar = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBe("foo"); + expect(this.bar).toBeUndefined(); + })(); +} + +expect(counter).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-init-and-set-consistent/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-init-and-set-consistent/exec.js new file mode 100644 index 000000000000..468ab5d32fb1 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-init-and-set-consistent/exec.js @@ -0,0 +1,30 @@ +function minusTwo({ set }) { + return { + set(v) { + set.call(this, v - 2); + }, + init(v) { + return v - 2; + }, + }; +} + +function timesFour({ set }) { + return { + set(v) { + set.call(this, v * 4); + }, + init(v) { + return v * 4; + }, + }; +} + +class Foo { + @minusTwo @timesFour accessor bar = 5; +} +const foo = new Foo(); +expect(foo.bar).toBe(12); // (5 - 2) * 4 + +foo.bar = 5; +expect(foo.bar).toBe(12); // (5 - 2) * 4 diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-fields/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-fields/exec.js new file mode 100644 index 000000000000..ad49bc03d1e7 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-fields/exec.js @@ -0,0 +1,67 @@ +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), + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 26) + @logAccessorDecoratorRun(3, 10, 22, 27) + accessor a; + + @logFieldDecoratorRun(4, 15) + @logFieldDecoratorRun(5, 14) + b; + + @logFieldDecoratorRun(6, 17) + @logFieldDecoratorRun(7, 16) + #c; + + @logAccessorDecoratorRun(8, 13, 25, 28) + @logAccessorDecoratorRun(9, 12, 24, 29) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-setters/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-setters/exec.js new file mode 100644 index 000000000000..2d4e63303055 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-initializers-setters/exec.js @@ -0,0 +1,61 @@ +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, e, f) { + push(a); + return function ({ set }, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return { + init: () => push(d), + set(v) { push(e); const result = set.call(this, v); push(f); return result; } + }; + }; +} + +function logFieldDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logClassDecoratorRun(0, 11, 13) +@logClassDecoratorRun(1, 10, 12) +class A { + @logAccessorDecoratorRun(2, 7, 15, 18, 22, 25) + @logAccessorDecoratorRun(3, 6, 14, 19, 23, 24) + accessor a; + + @logAccessorDecoratorRun(4, 9, 17, 20, 26, 29) + @logAccessorDecoratorRun(5, 8, 16, 21, 27, 28) + accessor #b; + + constructor() { + this.a = null; + this.#b = null; + } +} + +var nums = Array.from({ length: 14 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-method-initializers/exec.js new file mode 100644 index 000000000000..405ad66b271b --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-method-initializers/exec.js @@ -0,0 +1,75 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 21) +@logClassDecoratorRun(1, 18, 20) +class A { + @logAccessorDecoratorRun(2, 11, 23, 30) + @logAccessorDecoratorRun(3, 10, 22, 31) + accessor a; + + @logMethodDecoratorRun(4, 13, 25, 35) + @logMethodDecoratorRun(5, 12, 24, 34) + b() {}; + + @logMethodDecoratorRun(6, 15, 27, 37) + @logMethodDecoratorRun(7, 14, 26, 36) + #c() {}; + + @logAccessorDecoratorRun(8, 17, 29, 32) + @logAccessorDecoratorRun(9, 16, 28, 33) + accessor #d; + + constructor() { + this.b(); + this.#c(); + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-accessor-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-accessor-initializers/exec.js new file mode 100644 index 000000000000..232218430bdf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-accessor-initializers/exec.js @@ -0,0 +1,51 @@ +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, 34) + @logAccessorDecoratorRun(3, 14, 30, 35) + accessor a; + + @logAccessorDecoratorRun(4, 11, 21, 24) + @logAccessorDecoratorRun(5, 10, 20, 25) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 26) + @logAccessorDecoratorRun(7, 12, 22, 27) + static accessor #c; + + @logAccessorDecoratorRun(8, 17, 33, 36) + @logAccessorDecoratorRun(9, 16, 32, 37) + accessor #d; +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-method-initializers/exec.js new file mode 100644 index 000000000000..2e6c4f3191f6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/accessor-static-method-initializers/exec.js @@ -0,0 +1,77 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + static { + A.b(), A.#c(); + } + + @logAccessorDecoratorRun(2, 15, 31, 34) + @logAccessorDecoratorRun(3, 14, 30, 35) + accessor a; + + @logMethodDecoratorRun(4, 11, 21, 25) + @logMethodDecoratorRun(5, 10, 20, 24) + static b() {}; + + @logMethodDecoratorRun(6, 13, 23, 27) + @logMethodDecoratorRun(7, 12, 22, 26) + static #c() {}; + + @logAccessorDecoratorRun(8, 17, 33, 36) + @logAccessorDecoratorRun(9, 16, 32, 37) + accessor #d; + + constructor() { + this.a = this.#d = null; + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field-initializers-after-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field-initializers-after-methods/exec.js new file mode 100644 index 000000000000..46e71f7281db --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field-initializers-after-methods/exec.js @@ -0,0 +1,27 @@ +var counter = 0; + +@(x => x) +class A { + foo = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBeUndefined(); + expect(this.bar).toBeUndefined(); + return "foo"; + })(); + + method() {} + + bar = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBe("foo"); + expect(this.bar).toBeUndefined(); + })(); +} + +expect(counter).toBe(0); + +new A(); + +expect(counter).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field/exec.js new file mode 100644 index 000000000000..7a51e8b93b7e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/field/exec.js @@ -0,0 +1,31 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logDecoratorRun(a, b) { + push(a); + return function (el) { push(b); return el; }; +} + +@logDecoratorRun(0, 21) +@logDecoratorRun(1, 20) +class A { + @logDecoratorRun(2, 17) + @logDecoratorRun(3, 16) + [push(4)]; + + @logDecoratorRun(5, 13) + @logDecoratorRun(6, 12) + static [push(7)]; + + @logDecoratorRun(8, 15) + @logDecoratorRun(9, 14) + static #c; + + @logDecoratorRun(10, 19) + @logDecoratorRun(11, 18) + #d; +} + +var nums = Array.from({ length: 22 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/initializers/exec.js new file mode 100644 index 000000000000..2a43cb651ee9 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/initializers/exec.js @@ -0,0 +1,56 @@ +var log = []; + +function push(x) { log.push(x); return x; } + +function logDecoratorRun(a, b, c) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { push(c); }); + return el; + }; +} + +@logDecoratorRun(0, 35, 45) +@logDecoratorRun(1, 34, 44) +class A { + @logDecoratorRun(2, 27, 47) + @logDecoratorRun(3, 26, 46) + a() {}; + + @logDecoratorRun(4, 19, 37) + @logDecoratorRun(5, 18, 36) + static b() {}; + + @logDecoratorRun(6, 21, 39) + @logDecoratorRun(7, 20, 38) + static #c() {}; + + @logDecoratorRun(8, 29, 49) + @logDecoratorRun(9, 28, 48) + #d() {}; + + @logDecoratorRun(10, 31, 51) + @logDecoratorRun(11, 30, 50) + accessor e; + + @logDecoratorRun(12, 23, 41) + @logDecoratorRun(13, 22, 40) + static accessor f; + + @logDecoratorRun(14, 25, 43) + @logDecoratorRun(15, 24, 42) + static accessor #g; + + @logDecoratorRun(16, 33, 53) + @logDecoratorRun(17, 32, 52) + accessor #h; +} + +var nums = Array.from({ length: 46 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 54 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/options.json new file mode 100644 index 000000000000..480fe17629b4 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/options.json @@ -0,0 +1,4 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]], + "minNodeVersion": "16.11.0" +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-accessor-method-initializers/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-accessor-method-initializers/exec.js new file mode 100644 index 000000000000..c3e18ec7f687 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-accessor-method-initializers/exec.js @@ -0,0 +1,74 @@ +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), + }; + }; +} + +function logMethodDecoratorRun(a, b, c, d) { + push(a); + return function (el, { addInitializer }) { + push(b); + addInitializer(function () { + push(c); + }); + return () => (el(), push(d)) + }; +} + +@logClassDecoratorRun(0, 19, 29) +@logClassDecoratorRun(1, 18, 28) +class A { + @logMethodDecoratorRun(2, 15, 31, 35) + @logMethodDecoratorRun(3, 14, 30, 34) + a() {} + + @logAccessorDecoratorRun(4, 11, 21, 24) + @logAccessorDecoratorRun(5, 10, 20, 25) + static accessor b; + + @logAccessorDecoratorRun(6, 13, 23, 26) + @logAccessorDecoratorRun(7, 12, 22, 27) + static accessor #c; + + @logMethodDecoratorRun(8, 17, 33, 37) + @logMethodDecoratorRun(9, 16, 32, 36) + #d() {} + + constructor() { + this.a(); + this.#d(); + } +} + +var nums = Array.from({ length: 30 }, (_, i) => i); +expect(log).toEqual(nums); + +new A(); + +var nums = Array.from({ length: 38 }, (_, i) => i); +expect(log).toEqual(nums); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-field-initializers-after-methods/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-field-initializers-after-methods/exec.js new file mode 100644 index 000000000000..0703f091cc8d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-ordering/static-field-initializers-after-methods/exec.js @@ -0,0 +1,23 @@ +var counter = 0; + +@(x => x) +class A { + static foo = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBeUndefined(); + expect(this.bar).toBeUndefined(); + return "foo"; + })(); + + static method() {} + + static bar = (() => { + counter++; + expect(typeof this.method).toBe("function"); + expect(this.foo).toBe("foo"); + expect(this.bar).toBeUndefined(); + })(); +} + +expect(counter).toBe(2); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js new file mode 100644 index 000000000000..cd977fce13e0 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-accessor-decorator-return/exec.js @@ -0,0 +1,20 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {} +const returnsGetSet = () => ({ set(v) {}, get() {} }); +const returnsInit = () => ({ init() {} }); +const returnsGetFalse = () => ({ get: false }); +const returnsSetFalse = () => ({ set: false }); +const returnsInitFalse = () => ({ init: false }); + +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 { @returnsInitFalse accessor a }).toThrow("accessor.init must be a function"); + +expect(() => class { @returnsGetSet accessor a }).not.toThrow(); +expect(() => class { @returnsInit accessor a }).not.toThrow(); +expect(() => class { @returnsUndefined accessor a }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-add-initializer/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-add-initializer/exec.js new file mode 100644 index 000000000000..a91c771ca9b8 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-add-initializer/exec.js @@ -0,0 +1,6 @@ +const decWithInitializer = (init) => (_, context) => { context.addInitializer(init) } + +expect(() => class { @decWithInitializer(null) static m() {} }).toThrow("An initializer must be a function") +expect(() => class { @decWithInitializer(false) static m() {} }).toThrow("An initializer must be a function") +expect(() => class { @decWithInitializer(void 0) static m() {} }).toThrow("An initializer must be a function"); +expect(() => class { @decWithInitializer(() => {}) static m() {} }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-class-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-class-decorator-return/exec.js new file mode 100644 index 000000000000..dc0b566937ae --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-class-decorator-return/exec.js @@ -0,0 +1,9 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {}; + +expect(() => @returnsNull class {}).toThrow("class decorators must return a function or void 0") +expect(() => @returnsFalse class {}).toThrow("class decorators must return a function or void 0") +expect(() => @returnsFunction class {}).not.toThrow(); +expect(() => @returnsUndefined class {}).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-field-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-field-decorator-return/exec.js new file mode 100644 index 000000000000..2776eb4561c6 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-field-decorator-return/exec.js @@ -0,0 +1,9 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {}; + +expect(() => class { @returnsNull m() {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFalse m() {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFunction m() {} }).not.toThrow(); +expect(() => class { @returnsUndefined m() {} }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-getter-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-getter-decorator-return/exec.js new file mode 100644 index 000000000000..eb9e8bcdcc03 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-getter-decorator-return/exec.js @@ -0,0 +1,9 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {}; + +expect(() => class { @returnsNull get p() {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFalse get p() {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFunction get p() {} }).not.toThrow(); +expect(() => class { @returnsUndefined get p() {} }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-method-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-method-decorator-return/exec.js new file mode 100644 index 000000000000..181333cf4840 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-method-decorator-return/exec.js @@ -0,0 +1,9 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {}; + +expect(() => class { @returnsNull p }).toThrow("field decorators must return a function or void 0") +expect(() => class { @returnsFalse p }).toThrow("field decorators must return a function or void 0") +expect(() => class { @returnsFunction p }).not.toThrow(); +expect(() => class { @returnsUndefined p }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-setter-decorator-return/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-setter-decorator-return/exec.js new file mode 100644 index 000000000000..d0f7f5115a97 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/invalid-setter-decorator-return/exec.js @@ -0,0 +1,9 @@ +const returnsUndefined = () => void 0; +const returnsNull = () => null; +const returnsFalse = () => false; +const returnsFunction = () => () => {}; + +expect(() => class { @returnsNull set p(v) {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFalse set p(v) {} }).toThrow("method decorators must return a function or void 0") +expect(() => class { @returnsFunction set p(v) {} }).not.toThrow(); +expect(() => class { @returnsUndefined set p(v) {} }).not.toThrow(); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-runtime-errors--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/options.json new file mode 100644 index 000000000000..90031fe8187a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/options.json @@ -0,0 +1,8 @@ +{ + "plugins": [ + ["proposal-decorators", { "version": "2023-05" }], + "proposal-class-properties", + "proposal-private-methods", + "proposal-class-static-block" + ] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/exec.js new file mode 100644 index 000000000000..2e5ed8d74721 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/exec.js @@ -0,0 +1,44 @@ +function dec(set, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} + +let foo = new Foo(); + +const aContext = foo['#aContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(false); + +expect(foo.value).toBe(1); +aContext.access.set(foo, 123); +expect(foo.value).toBe(124); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(foo.value).toBe(124); +foo.setA(456); +expect(foo.value).toBe(457); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/input.js new file mode 100644 index 000000000000..66dd78268d19 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/output.js new file mode 100644 index 000000000000..bbe10fb4677a --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/private/output.js @@ -0,0 +1,22 @@ +var _call_a, _initProto; +const dec = () => {}; +var _a = /*#__PURE__*/new WeakMap(); +class Foo { + constructor(...args) { + babelHelpers.classPrivateFieldInitSpec(this, _a, { + get: void 0, + set: _set_a + }); + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + setA(v) { + babelHelpers.classPrivateFieldSet(this, _a, v); + } +} +function _set_a(v) { + _call_a(this, v); +} +[_call_a, _initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 4, "a", function (v) { + return this.value = v; +}]], [], 0, _ => _a.has(babelHelpers.checkInRHS(_))).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/exec.js new file mode 100644 index 000000000000..f82fb5e637da --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/exec.js @@ -0,0 +1,52 @@ +function dec(set, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} + +let foo = new Foo(); + +const aContext = foo['aContext']; +const bContext = foo['bContext']; + +expect(aContext.access.has(foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(foo.value).toBe(1); +foo.a = 123; +expect(foo.value).toBe(124); +aContext.access.set(foo, 456); +expect(foo.value).toBe(457); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.static).toBe(false); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('setter'); +expect(bContext.static).toBe(false); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/input.js new file mode 100644 index 000000000000..9a7faf175edd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/output.js new file mode 100644 index 000000000000..41cd48423ff5 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + constructor(...args) { + babelHelpers.defineProperty(this, "value", 1); + _initProto(this); + } + set a(v) { + return this.value = v; + } + set [_computedKey](v) { + return this.value = v; + } +} +[_initProto] = babelHelpers.applyDecs2305(Foo, [[dec, 4, "a"], [dec, 4, _computedKey]], []).e; diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/exec.js new file mode 100644 index 000000000000..6314a3393e05 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/exec.js @@ -0,0 +1,42 @@ +function dec(set, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} + +const aContext = Foo['#aContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(false); + +expect(Foo.value).toBe(1); +aContext.access.set(Foo, 123); +expect(Foo.value).toBe(124); +expect(() => aContext.access.set({}, 456)).toThrow(TypeError); +expect(Foo.value).toBe(124); +Foo.setA(456); +expect(Foo.value).toBe(457); + +expect(aContext.name).toBe('#a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(true); +expect(typeof aContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/input.js new file mode 100644 index 000000000000..6bf29a2a2c94 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/output.js new file mode 100644 index 000000000000..cc884c193f4d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-private/output.js @@ -0,0 +1,21 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static setA(v) { + babelHelpers.classStaticPrivateFieldSpecSet(this, Foo, _a, v); + } +} +function _set_a(v) { + _call_a(this, v); +} +var _a = { + get: void 0, + set: _set_a +}; +(() => { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 12, "a", function (v) { + return this.value = v; + }]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/exec.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/exec.js new file mode 100644 index 000000000000..e6c67fe0217d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/exec.js @@ -0,0 +1,50 @@ +function dec(set, context) { + context.addInitializer(function() { + this[context.name + 'Context'] = context; + }); + + return function (v) { + return set.call(this, v + 1); + } +} + +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} + +const aContext = Foo['aContext']; +const bContext = Foo['bContext']; + +expect(aContext.access.has(Foo)).toBe(true); +expect(aContext.access.has({})).toBe(false); +expect(aContext.access.has(Object.create(Foo))).toBe(true); +expect(aContext.access.has({ a: 1 })).toBe(true); +expect(aContext.access.has(Object.create({ a: 1 }))).toBe(true); + +expect(Foo.value).toBe(1); +Foo.a = 123; +expect(Foo.value).toBe(124); +aContext.access.set(Foo, 456); +expect(Foo.value).toBe(457); + +expect(aContext.name).toBe('a'); +expect(aContext.kind).toBe('setter'); +expect(aContext.static).toBe(true); +expect(aContext.private).toBe(false); +expect(typeof aContext.addInitializer).toBe('function'); + +expect(bContext.name).toBe('b'); +expect(bContext.kind).toBe('setter'); +expect(bContext.static).toBe(true); +expect(bContext.private).toBe(false); +expect(typeof bContext.addInitializer).toBe('function'); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/input.js new file mode 100644 index 000000000000..ed0a661dcb2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/output.js new file mode 100644 index 000000000000..b035cd37ae2c --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters--to-es2015/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static set a(v) { + return this.value = v; + } + static set [_computedKey](v) { + return this.value = v; + } +} +(() => { + [_initStatic] = babelHelpers.applyDecs2305(Foo, [[dec, 12, "a"], [dec, 12, _computedKey]], []).e; + _initStatic(Foo); +})(); +babelHelpers.defineProperty(Foo, "value", 1); diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/options.json b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/options.json new file mode 100644 index 000000000000..15054fd05183 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-decorators", { "version": "2023-05" }]] +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/input.js new file mode 100644 index 000000000000..66dd78268d19 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + set #a(v) { + return this.value = v; + } + + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/output.js new file mode 100644 index 000000000000..73a6d735335e --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/private/output.js @@ -0,0 +1,19 @@ +var _call_a, _initProto; +const dec = () => {}; +class Foo { + static { + [_call_a, _initProto] = babelHelpers.applyDecs2305(this, [[dec, 4, "a", function (v) { + return this.value = v; + }]], [], 0, _ => #a in _).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + set #a(v) { + _call_a(this, v); + } + setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/input.js new file mode 100644 index 000000000000..9a7faf175edd --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + value = 1; + + @dec + set a(v) { + return this.value = v; + } + + @dec + set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/output.js new file mode 100644 index 000000000000..1af777204389 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/public/output.js @@ -0,0 +1,18 @@ +var _computedKey, _initProto; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initProto] = babelHelpers.applyDecs2305(this, [[dec, 4, "a"], [dec, 4, _computedKey]], []).e; + } + constructor(...args) { + _initProto(this); + } + value = 1; + set a(v) { + return this.value = v; + } + set [_computedKey](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/input.js new file mode 100644 index 000000000000..6bf29a2a2c94 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/input.js @@ -0,0 +1,13 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static set #a(v) { + return this.value = v; + } + + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/output.js new file mode 100644 index 000000000000..201e9f0463bf --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-private/output.js @@ -0,0 +1,17 @@ +var _call_a, _initStatic; +const dec = () => {}; +class Foo { + static { + [_call_a, _initStatic] = babelHelpers.applyDecs2305(this, [[dec, 12, "a", function (v) { + return this.value = v; + }]], []).e; + _initStatic(this); + } + static value = 1; + static set #a(v) { + _call_a(this, v); + } + static setA(v) { + this.#a = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/input.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/input.js new file mode 100644 index 000000000000..ed0a661dcb2d --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/input.js @@ -0,0 +1,14 @@ +const dec = () => {}; +class Foo { + static value = 1; + + @dec + static set a(v) { + return this.value = v; + } + + @dec + static set ['b'](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/output.js b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/output.js new file mode 100644 index 000000000000..fdb2ec3a0d61 --- /dev/null +++ b/packages/babel-plugin-proposal-decorators/test/fixtures/2023-05-setters/static-public/output.js @@ -0,0 +1,16 @@ +var _computedKey, _initStatic; +const dec = () => {}; +_computedKey = 'b'; +class Foo { + static { + [_initStatic] = babelHelpers.applyDecs2305(this, [[dec, 12, "a"], [dec, 12, _computedKey]], []).e; + _initStatic(this); + } + static value = 1; + static set a(v) { + return this.value = v; + } + static set [_computedKey](v) { + return this.value = v; + } +} diff --git a/packages/babel-plugin-syntax-decorators/src/index.ts b/packages/babel-plugin-syntax-decorators/src/index.ts index 292e48fe2587..992ff5b8a33e 100644 --- a/packages/babel-plugin-syntax-decorators/src/index.ts +++ b/packages/babel-plugin-syntax-decorators/src/index.ts @@ -3,8 +3,14 @@ import { declare } from "@babel/helper-plugin-utils"; export interface Options { // TODO(Babel 8): Remove legacy?: boolean; - // TODO(Babel 8): Remove "2018-09", "2021-12", and '2022-03' - version?: "legacy" | "2018-09" | "2021-12" | "2022-03" | "2023-01"; + // TODO(Babel 8): Remove "2018-09", "2021-12", '2022-03', and '2023-01' + version?: + | "legacy" + | "2018-09" + | "2021-12" + | "2022-03" + | "2023-01" + | "2023-05"; // TODO(Babel 8): Remove decoratorsBeforeExport?: boolean; } @@ -18,10 +24,11 @@ export default declare((api, options: Options) => { if (version === undefined) { throw new Error( "The decorators plugin requires a 'version' option, whose value must be one of: " + - "'2023-01', '2022-03', '2021-12', '2018-09', or 'legacy'.", + "'2023-05', '2023-01', '2022-03', '2021-12', '2018-09', or 'legacy'.", ); } if ( + version !== "2023-05" && version !== "2023-01" && version !== "2022-03" && version !== "2021-12" && @@ -56,6 +63,7 @@ export default declare((api, options: Options) => { if (version === undefined) { version = legacy ? "legacy" : "2018-09"; } else if ( + version !== "2023-05" && version !== "2023-01" && version !== "2022-03" && version !== "2021-12" && @@ -104,7 +112,7 @@ export default declare((api, options: Options) => { "decoratorAutoAccessors", ); } else { - if (version === "2023-01") { + if (version === "2023-01" || version === "2023-05") { parserOpts.plugins.push( ["decorators", { allowCallParenthesized: false }], "decoratorAutoAccessors", diff --git a/packages/babel-runtime-corejs2/package.json b/packages/babel-runtime-corejs2/package.json index f08289d4eca5..724ff204671a 100644 --- a/packages/babel-runtime-corejs2/package.json +++ b/packages/babel-runtime-corejs2/package.json @@ -72,6 +72,15 @@ "./helpers/applyDecs2301.js" ], "./helpers/esm/applyDecs2301": "./helpers/esm/applyDecs2301.js", + "./helpers/applyDecs2305": [ + { + "node": "./helpers/applyDecs2305.js", + "import": "./helpers/esm/applyDecs2305.js", + "default": "./helpers/applyDecs2305.js" + }, + "./helpers/applyDecs2305.js" + ], + "./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js", "./helpers/asyncGeneratorDelegate": [ { "node": "./helpers/asyncGeneratorDelegate.js", diff --git a/packages/babel-runtime-corejs3/package.json b/packages/babel-runtime-corejs3/package.json index e191ade33e24..265d9ac5f488 100644 --- a/packages/babel-runtime-corejs3/package.json +++ b/packages/babel-runtime-corejs3/package.json @@ -71,6 +71,15 @@ "./helpers/applyDecs2301.js" ], "./helpers/esm/applyDecs2301": "./helpers/esm/applyDecs2301.js", + "./helpers/applyDecs2305": [ + { + "node": "./helpers/applyDecs2305.js", + "import": "./helpers/esm/applyDecs2305.js", + "default": "./helpers/applyDecs2305.js" + }, + "./helpers/applyDecs2305.js" + ], + "./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js", "./helpers/asyncGeneratorDelegate": [ { "node": "./helpers/asyncGeneratorDelegate.js", diff --git a/packages/babel-runtime/package.json b/packages/babel-runtime/package.json index 156dd84c643a..5d5cad35bee3 100644 --- a/packages/babel-runtime/package.json +++ b/packages/babel-runtime/package.json @@ -71,6 +71,15 @@ "./helpers/applyDecs2301.js" ], "./helpers/esm/applyDecs2301": "./helpers/esm/applyDecs2301.js", + "./helpers/applyDecs2305": [ + { + "node": "./helpers/applyDecs2305.js", + "import": "./helpers/esm/applyDecs2305.js", + "default": "./helpers/applyDecs2305.js" + }, + "./helpers/applyDecs2305.js" + ], + "./helpers/esm/applyDecs2305": "./helpers/esm/applyDecs2305.js", "./helpers/asyncGeneratorDelegate": [ { "node": "./helpers/asyncGeneratorDelegate.js",