diff --git a/crates/swc/tests/fixture/issue-1306/case1/output/index.ts b/crates/swc/tests/fixture/issue-1306/case1/output/index.ts index 365e49c39dc..01242a7ec12 100644 --- a/crates/swc/tests/fixture/issue-1306/case1/output/index.ts +++ b/crates/swc/tests/fixture/issue-1306/case1/output/index.ts @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _name = new WeakMap(); var Animal = /*#__PURE__*/ function() { "use strict"; function Animal(name) { @@ -63,4 +78,3 @@ var Animal = /*#__PURE__*/ function() { ]); return Animal; }(); -var _name = new WeakMap(); diff --git a/crates/swc/tests/fixture/issue-1306/case2/output/index.js b/crates/swc/tests/fixture/issue-1306/case2/output/index.js index 365e49c39dc..01242a7ec12 100644 --- a/crates/swc/tests/fixture/issue-1306/case2/output/index.js +++ b/crates/swc/tests/fixture/issue-1306/case2/output/index.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _name = new WeakMap(); var Animal = /*#__PURE__*/ function() { "use strict"; function Animal(name) { @@ -63,4 +78,3 @@ var Animal = /*#__PURE__*/ function() { ]); return Animal; }(); -var _name = new WeakMap(); diff --git a/crates/swc/tests/fixture/issue-1333/case1/output/index.js b/crates/swc/tests/fixture/issue-1333/case1/output/index.js index ebef7e1e060..5147715ec4a 100644 --- a/crates/swc/tests/fixture/issue-1333/case1/output/index.js +++ b/crates/swc/tests/fixture/issue-1333/case1/output/index.js @@ -4,16 +4,27 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _ws = new WeakMap(), _ws2 = new WeakMap(); class Foo { get connected() { return _classPrivateFieldGet(this, _ws2) && _classPrivateFieldGet(this, _ws).readyState === _ws1.default.OPEN; @@ -29,5 +40,3 @@ class Foo { }); } } -var _ws = new WeakMap(); -var _ws2 = new WeakMap(); diff --git a/crates/swc/tests/fixture/issue-1333/case2/output/index.js b/crates/swc/tests/fixture/issue-1333/case2/output/index.js index 63eaea4ad8b..8d18f487883 100644 --- a/crates/swc/tests/fixture/issue-1333/case2/output/index.js +++ b/crates/swc/tests/fixture/issue-1333/case2/output/index.js @@ -12,25 +12,39 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _interopRequireDefault(obj) { @@ -39,6 +53,31 @@ function _interopRequireDefault(obj) { }; } const connectionStates = Object.keys(_ws.default); +var /** + * The serialization handler. + * @type {Serialization} + */ _serialization1 = new WeakMap(), /** + * The compression handler. + * @type {Compression} + */ _compression1 = new WeakMap(), /** + * The current sequence. + * @type {number} + */ _seq = new WeakMap(), /** + * The shard sequence when the websocket last closed. + * @type {number} + */ _closingSeq = new WeakMap(), /** + * The rate-limit bucket. + * @type {Bucket} + */ _bucket = new WeakMap(), /** + * The rate-limit bucket for presence updates. + * @type {Bucket} + */ _presenceBucket = new WeakMap(), /** + * The current connection. + * @type {WebSocket} + */ _ws1 = new WeakMap(), /** + * Packets that are waiting to be sent. + * @type {DiscordPacket[]} + */ _queue = new WeakMap(); class Shard extends _utils.Emitter { /** * The current sequence @@ -342,59 +381,35 @@ class Shard extends _utils.Emitter { * @param {number} id The ID of this shard. */ constructor(manager, id){ super(); - _classPrivateFieldInit(this, /** - * The serialization handler. - * @type {Serialization} - */ _serialization1, { + _classPrivateFieldInit(this, _serialization1, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The compression handler. - * @type {Compression} - */ _compression1, { + _classPrivateFieldInit(this, _compression1, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The current sequence. - * @type {number} - */ _seq, { + _classPrivateFieldInit(this, _seq, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The shard sequence when the websocket last closed. - * @type {number} - */ _closingSeq, { + _classPrivateFieldInit(this, _closingSeq, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The rate-limit bucket. - * @type {Bucket} - */ _bucket, { + _classPrivateFieldInit(this, _bucket, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The rate-limit bucket for presence updates. - * @type {Bucket} - */ _presenceBucket, { + _classPrivateFieldInit(this, _presenceBucket, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * The current connection. - * @type {WebSocket} - */ _ws1, { + _classPrivateFieldInit(this, _ws1, { writable: true, value: void 0 }); - _classPrivateFieldInit(this, /** - * Packets that are waiting to be sent. - * @type {DiscordPacket[]} - */ _queue, { + _classPrivateFieldInit(this, _queue, { writable: true, value: void 0 }); @@ -448,11 +463,3 @@ class Shard extends _utils.Emitter { * @property {number} [code=1000] The code to use. */ exports.Shard = Shard; -var _serialization1 = new WeakMap(); -var _compression1 = new WeakMap(); -var _seq = new WeakMap(); -var _closingSeq = new WeakMap(); -var _bucket = new WeakMap(); -var _presenceBucket = new WeakMap(); -var _ws1 = new WeakMap(); -var _queue = new WeakMap(); diff --git a/crates/swc/tests/fixture/issue-1333/case3/output/index.js b/crates/swc/tests/fixture/issue-1333/case3/output/index.js index 070a06f98c6..f99535cf13d 100644 --- a/crates/swc/tests/fixture/issue-1333/case3/output/index.js +++ b/crates/swc/tests/fixture/issue-1333/case3/output/index.js @@ -11,11 +11,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -33,6 +43,10 @@ const headers = [ "x-ratelimit-reset", "via" ]; +var /** + * Used for sequential requests. + * @type {AsyncQueue} + */ _queue = new WeakMap(); class RequestHandler { /** * Whether this handler is inactive or not. @@ -186,10 +200,7 @@ class RequestHandler { * @param {Rest} rest The REST Manager. * @param {string} id The ID of this request handler. */ constructor(rest, id){ - _classPrivateFieldInit(this, /** - * Used for sequential requests. - * @type {AsyncQueue} - */ _queue, { + _classPrivateFieldInit(this, _queue, { writable: true, value: new _utils.AsyncQueue() }); @@ -216,7 +227,6 @@ class RequestHandler { } } exports.RequestHandler = RequestHandler; -var _queue = new WeakMap(); /** * Bulk fetch headers from a node-fetch response. * @param {Response} res The request response. diff --git a/crates/swc/tests/fixture/issue-1742/case1/output/index.js b/crates/swc/tests/fixture/issue-1742/case1/output/index.js index 2b5727c36a8..b3adb1e528e 100644 --- a/crates/swc/tests/fixture/issue-1742/case1/output/index.js +++ b/crates/swc/tests/fixture/issue-1742/case1/output/index.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -56,7 +66,7 @@ function _templateObject1() { }; return data; } -var _tag = new WeakSet(); +var _tag = new WeakSet(), _tag2 = new WeakMap(); var Foo = function Foo() { "use strict"; _classCallCheck(this, Foo); @@ -70,7 +80,6 @@ var Foo = function Foo() { var receiver2 = _classPrivateFieldGet(this, _tag2).bind(this)(_templateObject1()); console.log(receiver2 === this); }; -var _tag2 = new WeakMap(); function tag() { return this; } diff --git a/crates/swc/tests/tsc-references/classStaticBlock11_es2015.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock11_es2015.1.normal.js index c15259d12fe..7a3f9fc26f3 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock11_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock11_es2015.1.normal.js @@ -3,29 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } // @target: esnext, es2022, es2015 let getX; +var _x = new WeakMap(); class C { constructor(x){ _classPrivateFieldInit(this, _x, { @@ -35,7 +50,6 @@ class C { _classPrivateFieldSet(this, _x, x); } } -var _x = new WeakMap(); var __ = { writable: true, value: (()=>{ diff --git a/crates/swc/tests/tsc-references/classStaticBlock11_es5.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock11_es5.1.normal.js index 6bd122a1f9f..3c6a83ab698 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock11_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock11_es5.1.normal.js @@ -3,34 +3,49 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } // @target: esnext, es2022, es2015 var getX; +var _x = new WeakMap(); var C = function C(x) { "use strict"; _classCallCheck(this, C); @@ -40,7 +55,6 @@ var C = function C(x) { }); _classPrivateFieldSet(this, _x, x); }; -var _x = new WeakMap(); var __ = { writable: true, value: function() { diff --git a/crates/swc/tests/tsc-references/classStaticBlock11_es5.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock11_es5.2.minified.js index 126f1c988b2..81aec4648ae 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock11_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock11_es5.2.minified.js @@ -1,6 +1,6 @@ -var C = function(x) { +var _x = new WeakMap(), C = function(x) { "use strict"; - var obj, privateMap, value; + var obj, privateMap, value, receiver, privateMap, value, descriptor; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, C), obj = this, value = { @@ -8,10 +8,14 @@ var C = function(x) { value: 1 }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateMap = _x), privateMap.set(obj, value), (function(receiver, privateMap, value) { + })(obj, privateMap = _x), privateMap.set(obj, value), receiver = this, privateMap = _x, value = x, descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(this, _x, x); -}, _x = new WeakMap(); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); +}; diff --git a/crates/swc/tests/tsc-references/classStaticBlock12_es2015.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock12_es2015.1.normal.js index 4f7d23f468e..9148d8fbfea 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock12_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock12_es2015.1.normal.js @@ -1,8 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } // @useDefineForClassFields: false // @target: es2015 diff --git a/crates/swc/tests/tsc-references/classStaticBlock12_es2015.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock12_es2015.2.minified.js index 3bd02377666..09771c20fb9 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock12_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock12_es2015.2.minified.js @@ -1,9 +1,14 @@ class C { } -!function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; -}(C, C, { +var _x = { writable: !0, value: 1 -}); +}; +(()=>{ + var receiver, classConstructor, descriptor, receiver, descriptor; + receiver = C, classConstructor = C, descriptor = _x, (function(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + })(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; +})(); diff --git a/crates/swc/tests/tsc-references/classStaticBlock12_es5.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock12_es5.1.normal.js index 67886ca541e..9eba2ae35d3 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock12_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock12_es5.1.normal.js @@ -1,13 +1,28 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } var C = function C() { "use strict"; diff --git a/crates/swc/tests/tsc-references/classStaticBlock12_es5.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock12_es5.2.minified.js index d6070f3cf6e..ac58cfccc51 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock12_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock12_es5.2.minified.js @@ -3,11 +3,15 @@ var C = function() { !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, C); -}; -!function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; -}(C, C, { +}, _x = { writable: !0, value: 1 -}); +}; +!function() { + var receiver, classConstructor, descriptor, receiver, descriptor; + receiver = C, classConstructor = C, descriptor = _x, (function(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + })(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; +}(); diff --git a/crates/swc/tests/tsc-references/classStaticBlock13_es2015.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock13_es2015.1.normal.js index 2bad1798d09..0ecb711c8ad 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock13_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock13_es2015.1.normal.js @@ -1,8 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } // @target: esnext, es2022, es2015 // @useDefineForClassFields: true diff --git a/crates/swc/tests/tsc-references/classStaticBlock13_es2015.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock13_es2015.2.minified.js index d711cd27504..f61d1594f8a 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock13_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock13_es2015.2.minified.js @@ -1,6 +1,11 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; } class C { foo() { diff --git a/crates/swc/tests/tsc-references/classStaticBlock13_es5.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock13_es5.1.normal.js index 966290d5418..09e2202591f 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock13_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock13_es5.1.normal.js @@ -1,13 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -23,6 +33,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} var C = // @target: esnext, es2022, es2015 // @useDefineForClassFields: true /*#__PURE__*/ function() { diff --git a/crates/swc/tests/tsc-references/classStaticBlock13_es5.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock13_es5.2.minified.js index 6d224e07225..af1ff8a4687 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock13_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock13_es5.2.minified.js @@ -1,6 +1,8 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -8,6 +10,9 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} var C = function() { "use strict"; var Constructor, protoProps, staticProps; diff --git a/crates/swc/tests/tsc-references/classStaticBlock17_es2015.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock17_es2015.1.normal.js index c82d076778b..88ef41fdd53 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock17_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock17_es2015.1.normal.js @@ -3,29 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } // @target: es2015 let friendA; +var _x = new WeakMap(); class A { getX() { return _classPrivateFieldGet(this, _x); @@ -38,7 +53,6 @@ class A { _classPrivateFieldSet(this, _x, v); } } -var _x = new WeakMap(); var __ = { writable: true, value: (()=>{ diff --git a/crates/swc/tests/tsc-references/classStaticBlock17_es2015.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock17_es2015.2.minified.js index 90a4f40514e..57b6ef962ea 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock17_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock17_es2015.2.minified.js @@ -1,12 +1,20 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } let friendA; var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/classStaticBlock17_es5.1.normal.js b/crates/swc/tests/tsc-references/classStaticBlock17_es5.1.normal.js index 2c564cd911a..ed5c09e5113 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock17_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/classStaticBlock17_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -45,6 +59,7 @@ function _createClass(Constructor, protoProps, staticProps) { } // @target: es2015 var friendA; +var _x = new WeakMap(); var A = /*#__PURE__*/ function() { "use strict"; function A(v) { @@ -65,7 +80,6 @@ var A = /*#__PURE__*/ function() { ]); return A; }(); -var _x = new WeakMap(); var __ = { writable: true, value: function() { diff --git a/crates/swc/tests/tsc-references/classStaticBlock17_es5.2.minified.js b/crates/swc/tests/tsc-references/classStaticBlock17_es5.2.minified.js index 1daf676b2d8..0c86e8fd504 100644 --- a/crates/swc/tests/tsc-references/classStaticBlock17_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/classStaticBlock17_es5.2.minified.js @@ -1,15 +1,23 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -17,7 +25,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var friendA, A = function() { +var friendA, _x = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A(v) { @@ -37,7 +45,7 @@ var friendA, A = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _x = new WeakMap(); +}(); friendA = { getX: function(obj) { return _classPrivateFieldGet(obj, _x); diff --git a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.1.normal.js b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.1.normal.js index 2c395ff17a6..d25152e0136 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.1.normal.js @@ -3,27 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _value = new WeakMap(); // @target: esnext // @lib: esnext // @declaration: true @@ -48,5 +63,4 @@ function _classPrivateFieldSet(receiver, privateMap, value) { _classPrivateFieldSet(this, _value, initialValue); } } -var _value = new WeakMap(); new Box(3).value = 3; diff --git a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.2.minified.js b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.2.minified.js index 9f61cfc65de..36bbab3309c 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es2015.2.minified.js @@ -1,16 +1,22 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } var _value = new WeakMap(); new class { get value() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _value); + var receiver, privateMap, descriptor, receiver, descriptor; + return receiver = this, (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _value, "get")).get ? descriptor.get.call(receiver) : descriptor.value; } set value(value) { _classPrivateFieldSet(this, _value, value); diff --git a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.1.normal.js b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.1.normal.js index 2bf33750ef5..0a55e6bcf5d 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _value = new WeakMap(); var Box = // @target: esnext // @lib: esnext // @declaration: true @@ -75,5 +90,4 @@ var Box = // @target: esnext ]); return Box; }(); -var _value = new WeakMap(); new Box(3).value = 3; diff --git a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.2.minified.js b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.2.minified.js index 009b77492d8..47badba3846 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassTypeJsDoc_es5.2.minified.js @@ -1,8 +1,16 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -10,7 +18,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var Box = function() { +var _value = new WeakMap(), Box = function() { "use strict"; var Constructor, protoProps, staticProps; function Box(initialValue) { @@ -28,15 +36,13 @@ var Box = function() { { key: "value", get: function() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _value); + var receiver, privateMap, descriptor, receiver, descriptor; + return receiver = this, (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _value, "get")).get ? descriptor.get.call(receiver) : descriptor.value; }, set: function(value) { _classPrivateFieldSet(this, _value, value); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Box; -}(), _value = new WeakMap(); +}(); new Box(3).value = 3; diff --git a/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.1.normal.js b/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.1.normal.js index 176c511b899..e8f84541195 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.1.normal.js @@ -3,25 +3,39 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } // @target: esnext @@ -35,6 +49,7 @@ var Generic; } var c = new C(); c.y = c.y; + var _value = new WeakMap(); class Box { get value() { return _classPrivateFieldGet(this, _value); @@ -49,6 +64,5 @@ var Generic; }); } } - var _value = new WeakMap(); new Box().value = 3; })(Generic || (Generic = {})); diff --git a/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.2.minified.js b/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.2.minified.js index 77d71f40b86..ec43591a8f6 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassType_es2015.2.minified.js @@ -1,4 +1,8 @@ var Generic; +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} !function(Generic) { var c = new class { get y() { @@ -10,27 +14,27 @@ var Generic; var _value = new WeakMap(); new class { get value() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _value); + var receiver, privateMap, descriptor, receiver, descriptor; + return receiver = this, (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _value, "get")).get ? descriptor.get.call(receiver) : descriptor.value; } - set value(value1) { - !function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _value, value1); + set value(value) { + var receiver, privateMap, value1, descriptor; + receiver = this, privateMap = _value, value1 = value, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"), (function(receiver, descriptor, value1) { + if (descriptor.set) descriptor.set.call(receiver, value1); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value1; + } + })(receiver, descriptor, value1); } constructor(){ - var obj, privateMap, value; - obj = this, value = { + var obj, privateMap, value1; + obj = this, value1 = { writable: !0, value: void 0 }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateMap = _value), privateMap.set(obj, value); + })(obj, privateMap = _value), privateMap.set(obj, value1); } }().value = 3; }(Generic || (Generic = {})); diff --git a/crates/swc/tests/tsc-references/genericSetterInClassType_es5.1.normal.js b/crates/swc/tests/tsc-references/genericSetterInClassType_es5.1.normal.js index ad48c464f26..b66b4667715 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassType_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassType_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -64,6 +78,7 @@ var Generic; }(); var c = new C(); c.y = c.y; + var _value = new WeakMap(); var Box = /*#__PURE__*/ function() { "use strict"; function Box() { @@ -86,6 +101,5 @@ var Generic; ]); return Box; }(); - var _value = new WeakMap(); new Box().value = 3; })(Generic || (Generic = {})); diff --git a/crates/swc/tests/tsc-references/genericSetterInClassType_es5.2.minified.js b/crates/swc/tests/tsc-references/genericSetterInClassType_es5.2.minified.js index f0390be5cef..89b92011da4 100644 --- a/crates/swc/tests/tsc-references/genericSetterInClassType_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/genericSetterInClassType_es5.2.minified.js @@ -2,6 +2,10 @@ var Generic; function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; @@ -28,36 +32,36 @@ function _createClass(Constructor, protoProps, staticProps) { ]), C; }(), c = new C(); c.y = c.y; - var Box = function() { + var _value = new WeakMap(), Box = function() { "use strict"; function Box() { - var obj, privateMap, value; - _classCallCheck(this, Box), obj = this, value = { + var obj, privateMap, value1; + _classCallCheck(this, Box), obj = this, value1 = { writable: !0, value: void 0 }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateMap = _value), privateMap.set(obj, value); + })(obj, privateMap = _value), privateMap.set(obj, value1); } return _createClass(Box, [ { key: "value", get: function() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _value); + var receiver, privateMap, descriptor, receiver, descriptor; + return receiver = this, (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _value, "get")).get ? descriptor.get.call(receiver) : descriptor.value; }, - set: function(value1) { - !function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _value, value1); + set: function(value) { + var receiver, privateMap, value1, descriptor; + receiver = this, privateMap = _value, value1 = value, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"), (function(receiver, descriptor, value1) { + if (descriptor.set) descriptor.set.call(receiver, value1); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value1; + } + })(receiver, descriptor, value1); } } ]), Box; - }(), _value = new WeakMap(); + }(); new Box().value = 3; }(Generic || (Generic = {})); diff --git a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.1.normal.js b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.1.normal.js index f62c0c14b75..d2fca2d1a77 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -17,7 +27,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _calcHello = new WeakSet(), _screamingHello = new WeakSet(), _screamingHello = new WeakSet(); +var _hello = new WeakMap(), _world = new WeakMap(), _calcHello = new WeakSet(), _screamingHello = new WeakMap(); // @target: esnext // @allowJS: true // @declaration: true @@ -37,18 +47,18 @@ export class C { value: 100 }); _classPrivateMethodInit(this, _calcHello); - _classPrivateMethodInit(this, _screamingHello); - /** @param value {string} */ _classPrivateMethodInit(this, _screamingHello); + _classPrivateFieldInit(this, _screamingHello, { + get: get_screamingHello, + set: /** @param value {string} */ set_screamingHello + }); } } -var _hello = new WeakMap(); -var _world = new WeakMap(); function calcHello() { return _classPrivateFieldGet(this, _hello); } -function screamingHello() { +function get_screamingHello() { return _classPrivateFieldGet(this, _hello).toUpperCase(); } -function screamingHello(value) { +function set_screamingHello(value) { throw "NO"; } diff --git a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.2.minified.js index c3addf61f62..4329a1c1094 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es2015.2.minified.js @@ -1,19 +1,20 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } +function _classPrivateFieldGet(receiver, privateMap) { + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); -} -var _calcHello = new WeakSet(), _screamingHello = new WeakSet(), _screamingHello = new WeakSet(); +var _hello = new WeakMap(), _world = new WeakMap(), _calcHello = new WeakSet(), _screamingHello = new WeakMap(); export class C { getWorld() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _world); + return _classPrivateFieldGet(this, _world); } constructor(){ _classPrivateFieldInit(this, _hello, { @@ -22,7 +23,15 @@ export class C { }), _classPrivateFieldInit(this, _world, { writable: !0, value: 100 - }), _classPrivateMethodInit(this, _calcHello), _classPrivateMethodInit(this, _screamingHello), _classPrivateMethodInit(this, _screamingHello); + }), (function(obj, privateSet) { + _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); + })(this, _calcHello), _classPrivateFieldInit(this, _screamingHello, { + get: function() { + return _classPrivateFieldGet(this, _hello).toUpperCase(); + }, + set: function(value) { + throw "NO"; + } + }); } } -var _hello = new WeakMap(), _world = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.1.normal.js b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.1.normal.js index c3f6bdc0176..f7ddef12baa 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -36,7 +46,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } -var _calcHello = new WeakSet(), _screamingHello = new WeakSet(), _screamingHello = new WeakSet(); +var _hello = new WeakMap(), _world = new WeakMap(), _calcHello = new WeakSet(), _screamingHello = new WeakMap(); // @target: esnext // @allowJS: true // @declaration: true @@ -55,8 +65,10 @@ export var C = /*#__PURE__*/ function() { value: 100 }); _classPrivateMethodInit(this, _calcHello); - _classPrivateMethodInit(this, _screamingHello); - /** @param value {string} */ _classPrivateMethodInit(this, _screamingHello); + _classPrivateFieldInit(this, _screamingHello, { + get: get_screamingHello, + set: /** @param value {string} */ set_screamingHello + }); } _createClass(C, [ { @@ -68,14 +80,12 @@ export var C = /*#__PURE__*/ function() { ]); return C; }(); -var _hello = new WeakMap(); -var _world = new WeakMap(); function calcHello() { return _classPrivateFieldGet(this, _hello); } -function screamingHello() { +function get_screamingHello() { return _classPrivateFieldGet(this, _hello).toUpperCase(); } -function screamingHello(value) { +function set_screamingHello(value) { throw "NO"; } diff --git a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.2.minified.js b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.2.minified.js index b46f16eab95..bba6f62efca 100644 --- a/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/jsDeclarationsPrivateFields01_es5.2.minified.js @@ -1,23 +1,28 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } +function _classPrivateFieldGet(receiver, privateMap) { + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); -} function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var _calcHello = new WeakSet(), _screamingHello = new WeakSet(), _screamingHello = new WeakSet(); +var _hello = new WeakMap(), _world = new WeakMap(), _calcHello = new WeakSet(), _screamingHello = new WeakMap(); export var C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { + var obj, privateSet; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, C), _classPrivateFieldInit(this, _hello, { @@ -26,18 +31,23 @@ export var C = function() { }), _classPrivateFieldInit(this, _world, { writable: !0, value: 100 - }), _classPrivateMethodInit(this, _calcHello), _classPrivateMethodInit(this, _screamingHello), _classPrivateMethodInit(this, _screamingHello); + }), obj = this, _checkPrivateRedeclaration(obj, privateSet = _calcHello), privateSet.add(obj), _classPrivateFieldInit(this, _screamingHello, { + get: get_screamingHello, + set: set_screamingHello + }); } return Constructor = C, protoProps = [ { key: "getWorld", value: function() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _world); + return _classPrivateFieldGet(this, _world); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; }(); -var _hello = new WeakMap(), _world = new WeakMap(); +function get_screamingHello() { + return _classPrivateFieldGet(this, _hello).toUpperCase(); +} +function set_screamingHello(value) { + throw "NO"; +} diff --git a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.1.normal.js index 3278594f9c3..71bf7e9b00e 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.1.normal.js @@ -3,17 +3,17 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -var _fieldFunc = new WeakSet(), _fieldFunc2 = new WeakSet(); +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); // @target: es2015 class A { test() { @@ -35,16 +35,22 @@ class A { return new A(); } constructor(){ - _classPrivateMethodInit(this, _fieldFunc); - _classPrivateMethodInit(this, _fieldFunc2); + _classPrivateFieldInit(this, _fieldFunc, { + get: get_fieldFunc, + set: void 0 + }); + _classPrivateFieldInit(this, _fieldFunc2, { + get: get_fieldFunc2, + set: void 0 + }); this.x = 1; } } -function fieldFunc() { +function get_fieldFunc() { return function() { this.x = 10; }; } -function fieldFunc2() { +function get_fieldFunc2() { return function(a, ...b) {}; } diff --git a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.2.minified.js index 3e33ca068a9..309f3d08132 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es2015.2.minified.js @@ -1,13 +1,13 @@ +function _classPrivateFieldInit(obj, privateMap, value) { + !function(obj, privateCollection) { + if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + }(obj, privateMap), privateMap.set(obj, value); +} function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - !function(obj, privateCollection) { - if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); -} -var _fieldFunc = new WeakSet(), _fieldFunc2 = new WeakSet(); +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); class A { test() { var _ref; @@ -24,14 +24,18 @@ class A { return new A(); } constructor(){ - _classPrivateMethodInit(this, _fieldFunc), _classPrivateMethodInit(this, _fieldFunc2), this.x = 1; + _classPrivateFieldInit(this, _fieldFunc, { + get: function() { + return function() { + this.x = 10; + }; + }, + set: void 0 + }), _classPrivateFieldInit(this, _fieldFunc2, { + get: function() { + return function(a, ...b) {}; + }, + set: void 0 + }), this.x = 1; } } -function fieldFunc() { - return function() { - this.x = 10; - }; -} -function fieldFunc2() { - return function(a, ...b) {}; -} diff --git a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.1.normal.js index 8c1bbf275fc..a4bda95b29c 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.1.normal.js @@ -16,16 +16,16 @@ function _classCallCheck(instance, Constructor) { throw new TypeError("Cannot call a class as a function"); } } +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; @@ -124,14 +124,20 @@ function _templateObject1() { }; return data; } -var _fieldFunc = new WeakSet(), _fieldFunc2 = new WeakSet(); +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); var A = // @target: es2015 /*#__PURE__*/ function() { "use strict"; function A() { _classCallCheck(this, A); - _classPrivateMethodInit(this, _fieldFunc); - _classPrivateMethodInit(this, _fieldFunc2); + _classPrivateFieldInit(this, _fieldFunc, { + get: get_fieldFunc, + set: void 0 + }); + _classPrivateFieldInit(this, _fieldFunc2, { + get: get_fieldFunc2, + set: void 0 + }); this.x = 1; } _createClass(A, [ @@ -172,12 +178,12 @@ var A = // @target: es2015 ]); return A; }(); -function fieldFunc() { +function get_fieldFunc() { return function() { this.x = 10; }; } -function fieldFunc2() { +function get_fieldFunc2() { return function(a) { for(var _len = arguments.length, b = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){ b[_key - 1] = arguments[_key]; diff --git a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.2.minified.js index 97a6ab61d5e..c535bb23aab 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAccessorsCallExpression_es5.2.minified.js @@ -3,15 +3,15 @@ function _arrayLikeToArray(arr, len) { for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } +function _classPrivateFieldInit(obj, privateMap, value) { + !function(obj, privateCollection) { + if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + }(obj, privateMap), privateMap.set(obj, value); +} function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - !function(obj, privateCollection) { - if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); -} function _construct(Parent, args, Class) { return (_construct = !function() { if ("undefined" == typeof Reflect || !Reflect.construct) return !1; @@ -86,13 +86,19 @@ function _templateObject1() { return data; }, data; } -var _fieldFunc = new WeakSet(), _fieldFunc2 = new WeakSet(), A = function() { +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A() { !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, A), _classPrivateMethodInit(this, _fieldFunc), _classPrivateMethodInit(this, _fieldFunc2), this.x = 1; + }(this, A), _classPrivateFieldInit(this, _fieldFunc, { + get: get_fieldFunc, + set: void 0 + }), _classPrivateFieldInit(this, _fieldFunc2, { + get: get_fieldFunc2, + set: void 0 + }), this.x = 1; } return Constructor = A, protoProps = [ { @@ -123,12 +129,12 @@ var _fieldFunc = new WeakSet(), _fieldFunc2 = new WeakSet(), A = function() { } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; }(); -function fieldFunc() { +function get_fieldFunc() { return function() { this.x = 10; }; } -function fieldFunc2() { +function get_fieldFunc2() { return function(a) { for(var _len = arguments.length, b = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++)b[_key - 1] = arguments[_key]; }; diff --git a/crates/swc/tests/tsc-references/privateNameAccessors_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameAccessors_es2015.1.normal.js index cde49592601..af7c2ddf06d 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessors_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAccessors_es2015.1.normal.js @@ -3,15 +3,29 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -20,28 +34,29 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -var _prop = new WeakSet(), _prop = new WeakSet(), _roProp = new WeakSet(); +var _prop = new WeakMap(), _roProp = new WeakMap(); // @strict: true // @target: es6 class A1 { constructor(name){ - _classPrivateMethodInit(this, _prop); - _classPrivateMethodInit(this, _prop); - _classPrivateMethodInit(this, _roProp); + _classPrivateFieldInit(this, _prop, { + get: get_prop, + set: set_prop + }); + _classPrivateFieldInit(this, _roProp, { + get: get_roProp, + set: void 0 + }); _classPrivateFieldSet(this, _prop, ""); _classPrivateFieldSet(this, _roProp, ""); // Error console.log(_classPrivateMethodGet(this, _prop, prop)); console.log(_classPrivateMethodGet(this, _roProp, roProp)); } } -function prop() { +function get_prop() { return ""; } -function prop(param) {} -function roProp() { +function set_prop(param) {} +function get_roProp() { return ""; } diff --git a/crates/swc/tests/tsc-references/privateNameAccessors_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameAccessors_es2015.2.minified.js index 4a1c8a17be0..ed16f482a76 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessors_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAccessors_es2015.2.minified.js @@ -1 +1 @@ -new WeakSet(), new WeakSet(), new WeakSet(); +new WeakMap(), new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameAccessors_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameAccessors_es5.1.normal.js index 65ed2a73805..38cb3c4fb98 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessors_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAccessors_es5.1.normal.js @@ -3,20 +3,34 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -25,26 +39,27 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -var _prop = new WeakSet(), _prop = new WeakSet(), _roProp = new WeakSet(); +var _prop = new WeakMap(), _roProp = new WeakMap(); var A1 = function A1(name) { "use strict"; _classCallCheck(this, A1); - _classPrivateMethodInit(this, _prop); - _classPrivateMethodInit(this, _prop); - _classPrivateMethodInit(this, _roProp); + _classPrivateFieldInit(this, _prop, { + get: get_prop, + set: set_prop + }); + _classPrivateFieldInit(this, _roProp, { + get: get_roProp, + set: void 0 + }); _classPrivateFieldSet(this, _prop, ""); _classPrivateFieldSet(this, _roProp, ""); // Error console.log(_classPrivateMethodGet(this, _prop, prop)); console.log(_classPrivateMethodGet(this, _roProp, roProp)); }; -function prop() { +function get_prop() { return ""; } -function prop(param) {} -function roProp() { +function set_prop(param) {} +function get_roProp() { return ""; } diff --git a/crates/swc/tests/tsc-references/privateNameAccessors_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameAccessors_es5.2.minified.js index 57f61ca64af..876b8987631 100644 --- a/crates/swc/tests/tsc-references/privateNameAccessors_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAccessors_es5.2.minified.js @@ -1,28 +1,41 @@ +function _classPrivateFieldInit(obj, privateMap, value) { + !function(obj, privateCollection) { + if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + }(obj, privateMap), privateMap.set(obj, value); +} function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - !function(obj, privateCollection) { - if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); -} -var _prop = new WeakSet(), _prop = new WeakSet(), _roProp = new WeakSet(), A1 = function(name) { +var _prop = new WeakMap(), _roProp = new WeakMap(), A1 = function(name) { "use strict"; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, A1), _classPrivateMethodInit(this, _prop), _classPrivateMethodInit(this, _prop), _classPrivateMethodInit(this, _roProp), _classPrivateFieldSet(this, _prop, ""), _classPrivateFieldSet(this, _roProp, ""), console.log(_classPrivateMethodGet(this, _prop, prop)), console.log(_classPrivateMethodGet(this, _roProp, roProp)); + }(this, A1), _classPrivateFieldInit(this, _prop, { + get: get_prop, + set: set_prop + }), _classPrivateFieldInit(this, _roProp, { + get: get_roProp, + set: void 0 + }), _classPrivateFieldSet(this, _prop, ""), _classPrivateFieldSet(this, _roProp, ""), console.log(_classPrivateMethodGet(this, _prop, prop)), console.log(_classPrivateMethodGet(this, _roProp, roProp)); }; -function prop() { +function get_prop() { return ""; } -function prop(param) {} -function roProp() { +function set_prop(param) {} +function get_roProp() { return ""; } diff --git a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es2015.1.normal.js index d39ffc08e86..5ad93c71037 100644 --- a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _prop = new WeakMap(); class B { constructor(){ _classPrivateFieldInit(this, _prop, { @@ -15,4 +16,3 @@ class B { }); } } -var _prop = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.1.normal.js index 064d3e756f3..b5b20edd422 100644 --- a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _prop = new WeakMap(); var B = function B() { "use strict"; _classCallCheck(this, B); @@ -20,4 +21,3 @@ var B = function B() { value: void 0 }); }; -var _prop = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.2.minified.js index 8310a73919e..23d0ac5c366 100644 --- a/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAmbientNoImplicitAny_es5.2.minified.js @@ -1,4 +1,4 @@ -var B = function() { +var _prop = new WeakMap(), B = function() { "use strict"; var obj, privateMap, value; !function(instance, Constructor) { @@ -9,4 +9,4 @@ var B = function() { }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); })(obj, privateMap = _prop), privateMap.set(obj, value); -}, _prop = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.1.normal.js index caeae0996c6..aa02c19e317 100644 --- a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.1.normal.js @@ -3,21 +3,35 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperty(obj, key, value) { if (key in obj) { @@ -61,6 +75,12 @@ function _objectSpread(target) { } return target; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} +var _prop = new WeakMap(); // @strict: true // @target: es6 class C { @@ -81,7 +101,6 @@ class C { }); } } -var _prop = new WeakMap(); var _propStatic = { writable: true, value: 1 diff --git a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.2.minified.js index e0cdc4c449d..92331ae6b1e 100644 --- a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es2015.2.minified.js @@ -1,10 +1,17 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + }(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } function _defineProperty(obj, key, value) { return key in obj ? Object.defineProperty(obj, key, { @@ -34,6 +41,10 @@ function _objectSpread(target) { } return target; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} +var _prop = new WeakMap(); class C { method(other) { const obj = _objectSpread({}, other); @@ -56,7 +67,7 @@ class C { }); } } -var _prop = new WeakMap(), _propStatic = { +var _propStatic = { writable: !0, value: 1 }; diff --git a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.1.normal.js index 62ee8ffbbb4..f9c4f6db63a 100644 --- a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.1.normal.js @@ -3,26 +3,40 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return privateMap.get(receiver).value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -80,6 +94,12 @@ function _objectSpread(target) { } return target; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} +var _prop = new WeakMap(); var C = // @strict: true // @target: es6 /*#__PURE__*/ function() { @@ -108,7 +128,6 @@ var C = // @strict: true ]); return C; }(); -var _prop = new WeakMap(); var _propStatic = { writable: true, value: 1 diff --git a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.2.minified.js index 33ea063783a..40b1518b568 100644 --- a/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAndObjectRestSpread_es5.2.minified.js @@ -1,10 +1,17 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + }(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -40,7 +47,10 @@ function _objectSpread(target) { } return target; } -var C = function() { +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} +var _prop = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -62,7 +72,7 @@ var C = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; -}(), _prop = new WeakMap(), _propStatic = { +}(), _propStatic = { writable: !0, value: 1 }; diff --git a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.1.normal.js index 3db46a73331..a696bec081c 100644 --- a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(), _prop = new WeakMap(); // @target: esnext, es2022, es2015 class A { constructor(){ @@ -20,6 +21,4 @@ class A { }); } } -var _foo = new WeakMap(); -var _prop = new WeakMap(); A.inst = new A(); diff --git a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.2.minified.js index 2d088d317dc..ce071773bda 100644 --- a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es2015.2.minified.js @@ -3,6 +3,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } +var _foo = new WeakMap(), _prop = new WeakMap(); class A { constructor(){ _classPrivateFieldInit(this, _foo, { @@ -14,5 +15,4 @@ class A { }); } } -var _foo = new WeakMap(), _prop = new WeakMap(); A.inst = new A(); diff --git a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.1.normal.js index 4c65ab4e949..def66c19038 100644 --- a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(), _prop = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -24,6 +25,4 @@ var A = function A() { value: 2 }); }; -var _foo = new WeakMap(); -var _prop = new WeakMap(); A.inst = new A(); diff --git a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.2.minified.js index 3041f3850a1..9f57d20768f 100644 --- a/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameAndStaticInitializer_es5.2.minified.js @@ -3,7 +3,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var A = function() { +var _foo = new WeakMap(), _prop = new WeakMap(), A = function() { "use strict"; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); @@ -14,5 +14,5 @@ var A = function() { writable: !0, value: 2 }); -}, _foo = new WeakMap(), _prop = new WeakMap(); +}; A.inst = new A(); diff --git a/crates/swc/tests/tsc-references/privateNameCircularReference_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameCircularReference_es2015.1.normal.js index 9ef0fee1494..c52bbe0f521 100644 --- a/crates/swc/tests/tsc-references/privateNameCircularReference_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameCircularReference_es2015.1.normal.js @@ -3,17 +3,28 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } var _key; +var _foo = new WeakMap(), _bar = new WeakMap(); // @strict: true // @target: es6 class A { @@ -30,6 +41,4 @@ class A { = this["#baz"]; } } -var _foo = new WeakMap(); -var _bar = new WeakMap(); _key = "#baz"; diff --git a/crates/swc/tests/tsc-references/privateNameCircularReference_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameCircularReference_es5.1.normal.js index 68d823246d0..a48b310c78f 100644 --- a/crates/swc/tests/tsc-references/privateNameCircularReference_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameCircularReference_es5.1.normal.js @@ -3,22 +3,33 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } var _key; +var _foo = new WeakMap(), _bar = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -35,6 +46,4 @@ var A = function A() { this[_key] // Error (should *not* be private name error) = this["#baz"]; }; -var _foo = new WeakMap(); -var _bar = new WeakMap(); _key = "#baz"; diff --git a/crates/swc/tests/tsc-references/privateNameCircularReference_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameCircularReference_es5.2.minified.js index 09d85386936..9e8c257a4e9 100644 --- a/crates/swc/tests/tsc-references/privateNameCircularReference_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameCircularReference_es5.2.minified.js @@ -1,13 +1,16 @@ function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var _key, A = function() { +var _key, _foo = new WeakMap(), _bar = new WeakMap(), A = function() { "use strict"; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); @@ -18,5 +21,5 @@ var _key, A = function() { writable: !0, value: _classPrivateFieldGet(this, _foo) }), this[_key] = this["#baz"]; -}, _foo = new WeakMap(), _bar = new WeakMap(); +}; _key = "#baz"; diff --git a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.1.normal.js index 3f06cbada13..444e6512900 100644 --- a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.1.normal.js @@ -15,7 +15,7 @@ function _classPrivateMethodInit(obj, privateSet) { const array = []; for(let i = 0; i < 10; ++i){ array.push(function() { - var _method = new WeakSet(), _accessor = new WeakSet(), _accessor = new WeakSet(); + var _myField = new WeakMap(), _method = new WeakSet(), _accessor = new WeakMap(); class C { constructor(){ _classPrivateFieldInit(this, _myField, { @@ -23,16 +23,17 @@ for(let i = 0; i < 10; ++i){ value: "hello" }); _classPrivateMethodInit(this, _method); - _classPrivateMethodInit(this, _accessor); - _classPrivateMethodInit(this, _accessor); + _classPrivateFieldInit(this, _accessor, { + get: get_accessor, + set: set_accessor + }); } } - var _myField = new WeakMap(); function method() {} - function accessor() { + function get_accessor() { return 42; } - function accessor(val) {} + function set_accessor(val) {} return C; }()); } diff --git a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.2.minified.js index 53c014d6108..aee81cfb2ed 100644 --- a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es2015.2.minified.js @@ -9,15 +9,20 @@ function _classPrivateMethodInit(obj, privateSet) { } const array = []; for(let i = 0; i < 10; ++i)array.push(function() { - var _method = new WeakSet(), _accessor = new WeakSet(), _accessor = new WeakSet(); + var _myField = new WeakMap(), _method = new WeakSet(), _accessor = new WeakMap(); class C { constructor(){ _classPrivateFieldInit(this, _myField, { writable: !0, value: "hello" - }), _classPrivateMethodInit(this, _method), _classPrivateMethodInit(this, _accessor), _classPrivateMethodInit(this, _accessor); + }), _classPrivateMethodInit(this, _method), _classPrivateFieldInit(this, _accessor, { + get: function() { + return 42; + }, + set: set_accessor + }); } } - var _myField = new WeakMap(); + function set_accessor(val) {} return C; }()); diff --git a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.1.normal.js index 467efe9a95b..50742b99172 100644 --- a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.1.normal.js @@ -21,11 +21,11 @@ var array = []; for(var i = 0; i < 10; ++i){ array.push(function() { var method = function method() {}; - var accessor = function accessor() { + var get_accessor = function get_accessor() { return 42; }; - var accessor = function accessor(val) {}; - var _method = new WeakSet(), _accessor = new WeakSet(), _accessor = new WeakSet(); + var set_accessor = function set_accessor(val) {}; + var _myField = new WeakMap(), _method = new WeakSet(), _accessor = new WeakMap(); var C = function C() { "use strict"; _classCallCheck(this, C); @@ -34,10 +34,11 @@ for(var i = 0; i < 10; ++i){ value: "hello" }); _classPrivateMethodInit(this, _method); - _classPrivateMethodInit(this, _accessor); - _classPrivateMethodInit(this, _accessor); + _classPrivateFieldInit(this, _accessor, { + get: get_accessor, + set: set_accessor + }); }; - var _myField = new WeakMap(); return C; }()); } diff --git a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.2.minified.js index 8f75bba79a6..c051477ccbf 100644 --- a/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameClassExpressionLoop_es5.2.minified.js @@ -11,12 +11,17 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); } for(var array = [], i = 0; i < 10; ++i)array.push(function() { - var _method = new WeakSet(), _accessor = new WeakSet(), _accessor = new WeakSet(), C = function() { + var get_accessor = function() { + return 42; + }, set_accessor = function(val) {}, _myField = new WeakMap(), _method = new WeakSet(), _accessor = new WeakMap(), C = function() { "use strict"; _classCallCheck(this, C), _classPrivateFieldInit(this, _myField, { writable: !0, value: "hello" - }), _classPrivateMethodInit(this, _method), _classPrivateMethodInit(this, _accessor), _classPrivateMethodInit(this, _accessor); - }, _myField = new WeakMap(); + }), _classPrivateMethodInit(this, _method), _classPrivateFieldInit(this, _accessor, { + get: get_accessor, + set: set_accessor + }); + }; return C; }()); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.1.normal.js index 4a8d72509fb..b1b7883d03c 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.1.normal.js @@ -3,27 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap(); // @target: esnext, es2022, es2015 class A { test() { @@ -68,9 +83,4 @@ class A { _classPrivateFieldSet(this, _d, 'd'); } } -var _a = new WeakMap(); -var _b = new WeakMap(); -var _c = new WeakMap(); -var _d = new WeakMap(); -var _e = new WeakMap(); new A().test(); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.2.minified.js index 90ed4f8358b..7908482a3c7 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es2015.2.minified.js @@ -1,6 +1,10 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -8,10 +12,14 @@ function _classPrivateFieldInit(obj, privateMap, value) { }(obj, privateMap), privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } var _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap(); new class { diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.1.normal.js index c8b2a912de1..a5550af6772 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -56,6 +70,7 @@ function _defineProperty(obj, key, value) { } return obj; } +var _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap(); var A = // @target: esnext, es2022, es2015 /*#__PURE__*/ function() { "use strict"; @@ -107,9 +122,4 @@ var A = // @target: esnext, es2022, es2015 ]); return A; }(); -var _a = new WeakMap(); -var _b = new WeakMap(); -var _c = new WeakMap(); -var _d = new WeakMap(); -var _e = new WeakMap(); new A().test(); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.2.minified.js index 872bc7240e9..abd36f91536 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName1_es5.2.minified.js @@ -1,6 +1,10 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -8,10 +12,14 @@ function _classPrivateFieldInit(obj, privateMap, value) { }(obj, privateMap), privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -19,7 +27,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var A = function() { +var _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A() { @@ -65,5 +73,5 @@ var A = function() { data[_classPrivateFieldGet(this, _e)], console.log(a1, b1, c1, d1); }) ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _d = new WeakMap(), _e = new WeakMap(); +}(); new A().test(); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.1.normal.js index 18f4de3fb32..1a87b539403 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -15,7 +25,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { } // @target: esnext, es2022, es2015 let getX; -var tmp = (getX = (a)=>_classPrivateFieldGet(a, _x) +var _x = new WeakMap(), tmp = (getX = (a)=>_classPrivateFieldGet(a, _x) , "_"); class A { [tmp]() {} @@ -26,5 +36,4 @@ class A { }); } } -var _x = new WeakMap(); console.log(getX(new A)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.2.minified.js index 195786112ed..b30102fa625 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es2015.2.minified.js @@ -1,9 +1,12 @@ let getX; -var tmp = (getX = (a)=>(function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; +var _x = new WeakMap(), tmp = (getX = (a)=>(function(receiver, privateMap) { + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; })(a, _x) -, "_"), _x = new WeakMap(); +, "_"); console.log(getX(new class { [tmp]() {} constructor(){ diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.1.normal.js index 611e1569994..99b62ee34bb 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -34,7 +44,7 @@ function _createClass(Constructor, protoProps, staticProps) { } // @target: esnext, es2022, es2015 var getX; -var tmp = (getX = function(a) { +var _x = new WeakMap(), tmp = (getX = function(a) { return _classPrivateFieldGet(a, _x); }, "_"); var A = /*#__PURE__*/ function() { @@ -54,5 +64,4 @@ var A = /*#__PURE__*/ function() { ]); return A; }(); -var _x = new WeakMap(); console.log(getX(new A)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.2.minified.js index a4fe4a2547e..3800b8db771 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName2_es5.2.minified.js @@ -4,11 +4,12 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var getX, tmp = (getX = function(a) { - return (function(receiver, privateMap) { +var getX, _x = new WeakMap(), tmp = (getX = function(a) { + var receiver, privateMap, descriptor, receiver, descriptor; + return (descriptor = descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(a, _x); + return privateMap.get(receiver); + })(receiver = a, privateMap = _x, "get")).get ? descriptor.get.call(receiver) : descriptor.value; }, "_"), A = function() { "use strict"; var Constructor, protoProps, staticProps; @@ -29,5 +30,5 @@ var getX, tmp = (getX = function(a) { value: function() {} } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _x = new WeakMap(); +}(); console.log(getX(new A)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.1.normal.js index dd7811882ed..49a6bab9b4a 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.1.normal.js @@ -3,32 +3,47 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _name = new WeakMap(); // @target: esnext, es2022, es2015 class Foo { getValue(x) { const obj = this; - var tmp = _classPrivateFieldGet(obj, _name); + var _y = new WeakMap(), tmp = _classPrivateFieldGet(obj, _name); class Bar { [tmp]() { return x + _classPrivateFieldGet(this, _y); @@ -40,7 +55,6 @@ class Foo { }); } } - var _y = new WeakMap(); return new Bar()[_classPrivateFieldGet(obj, _name)](); } constructor(name){ @@ -51,5 +65,4 @@ class Foo { _classPrivateFieldSet(this, _name, name); } } -var _name = new WeakMap(); console.log(new Foo("NAME").getValue(100)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.2.minified.js index f5d181ba8e9..6e485070750 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es2015.2.minified.js @@ -1,6 +1,10 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -10,7 +14,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { var _name = new WeakMap(); console.log(new class { getValue(x) { - var tmp = _classPrivateFieldGet(this, _name), _y = new WeakMap(); + var _y = new WeakMap(), tmp = _classPrivateFieldGet(this, _name); return new class { [tmp]() { return x + _classPrivateFieldGet(this, _y); @@ -28,10 +32,14 @@ console.log(new class { writable: !0, value: void 0 }), (function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); })(this, _name, name); } }("NAME").getValue(100)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.1.normal.js index a33319374c6..2e761f9513b 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _name = new WeakMap(); var Foo = // @target: esnext, es2022, es2015 /*#__PURE__*/ function() { "use strict"; @@ -59,7 +74,7 @@ var Foo = // @target: esnext, es2022, es2015 key: "getValue", value: function getValue(x) { var obj = this; - var tmp = _classPrivateFieldGet(obj, _name); + var _y = new WeakMap(), tmp = _classPrivateFieldGet(obj, _name); var Bar = /*#__PURE__*/ function() { function Bar() { _classCallCheck(this, Bar); @@ -78,12 +93,10 @@ var Foo = // @target: esnext, es2022, es2015 ]); return Bar; }(); - var _y = new WeakMap(); return new Bar()[_classPrivateFieldGet(obj, _name)](); } } ]); return Foo; }(); -var _name = new WeakMap(); console.log(new Foo("NAME").getValue(100)); diff --git a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.2.minified.js index 404b904878d..8a4a7961f92 100644 --- a/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameComputedPropertyName3_es5.2.minified.js @@ -1,9 +1,13 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -19,24 +23,26 @@ function _defineProperties(target, props) { function _createClass(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } -var Foo = function() { +var _name = new WeakMap(), Foo = function() { "use strict"; function Foo(name) { + var receiver, privateMap, value, descriptor; _classCallCheck(this, Foo), _classPrivateFieldInit(this, _name, { writable: !0, value: void 0 - }), (function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(this, _name, name); + }), receiver = this, privateMap = _name, value = name, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); } return _createClass(Foo, [ { key: "getValue", value: function(x) { - var tmp = _classPrivateFieldGet(this, _name), Bar = function() { + var _y = new WeakMap(), tmp = _classPrivateFieldGet(this, _name), Bar = function() { function Bar() { _classCallCheck(this, Bar), _classPrivateFieldInit(this, _y, { writable: !0, @@ -51,10 +57,10 @@ var Foo = function() { } } ]), Bar; - }(), _y = new WeakMap(); + }(); return new Bar()[_classPrivateFieldGet(this, _name)](); } } ]), Foo; -}(), _name = new WeakMap(); +}(); console.log(new Foo("NAME").getValue(100)); diff --git a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.1.normal.js index bc6b29a079b..573df1482fe 100644 --- a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.1.normal.js @@ -3,21 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _x = new WeakMap(); class C { static test() { _classPrivateFieldSet(new C(), _x, 10); @@ -32,4 +43,3 @@ class C { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.2.minified.js index 6b6e55efa71..264ad53b53d 100644 --- a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es2015.2.minified.js @@ -1,11 +1,17 @@ +var _x = new WeakMap(); class C { static test() { - !function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor; + value = 10, descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(new C(), _x, 10); + return privateMap.get(receiver); + })(receiver = new C(), privateMap = _x, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); const y = new C(), z = new y(); z.x = 123; } @@ -20,4 +26,3 @@ class C { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.1.normal.js index 0b2007f2531..8b6b1cf3ddd 100644 --- a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.1.normal.js @@ -3,24 +3,34 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -37,6 +47,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _x = new WeakMap(); var C = /*#__PURE__*/ function() { "use strict"; function C() { @@ -59,4 +70,3 @@ var C = /*#__PURE__*/ function() { ]); return C; }(); -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.2.minified.js index 63502270aeb..ba34ec3bdde 100644 --- a/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameConstructorSignature_es5.2.minified.js @@ -4,7 +4,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var C = function() { +var _x = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -22,13 +22,18 @@ var C = function() { { key: "test", value: function() { - (function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor; + value = 10, descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(new C(), _x, 10), new new C()().x = 123; + return privateMap.get(receiver); + })(receiver = new C(), privateMap = _x, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value), new new C()().x = 123; } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; -}(), _x = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.1.normal.js index 8a59f3a9965..269cd4550e8 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -16,6 +26,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { // @target: es6 class D { } +var _x = new WeakMap(); class C { foo() { const c = new C(); @@ -30,4 +41,3 @@ class C { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.2.minified.js index 90b2c933567..be386f97386 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es2015.2.minified.js @@ -1,7 +1,11 @@ function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } +var _x = new WeakMap(); class C { foo() { const c = new C(); @@ -20,4 +24,3 @@ class C { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.1.normal.js index 46c46540b64..f1c9a1706c6 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -36,6 +46,7 @@ var D = function D() { "use strict"; _classCallCheck(this, D); }; +var _x = new WeakMap(); var C = /*#__PURE__*/ function() { "use strict"; function C() { @@ -58,4 +69,3 @@ var C = /*#__PURE__*/ function() { ]); return C; }(); -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.2.minified.js index 95f5d1b93f6..d6d6e61fd0e 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameDeclarationMerging_es5.2.minified.js @@ -2,8 +2,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -14,7 +17,7 @@ function _defineProperties(target, props) { var D = function() { "use strict"; _classCallCheck(this, D); -}, C = function() { +}, _x = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -34,4 +37,4 @@ var D = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; -}(), _x = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclaration_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameDeclaration_es2015.1.normal.js index 880f6549ecd..3ab44f268c5 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclaration_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDeclaration_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(), _bar = new WeakMap(); // @declaration: true // @target: es2015 class A { @@ -23,5 +24,3 @@ class A { this.qux = 6; } } -var _foo = new WeakMap(); -var _bar = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclaration_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameDeclaration_es5.1.normal.js index 628237fd44a..b5dab62a8db 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclaration_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDeclaration_es5.1.normal.js @@ -26,6 +26,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _foo = new WeakMap(), _bar = new WeakMap(); var A = // @declaration: true // @target: es2015 /*#__PURE__*/ function() { @@ -50,5 +51,3 @@ var A = // @declaration: true ]); return A; }(); -var _foo = new WeakMap(); -var _bar = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameDeclaration_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameDeclaration_es5.2.minified.js index f0659dc67d0..97275efd249 100644 --- a/crates/swc/tests/tsc-references/privateNameDeclaration_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameDeclaration_es5.2.minified.js @@ -9,7 +9,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var A = function() { +var _foo = new WeakMap(), _bar = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A() { @@ -29,4 +29,4 @@ var A = function() { value: function() {} } ], _defineProperties((Constructor = A).prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _foo = new WeakMap(), _bar = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameDuplicateField_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameDuplicateField_es2015.1.normal.js index 06f7b52634f..1e9c780ab56 100644 --- a/crates/swc/tests/tsc-references/privateNameDuplicateField_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDuplicateField_es2015.1.normal.js @@ -14,6 +14,7 @@ function _classPrivateMethodInit(obj, privateSet) { // @strict: true // @target: es6 function Field() { + var _foo = new WeakMap(), _foo = new WeakMap(); // Error class A_Field_Field { constructor(){ @@ -27,9 +28,7 @@ function Field() { }); } } - var _foo = new WeakMap(); - var _foo = new WeakMap(); - var _foo1 = new WeakSet(); + var _foo1 = new WeakMap(), _foo1 = new WeakSet(); // Error class A_Field_Method { constructor(){ @@ -40,9 +39,8 @@ function Field() { _classPrivateMethodInit(this, _foo1); } } - var _foo1 = new WeakMap(); function foo() {} - var _foo2 = new WeakSet(); + var _foo2 = new WeakMap(), _foo2 = new WeakMap(); // Error class A_Field_Getter { constructor(){ @@ -50,14 +48,16 @@ function Field() { writable: true, value: "foo" }); - _classPrivateMethodInit(this, _foo2); + _classPrivateFieldInit(this, _foo2, { + get: get_foo, + set: void 0 + }); } } - var _foo2 = new WeakMap(); - function foo1() { + function get_foo() { return ""; } - var _foo3 = new WeakSet(); + var _foo3 = new WeakMap(), _foo3 = new WeakMap(); // Error class A_Field_Setter { constructor(){ @@ -65,11 +65,14 @@ function Field() { writable: true, value: "foo" }); - _classPrivateMethodInit(this, _foo3); + _classPrivateFieldInit(this, _foo3, { + get: void 0, + set: set_foo + }); } } - var _foo3 = new WeakMap(); - function foo2(value) {} + function set_foo(value) {} + var _foo4 = new WeakMap(); // Error class A_Field_StaticField { constructor(){ @@ -79,11 +82,11 @@ function Field() { }); } } - var _foo4 = new WeakMap(); var _foo4 = { writable: true, value: "foo" }; + var _foo5 = new WeakMap(); // Error class A_Field_StaticMethod { constructor(){ @@ -93,8 +96,8 @@ function Field() { }); } } - var _foo5 = new WeakMap(); - function foo3() {} + function foo1() {} + var _foo6 = new WeakMap(); // Error class A_Field_StaticGetter { constructor(){ @@ -104,10 +107,14 @@ function Field() { }); } } - var _foo6 = new WeakMap(); - function foo4() { + var _foo6 = { + get: get_foo1, + set: void 0 + }; + function get_foo1() { return ""; } + var _foo7 = new WeakMap(); // Error class A_Field_StaticSetter { constructor(){ @@ -117,11 +124,14 @@ function Field() { }); } } - var _foo7 = new WeakMap(); - function foo5(value) {} + var _foo7 = { + get: void 0, + set: set_foo1 + }; + function set_foo1(value) {} } function Method() { - var _foo = new WeakSet(); + var _foo = new WeakSet(), _foo = new WeakMap(); // Error class A_Method_Field { constructor(){ @@ -132,7 +142,6 @@ function Method() { }); } } - var _foo = new WeakMap(); function foo() {} var _foo8 = new WeakSet(), _foo8 = new WeakSet(); // Error @@ -142,30 +151,36 @@ function Method() { _classPrivateMethodInit(this, _foo8); } } - function foo6() {} - function foo6() {} - var _foo9 = new WeakSet(), _foo9 = new WeakSet(); + function foo2() {} + function foo2() {} + var _foo9 = new WeakSet(), _foo9 = new WeakMap(); // Error class A_Method_Getter { constructor(){ _classPrivateMethodInit(this, _foo9); - _classPrivateMethodInit(this, _foo9); + _classPrivateFieldInit(this, _foo9, { + get: get_foo, + set: void 0 + }); } } - function foo7() {} - function foo7() { + function foo3() {} + function get_foo() { return ""; } - var _foo10 = new WeakSet(), _foo10 = new WeakSet(); + var _foo10 = new WeakSet(), _foo10 = new WeakMap(); // Error class A_Method_Setter { constructor(){ _classPrivateMethodInit(this, _foo10); - _classPrivateMethodInit(this, _foo10); + _classPrivateFieldInit(this, _foo10, { + get: void 0, + set: set_foo + }); } } - function foo8() {} - function foo8(value) {} + function foo4() {} + function set_foo(value) {} var _foo11 = new WeakSet(); // Error class A_Method_StaticField { @@ -177,7 +192,7 @@ function Method() { writable: true, value: "foo" }; - function foo9() {} + function foo5() {} var _foo12 = new WeakSet(); // Error class A_Method_StaticMethod { @@ -185,8 +200,8 @@ function Method() { _classPrivateMethodInit(this, _foo12); } } - function foo10() {} - function foo10() {} + function foo6() {} + function foo6() {} var _foo13 = new WeakSet(); // Error class A_Method_StaticGetter { @@ -194,8 +209,12 @@ function Method() { _classPrivateMethodInit(this, _foo13); } } - function foo11() {} - function foo11() { + var _foo13 = { + get: get_foo2, + set: void 0 + }; + function foo7() {} + function get_foo2() { return ""; } var _foo14 = new WeakSet(); @@ -205,199 +224,262 @@ function Method() { _classPrivateMethodInit(this, _foo14); } } - function foo12() {} - function foo12(value) {} + var _foo14 = { + get: void 0, + set: set_foo2 + }; + function foo8() {} + function set_foo2(value) {} } function Getter() { - var _foo = new WeakSet(); + var _foo = new WeakMap(), _foo = new WeakMap(); // Error class A_Getter_Field { constructor(){ - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: get_foo, + set: void 0 + }); _classPrivateFieldInit(this, _foo, { writable: true, value: "foo" }); } } - var _foo = new WeakMap(); - function foo() { + function get_foo() { return ""; } - var _foo15 = new WeakSet(), _foo15 = new WeakSet(); + var _foo15 = new WeakMap(), _foo15 = new WeakSet(); // Error class A_Getter_Method { constructor(){ - _classPrivateMethodInit(this, _foo15); + _classPrivateFieldInit(this, _foo15, { + get: get_foo3, + set: void 0 + }); _classPrivateMethodInit(this, _foo15); } } - function foo13() { + function get_foo3() { return ""; } - function foo13() {} - var _foo16 = new WeakSet(), _foo16 = new WeakSet(); + function foo() {} + var _foo16 = new WeakMap(); // Error class A_Getter_Getter { constructor(){ - _classPrivateMethodInit(this, _foo16); - _classPrivateMethodInit(this, _foo16); + _classPrivateFieldInit(this, _foo16, { + get: get_foo4, + set: void 0 + }); } } - function foo14() { + function get_foo4() { return ""; } - function foo14() { + function get_foo4() { return ""; } - var _foo17 = new WeakSet(), _foo17 = new WeakSet(); + var _foo17 = new WeakMap(); //OK class A_Getter_Setter { constructor(){ - _classPrivateMethodInit(this, _foo17); - _classPrivateMethodInit(this, _foo17); + _classPrivateFieldInit(this, _foo17, { + get: get_foo5, + set: set_foo + }); } } - function foo15() { + function get_foo5() { return ""; } - function foo15(value) {} - var _foo18 = new WeakSet(); + function set_foo(value) {} + var _foo18 = new WeakMap(); // Error class A_Getter_StaticField { constructor(){ - _classPrivateMethodInit(this, _foo18); + _classPrivateFieldInit(this, _foo18, { + get: get_foo6, + set: void 0 + }); } } - function foo16() { + function get_foo6() { return ""; } - function foo16() {} - var _foo19 = new WeakSet(); + function foo9() {} + var _foo19 = new WeakMap(); // Error class A_Getter_StaticMethod { constructor(){ - _classPrivateMethodInit(this, _foo19); + _classPrivateFieldInit(this, _foo19, { + get: get_foo7, + set: void 0 + }); } } - function foo17() { + function get_foo7() { return ""; } - function foo17() {} - var _foo20 = new WeakSet(); + function foo10() {} + var _foo20 = new WeakMap(); // Error class A_Getter_StaticGetter { constructor(){ - _classPrivateMethodInit(this, _foo20); + _classPrivateFieldInit(this, _foo20, { + get: get_foo8, + set: void 0 + }); } } - function foo18() { + var _foo20 = { + get: get_foo8, + set: void 0 + }; + function get_foo8() { return ""; } - function foo18() { + function get_foo8() { return ""; } - var _foo21 = new WeakSet(); + var _foo21 = new WeakMap(); // Error class A_Getter_StaticSetter { constructor(){ - _classPrivateMethodInit(this, _foo21); + _classPrivateFieldInit(this, _foo21, { + get: get_foo9, + set: void 0 + }); } } - function foo19() { + var _foo21 = { + get: void 0, + set: set_foo3 + }; + function get_foo9() { return ""; } - function foo19(value) {} + function set_foo3(value) {} } function Setter() { - var _foo = new WeakSet(); + var _foo = new WeakMap(), _foo = new WeakMap(); // Error class A_Setter_Field { constructor(){ - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: void 0, + set: set_foo + }); _classPrivateFieldInit(this, _foo, { writable: true, value: "foo" }); } } - var _foo = new WeakMap(); - function foo(value) {} - var _foo22 = new WeakSet(), _foo22 = new WeakSet(); + function set_foo(value) {} + var _foo22 = new WeakMap(), _foo22 = new WeakSet(); // Error class A_Setter_Method { constructor(){ - _classPrivateMethodInit(this, _foo22); + _classPrivateFieldInit(this, _foo22, { + get: void 0, + set: set_foo4 + }); _classPrivateMethodInit(this, _foo22); } } - function foo20(value) {} - function foo20() {} - var _foo23 = new WeakSet(), _foo23 = new WeakSet(); + function set_foo4(value) {} + function foo() {} + var _foo23 = new WeakMap(); // OK class A_Setter_Getter { constructor(){ - _classPrivateMethodInit(this, _foo23); - _classPrivateMethodInit(this, _foo23); + _classPrivateFieldInit(this, _foo23, { + get: get_foo, + set: set_foo5 + }); } } - function foo21(value) {} - function foo21() { + function set_foo5(value) {} + function get_foo() { return ""; } - var _foo24 = new WeakSet(), _foo24 = new WeakSet(); + var _foo24 = new WeakMap(); // Error class A_Setter_Setter { constructor(){ - _classPrivateMethodInit(this, _foo24); - _classPrivateMethodInit(this, _foo24); + _classPrivateFieldInit(this, _foo24, { + get: void 0, + set: set_foo6 + }); } } - function foo22(value) {} - function foo22(value) {} - var _foo25 = new WeakSet(); + function set_foo6(value) {} + function set_foo6(value) {} + var _foo25 = new WeakMap(); // Error class A_Setter_StaticField { constructor(){ - _classPrivateMethodInit(this, _foo25); + _classPrivateFieldInit(this, _foo25, { + get: void 0, + set: set_foo7 + }); } } var _foo25 = { writable: true, value: "foo" }; - function foo23(value) {} - var _foo26 = new WeakSet(); + function set_foo7(value) {} + var _foo26 = new WeakMap(); // Error class A_Setter_StaticMethod { constructor(){ - _classPrivateMethodInit(this, _foo26); + _classPrivateFieldInit(this, _foo26, { + get: void 0, + set: set_foo8 + }); } } - function foo24(value) {} - function foo24() {} - var _foo27 = new WeakSet(); + function set_foo8(value) {} + function foo11() {} + var _foo27 = new WeakMap(); // Error class A_Setter_StaticGetter { constructor(){ - _classPrivateMethodInit(this, _foo27); + _classPrivateFieldInit(this, _foo27, { + get: void 0, + set: set_foo9 + }); } } - function foo25(value) {} - function foo25() { + var _foo27 = { + get: get_foo10, + set: void 0 + }; + function set_foo9(value) {} + function get_foo10() { return ""; } - var _foo28 = new WeakSet(); + var _foo28 = new WeakMap(); // Error class A_Setter_StaticSetter { constructor(){ - _classPrivateMethodInit(this, _foo28); + _classPrivateFieldInit(this, _foo28, { + get: void 0, + set: set_foo10 + }); } } - function foo26(value) {} - function foo26(value) {} + var _foo28 = { + get: void 0, + set: set_foo10 + }; + function set_foo10(value) {} + function set_foo10(value) {} } function StaticField() { + var _foo = new WeakMap(); // Error class A_StaticField_Field { constructor(){ @@ -411,7 +493,6 @@ function StaticField() { writable: true, value: "foo" }; - var _foo = new WeakMap(); var _foo29 = new WeakSet(); // Error class A_StaticField_Method { @@ -424,32 +505,38 @@ function StaticField() { value: "foo" }; function foo() {} - var _foo30 = new WeakSet(); + var _foo30 = new WeakMap(); // Error class A_StaticField_Getter { constructor(){ - _classPrivateMethodInit(this, _foo30); + _classPrivateFieldInit(this, _foo30, { + get: get_foo, + set: void 0 + }); } } var _foo30 = { writable: true, value: "foo" }; - function foo27() { + function get_foo() { return ""; } - var _foo31 = new WeakSet(); + var _foo31 = new WeakMap(); // Error class A_StaticField_Setter { constructor(){ - _classPrivateMethodInit(this, _foo31); + _classPrivateFieldInit(this, _foo31, { + get: void 0, + set: set_foo + }); } } var _foo31 = { writable: true, value: "foo" }; - function foo28(value) {} + function set_foo(value) {} // Error class A_StaticField_StaticField { } @@ -468,7 +555,7 @@ function StaticField() { writable: true, value: "foo" }; - function foo29() {} + function foo12() {} // Error class A_StaticField_StaticGetter { } @@ -476,7 +563,11 @@ function StaticField() { writable: true, value: "foo" }; - function foo30() { + var _foo34 = { + get: get_foo11, + set: void 0 + }; + function get_foo11() { return ""; } // Error @@ -486,9 +577,14 @@ function StaticField() { writable: true, value: "foo" }; - function foo31(value) {} + var _foo35 = { + get: void 0, + set: set_foo11 + }; + function set_foo11(value) {} } function StaticMethod() { + var _foo = new WeakMap(); // Error class A_StaticMethod_Field { constructor(){ @@ -498,7 +594,6 @@ function StaticMethod() { }); } } - var _foo = new WeakMap(); function foo() {} var _foo36 = new WeakSet(); // Error @@ -507,28 +602,34 @@ function StaticMethod() { _classPrivateMethodInit(this, _foo36); } } - function foo32() {} - function foo32() {} - var _foo37 = new WeakSet(); + function foo13() {} + function foo13() {} + var _foo37 = new WeakMap(); // Error class A_StaticMethod_Getter { constructor(){ - _classPrivateMethodInit(this, _foo37); + _classPrivateFieldInit(this, _foo37, { + get: get_foo, + set: void 0 + }); } } - function foo33() {} - function foo33() { + function foo14() {} + function get_foo() { return ""; } - var _foo38 = new WeakSet(); + var _foo38 = new WeakMap(); // Error class A_StaticMethod_Setter { constructor(){ - _classPrivateMethodInit(this, _foo38); + _classPrivateFieldInit(this, _foo38, { + get: void 0, + set: set_foo + }); } } - function foo34() {} - function foo34(value) {} + function foo15() {} + function set_foo(value) {} // Error class A_StaticMethod_StaticField { } @@ -536,26 +637,35 @@ function StaticMethod() { writable: true, value: "foo" }; - function foo35() {} + function foo16() {} // Error class A_StaticMethod_StaticMethod { } - function foo36() {} - function foo36() {} + function foo17() {} + function foo17() {} // Error class A_StaticMethod_StaticGetter { } - function foo37() {} - function foo37() { + var _foo40 = { + get: get_foo12, + set: void 0 + }; + function foo18() {} + function get_foo12() { return ""; } // Error class A_StaticMethod_StaticSetter { } - function foo38() {} - function foo38(value) {} + var _foo41 = { + get: void 0, + set: set_foo12 + }; + function foo19() {} + function set_foo12(value) {} } function StaticGetter() { + var _foo = new WeakMap(); // Error class A_StaticGetter_Field { constructor(){ @@ -565,77 +675,115 @@ function StaticGetter() { }); } } - var _foo = new WeakMap(); - function foo() { - return ""; - } - var _foo40 = new WeakSet(); - // Error - class A_StaticGetter_Method { - constructor(){ - _classPrivateMethodInit(this, _foo40); - } - } - function foo39() { - return ""; - } - function foo39() {} - var _foo41 = new WeakSet(); - // Error - class A_StaticGetter_Getter { - constructor(){ - _classPrivateMethodInit(this, _foo41); - } - } - function foo40() { - return ""; - } - function foo40() { + var _foo = { + get: get_foo, + set: void 0 + }; + function get_foo() { return ""; } var _foo42 = new WeakSet(); // Error - class A_StaticGetter_Setter { + class A_StaticGetter_Method { constructor(){ _classPrivateMethodInit(this, _foo42); } } - function foo41() { + var _foo42 = { + get: get_foo13, + set: void 0 + }; + function get_foo13() { return ""; } - function foo41(value) {} + function foo() {} + var _foo43 = new WeakMap(); + // Error + class A_StaticGetter_Getter { + constructor(){ + _classPrivateFieldInit(this, _foo43, { + get: get_foo14, + set: void 0 + }); + } + } + var _foo43 = { + get: get_foo14, + set: void 0 + }; + function get_foo14() { + return ""; + } + function get_foo14() { + return ""; + } + var _foo44 = new WeakMap(); + // Error + class A_StaticGetter_Setter { + constructor(){ + _classPrivateFieldInit(this, _foo44, { + get: void 0, + set: set_foo + }); + } + } + var _foo44 = { + get: get_foo15, + set: void 0 + }; + function get_foo15() { + return ""; + } + function set_foo(value) {} // Error class A_StaticGetter_StaticField { } - function foo42() { + var _foo45 = { + get: get_foo16, + set: void 0 + }; + function get_foo16() { return ""; } - function foo42() {} + function foo20() {} // Error class A_StaticGetter_StaticMethod { } - function foo43() { + var _foo46 = { + get: get_foo17, + set: void 0 + }; + function get_foo17() { return ""; } - function foo43() {} + function foo21() {} // Error class A_StaticGetter_StaticGetter { } - function foo44() { + var _foo47 = { + get: get_foo18, + set: void 0 + }; + function get_foo18() { return ""; } - function foo44() { + function get_foo18() { return ""; } // OK class A_StaticGetter_StaticSetter { } - function foo45() { + var _foo48 = { + get: get_foo19, + set: set_foo13 + }; + function get_foo19() { return ""; } - function foo45(value) {} + function set_foo13(value) {} } function StaticSetter() { + var _foo = new WeakMap(); // Error class A_StaticSetter_Field { constructor(){ @@ -645,60 +793,97 @@ function StaticSetter() { }); } } - var _foo = new WeakMap(); - function foo(value) {} - var _foo43 = new WeakSet(); + var _foo = { + get: void 0, + set: set_foo + }; + function set_foo(value) {} + var _foo49 = new WeakSet(); // Error class A_StaticSetter_Method { constructor(){ - _classPrivateMethodInit(this, _foo43); + _classPrivateMethodInit(this, _foo49); } } - function foo46(value) {} - function foo46() {} - var _foo44 = new WeakSet(); + var _foo49 = { + get: void 0, + set: set_foo14 + }; + function set_foo14(value) {} + function foo() {} + var _foo50 = new WeakMap(); // Error class A_StaticSetter_Getter { constructor(){ - _classPrivateMethodInit(this, _foo44); + _classPrivateFieldInit(this, _foo50, { + get: get_foo, + set: void 0 + }); } } - function foo47(value) {} - function foo47() { + var _foo50 = { + get: void 0, + set: set_foo15 + }; + function set_foo15(value) {} + function get_foo() { return ""; } - var _foo45 = new WeakSet(); + var _foo51 = new WeakMap(); // Error class A_StaticSetter_Setter { constructor(){ - _classPrivateMethodInit(this, _foo45); + _classPrivateFieldInit(this, _foo51, { + get: void 0, + set: set_foo16 + }); } } - function foo48(value) {} - function foo48(value) {} + var _foo51 = { + get: void 0, + set: set_foo16 + }; + function set_foo16(value) {} + function set_foo16(value) {} // Error class A_StaticSetter_StaticField { } - var _foo46 = { + var _foo52 = { + get: void 0, + set: set_foo17 + }; + var _foo52 = { writable: true, value: "foo" }; - function foo49(value) {} + function set_foo17(value) {} // Error class A_StaticSetter_StaticMethod { } - function foo50(value) {} - function foo50() {} + var _foo53 = { + get: void 0, + set: set_foo18 + }; + function set_foo18(value) {} + function foo22() {} // OK class A_StaticSetter_StaticGetter { } - function foo51(value) {} - function foo51() { + var _foo54 = { + get: get_foo20, + set: set_foo19 + }; + function set_foo19(value) {} + function get_foo20() { return ""; } // Error class A_StaticSetter_StaticSetter { } - function foo52(value) {} - function foo52(value) {} + var _foo55 = { + get: void 0, + set: set_foo20 + }; + function set_foo20(value) {} + function set_foo20(value) {} } diff --git a/crates/swc/tests/tsc-references/privateNameDuplicateField_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameDuplicateField_es5.1.normal.js index c49a56411f0..fef53a6a240 100644 --- a/crates/swc/tests/tsc-references/privateNameDuplicateField_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameDuplicateField_es5.1.normal.js @@ -20,15 +20,16 @@ function _classPrivateMethodInit(obj, privateSet) { // @target: es6 function Field() { var foo = function foo() {}; - var foo1 = function foo1() { + var get_foo = function get_foo() { return ""; }; - var foo2 = function foo2(value) {}; - var foo3 = function foo3() {}; - var foo4 = function foo4() { + var set_foo = function set_foo(value) {}; + var foo1 = function foo1() {}; + var get_foo1 = function get_foo1() { return ""; }; - var foo5 = function foo5(value) {}; + var set_foo1 = function set_foo1(value) {}; + var _foo = new WeakMap(), _foo = new WeakMap(); var A_Field_Field = function A_Field_Field() { "use strict"; _classCallCheck(this, A_Field_Field); @@ -41,9 +42,7 @@ function Field() { value: "foo" }); }; - var _foo = new WeakMap(); - var _foo = new WeakMap(); - var _foo1 = new WeakSet(); + var _foo1 = new WeakMap(), _foo1 = new WeakSet(); var A_Field_Method = function A_Field_Method() { "use strict"; _classCallCheck(this, A_Field_Method); @@ -53,8 +52,7 @@ function Field() { }); _classPrivateMethodInit(this, _foo1); }; - var _foo1 = new WeakMap(); - var _foo2 = new WeakSet(); + var _foo2 = new WeakMap(), _foo2 = new WeakMap(); var A_Field_Getter = function A_Field_Getter() { "use strict"; _classCallCheck(this, A_Field_Getter); @@ -62,10 +60,12 @@ function Field() { writable: true, value: "foo" }); - _classPrivateMethodInit(this, _foo2); + _classPrivateFieldInit(this, _foo2, { + get: get_foo, + set: void 0 + }); }; - var _foo2 = new WeakMap(); - var _foo3 = new WeakSet(); + var _foo3 = new WeakMap(), _foo3 = new WeakMap(); var A_Field_Setter = function A_Field_Setter() { "use strict"; _classCallCheck(this, A_Field_Setter); @@ -73,9 +73,12 @@ function Field() { writable: true, value: "foo" }); - _classPrivateMethodInit(this, _foo3); + _classPrivateFieldInit(this, _foo3, { + get: void 0, + set: set_foo + }); }; - var _foo3 = new WeakMap(); + var _foo4 = new WeakMap(); var A_Field_StaticField = function A_Field_StaticField() { "use strict"; _classCallCheck(this, A_Field_StaticField); @@ -84,11 +87,11 @@ function Field() { value: "foo" }); }; - var _foo4 = new WeakMap(); var _foo4 = { writable: true, value: "foo" }; + var _foo5 = new WeakMap(); var A_Field_StaticMethod = function A_Field_StaticMethod() { "use strict"; _classCallCheck(this, A_Field_StaticMethod); @@ -97,7 +100,7 @@ function Field() { value: "foo" }); }; - var _foo5 = new WeakMap(); + var _foo6 = new WeakMap(); var A_Field_StaticGetter = function A_Field_StaticGetter() { "use strict"; _classCallCheck(this, A_Field_StaticGetter); @@ -106,7 +109,11 @@ function Field() { value: "foo" }); }; - var _foo6 = new WeakMap(); + var _foo6 = { + get: get_foo1, + set: void 0 + }; + var _foo7 = new WeakMap(); var A_Field_StaticSetter = function A_Field_StaticSetter() { "use strict"; _classCallCheck(this, A_Field_StaticSetter); @@ -115,28 +122,31 @@ function Field() { value: "foo" }); }; - var _foo7 = new WeakMap(); + var _foo7 = { + get: void 0, + set: set_foo1 + }; } function Method() { var foo = function foo() {}; + var foo2 = function foo2() {}; + var foo2 = function foo2() {}; + var foo3 = function foo3() {}; + var get_foo = function get_foo() { + return ""; + }; + var foo4 = function foo4() {}; + var set_foo = function set_foo(value) {}; + var foo5 = function foo5() {}; var foo6 = function foo6() {}; var foo6 = function foo6() {}; var foo7 = function foo7() {}; - var foo7 = function foo7() { + var get_foo2 = function get_foo2() { return ""; }; var foo8 = function foo8() {}; - var foo8 = function foo8(value) {}; - var foo9 = function foo9() {}; - var foo10 = function foo10() {}; - var foo10 = function foo10() {}; - var foo11 = function foo11() {}; - var foo11 = function foo11() { - return ""; - }; - var foo12 = function foo12() {}; - var foo12 = function foo12(value) {}; - var _foo = new WeakSet(); + var set_foo2 = function set_foo2(value) {}; + var _foo = new WeakSet(), _foo = new WeakMap(); var A_Method_Field = function A_Method_Field() { "use strict"; _classCallCheck(this, A_Method_Field); @@ -146,7 +156,6 @@ function Method() { value: "foo" }); }; - var _foo = new WeakMap(); var _foo8 = new WeakSet(), _foo8 = new WeakSet(); var A_Method_Method = function A_Method_Method() { "use strict"; @@ -154,19 +163,25 @@ function Method() { _classPrivateMethodInit(this, _foo8); _classPrivateMethodInit(this, _foo8); }; - var _foo9 = new WeakSet(), _foo9 = new WeakSet(); + var _foo9 = new WeakSet(), _foo9 = new WeakMap(); var A_Method_Getter = function A_Method_Getter() { "use strict"; _classCallCheck(this, A_Method_Getter); _classPrivateMethodInit(this, _foo9); - _classPrivateMethodInit(this, _foo9); + _classPrivateFieldInit(this, _foo9, { + get: get_foo, + set: void 0 + }); }; - var _foo10 = new WeakSet(), _foo10 = new WeakSet(); + var _foo10 = new WeakSet(), _foo10 = new WeakMap(); var A_Method_Setter = function A_Method_Setter() { "use strict"; _classCallCheck(this, A_Method_Setter); _classPrivateMethodInit(this, _foo10); - _classPrivateMethodInit(this, _foo10); + _classPrivateFieldInit(this, _foo10, { + get: void 0, + set: set_foo + }); }; var _foo11 = new WeakSet(); var A_Method_StaticField = function A_Method_StaticField() { @@ -190,197 +205,264 @@ function Method() { _classCallCheck(this, A_Method_StaticGetter); _classPrivateMethodInit(this, _foo13); }; + var _foo13 = { + get: get_foo2, + set: void 0 + }; var _foo14 = new WeakSet(); var A_Method_StaticSetter = function A_Method_StaticSetter() { "use strict"; _classCallCheck(this, A_Method_StaticSetter); _classPrivateMethodInit(this, _foo14); }; + var _foo14 = { + get: void 0, + set: set_foo2 + }; } function Getter() { - var foo = function foo() { + var get_foo = function get_foo() { return ""; }; - var foo13 = function foo13() { + var get_foo3 = function get_foo3() { return ""; }; - var foo13 = function foo13() {}; - var foo14 = function foo14() { + var foo = function foo() {}; + var get_foo4 = function get_foo4() { return ""; }; - var foo14 = function foo14() { + var get_foo4 = function get_foo4() { return ""; }; - var foo15 = function foo15() { + var get_foo5 = function get_foo5() { return ""; }; - var foo15 = function foo15(value) {}; - var foo16 = function foo16() { + var set_foo = function set_foo(value) {}; + var get_foo6 = function get_foo6() { return ""; }; - var foo16 = function foo16() {}; - var foo17 = function foo17() { + var foo9 = function foo9() {}; + var get_foo7 = function get_foo7() { return ""; }; - var foo17 = function foo17() {}; - var foo18 = function foo18() { + var foo10 = function foo10() {}; + var get_foo8 = function get_foo8() { return ""; }; - var foo18 = function foo18() { + var get_foo8 = function get_foo8() { return ""; }; - var foo19 = function foo19() { + var get_foo9 = function get_foo9() { return ""; }; - var foo19 = function foo19(value) {}; - var _foo = new WeakSet(); + var set_foo3 = function set_foo3(value) {}; + var _foo = new WeakMap(), _foo = new WeakMap(); var A_Getter_Field = function A_Getter_Field() { "use strict"; _classCallCheck(this, A_Getter_Field); - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: get_foo, + set: void 0 + }); _classPrivateFieldInit(this, _foo, { writable: true, value: "foo" }); }; - var _foo = new WeakMap(); - var _foo15 = new WeakSet(), _foo15 = new WeakSet(); + var _foo15 = new WeakMap(), _foo15 = new WeakSet(); var A_Getter_Method = function A_Getter_Method() { "use strict"; _classCallCheck(this, A_Getter_Method); - _classPrivateMethodInit(this, _foo15); + _classPrivateFieldInit(this, _foo15, { + get: get_foo3, + set: void 0 + }); _classPrivateMethodInit(this, _foo15); }; - var _foo16 = new WeakSet(), _foo16 = new WeakSet(); + var _foo16 = new WeakMap(); var A_Getter_Getter = function A_Getter_Getter() { "use strict"; _classCallCheck(this, A_Getter_Getter); - _classPrivateMethodInit(this, _foo16); - _classPrivateMethodInit(this, _foo16); + _classPrivateFieldInit(this, _foo16, { + get: get_foo4, + set: void 0 + }); }; - var _foo17 = new WeakSet(), _foo17 = new WeakSet(); + var _foo17 = new WeakMap(); var A_Getter_Setter = function A_Getter_Setter() { "use strict"; _classCallCheck(this, A_Getter_Setter); - _classPrivateMethodInit(this, _foo17); - _classPrivateMethodInit(this, _foo17); + _classPrivateFieldInit(this, _foo17, { + get: get_foo5, + set: set_foo + }); }; - var _foo18 = new WeakSet(); + var _foo18 = new WeakMap(); var A_Getter_StaticField = function A_Getter_StaticField() { "use strict"; _classCallCheck(this, A_Getter_StaticField); - _classPrivateMethodInit(this, _foo18); + _classPrivateFieldInit(this, _foo18, { + get: get_foo6, + set: void 0 + }); }; - var _foo19 = new WeakSet(); + var _foo19 = new WeakMap(); var A_Getter_StaticMethod = function A_Getter_StaticMethod() { "use strict"; _classCallCheck(this, A_Getter_StaticMethod); - _classPrivateMethodInit(this, _foo19); + _classPrivateFieldInit(this, _foo19, { + get: get_foo7, + set: void 0 + }); }; - var _foo20 = new WeakSet(); + var _foo20 = new WeakMap(); var A_Getter_StaticGetter = function A_Getter_StaticGetter() { "use strict"; _classCallCheck(this, A_Getter_StaticGetter); - _classPrivateMethodInit(this, _foo20); + _classPrivateFieldInit(this, _foo20, { + get: get_foo8, + set: void 0 + }); }; - var _foo21 = new WeakSet(); + var _foo20 = { + get: get_foo8, + set: void 0 + }; + var _foo21 = new WeakMap(); var A_Getter_StaticSetter = function A_Getter_StaticSetter() { "use strict"; _classCallCheck(this, A_Getter_StaticSetter); - _classPrivateMethodInit(this, _foo21); + _classPrivateFieldInit(this, _foo21, { + get: get_foo9, + set: void 0 + }); + }; + var _foo21 = { + get: void 0, + set: set_foo3 }; } function Setter() { - var foo = function foo(value) {}; - var foo20 = function foo20(value) {}; - var foo20 = function foo20() {}; - var foo21 = function foo21(value) {}; - var foo21 = function foo21() { + var set_foo = function set_foo(value) {}; + var set_foo4 = function set_foo4(value) {}; + var foo = function foo() {}; + var set_foo5 = function set_foo5(value) {}; + var get_foo = function get_foo() { return ""; }; - var foo22 = function foo22(value) {}; - var foo22 = function foo22(value) {}; - var foo23 = function foo23(value) {}; - var foo24 = function foo24(value) {}; - var foo24 = function foo24() {}; - var foo25 = function foo25(value) {}; - var foo25 = function foo25() { + var set_foo6 = function set_foo6(value) {}; + var set_foo6 = function set_foo6(value) {}; + var set_foo7 = function set_foo7(value) {}; + var set_foo8 = function set_foo8(value) {}; + var foo11 = function foo11() {}; + var set_foo9 = function set_foo9(value) {}; + var get_foo10 = function get_foo10() { return ""; }; - var foo26 = function foo26(value) {}; - var foo26 = function foo26(value) {}; - var _foo = new WeakSet(); + var set_foo10 = function set_foo10(value) {}; + var set_foo10 = function set_foo10(value) {}; + var _foo = new WeakMap(), _foo = new WeakMap(); var A_Setter_Field = function A_Setter_Field() { "use strict"; _classCallCheck(this, A_Setter_Field); - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: void 0, + set: set_foo + }); _classPrivateFieldInit(this, _foo, { writable: true, value: "foo" }); }; - var _foo = new WeakMap(); - var _foo22 = new WeakSet(), _foo22 = new WeakSet(); + var _foo22 = new WeakMap(), _foo22 = new WeakSet(); var A_Setter_Method = function A_Setter_Method() { "use strict"; _classCallCheck(this, A_Setter_Method); - _classPrivateMethodInit(this, _foo22); + _classPrivateFieldInit(this, _foo22, { + get: void 0, + set: set_foo4 + }); _classPrivateMethodInit(this, _foo22); }; - var _foo23 = new WeakSet(), _foo23 = new WeakSet(); + var _foo23 = new WeakMap(); var A_Setter_Getter = function A_Setter_Getter() { "use strict"; _classCallCheck(this, A_Setter_Getter); - _classPrivateMethodInit(this, _foo23); - _classPrivateMethodInit(this, _foo23); + _classPrivateFieldInit(this, _foo23, { + get: get_foo, + set: set_foo5 + }); }; - var _foo24 = new WeakSet(), _foo24 = new WeakSet(); + var _foo24 = new WeakMap(); var A_Setter_Setter = function A_Setter_Setter() { "use strict"; _classCallCheck(this, A_Setter_Setter); - _classPrivateMethodInit(this, _foo24); - _classPrivateMethodInit(this, _foo24); + _classPrivateFieldInit(this, _foo24, { + get: void 0, + set: set_foo6 + }); }; - var _foo25 = new WeakSet(); + var _foo25 = new WeakMap(); var A_Setter_StaticField = function A_Setter_StaticField() { "use strict"; _classCallCheck(this, A_Setter_StaticField); - _classPrivateMethodInit(this, _foo25); + _classPrivateFieldInit(this, _foo25, { + get: void 0, + set: set_foo7 + }); }; var _foo25 = { writable: true, value: "foo" }; - var _foo26 = new WeakSet(); + var _foo26 = new WeakMap(); var A_Setter_StaticMethod = function A_Setter_StaticMethod() { "use strict"; _classCallCheck(this, A_Setter_StaticMethod); - _classPrivateMethodInit(this, _foo26); + _classPrivateFieldInit(this, _foo26, { + get: void 0, + set: set_foo8 + }); }; - var _foo27 = new WeakSet(); + var _foo27 = new WeakMap(); var A_Setter_StaticGetter = function A_Setter_StaticGetter() { "use strict"; _classCallCheck(this, A_Setter_StaticGetter); - _classPrivateMethodInit(this, _foo27); + _classPrivateFieldInit(this, _foo27, { + get: void 0, + set: set_foo9 + }); }; - var _foo28 = new WeakSet(); + var _foo27 = { + get: get_foo10, + set: void 0 + }; + var _foo28 = new WeakMap(); var A_Setter_StaticSetter = function A_Setter_StaticSetter() { "use strict"; _classCallCheck(this, A_Setter_StaticSetter); - _classPrivateMethodInit(this, _foo28); + _classPrivateFieldInit(this, _foo28, { + get: void 0, + set: set_foo10 + }); + }; + var _foo28 = { + get: void 0, + set: set_foo10 }; } function StaticField() { var foo = function foo() {}; - var foo27 = function foo27() { + var get_foo = function get_foo() { return ""; }; - var foo28 = function foo28(value) {}; - var foo29 = function foo29() {}; - var foo30 = function foo30() { + var set_foo = function set_foo(value) {}; + var foo12 = function foo12() {}; + var get_foo11 = function get_foo11() { return ""; }; - var foo31 = function foo31(value) {}; + var set_foo11 = function set_foo11(value) {}; + var _foo = new WeakMap(); var A_StaticField_Field = function A_StaticField_Field() { "use strict"; _classCallCheck(this, A_StaticField_Field); @@ -393,7 +475,6 @@ function StaticField() { writable: true, value: "foo" }; - var _foo = new WeakMap(); var _foo29 = new WeakSet(); var A_StaticField_Method = function A_StaticField_Method() { "use strict"; @@ -404,21 +485,27 @@ function StaticField() { writable: true, value: "foo" }; - var _foo30 = new WeakSet(); + var _foo30 = new WeakMap(); var A_StaticField_Getter = function A_StaticField_Getter() { "use strict"; _classCallCheck(this, A_StaticField_Getter); - _classPrivateMethodInit(this, _foo30); + _classPrivateFieldInit(this, _foo30, { + get: get_foo, + set: void 0 + }); }; var _foo30 = { writable: true, value: "foo" }; - var _foo31 = new WeakSet(); + var _foo31 = new WeakMap(); var A_StaticField_Setter = function A_StaticField_Setter() { "use strict"; _classCallCheck(this, A_StaticField_Setter); - _classPrivateMethodInit(this, _foo31); + _classPrivateFieldInit(this, _foo31, { + get: void 0, + set: set_foo + }); }; var _foo31 = { writable: true, @@ -452,6 +539,10 @@ function StaticField() { writable: true, value: "foo" }; + var _foo34 = { + get: get_foo11, + set: void 0 + }; var A_StaticField_StaticSetter = function A_StaticField_StaticSetter() { "use strict"; _classCallCheck(this, A_StaticField_StaticSetter); @@ -460,26 +551,31 @@ function StaticField() { writable: true, value: "foo" }; + var _foo35 = { + get: void 0, + set: set_foo11 + }; } function StaticMethod() { var foo = function foo() {}; - var foo32 = function foo32() {}; - var foo32 = function foo32() {}; - var foo33 = function foo33() {}; - var foo33 = function foo33() { + var foo13 = function foo13() {}; + var foo13 = function foo13() {}; + var foo14 = function foo14() {}; + var get_foo = function get_foo() { return ""; }; - var foo34 = function foo34() {}; - var foo34 = function foo34(value) {}; - var foo35 = function foo35() {}; - var foo36 = function foo36() {}; - var foo36 = function foo36() {}; - var foo37 = function foo37() {}; - var foo37 = function foo37() { + var foo15 = function foo15() {}; + var set_foo = function set_foo(value) {}; + var foo16 = function foo16() {}; + var foo17 = function foo17() {}; + var foo17 = function foo17() {}; + var foo18 = function foo18() {}; + var get_foo12 = function get_foo12() { return ""; }; - var foo38 = function foo38() {}; - var foo38 = function foo38(value) {}; + var foo19 = function foo19() {}; + var set_foo12 = function set_foo12(value) {}; + var _foo = new WeakMap(); var A_StaticMethod_Field = function A_StaticMethod_Field() { "use strict"; _classCallCheck(this, A_StaticMethod_Field); @@ -488,24 +584,29 @@ function StaticMethod() { value: "foo" }); }; - var _foo = new WeakMap(); var _foo36 = new WeakSet(); var A_StaticMethod_Method = function A_StaticMethod_Method() { "use strict"; _classCallCheck(this, A_StaticMethod_Method); _classPrivateMethodInit(this, _foo36); }; - var _foo37 = new WeakSet(); + var _foo37 = new WeakMap(); var A_StaticMethod_Getter = function A_StaticMethod_Getter() { "use strict"; _classCallCheck(this, A_StaticMethod_Getter); - _classPrivateMethodInit(this, _foo37); + _classPrivateFieldInit(this, _foo37, { + get: get_foo, + set: void 0 + }); }; - var _foo38 = new WeakSet(); + var _foo38 = new WeakMap(); var A_StaticMethod_Setter = function A_StaticMethod_Setter() { "use strict"; _classCallCheck(this, A_StaticMethod_Setter); - _classPrivateMethodInit(this, _foo38); + _classPrivateFieldInit(this, _foo38, { + get: void 0, + set: set_foo + }); }; var A_StaticMethod_StaticField = function A_StaticMethod_StaticField() { "use strict"; @@ -523,47 +624,56 @@ function StaticMethod() { "use strict"; _classCallCheck(this, A_StaticMethod_StaticGetter); }; + var _foo40 = { + get: get_foo12, + set: void 0 + }; var A_StaticMethod_StaticSetter = function A_StaticMethod_StaticSetter() { "use strict"; _classCallCheck(this, A_StaticMethod_StaticSetter); }; + var _foo41 = { + get: void 0, + set: set_foo12 + }; } function StaticGetter() { - var foo = function foo() { + var get_foo = function get_foo() { return ""; }; - var foo39 = function foo39() { + var get_foo13 = function get_foo13() { return ""; }; - var foo39 = function foo39() {}; - var foo40 = function foo40() { + var foo = function foo() {}; + var get_foo14 = function get_foo14() { return ""; }; - var foo40 = function foo40() { + var get_foo14 = function get_foo14() { return ""; }; - var foo41 = function foo41() { + var get_foo15 = function get_foo15() { return ""; }; - var foo41 = function foo41(value) {}; - var foo42 = function foo42() { + var set_foo = function set_foo(value) {}; + var get_foo16 = function get_foo16() { return ""; }; - var foo42 = function foo42() {}; - var foo43 = function foo43() { + var foo20 = function foo20() {}; + var get_foo17 = function get_foo17() { return ""; }; - var foo43 = function foo43() {}; - var foo44 = function foo44() { + var foo21 = function foo21() {}; + var get_foo18 = function get_foo18() { return ""; }; - var foo44 = function foo44() { + var get_foo18 = function get_foo18() { return ""; }; - var foo45 = function foo45() { + var get_foo19 = function get_foo19() { return ""; }; - var foo45 = function foo45(value) {}; + var set_foo13 = function set_foo13(value) {}; + var _foo = new WeakMap(); var A_StaticGetter_Field = function A_StaticGetter_Field() { "use strict"; _classCallCheck(this, A_StaticGetter_Field); @@ -572,61 +682,99 @@ function StaticGetter() { value: "foo" }); }; - var _foo = new WeakMap(); - var _foo40 = new WeakSet(); + var _foo = { + get: get_foo, + set: void 0 + }; + var _foo42 = new WeakSet(); var A_StaticGetter_Method = function A_StaticGetter_Method() { "use strict"; _classCallCheck(this, A_StaticGetter_Method); - _classPrivateMethodInit(this, _foo40); + _classPrivateMethodInit(this, _foo42); }; - var _foo41 = new WeakSet(); + var _foo42 = { + get: get_foo13, + set: void 0 + }; + var _foo43 = new WeakMap(); var A_StaticGetter_Getter = function A_StaticGetter_Getter() { "use strict"; _classCallCheck(this, A_StaticGetter_Getter); - _classPrivateMethodInit(this, _foo41); + _classPrivateFieldInit(this, _foo43, { + get: get_foo14, + set: void 0 + }); }; - var _foo42 = new WeakSet(); + var _foo43 = { + get: get_foo14, + set: void 0 + }; + var _foo44 = new WeakMap(); var A_StaticGetter_Setter = function A_StaticGetter_Setter() { "use strict"; _classCallCheck(this, A_StaticGetter_Setter); - _classPrivateMethodInit(this, _foo42); + _classPrivateFieldInit(this, _foo44, { + get: void 0, + set: set_foo + }); + }; + var _foo44 = { + get: get_foo15, + set: void 0 }; var A_StaticGetter_StaticField = function A_StaticGetter_StaticField() { "use strict"; _classCallCheck(this, A_StaticGetter_StaticField); }; + var _foo45 = { + get: get_foo16, + set: void 0 + }; var A_StaticGetter_StaticMethod = function A_StaticGetter_StaticMethod() { "use strict"; _classCallCheck(this, A_StaticGetter_StaticMethod); }; + var _foo46 = { + get: get_foo17, + set: void 0 + }; var A_StaticGetter_StaticGetter = function A_StaticGetter_StaticGetter() { "use strict"; _classCallCheck(this, A_StaticGetter_StaticGetter); }; + var _foo47 = { + get: get_foo18, + set: void 0 + }; var A_StaticGetter_StaticSetter = function A_StaticGetter_StaticSetter() { "use strict"; _classCallCheck(this, A_StaticGetter_StaticSetter); }; + var _foo48 = { + get: get_foo19, + set: set_foo13 + }; } function StaticSetter() { - var foo = function foo(value) {}; - var foo46 = function foo46(value) {}; - var foo46 = function foo46() {}; - var foo47 = function foo47(value) {}; - var foo47 = function foo47() { + var set_foo = function set_foo(value) {}; + var set_foo14 = function set_foo14(value) {}; + var foo = function foo() {}; + var set_foo15 = function set_foo15(value) {}; + var get_foo = function get_foo() { return ""; }; - var foo48 = function foo48(value) {}; - var foo48 = function foo48(value) {}; - var foo49 = function foo49(value) {}; - var foo50 = function foo50(value) {}; - var foo50 = function foo50() {}; - var foo51 = function foo51(value) {}; - var foo51 = function foo51() { + var set_foo16 = function set_foo16(value) {}; + var set_foo16 = function set_foo16(value) {}; + var set_foo17 = function set_foo17(value) {}; + var set_foo18 = function set_foo18(value) {}; + var foo22 = function foo22() {}; + var set_foo19 = function set_foo19(value) {}; + var get_foo20 = function get_foo20() { return ""; }; - var foo52 = function foo52(value) {}; - var foo52 = function foo52(value) {}; + var set_foo20 = function set_foo20(value) {}; + var set_foo20 = function set_foo20(value) {}; + var _foo = new WeakMap(); var A_StaticSetter_Field = function A_StaticSetter_Field() { "use strict"; _classCallCheck(this, A_StaticSetter_Field); @@ -635,30 +783,55 @@ function StaticSetter() { value: "foo" }); }; - var _foo = new WeakMap(); - var _foo43 = new WeakSet(); + var _foo = { + get: void 0, + set: set_foo + }; + var _foo49 = new WeakSet(); var A_StaticSetter_Method = function A_StaticSetter_Method() { "use strict"; _classCallCheck(this, A_StaticSetter_Method); - _classPrivateMethodInit(this, _foo43); + _classPrivateMethodInit(this, _foo49); }; - var _foo44 = new WeakSet(); + var _foo49 = { + get: void 0, + set: set_foo14 + }; + var _foo50 = new WeakMap(); var A_StaticSetter_Getter = function A_StaticSetter_Getter() { "use strict"; _classCallCheck(this, A_StaticSetter_Getter); - _classPrivateMethodInit(this, _foo44); + _classPrivateFieldInit(this, _foo50, { + get: get_foo, + set: void 0 + }); }; - var _foo45 = new WeakSet(); + var _foo50 = { + get: void 0, + set: set_foo15 + }; + var _foo51 = new WeakMap(); var A_StaticSetter_Setter = function A_StaticSetter_Setter() { "use strict"; _classCallCheck(this, A_StaticSetter_Setter); - _classPrivateMethodInit(this, _foo45); + _classPrivateFieldInit(this, _foo51, { + get: void 0, + set: set_foo16 + }); + }; + var _foo51 = { + get: void 0, + set: set_foo16 }; var A_StaticSetter_StaticField = function A_StaticSetter_StaticField() { "use strict"; _classCallCheck(this, A_StaticSetter_StaticField); }; - var _foo46 = { + var _foo52 = { + get: void 0, + set: set_foo17 + }; + var _foo52 = { writable: true, value: "foo" }; @@ -666,12 +839,24 @@ function StaticSetter() { "use strict"; _classCallCheck(this, A_StaticSetter_StaticMethod); }; + var _foo53 = { + get: void 0, + set: set_foo18 + }; var A_StaticSetter_StaticGetter = function A_StaticSetter_StaticGetter() { "use strict"; _classCallCheck(this, A_StaticSetter_StaticGetter); }; + var _foo54 = { + get: get_foo20, + set: set_foo19 + }; var A_StaticSetter_StaticSetter = function A_StaticSetter_StaticSetter() { "use strict"; _classCallCheck(this, A_StaticSetter_StaticSetter); }; + var _foo55 = { + get: void 0, + set: set_foo20 + }; } diff --git a/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.1.normal.js index 76b12f00b2b..ab99cb04137 100644 --- a/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.1.normal.js @@ -11,7 +11,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _method = new WeakSet(), _acc = new WeakSet(), _acc = new WeakSet(); +var _field = new WeakMap(), _method = new WeakSet(), _acc = new WeakMap(); // @target: es5, es3 class A { constructor(){ @@ -20,22 +20,27 @@ class A { value: 123 }); _classPrivateMethodInit(this, _method); - _classPrivateMethodInit(this, _acc); - _classPrivateMethodInit(this, _acc); + _classPrivateFieldInit(this, _acc, { + get: get_acc, + set: set_acc + }); } } -var _field = new WeakMap(); var _sField = { writable: true, value: "hello world" }; +var _sAcc = { + get: get_sAcc, + set: set_sAcc +}; function method() {} function sMethod() {} -function acc() { +function get_acc() { return ""; } -function acc(x) {} -function sAcc() { +function set_acc(x) {} +function get_sAcc() { return 0; } -function sAcc(x) {} +function set_sAcc(x) {} diff --git a/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.2.minified.js index fa627425d84..08a513adefa 100644 --- a/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameES5Ban_es2015.2.minified.js @@ -1 +1 @@ -new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(); +new WeakMap(), new WeakSet(), new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameES5Ban_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameES5Ban_es5.1.normal.js index 2c7a83ee3a6..49dbc1bbdca 100644 --- a/crates/swc/tests/tsc-references/privateNameES5Ban_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameES5Ban_es5.1.normal.js @@ -16,7 +16,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _method = new WeakSet(), _acc = new WeakSet(), _acc = new WeakSet(); +var _field = new WeakMap(), _method = new WeakSet(), _acc = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -25,21 +25,26 @@ var A = function A() { value: 123 }); _classPrivateMethodInit(this, _method); - _classPrivateMethodInit(this, _acc); - _classPrivateMethodInit(this, _acc); + _classPrivateFieldInit(this, _acc, { + get: get_acc, + set: set_acc + }); }; -var _field = new WeakMap(); var _sField = { writable: true, value: "hello world" }; +var _sAcc = { + get: get_sAcc, + set: set_sAcc +}; function method() {} function sMethod() {} -function acc() { +function get_acc() { return ""; } -function acc(x) {} -function sAcc() { +function set_acc(x) {} +function get_sAcc() { return 0; } -function sAcc(x) {} +function set_sAcc(x) {} diff --git a/crates/swc/tests/tsc-references/privateNameES5Ban_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameES5Ban_es5.2.minified.js index 9f359ea80dd..556a9676b0b 100644 --- a/crates/swc/tests/tsc-references/privateNameES5Ban_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameES5Ban_es5.2.minified.js @@ -1,16 +1,23 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); } -var _method = new WeakSet(), _acc = new WeakSet(), _acc = new WeakSet(), A = function() { +var _field = new WeakMap(), _method = new WeakSet(), _acc = new WeakMap(), A = function() { "use strict"; - var obj, privateMap, value; + var obj, privateSet; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, A), obj = this, value = { + }(this, A), _classPrivateFieldInit(this, _field, { writable: !0, value: 123 - }, _checkPrivateRedeclaration(obj, privateMap = _field), privateMap.set(obj, value), _classPrivateMethodInit(this, _method), _classPrivateMethodInit(this, _acc), _classPrivateMethodInit(this, _acc); -}, _field = new WeakMap(); + }), obj = this, _checkPrivateRedeclaration(obj, privateSet = _method), privateSet.add(obj), _classPrivateFieldInit(this, _acc, { + get: get_acc, + set: set_acc + }); +}; +function get_acc() { + return ""; +} +function set_acc(x) {} diff --git a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.1.normal.js index 215d50517d6..f099daa8032 100644 --- a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.1.normal.js @@ -3,32 +3,46 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _b = new WeakSet(), _c = new WeakSet(); +var _a = new WeakMap(), _b = new WeakSet(), _c = new WeakMap(); // @target: es2015 // @importHelpers: true // @isolatedModules: true @@ -40,13 +54,15 @@ export class C { value: 1 }); _classPrivateMethodInit(this, _b); - _classPrivateMethodInit(this, _c); + _classPrivateFieldInit(this, _c, { + get: void 0, + set: set_c + }); } } -var _a = new WeakMap(); function b() { _classPrivateFieldSet(this, _c, 42); } -function c(v) { +function set_c(v) { _classPrivateFieldSet(this, _a, _classPrivateFieldGet(this, _a) + v); } diff --git a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.2.minified.js index 375ac1a1e01..eaa4f9369e9 100644 --- a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es2015.2.minified.js @@ -1,18 +1,33 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); } -var _b = new WeakSet(), _c = new WeakSet(); +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); +} +var _a = new WeakMap(), _b = new WeakSet(), _c = new WeakMap(); export class C { constructor(){ - !function(obj, privateMap, value) { - _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); - }(this, _a, { + _classPrivateFieldInit(this, _a, { writable: !0, value: 1 - }), _classPrivateMethodInit(this, _b), _classPrivateMethodInit(this, _c); + }), (function(obj, privateSet) { + _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); + })(this, _b), _classPrivateFieldInit(this, _c, { + get: void 0, + set: function(v) { + var receiver, privateMap, descriptor, receiver, descriptor, receiver, privateMap, value, descriptor; + receiver = this, privateMap = _a, receiver = this, value = ((descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _a, "get")).get ? descriptor.get.call(receiver) : descriptor.value) + v, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); + } + }); } } -var _a = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.1.normal.js index 576de1179ff..f8299bf2205 100644 --- a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.1.normal.js @@ -3,37 +3,51 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _b = new WeakSet(), _c = new WeakSet(); +var _a = new WeakMap(), _b = new WeakSet(), _c = new WeakMap(); // @target: es2015 // @importHelpers: true // @isolatedModules: true @@ -46,12 +60,14 @@ export var C = function C() { value: 1 }); _classPrivateMethodInit(this, _b); - _classPrivateMethodInit(this, _c); + _classPrivateFieldInit(this, _c, { + get: void 0, + set: set_c + }); }; -var _a = new WeakMap(); function b() { _classPrivateFieldSet(this, _c, 42); } -function c(v) { +function set_c(v) { _classPrivateFieldSet(this, _a, _classPrivateFieldGet(this, _a) + v); } diff --git a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.2.minified.js index 25cb41fb505..0534ee6bb51 100644 --- a/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameEmitHelpers_es5.2.minified.js @@ -1,18 +1,34 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); } -var _b = new WeakSet(), _c = new WeakSet(); +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); +} +var _a = new WeakMap(), _b = new WeakSet(), _c = new WeakMap(); export var C = function() { "use strict"; - var obj, privateMap, value; + var obj, privateSet; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, C), obj = this, value = { + }(this, C), _classPrivateFieldInit(this, _a, { writable: !0, value: 1 - }, _checkPrivateRedeclaration(obj, privateMap = _a), privateMap.set(obj, value), _classPrivateMethodInit(this, _b), _classPrivateMethodInit(this, _c); + }), obj = this, _checkPrivateRedeclaration(obj, privateSet = _b), privateSet.add(obj), _classPrivateFieldInit(this, _c, { + get: void 0, + set: set_c + }); }; -var _a = new WeakMap(); +function set_c(v) { + var receiver, privateMap, descriptor, receiver, descriptor, receiver, privateMap, value, descriptor; + receiver = this, privateMap = _a, receiver = this, value = ((descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _a, "get")).get ? descriptor.get.call(receiver) : descriptor.value) + v, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); +} diff --git a/crates/swc/tests/tsc-references/privateNameFieldAccess_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldAccess_es2015.1.normal.js index 0f21f6873b9..22fdecc350f 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAccess_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAccess_es2015.1.normal.js @@ -3,16 +3,27 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _myField = new WeakMap(); // @target: es2015 class A { constructor(){ @@ -23,4 +34,3 @@ class A { console.log(_classPrivateFieldGet(this, _myField)); } } -var _myField = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.1.normal.js index faf7a6e66c0..c8ae27c8a63 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.1.normal.js @@ -3,21 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _myField = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -27,4 +38,3 @@ var A = function A() { }); console.log(_classPrivateFieldGet(this, _myField)); }; -var _myField = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.2.minified.js index 4f013cc3a96..a5379474552 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAccess_es5.2.minified.js @@ -1,6 +1,6 @@ -var A = function() { +var _myField = new WeakMap(), A = function() { "use strict"; - var obj, privateMap, value; + var obj, privateMap, value, receiver, privateMap, descriptor, receiver, descriptor; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, A), obj = this, value = { @@ -8,8 +8,8 @@ var A = function() { value: "hello world" }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateMap = _myField), privateMap.set(obj, value), console.log(function(receiver, privateMap) { + })(obj, privateMap = _myField), privateMap.set(obj, value), console.log((receiver = this, (descriptor = descriptor = function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(this, _myField)); -}, _myField = new WeakMap(); + return privateMap.get(receiver); + }(receiver, privateMap = _myField, "get")).get ? descriptor.get.call(receiver) : descriptor.value)); +}; diff --git a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.1.normal.js index f4a5070ba65..62613ce41c2 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.1.normal.js @@ -3,27 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _field = new WeakMap(); // @target: es2015 class A { static getInstance() { @@ -63,4 +78,3 @@ class A { _classPrivateFieldSet(_ref11 = A.getInstance(), _field, _classPrivateFieldGet(_ref11, _field) ^ 13); } } -var _field = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.2.minified.js index 34da1008aa1..efd456229f8 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es2015.2.minified.js @@ -1,13 +1,22 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } +var _field = new WeakMap(); class A { static getInstance() { return new A(); @@ -24,4 +33,3 @@ class A { }), _classPrivateFieldSet(this, _field, 1), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) + 2), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) - 3), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) / 4), _classPrivateFieldSet(this, _field, 5 * _classPrivateFieldGet(this, _field)), _classPrivateFieldSet(this, _field, Math.pow(_classPrivateFieldGet(this, _field), 6)), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) % 7), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) << 8), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) >> 9), _classPrivateFieldSet(this, _field, _classPrivateFieldGet(this, _field) >>> 10), _classPrivateFieldSet(this, _field, 11 & _classPrivateFieldGet(this, _field)), _classPrivateFieldSet(this, _field, 12 | _classPrivateFieldGet(this, _field)), _classPrivateFieldSet(this, _field, 13 ^ _classPrivateFieldGet(this, _field)), _classPrivateFieldSet(A.getInstance(), _field, 1), _classPrivateFieldSet(_ref = A.getInstance(), _field, _classPrivateFieldGet(_ref, _field) + 2), _classPrivateFieldSet(_ref1 = A.getInstance(), _field, _classPrivateFieldGet(_ref1, _field) - 3), _classPrivateFieldSet(_ref2 = A.getInstance(), _field, _classPrivateFieldGet(_ref2, _field) / 4), _classPrivateFieldSet(_ref3 = A.getInstance(), _field, 5 * _classPrivateFieldGet(_ref3, _field)), _classPrivateFieldSet(_ref4 = A.getInstance(), _field, Math.pow(_classPrivateFieldGet(_ref4, _field), 6)), _classPrivateFieldSet(_ref5 = A.getInstance(), _field, _classPrivateFieldGet(_ref5, _field) % 7), _classPrivateFieldSet(_ref6 = A.getInstance(), _field, _classPrivateFieldGet(_ref6, _field) << 8), _classPrivateFieldSet(_ref7 = A.getInstance(), _field, _classPrivateFieldGet(_ref7, _field) >> 9), _classPrivateFieldSet(_ref8 = A.getInstance(), _field, _classPrivateFieldGet(_ref8, _field) >>> 10), _classPrivateFieldSet(_ref9 = A.getInstance(), _field, 11 & _classPrivateFieldGet(_ref9, _field)), _classPrivateFieldSet(_ref10 = A.getInstance(), _field, 12 | _classPrivateFieldGet(_ref10, _field)), _classPrivateFieldSet(_ref11 = A.getInstance(), _field, 13 ^ _classPrivateFieldGet(_ref11, _field)); } } -var _field = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.1.normal.js index 3a0160cdbff..b3e56cd4bf5 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _field = new WeakMap(); var A = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -90,4 +105,3 @@ var A = // @target: es2015 ]); return A; }(); -var _field = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.2.minified.js index fc9b2aeb25c..0d3bf4d3e9e 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldAssignment_es5.2.minified.js @@ -1,12 +1,20 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -14,7 +22,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var A = function() { +var _field = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A() { @@ -36,4 +44,4 @@ var A = function() { } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _field = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.1.normal.js index 86c71875557..78b3446ed96 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.1.normal.js @@ -3,16 +3,27 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); // @target: es2015 class A { test() { @@ -49,5 +60,3 @@ class A { this.x = 1; } } -var _fieldFunc = new WeakMap(); -var _fieldFunc2 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.2.minified.js index 151fb435557..0c7b863b243 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es2015.2.minified.js @@ -1,12 +1,16 @@ function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); class A { test() { var ref, _ref; @@ -34,4 +38,3 @@ class A { }), this.x = 1; } } -var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.1.normal.js index 74858753b8c..114bbee0b27 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.1.normal.js @@ -11,16 +11,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -124,6 +134,7 @@ function _templateObject1() { }; return data; } +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); var A = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -185,5 +196,3 @@ var A = // @target: es2015 ]); return A; }(); -var _fieldFunc = new WeakMap(); -var _fieldFunc2 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.2.minified.js index fe75a51d335..d2d406a5e9c 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldCallExpression_es5.2.minified.js @@ -4,8 +4,11 @@ function _arrayLikeToArray(arr, len) { return arr2; } function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -86,7 +89,7 @@ function _templateObject1() { return data; }, data; } -var A = function() { +var _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(), A = function() { "use strict"; var Constructor, protoProps, staticProps; function A() { @@ -132,4 +135,4 @@ var A = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _fieldFunc = new WeakMap(), _fieldFunc2 = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es2015.1.normal.js index b7ea8405d43..e90a88fcb11 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es2015.1.normal.js @@ -8,6 +8,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { privateMap.set(obj, value); } var _class, _class1; +var _foo = new WeakMap(), _foo2 = new WeakMap(); // @target: es2015 class B { constructor(){ @@ -26,5 +27,3 @@ class B { }); } } -var _foo = new WeakMap(); -var _foo2 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.1.normal.js index 208d2096fd6..b1de7936af1 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.1.normal.js @@ -13,6 +13,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { privateMap.set(obj, value); } var _class, _class1; +var _foo = new WeakMap(), _foo2 = new WeakMap(); var B = function B() { "use strict"; _classCallCheck(this, B); @@ -30,5 +31,3 @@ var B = function B() { }, _class1.otherClass = 123, _class1) }); }; -var _foo = new WeakMap(); -var _foo2 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.2.minified.js index 3f85c36104c..8b83e960bc5 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldClassExpression_es5.2.minified.js @@ -6,7 +6,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var _class, _class1, B = function() { +var _class, _class1, _foo = new WeakMap(), _foo2 = new WeakMap(), B = function() { "use strict"; _classCallCheck(this, B), _classPrivateFieldInit(this, _foo, { writable: !0, @@ -19,4 +19,4 @@ var _class, _class1, B = function() { _classCallCheck(this, Foo); }).otherClass = 123, _class1) }); -}, _foo = new WeakMap(), _foo2 = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es2015.1.normal.js index 860aae4b504..fcf35ec766f 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _field = new WeakMap(), _uninitialized = new WeakMap(); // @target: es2015 class A { constructor(){ @@ -20,5 +21,3 @@ class A { }); } } -var _field = new WeakMap(); -var _uninitialized = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.1.normal.js index f6417956116..0628b686894 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _field = new WeakMap(), _uninitialized = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -24,5 +25,3 @@ var A = function A() { value: void 0 }); }; -var _field = new WeakMap(); -var _uninitialized = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.2.minified.js index e1387d15923..ef37686abf9 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldInitializer_es5.2.minified.js @@ -3,7 +3,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var A = function() { +var _field = new WeakMap(), _uninitialized = new WeakMap(), A = function() { "use strict"; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); @@ -14,4 +14,4 @@ var A = function() { writable: !0, value: void 0 }); -}, _field = new WeakMap(), _uninitialized = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.1.normal.js index 2121ceee716..fb722fdd7c0 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.1.normal.js @@ -3,27 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _test = new WeakMap(); // @target: es2015 class C { test() { @@ -60,4 +75,3 @@ class C { for(_classPrivateFieldSet(this, _test, 0); _classPrivateFieldGet(this, _test) < 10; _classPrivateFieldSet(this, _test, (_this_test8 = +_classPrivateFieldGet(this, _test)) + 1), _this_test8){} } } -var _test = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.2.minified.js index c25aa26ff47..b1761708ab7 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es2015.2.minified.js @@ -1,13 +1,22 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } +var _test = new WeakMap(); class C { test() { var _ref, _ref1, _ref2, _ref3, _ref4, _ref5, _ref6, _ref7, _ref8, _ref9, _this_test; @@ -30,4 +39,3 @@ class C { for(_classPrivateFieldSet(this, _test, 0); 10 > _classPrivateFieldGet(this, _test); _classPrivateFieldSet(this, _test, (_this_test = +_classPrivateFieldGet(this, _test)) + 1), _this_test); } } -var _test = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.1.normal.js index 925dcd0791c..6f515cb14db 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _test = new WeakMap(); var C = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -90,4 +105,3 @@ var C = // @target: es2015 ]); return C; }(); -var _test = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.2.minified.js index 9a11a75a5ea..0609a349587 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldUnaryMutation_es5.2.minified.js @@ -1,12 +1,20 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -14,7 +22,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var C = function() { +var _test = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -45,4 +53,4 @@ var C = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; -}(), _test = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameField_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameField_es2015.1.normal.js index e2509aaf8e4..61557239c04 100644 --- a/crates/swc/tests/tsc-references/privateNameField_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameField_es2015.1.normal.js @@ -3,21 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _name = new WeakMap(); // @strict: true // @target: es6 class A { @@ -29,4 +40,3 @@ class A { _classPrivateFieldSet(this, _name, name); } } -var _name = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameField_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameField_es5.1.normal.js index 7ac837fffa8..a676ad92f7c 100644 --- a/crates/swc/tests/tsc-references/privateNameField_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameField_es5.1.normal.js @@ -3,26 +3,37 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _name = new WeakMap(); var A = function A(name) { "use strict"; _classCallCheck(this, A); @@ -32,4 +43,3 @@ var A = function A(name) { }); _classPrivateFieldSet(this, _name, name); }; -var _name = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameField_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameField_es5.2.minified.js index 109bac1c479..d97799292ca 100644 --- a/crates/swc/tests/tsc-references/privateNameField_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameField_es5.2.minified.js @@ -1,6 +1,6 @@ -var A = function(name) { +var _name = new WeakMap(), A = function(name) { "use strict"; - var obj, privateMap, value; + var obj, privateMap, value, receiver, privateMap, value, descriptor; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, A), obj = this, value = { @@ -8,10 +8,14 @@ var A = function(name) { value: void 0 }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateMap = _name), privateMap.set(obj, value), (function(receiver, privateMap, value) { + })(obj, privateMap = _name), privateMap.set(obj, value), receiver = this, privateMap = _name, value = name, descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(this, _name, name); -}, _name = new WeakMap(); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); +}; diff --git a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.1.normal.js index cabcce3a808..0326f3313ad 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.1.normal.js @@ -3,43 +3,63 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; } +var _a = new WeakMap(), _b = new WeakMap(), _something = new WeakMap(); // @target: esnext, es2022 // @useDefineForClassFields: false class C { @@ -69,8 +89,6 @@ class C { this.c = "hello"; } } -var _a = new WeakMap(); -var _b = new WeakMap(); var _m = { writable: true, value: "test" @@ -79,4 +97,3 @@ var _x = { writable: true, value: void 0 }; -var _something = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.2.minified.js index c18e1214987..6a37c01f507 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es2015.2.minified.js @@ -1,30 +1,41 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} +var _a = new WeakMap(), _b = new WeakMap(), _something = new WeakMap(); class C { method() { - console.log(_classPrivateFieldGet(this, _a)), (function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(this, _a, "hello"), console.log(_classPrivateFieldGet(this, _b)); + var receiver, privateMap, value, descriptor; + console.log(_classPrivateFieldGet(this, _a)), receiver = this, value = "hello", descriptor = _classExtractFieldDescriptor(receiver, privateMap = _a, "set"), _classApplyDescriptorSet(receiver, descriptor, value), console.log(_classPrivateFieldGet(this, _b)); } static test() { - console.log(function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - }(this, C, _m)), console.log(function(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, C, _x, "test")); + var receiver, classConstructor, descriptor, receiver, classConstructor, descriptor, value; + console.log((receiver = this, classConstructor = C, descriptor = _m, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor))), console.log((receiver = this, classConstructor = C, descriptor = _x, value = "test", _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), _classApplyDescriptorSet(receiver, descriptor, value), value)); } constructor(){ _classPrivateFieldInit(this, _a, { @@ -39,10 +50,10 @@ class C { }), this.a = 123, this.c = "hello"; } } -var _a = new WeakMap(), _b = new WeakMap(), _m = { +var _m = { writable: !0, value: "test" }, _x = { writable: !0, value: void 0 -}, _something = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.1.normal.js index 9f7b26752c5..df7ff69f8c2 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.1.normal.js @@ -3,46 +3,60 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return privateMap.get(receiver).value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -59,6 +73,12 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} +var _a = new WeakMap(), _b = new WeakMap(), _something = new WeakMap(); var C = // @target: esnext, es2022 // @useDefineForClassFields: false /*#__PURE__*/ function() { @@ -102,8 +122,6 @@ var C = // @target: esnext, es2022 ]); return C; }(); -var _a = new WeakMap(); -var _b = new WeakMap(); var _m = { writable: true, value: "test" @@ -112,4 +130,3 @@ var _x = { writable: true, value: void 0 }; -var _something = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.2.minified.js index 90f12489287..5b86ca81cb0 100644 --- a/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameFieldsESNext_es5.2.minified.js @@ -1,6 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { @@ -13,7 +30,10 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var C = function() { +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} +var _a = new WeakMap(), _b = new WeakMap(), _something = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { @@ -36,33 +56,23 @@ var C = function() { { key: "method", value: function() { - console.log(_classPrivateFieldGet(this, _a)), (function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - descriptor.value = value; - })(this, _a, "hello"), console.log(_classPrivateFieldGet(this, _b)); + var receiver, privateMap, value, descriptor; + console.log(_classPrivateFieldGet(this, _a)), receiver = this, value = "hello", descriptor = _classExtractFieldDescriptor(receiver, privateMap = _a, "set"), _classApplyDescriptorSet(receiver, descriptor, value), console.log(_classPrivateFieldGet(this, _b)); } } ], staticProps = [ { key: "test", value: function() { - console.log(function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - }(this, C, _m)), console.log(function(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, C, _x, "test")); + var receiver, classConstructor, descriptor, receiver, classConstructor, descriptor, value; + console.log((receiver = this, classConstructor = C, descriptor = _m, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor))), console.log((receiver = this, classConstructor = C, descriptor = _x, value = "test", _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), _classApplyDescriptorSet(receiver, descriptor, value), value)); } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; -}(), _a = new WeakMap(), _b = new WeakMap(), _m = { +}(), _m = { writable: !0, value: "test" }, _x = { writable: !0, value: void 0 -}, _something = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es2015.1.normal.js index 5e4bf7b2319..83c41078e19 100644 --- a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es2015.1.normal.js @@ -8,6 +8,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { privateMap.set(obj, value); } var _brand_check_brand = new WeakSet(); +var _unused = new WeakMap(), _brand = new WeakMap(); // @strict: true // @noUnusedLocals: true // @target: esnext, es2022 @@ -24,8 +25,7 @@ class Foo { _classPrivateFieldInit(this, _brand, { writable: true, value: void _brand_check_brand.add(this) - }); + }) // expect no error + ; } } -var _unused = new WeakMap(); -var _brand = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.1.normal.js index e05bd3fd167..bd31f815549 100644 --- a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.1.normal.js @@ -27,6 +27,7 @@ function _createClass(Constructor, protoProps, staticProps) { return Constructor; } var _brand_check_brand = new WeakSet(); +var _unused = new WeakMap(), _brand = new WeakMap(); var Foo = // @strict: true // @noUnusedLocals: true // @target: esnext, es2022 @@ -41,7 +42,8 @@ var Foo = // @strict: true _classPrivateFieldInit(this, _brand, { writable: true, value: void _brand_check_brand.add(this) - }); + }) // expect no error + ; } _createClass(Foo, [ { @@ -54,5 +56,3 @@ var Foo = // @strict: true ]); return Foo; }(); -var _unused = new WeakMap(); -var _brand = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.2.minified.js index d8ccf693327..fc161d6ee12 100644 --- a/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameInInExpressionUnused_es5.2.minified.js @@ -9,7 +9,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var _brand_check_brand = new WeakSet(), Foo = function() { +var _brand_check_brand = new WeakSet(), _unused = new WeakMap(), _brand = new WeakMap(), Foo = function() { "use strict"; var Constructor, protoProps, staticProps; function Foo() { @@ -31,4 +31,4 @@ var _brand_check_brand = new WeakSet(), Foo = function() { } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Foo; -}(), _unused = new WeakMap(), _brand = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es2015.1.normal.js index 1b32942d815..575ec1e830a 100644 --- a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es2015.1.normal.js @@ -3,32 +3,48 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _y = new WeakMap(); // @target: es2015 class Test { static something(obj) { var _s; _classPrivateFieldSet(obj[(new (function() { + var _x = new WeakMap(); class _class { constructor(){ _classPrivateFieldInit(this, _x, { @@ -38,10 +54,10 @@ class Test { this.s = "prop"; } } - var _x = new WeakMap(); return _class; }())).s], _y, 1); _classPrivateFieldSet(_s = obj[(new (function() { + var _x = new WeakMap(); class _class { constructor(){ _classPrivateFieldInit(this, _x, { @@ -51,7 +67,6 @@ class Test { this.s = "prop"; } } - var _x = new WeakMap(); return _class; }())).s], _y, _classPrivateFieldGet(_s, _y) + 1); } @@ -62,4 +77,3 @@ class Test { }); } } -var _y = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.1.normal.js index 240cf5af87e..c86a6aa3594 100644 --- a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.1.normal.js @@ -3,30 +3,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -43,6 +57,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _y = new WeakMap(); var Test = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -59,6 +74,7 @@ var Test = // @target: es2015 value: function something(obj) { var _s; _classPrivateFieldSet(obj[(new (function() { + var _x = new WeakMap(); var _class = function _class() { _classCallCheck(this, _class); _classPrivateFieldInit(this, _x, { @@ -67,10 +83,10 @@ var Test = // @target: es2015 }); this.s = "prop"; }; - var _x = new WeakMap(); return _class; }())).s], _y, 1); _classPrivateFieldSet(_s = obj[(new (function() { + var _x = new WeakMap(); var _class = function _class() { _classCallCheck(this, _class); _classPrivateFieldInit(this, _x, { @@ -79,7 +95,6 @@ var Test = // @target: es2015 }); this.s = "prop"; }; - var _x = new WeakMap(); return _class; }())).s], _y, _classPrivateFieldGet(_s, _y) + 1); } @@ -87,4 +102,3 @@ var Test = // @target: es2015 ]); return Test; }(); -var _y = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.2.minified.js index bfc9d4d3d2f..0cdf4d97e9d 100644 --- a/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameInLhsReceiverExpression_es5.2.minified.js @@ -1,16 +1,24 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -18,7 +26,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var Test = function() { +var _y = new WeakMap(), Test = function() { "use strict"; var Constructor, protoProps, staticProps; function Test() { @@ -31,22 +39,19 @@ var Test = function() { { key: "something", value: function(obj) { - var _s, _class, _x, _class1, _x1; - _classPrivateFieldSet(obj[(new (_class = function() { + var _s, _x, _class, _x1, _class1, receiver, privateMap, descriptor, receiver, descriptor; + _classPrivateFieldSet(obj[(new (_x = new WeakMap(), _class = function() { _classCallCheck(this, _class), _classPrivateFieldInit(this, _x, { writable: !0, value: 1 }), this.s = "prop"; - }, _x = new WeakMap(), _class)).s], _y, 1), _classPrivateFieldSet(_s = obj[(new (_class1 = function() { + })).s], _y, 1), _classPrivateFieldSet(_s = obj[(new (_x1 = new WeakMap(), _class1 = function() { _classCallCheck(this, _class1), _classPrivateFieldInit(this, _x1, { writable: !0, value: 1 }), this.s = "prop"; - }, _x1 = new WeakMap(), _class1)).s], _y, function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(_s, _y) + 1); + })).s], _y, ((descriptor = descriptor = _classExtractFieldDescriptor(receiver = _s, privateMap = _y, "get")).get ? descriptor.get.call(receiver) : descriptor.value) + 1); } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Test; -}(), _y = new WeakMap(); +}(); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es2015.1.normal.js index a6e3799215d..1b513616d65 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es2015.1.normal.js @@ -11,6 +11,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { // @useDefineForClassFields: true class B { } +var _x = new WeakMap(); class A extends B { constructor(){ void 0; @@ -21,4 +22,3 @@ class A extends B { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.1.normal.js index 2ae9db4aad5..70d37a6eda9 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.1.normal.js @@ -82,6 +82,7 @@ var B = function B() { "use strict"; _classCallCheck(this, B); }; +var _x = new WeakMap(); var A = /*#__PURE__*/ function(B) { "use strict"; _inherits(A, B); @@ -99,4 +100,3 @@ var A = /*#__PURE__*/ function(B) { } return A; }(B); -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.2.minified.js index 96fe9c3c3d7..0b1229365b5 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuperUseDefineForClassFields_es5.2.minified.js @@ -18,7 +18,7 @@ function _setPrototypeOf(o, p) { var B = function() { "use strict"; _classCallCheck(this, B); -}, A = function(B1) { +}, _x = new WeakMap(), A = function(B1) { "use strict"; !function(subClass, superClass) { if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function"); @@ -57,4 +57,4 @@ var B = function() { })(obj = _assertThisInitialized(_this = _super.call(this)), privateMap = _x), privateMap.set(obj, value), _this; } return A; -}(B), _x = new WeakMap(); +}(B); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuper_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameLateSuper_es2015.1.normal.js index 11f69d2805d..7c6660cbdd5 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuper_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuper_es2015.1.normal.js @@ -10,6 +10,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { // @target: es2015 class B { } +var _x = new WeakMap(); class A extends B { constructor(){ void 0; @@ -20,4 +21,3 @@ class A extends B { }); } } -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuper_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameLateSuper_es5.1.normal.js index 2ae9db4aad5..70d37a6eda9 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuper_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuper_es5.1.normal.js @@ -82,6 +82,7 @@ var B = function B() { "use strict"; _classCallCheck(this, B); }; +var _x = new WeakMap(); var A = /*#__PURE__*/ function(B) { "use strict"; _inherits(A, B); @@ -99,4 +100,3 @@ var A = /*#__PURE__*/ function(B) { } return A; }(B); -var _x = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameLateSuper_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameLateSuper_es5.2.minified.js index 96fe9c3c3d7..0b1229365b5 100644 --- a/crates/swc/tests/tsc-references/privateNameLateSuper_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameLateSuper_es5.2.minified.js @@ -18,7 +18,7 @@ function _setPrototypeOf(o, p) { var B = function() { "use strict"; _classCallCheck(this, B); -}, A = function(B1) { +}, _x = new WeakMap(), A = function(B1) { "use strict"; !function(subClass, superClass) { if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function"); @@ -57,4 +57,4 @@ var B = function() { })(obj = _assertThisInitialized(_this = _super.call(this)), privateMap = _x), privateMap.set(obj, value), _this; } return A; -}(B), _x = new WeakMap(); +}(B); diff --git a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es2015.1.normal.js index 1cdf950f135..144746248a0 100644 --- a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es2015.1.normal.js @@ -3,32 +3,7 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; -} -function _classPrivateMethodGet(receiver, privateSet, fn) { - if (!privateSet.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); - } - return fn; -} -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); +function _classApplyDescriptorDestructureSet(receiver, descriptor) { if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { @@ -45,6 +20,41 @@ function _classPrivateFieldDestructureSet(receiver, privateMap) { return descriptor; } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classPrivateMethodGet(receiver, privateSet, fn) { + if (!privateSet.has(receiver)) { + throw new TypeError("attempted to get private field on non-instance"); + } + return fn; +} +function _classPrivateMethodInit(obj, privateSet) { + _checkPrivateRedeclaration(obj, privateSet); + privateSet.add(obj); +} +function _classPrivateFieldDestructureSet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} var _method = new WeakSet(); // @target: es2015 class A3 { diff --git a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.1.normal.js index 9f548e045ce..a1d964e4529 100644 --- a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.1.normal.js @@ -3,37 +3,7 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classCallCheck(instance, Constructor) { - if (!(instance instanceof Constructor)) { - throw new TypeError("Cannot call a class as a function"); - } -} -function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; -} -function _classPrivateMethodGet(receiver, privateSet, fn) { - if (!privateSet.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); - } - return fn; -} -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); +function _classApplyDescriptorDestructureSet(receiver, descriptor) { if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { @@ -50,6 +20,46 @@ function _classPrivateFieldDestructureSet(receiver, privateMap) { return descriptor; } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCallCheck(instance, Constructor) { + if (!(instance instanceof Constructor)) { + throw new TypeError("Cannot call a class as a function"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classPrivateMethodGet(receiver, privateSet, fn) { + if (!privateSet.has(receiver)) { + throw new TypeError("attempted to get private field on non-instance"); + } + return fn; +} +function _classPrivateMethodInit(obj, privateSet) { + _checkPrivateRedeclaration(obj, privateSet); + privateSet.add(obj); +} +function _classPrivateFieldDestructureSet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} var _method = new WeakSet(); var A3 = function A3(a, b) { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.2.minified.js index 4d633ae02b3..19989343ca5 100644 --- a/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameMethodAssignment_es5.2.minified.js @@ -1,8 +1,16 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); @@ -10,16 +18,14 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } var _method = new WeakSet(), A3 = function(a, b) { "use strict"; - var obj, privateSet, _b, ref; + var obj, privateSet, receiver, privateMap, descriptor, _b, ref; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, A3), obj = this, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); })(obj, privateSet = _method), privateSet.add(obj), _classPrivateFieldSet(this, _method, function() {}), _classPrivateFieldSet(a, _method, function() {}), _classPrivateFieldSet(b, _method, function() {}), ref = { x: function() {} - }, (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); + }, (receiver = this, descriptor = _classExtractFieldDescriptor(receiver, privateMap = _method, "set"), (function(receiver, descriptor) { if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { set value (v){ descriptor.set.call(receiver, v); @@ -27,6 +33,6 @@ var _method = new WeakSet(), A3 = function(a, b) { }), descriptor.__destrObj; if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); return descriptor; - })(this, _method).value = ref.x, _classPrivateMethodGet(this, _method, method), _classPrivateFieldSet(_b = b, _method, +_classPrivateMethodGet(_b, _method, method) + 1); + })(receiver, descriptor)).value = ref.x, _classPrivateMethodGet(this, _method, method), _classPrivateFieldSet(_b = b, _method, +_classPrivateMethodGet(_b, _method, method) + 1); }; function method() {} diff --git a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es2015.1.normal.js index dd132b8bbaa..89f01749ef3 100644 --- a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); // @target: es2015 class A { constructor(){ @@ -14,6 +15,7 @@ class A { writable: true, value: void 0 }); + var _foo1 = new WeakMap(); class A { constructor(){ _classPrivateFieldInit(this, _foo1, { @@ -22,7 +24,5 @@ class A { }); } } - var _foo1 = new WeakMap(); } } -var _foo = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.1.normal.js index 401a7cacfa8..317514bfefb 100644 --- a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -19,6 +20,7 @@ var A = function A() { writable: true, value: void 0 }); + var _foo1 = new WeakMap(); var A1 = function A1() { _classCallCheck(this, A1); _classPrivateFieldInit(this, _foo1, { @@ -26,6 +28,4 @@ var A = function A() { value: void 0 }); }; - var _foo1 = new WeakMap(); }; -var _foo = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.2.minified.js index eebd31df3fb..711167505a9 100644 --- a/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameNestedClassNameConflict_es5.2.minified.js @@ -6,16 +6,16 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var A = function() { +var _foo = new WeakMap(), A = function() { "use strict"; _classCallCheck(this, A), _classPrivateFieldInit(this, _foo, { writable: !0, value: void 0 }); - var A1 = function() { + var _foo1 = new WeakMap(), A1 = function() { _classCallCheck(this, A1), _classPrivateFieldInit(this, _foo1, { writable: !0, value: void 0 }); - }, _foo1 = new WeakMap(); -}, _foo = new WeakMap(); + }; +}; diff --git a/crates/swc/tests/tsc-references/privateNameReadonly_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameReadonly_es2015.1.normal.js index bf07837a99b..57440a8fe68 100644 --- a/crates/swc/tests/tsc-references/privateNameReadonly_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameReadonly_es2015.1.normal.js @@ -3,15 +3,25 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodInit(obj, privateSet) { diff --git a/crates/swc/tests/tsc-references/privateNameReadonly_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameReadonly_es2015.2.minified.js index 0ccdd0feb86..607b3aca175 100644 --- a/crates/swc/tests/tsc-references/privateNameReadonly_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameReadonly_es2015.2.minified.js @@ -2,12 +2,17 @@ const C = function() { var _bar = new WeakSet(); return class { foo() { - !function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor; + receiver = this, privateMap = _bar, value = console.log("should log this then throw"), descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _bar, console.log("should log this then throw")); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); } constructor(){ var obj, privateSet; diff --git a/crates/swc/tests/tsc-references/privateNameReadonly_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameReadonly_es5.1.normal.js index a9d64c81738..66c686dcd22 100644 --- a/crates/swc/tests/tsc-references/privateNameReadonly_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameReadonly_es5.1.normal.js @@ -3,20 +3,30 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodInit(obj, privateSet) { diff --git a/crates/swc/tests/tsc-references/privateNameReadonly_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameReadonly_es5.2.minified.js index eb15ae2feb6..14181f56b8f 100644 --- a/crates/swc/tests/tsc-references/privateNameReadonly_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameReadonly_es5.2.minified.js @@ -20,12 +20,17 @@ var C = function() { { key: "foo", value: function() { - !function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor; + receiver = this, privateMap = _bar, value = console.log("should log this then throw"), descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _bar, console.log("should log this then throw")); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), _class; diff --git a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.1.normal.js index 29145f5b47e..bd47547b5cb 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.1.normal.js @@ -3,22 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -var _foo = new WeakSet(); +var _foo = new WeakMap(); // @target: es2019 class C { bar() { @@ -26,8 +36,11 @@ class C { console.log(x); // 84 } constructor(){ - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: void 0, + set: set_foo + }); } } -function foo(a) {} +function set_foo(a) {} new C().bar(); diff --git a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.2.minified.js index b51cb8c23f8..5b310b2022b 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es2015.2.minified.js @@ -1,19 +1,27 @@ -var _foo = new WeakSet(); +var _foo = new WeakMap(); new class { bar() { - let x = function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor; + let x = (receiver = this, value = 84, descriptor = function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _foo, 84); + return privateMap.get(receiver); + }(receiver, privateMap = _foo, "set"), function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value); console.log(x); } constructor(){ - !function(obj, privateSet) { + !function(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); - }(this, _foo); + }(obj, privateMap), privateMap.set(obj, value); + }(this, _foo, { + get: void 0, + set: function(a) {} + }); } }().bar(); diff --git a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.1.normal.js index a511255114a..4bda7be352d 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.1.normal.js @@ -3,25 +3,35 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; + return privateMap.get(receiver); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -37,13 +47,16 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } -var _foo = new WeakSet(); +var _foo = new WeakMap(); var C = // @target: es2019 /*#__PURE__*/ function() { "use strict"; function C() { _classCallCheck(this, C); - _classPrivateMethodInit(this, _foo); + _classPrivateFieldInit(this, _foo, { + get: void 0, + set: set_foo + }); } _createClass(C, [ { @@ -56,5 +69,5 @@ var C = // @target: es2019 ]); return C; }(); -function foo(a) {} +function set_foo(a) {} new C().bar(); diff --git a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.2.minified.js index 02e59e942b0..ab070924f2f 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameSetterExprReturnValue_es5.2.minified.js @@ -4,30 +4,38 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var _foo = new WeakSet(), C = function() { +var _foo = new WeakMap(), C = function() { "use strict"; var Constructor, protoProps, staticProps; function C() { - var obj, privateSet; + var obj, privateMap, value; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, C), obj = this, (function(obj, privateCollection) { + }(this, C), obj = this, privateMap = _foo, value = { + get: void 0, + set: set_foo + }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateSet = _foo), privateSet.add(obj); + })(obj, privateMap), privateMap.set(obj, value); } return Constructor = C, protoProps = [ { key: "bar", value: function() { - var x = function(receiver, privateMap, value) { + var receiver, privateMap, value, descriptor, x = (receiver = this, value = 84, descriptor = function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _foo, 84); + return privateMap.get(receiver); + }(receiver, privateMap = _foo, "set"), function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value); console.log(x); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), C; }(); +function set_foo(a) {} new C().bar(); diff --git a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.1.normal.js index f8f3a9a5913..5108c904c22 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.1.normal.js @@ -3,15 +3,29 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -20,22 +34,21 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} // @target: es2015 const C = function() { - var _x = new WeakSet(); + var _x = new WeakMap(); class _class { m() { _classPrivateFieldSet(this, _x, _classPrivateMethodGet(this, _x, x) + 2); // Error } constructor(){ - _classPrivateMethodInit(this, _x); + _classPrivateFieldInit(this, _x, { + get: void 0, + set: set_x + }); } } - function x(x) {} + function set_x(x) {} return _class; }(); console.log(new C().m()); diff --git a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.2.minified.js index 73a67c5d14d..bed3a0c7365 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es2015.2.minified.js @@ -1,23 +1,30 @@ const C = function() { - var _x = new WeakSet(); - function x(x) {} + var _x = new WeakMap(); return class { m() { - !function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _x, function(receiver, privateSet, fn) { + var receiver, privateMap, value, descriptor; + receiver = this, privateMap = _x, value = (function(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; - }(this, _x, x) + 2); + })(this, _x, x) + 2, descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); } constructor(){ - var obj, privateSet; - obj = this, (function(obj, privateCollection) { + var obj, privateMap, value; + obj = this, privateMap = _x, value = { + get: void 0, + set: function(x) {} + }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateSet = _x), privateSet.add(obj); + })(obj, privateMap), privateMap.set(obj, value); } }; }(); diff --git a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.1.normal.js index 91ac13786d3..c7e0847503e 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.1.normal.js @@ -3,20 +3,34 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldSet(receiver, privateMap, value) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -25,10 +39,6 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; @@ -45,19 +55,22 @@ function _createClass(Constructor, protoProps, staticProps) { } // @target: es2015 var C = function() { - var x1 = function x1(x) {}; - var _x = new WeakSet(); + var set_x = function set_x(x) {}; + var _x = new WeakMap(); var _class = /*#__PURE__*/ function() { "use strict"; function _class() { _classCallCheck(this, _class); - _classPrivateMethodInit(this, _x); + _classPrivateFieldInit(this, _x, { + get: void 0, + set: set_x + }); } _createClass(_class, [ { key: "m", value: function m() { - _classPrivateFieldSet(this, _x, _classPrivateMethodGet(this, _x, x1) + 2); // Error + _classPrivateFieldSet(this, _x, _classPrivateMethodGet(this, _x, x) + 2); // Error } } ]); diff --git a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.2.minified.js index b62b6491476..055775b72a6 100644 --- a/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameSetterNoGetter_es5.2.minified.js @@ -5,30 +5,38 @@ function _defineProperties(target, props) { } } var C = function() { - var x = function(x) {}, _x = new WeakSet(), _class = function() { + var set_x = function(x) {}, _x = new WeakMap(), _class = function() { "use strict"; var Constructor, protoProps, staticProps; function _class() { - var obj, privateSet; + var obj, privateMap, value; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, _class), obj = this, (function(obj, privateCollection) { + }(this, _class), obj = this, privateMap = _x, value = { + get: void 0, + set: set_x + }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - })(obj, privateSet = _x), privateSet.add(obj); + })(obj, privateMap), privateMap.set(obj, value); } return Constructor = _class, protoProps = [ { key: "m", value: function() { - !function(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(this, _x, function(receiver, privateSet, fn) { + var receiver, privateMap, value, descriptor; + receiver = this, privateMap = _x, value = (function(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; - }(this, _x, x) + 2); + })(this, _x, x) + 2, descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); + return privateMap.get(receiver); + })(receiver, privateMap, "set"), (function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + })(receiver, descriptor, value); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), _class; diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.1.normal.js index da89efa1e6b..6e927cbda8f 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.1.normal.js @@ -1,11 +1,22 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { @@ -37,15 +48,23 @@ class A { return A; } } +var _fieldFunc = { + get: get_fieldFunc, + set: void 0 +}; +var _fieldFunc2 = { + get: get_fieldFunc2, + set: void 0 +}; var _x = { writable: true, value: 1 }; -function fieldFunc() { +function get_fieldFunc() { return function() { _classStaticPrivateFieldSpecSet(A, A, _x, 10); }; } -function fieldFunc2() { +function get_fieldFunc2() { return function(a, ...b) {}; } diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.2.minified.js index 015c002ea18..889c2821c7d 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es2015.2.minified.js @@ -19,19 +19,3 @@ class A { return A; } } -var _x = { - writable: !0, - value: 1 -}; -function fieldFunc() { - return function() { - !function(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(A, A, _x, 10); - }; -} -function fieldFunc2() { - return function(a, ...b) {}; -} diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.1.normal.js index 1aadae13692..d31a9ce796f 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.1.normal.js @@ -6,19 +6,30 @@ function _arrayLikeToArray(arr, len) { function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function isNativeReflectConstruct() { @@ -171,16 +182,24 @@ var A = // @target: es2015 ]); return A; }(); +var _fieldFunc = { + get: get_fieldFunc, + set: void 0 +}; +var _fieldFunc2 = { + get: get_fieldFunc2, + set: void 0 +}; var _x = { writable: true, value: 1 }; -function fieldFunc() { +function get_fieldFunc() { return function() { _classStaticPrivateFieldSpecSet(A, A, _x, 10); }; } -function fieldFunc2() { +function get_fieldFunc2() { return function(a) { for(var _len = arguments.length, b = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++){ b[_key - 1] = arguments[_key]; diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.2.minified.js index 86566ba2a58..bef10f258a3 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessorsCallExpression_es5.2.minified.js @@ -119,21 +119,4 @@ var A = function() { } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; -}(), _x = { - writable: !0, - value: 1 -}; -function fieldFunc() { - return function() { - !function(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; - }(A, A, _x, 10); - }; -} -function fieldFunc2() { - return function(a) { - for(var _len = arguments.length, b = new Array(_len > 1 ? _len - 1 : 0), _key = 1; _key < _len; _key++)b[_key - 1] = arguments[_key]; - }; -} +}(); diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.1.normal.js index 34a1c9cbcce..adba1e292d0 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.1.normal.js @@ -1,11 +1,22 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { @@ -27,10 +38,18 @@ class A1 { console.log(_classStaticPrivateMethodGet(A1, A1, roProp)); } } -function prop() { +var _prop = { + get: get_prop, + set: set_prop +}; +var _roProp = { + get: get_roProp, + set: void 0 +}; +function get_prop() { return ""; } -function prop(param) {} -function roProp() { +function set_prop(param) {} +function get_roProp() { return ""; } diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.2.minified.js index 0a77ee5ddfe..34d69641c69 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es2015.2.minified.js @@ -1,7 +1,13 @@ function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to set private static field before its declaration"); + }(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method; @@ -11,12 +17,17 @@ function _classCheckPrivateStaticAccess(receiver, classConstructor) { } class A1 { constructor(name){ - _classStaticPrivateFieldSpecSet(A1, A1, _prop, ""), _classStaticPrivateFieldSpecSet(A1, A1, _roProp, ""), console.log(_classStaticPrivateMethodGet(A1, A1, prop)), console.log(_classStaticPrivateMethodGet(A1, A1, function() { - return ""; - })); + _classStaticPrivateFieldSpecSet(A1, A1, _prop, ""), _classStaticPrivateFieldSpecSet(A1, A1, _roProp, ""), console.log(_classStaticPrivateMethodGet(A1, A1, prop)), console.log(_classStaticPrivateMethodGet(A1, A1, roProp)); } } -function prop() { - return ""; -} -function prop(param) {} +var _prop = { + get: function() { + return ""; + }, + set: function(param) {} +}, _roProp = { + get: function() { + return ""; + }, + set: void 0 +}; diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.1.normal.js index 8c2a74e4867..500a5b71040 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.1.normal.js @@ -1,16 +1,27 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { @@ -30,10 +41,18 @@ var A1 = function A1(name) { console.log(_classStaticPrivateMethodGet(A1, A1, prop)); console.log(_classStaticPrivateMethodGet(A1, A1, roProp)); }; -function prop() { +var _prop = { + get: get_prop, + set: set_prop +}; +var _roProp = { + get: get_roProp, + set: void 0 +}; +function get_prop() { return ""; } -function prop(param) {} -function roProp() { +function set_prop(param) {} +function get_roProp() { return ""; } diff --git a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.2.minified.js index 04547c1fe52..cecb0322b3b 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticAccessors_es5.2.minified.js @@ -1,7 +1,13 @@ function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to set private static field before its declaration"); + }(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method; @@ -14,11 +20,14 @@ var A1 = function(name) { !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, A1), _classStaticPrivateFieldSpecSet(A1, A1, _prop, ""), _classStaticPrivateFieldSpecSet(A1, A1, _roProp, ""), console.log(_classStaticPrivateMethodGet(A1, A1, prop)), console.log(_classStaticPrivateMethodGet(A1, A1, roProp)); +}, _prop = { + get: function() { + return ""; + }, + set: function(param) {} +}, _roProp = { + get: function() { + return ""; + }, + set: void 0 }; -function prop() { - return ""; -} -function prop(param) {} -function roProp() { - return ""; -} diff --git a/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es2015.1.normal.js index 4e68b10383e..104ccd06278 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es2015.1.normal.js @@ -1,11 +1,22 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { @@ -27,9 +38,13 @@ var _a = { writable: true, value: 1 }; +var _c = { + get: get_c, + set: void 0 +}; function b() { _classStaticPrivateFieldSpecSet(this, S, _a, 42); } -function c() { +function get_c() { return _classStaticPrivateMethodGet(S, S, b).call(S); } diff --git a/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es5.1.normal.js index bc1b9654cbb..0b57fc3bc68 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticEmitHelpers_es5.1.normal.js @@ -1,16 +1,27 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { @@ -34,9 +45,13 @@ var _a = { writable: true, value: 1 }; +var _c = { + get: get_c, + set: void 0 +}; function b() { _classStaticPrivateFieldSpecSet(this, S, _a, 42); } -function c() { +function get_c() { return _classStaticPrivateMethodGet(S, S, b).call(S); } diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.1.normal.js index 2fa45861800..498f208b311 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.1.normal.js @@ -1,8 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } // @target: es2015 class A { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.2.minified.js index 9d174a96e71..0feca474289 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es2015.2.minified.js @@ -1,6 +1,11 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; } class A { constructor(){ diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.1.normal.js index 051fcf2f970..c7db1df980a 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.1.normal.js @@ -1,13 +1,28 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } var A = function A() { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.2.minified.js index 6b58dbfc41b..91747b5008b 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAccess_es5.2.minified.js @@ -1,6 +1,11 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; } var A = function() { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.1.normal.js index 53c1f384b8c..0b8def92356 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.1.normal.js @@ -1,18 +1,39 @@ -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } return descriptor.value; } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; } // @target: es2015 class A { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.2.minified.js index aaa819aff85..f50b297d6e2 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es2015.2.minified.js @@ -1,11 +1,21 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; } class A { static getClass() { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.1.normal.js index 198cff55be0..63550da5fe4 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.1.normal.js @@ -1,22 +1,38 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -33,6 +49,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} var A = // @target: es2015 /*#__PURE__*/ function() { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.2.minified.js index 25dd7bcac60..16ddcd1876c 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldAssignment_es5.2.minified.js @@ -1,11 +1,18 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -13,6 +20,9 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} var A = function() { "use strict"; var Constructor, protoProps, staticProps; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.1.normal.js index 6de414f0c9a..85e21811564 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.1.normal.js @@ -1,8 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } // @target: es2015 class A { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.2.minified.js index 24ee9266282..a56d4830c36 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es2015.2.minified.js @@ -1,6 +1,11 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; } class A { test() { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.1.normal.js index 4347bc0a94d..b4c1be2e1a2 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.1.normal.js @@ -6,16 +6,26 @@ function _arrayLikeToArray(arr, len) { function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; @@ -93,6 +103,11 @@ function _unsupportedIterableToArray(o, minLen) { if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} function _templateObject() { var data = _taggedTemplateLiteral([ "head", diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.2.minified.js index 7cfb1222558..a6d64965d62 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldCallExpression_es5.2.minified.js @@ -4,8 +4,10 @@ function _arrayLikeToArray(arr, len) { return arr2; } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _construct(Parent, args, Class) { return (_construct = !function() { @@ -61,6 +63,9 @@ function _unsupportedIterableToArray(o, minLen) { if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} function _templateObject() { var data = _taggedTemplateLiteral([ "head", diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.1.normal.js index c9e7389e98e..bf6156d4de0 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.1.normal.js @@ -1,8 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } var _class, _class1; // @target: es2015 diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.2.minified.js index 7ed926f6bca..488aee21cf4 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es2015.2.minified.js @@ -1,6 +1,11 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; } class B { m() { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.1.normal.js index 2e7f0169c4e..2a37251adce 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.1.normal.js @@ -1,13 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -23,6 +33,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} var _class, _class1; var B = // @target: es2015 /*#__PURE__*/ function() { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.2.minified.js index a538b77a5b4..c183cf3a2d5 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldClassExpression_es5.2.minified.js @@ -2,8 +2,10 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), !function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + }(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -11,6 +13,9 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} var _class, _class1, B = function() { "use strict"; var Constructor, protoProps, staticProps; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.1.normal.js index 75c845594b6..2dbe1e9b704 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.1.normal.js @@ -1,8 +1,35 @@ -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } // @target: esnext, es2022, es2015 // @useDefineForClassFields: false @@ -20,32 +47,32 @@ class A { ]; } static test(_a) { - [_classStaticPrivateFieldSpecGet(_a, A, _field)] = [ + [_classStaticPrivateFieldDestructureSet(_a, _field).value] = [ 2 ]; } constructor(){ this.otherClass = A; let y; - ({ x: _classStaticPrivateFieldSpecGet(A, A, _field) , y } = this.testObject()); - [_classStaticPrivateFieldSpecGet(A, A, _field), y] = this.testArray(); - ({ a: _classStaticPrivateFieldSpecGet(A, A, _field) , b: [_classStaticPrivateFieldSpecGet(A, A, _field)] } = { + ({ x: _classStaticPrivateFieldDestructureSet(A, _field).value , y } = this.testObject()); + [_classStaticPrivateFieldDestructureSet(A, _field).value, y] = this.testArray(); + ({ a: _classStaticPrivateFieldDestructureSet(A, _field).value , b: [_classStaticPrivateFieldDestructureSet(A, _field).value] } = { a: 1, b: [ 2 ] }); - [_classStaticPrivateFieldSpecGet(A, A, _field), [_classStaticPrivateFieldSpecGet(A, A, _field)]] = [ + [_classStaticPrivateFieldDestructureSet(A, _field).value, [_classStaticPrivateFieldDestructureSet(A, _field).value]] = [ 1, [ 2 ] ]; - ({ a: _classStaticPrivateFieldSpecGet(A, A, _field) = 1 , b: [_classStaticPrivateFieldSpecGet(A, A, _field) = 1] } = { + ({ a: _classStaticPrivateFieldDestructureSet(A, _field).value = 1 , b: [_classStaticPrivateFieldDestructureSet(A, _field).value = 1] } = { b: [] }); - [_classStaticPrivateFieldSpecGet(A, A, _field) = 2] = []; - [_classStaticPrivateFieldSpecGet(this.otherClass, A, _field) = 2] = []; + [_classStaticPrivateFieldDestructureSet(A, _field).value = 2] = []; + [_classStaticPrivateFieldDestructureSet(this.otherClass, _field).value = 2] = []; } } var _field = { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.2.minified.js index a4b38962223..d6bf82c141f 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es2015.2.minified.js @@ -1,3 +1,17 @@ +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} class A { testObject() { return { @@ -12,26 +26,26 @@ class A { ]; } static test(_a) { - [_classStaticPrivateFieldSpecGet(_a, A, _field)] = [ + [_classStaticPrivateFieldDestructureSet(_a, _field).value] = [ 2 ]; } constructor(){ this.otherClass = A; let y; - ({ x: _classStaticPrivateFieldSpecGet(A, A, _field) , y } = this.testObject()), [_classStaticPrivateFieldSpecGet(A, A, _field), y] = this.testArray(), ({ a: _classStaticPrivateFieldSpecGet(A, A, _field) , b: [_classStaticPrivateFieldSpecGet(A, A, _field)] } = { + ({ x: _classStaticPrivateFieldDestructureSet(A, _field).value , y } = this.testObject()), [_classStaticPrivateFieldDestructureSet(A, _field).value, y] = this.testArray(), ({ a: _classStaticPrivateFieldDestructureSet(A, _field).value , b: [_classStaticPrivateFieldDestructureSet(A, _field).value] } = { a: 1, b: [ 2 ] - }), [_classStaticPrivateFieldSpecGet(A, A, _field), [_classStaticPrivateFieldSpecGet(A, A, _field)]] = [ + }), [_classStaticPrivateFieldDestructureSet(A, _field).value, [_classStaticPrivateFieldDestructureSet(A, _field).value]] = [ 1, [ 2 ] - ], ({ a: _classStaticPrivateFieldSpecGet(A, A, _field) = 1 , b: [_classStaticPrivateFieldSpecGet(A, A, _field) = 1] } = { + ], ({ a: _classStaticPrivateFieldDestructureSet(A, _field).value = 1 , b: [_classStaticPrivateFieldDestructureSet(A, _field).value = 1] } = { b: [] - }), [_classStaticPrivateFieldSpecGet(A, A, _field) = 2] = [], [_classStaticPrivateFieldSpecGet(this.otherClass, A, _field) = 2] = []; + }), [_classStaticPrivateFieldDestructureSet(A, _field).value = 2] = [], [_classStaticPrivateFieldDestructureSet(this.otherClass, _field).value = 2] = []; } } var _field = { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.1.normal.js index a839ad38ad7..10e79497fab 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.1.normal.js @@ -6,16 +6,33 @@ function _arrayLikeToArray(arr, len) { function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; } +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return descriptor.value; + return privateMap.get(receiver); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -69,6 +86,16 @@ function _unsupportedIterableToArray(o, minLen) { if (n === "Map" || n === "Set") return Array.from(n); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} var A = // @target: esnext, es2022, es2015 // @useDefineForClassFields: false /*#__PURE__*/ function() { @@ -78,28 +105,28 @@ var A = // @target: esnext, es2022, es2015 this.otherClass = A; var y; var ref; - ref = this.testObject(), _classStaticPrivateFieldSpecGet(A, A, _field) = ref.x, y = ref.y, ref; + ref = this.testObject(), _classStaticPrivateFieldDestructureSet(A, _field).value = ref.x, y = ref.y, ref; var ref1; - ref1 = _slicedToArray(this.testArray(), 2), _classStaticPrivateFieldSpecGet(A, A, _field) = ref1[0], y = ref1[1], ref1; + ref1 = _slicedToArray(this.testArray(), 2), _classStaticPrivateFieldDestructureSet(A, _field).value = ref1[0], y = ref1[1], ref1; var ref2, ref3; ref2 = { a: 1, b: [ 2 ] - }, _classStaticPrivateFieldSpecGet(A, A, _field) = ref2.a, ref3 = _slicedToArray(ref2.b, 1), _classStaticPrivateFieldSpecGet(A, A, _field) = ref3[0], ref3, ref2; + }, _classStaticPrivateFieldDestructureSet(A, _field).value = ref2.a, ref3 = _slicedToArray(ref2.b, 1), _classStaticPrivateFieldDestructureSet(A, _field).value = ref3[0], ref3, ref2; var ref4; - _classStaticPrivateFieldSpecGet(A, A, _field) = 1, ref4 = [ + _classStaticPrivateFieldDestructureSet(A, _field).value = 1, ref4 = [ 2 - ], _classStaticPrivateFieldSpecGet(A, A, _field) = ref4[0], ref4; + ], _classStaticPrivateFieldDestructureSet(A, _field).value = ref4[0], ref4; var ref5, ref6, ref7, ref8; ref5 = { b: [] - }, ref6 = ref5.a, _classStaticPrivateFieldSpecGet(A, A, _field) = ref6 === void 0 ? 1 : ref6, ref7 = _slicedToArray(ref5.b, 1), ref8 = ref7[0], _classStaticPrivateFieldSpecGet(A, A, _field) = ref8 === void 0 ? 1 : ref8, ref7, ref5; + }, ref6 = ref5.a, _classStaticPrivateFieldDestructureSet(A, _field).value = ref6 === void 0 ? 1 : ref6, ref7 = _slicedToArray(ref5.b, 1), ref8 = ref7[0], _classStaticPrivateFieldDestructureSet(A, _field).value = ref8 === void 0 ? 1 : ref8, ref7, ref5; var ref9, ref10; - ref9 = [], ref10 = ref9[0], _classStaticPrivateFieldSpecGet(A, A, _field) = ref10 === void 0 ? 2 : ref10, ref9; + ref9 = [], ref10 = ref9[0], _classStaticPrivateFieldDestructureSet(A, _field).value = ref10 === void 0 ? 2 : ref10, ref9; var ref11, ref12; - ref11 = [], ref12 = ref11[0], _classStaticPrivateFieldSpecGet(this.otherClass, A, _field) = ref12 === void 0 ? 2 : ref12, ref11; + ref11 = [], ref12 = ref11[0], _classStaticPrivateFieldDestructureSet(this.otherClass, _field).value = ref12 === void 0 ? 2 : ref12, ref11; } _createClass(A, [ { @@ -124,7 +151,7 @@ var A = // @target: esnext, es2022, es2015 { key: "test", value: function test(_a) { - _classStaticPrivateFieldSpecGet(_a, A, _field) = 2; + _classStaticPrivateFieldDestructureSet(_a, _field).value = 2; } } ]); diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.2.minified.js index aee285b6d1f..263f5bfe772 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldDestructuredBinding_es5.2.minified.js @@ -41,6 +41,20 @@ function _unsupportedIterableToArray(o, minLen) { if ("Arguments" === n || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } } +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} var A = function() { "use strict"; var Constructor, protoProps, staticProps; @@ -48,16 +62,16 @@ var A = function() { var ref, ref1, ref2, ref3, ref4, ref5, ref6, ref7, ref8, ref9; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, A), this.otherClass = A, ref = this.testObject(), _classStaticPrivateFieldSpecGet(A, A, _field) = ref.x, ref.y, ref1 = _slicedToArray(this.testArray(), 2), _classStaticPrivateFieldSpecGet(A, A, _field) = ref1[0], ref1[1], ref2 = { + }(this, A), this.otherClass = A, ref = this.testObject(), _classStaticPrivateFieldDestructureSet(A, _field).value = ref.x, ref.y, ref1 = _slicedToArray(this.testArray(), 2), _classStaticPrivateFieldDestructureSet(A, _field).value = ref1[0], ref1[1], ref2 = { a: 1, b: [ 2 ] - }, _classStaticPrivateFieldSpecGet(A, A, _field) = ref2.a, ref3 = _slicedToArray(ref2.b, 1), _classStaticPrivateFieldSpecGet(A, A, _field) = ref3[0], _classStaticPrivateFieldSpecGet(A, A, _field) = 1, ref4 = [ + }, _classStaticPrivateFieldDestructureSet(A, _field).value = ref2.a, ref3 = _slicedToArray(ref2.b, 1), _classStaticPrivateFieldDestructureSet(A, _field).value = ref3[0], _classStaticPrivateFieldDestructureSet(A, _field).value = 1, ref4 = [ 2 - ], _classStaticPrivateFieldSpecGet(A, A, _field) = ref4[0], ref6 = (ref5 = { + ], _classStaticPrivateFieldDestructureSet(A, _field).value = ref4[0], ref6 = (ref5 = { b: [] - }).a, _classStaticPrivateFieldSpecGet(A, A, _field) = void 0 === ref6 ? 1 : ref6, ref7 = _slicedToArray(ref5.b, 1)[0], _classStaticPrivateFieldSpecGet(A, A, _field) = void 0 === ref7 ? 1 : ref7, ref8 = void 0, _classStaticPrivateFieldSpecGet(A, A, _field) = void 0 === ref8 ? 2 : ref8, ref9 = void 0, _classStaticPrivateFieldSpecGet(this.otherClass, A, _field) = void 0 === ref9 ? 2 : ref9; + }).a, _classStaticPrivateFieldDestructureSet(A, _field).value = void 0 === ref6 ? 1 : ref6, ref7 = _slicedToArray(ref5.b, 1)[0], _classStaticPrivateFieldDestructureSet(A, _field).value = void 0 === ref7 ? 1 : ref7, ref8 = void 0, _classStaticPrivateFieldDestructureSet(A, _field).value = void 0 === ref8 ? 2 : ref8, ref9 = void 0, _classStaticPrivateFieldDestructureSet(this.otherClass, _field).value = void 0 === ref9 ? 2 : ref9; } return Constructor = A, protoProps = [ { @@ -82,7 +96,7 @@ var A = function() { { key: "test", value: function(_a) { - _classStaticPrivateFieldSpecGet(_a, A, _field) = 2; + _classStaticPrivateFieldDestructureSet(_a, _field).value = 2; } } ], protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), A; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.1.normal.js index 7c8c192e609..947ba0f1e65 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.1.normal.js @@ -1,18 +1,39 @@ -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } return descriptor.value; } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; - return value; } // @target: es2015 class C { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.2.minified.js index 64cdaa0c16f..049629cb766 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es2015.2.minified.js @@ -1,11 +1,21 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; } class C { test() { diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.1.normal.js index 3e25efa1017..ebc5d8313d1 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.1.normal.js @@ -1,22 +1,38 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _defineProperties(target, props) { @@ -33,6 +49,11 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} var C = // @target: es2015 /*#__PURE__*/ function() { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.2.minified.js index e09d7bcba07..26f35bbebdd 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticFieldUnaryMutation_es5.2.minified.js @@ -1,11 +1,18 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; + var receiver, descriptor; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -13,6 +20,9 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); +} var C = function() { "use strict"; var Constructor, protoProps, staticProps; diff --git a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.1.normal.js index 031fe1acf84..174f1c36d66 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.1.normal.js @@ -1,13 +1,52 @@ +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} function _classStaticPrivateMethodGet(receiver, classConstructor, method1) { _classCheckPrivateStaticAccess(receiver, classConstructor); return method1; @@ -26,7 +65,7 @@ class A3 { _classStaticPrivateFieldSpecSet(a, A3, _method, ()=>{}); // Error, not writable _classStaticPrivateFieldSpecSet(b, A3, _method, ()=>{} //Error, not writable ); - ({ x: _classStaticPrivateMethodGet(A3, A3, method) } = { + ({ x: _classStaticPrivateFieldDestructureSet(A3, _method).value } = { x: ()=>{} }); //Error, not writable let x = _classStaticPrivateMethodGet(A3, A3, method); diff --git a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.2.minified.js index 7455c718193..c0f410db823 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es2015.2.minified.js @@ -1,7 +1,14 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method1) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method1; @@ -11,7 +18,17 @@ function _classCheckPrivateStaticAccess(receiver, classConstructor) { } class A3 { constructor(a, b){ - _classStaticPrivateFieldSpecSet(A3, A3, _method, ()=>{}), _classStaticPrivateFieldSpecSet(a, A3, _method, ()=>{}), _classStaticPrivateFieldSpecSet(b, A3, _method, ()=>{}), ({ x: _classStaticPrivateMethodGet(A3, A3, method) } = { + _classStaticPrivateFieldSpecSet(A3, A3, _method, ()=>{}), _classStaticPrivateFieldSpecSet(a, A3, _method, ()=>{}), _classStaticPrivateFieldSpecSet(b, A3, _method, ()=>{}), ({ x: (function(receiver, classConstructor, descriptor) { + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor); + })(A3, _method).value } = { x: ()=>{} }), _classStaticPrivateMethodGet(A3, A3, method), _classStaticPrivateFieldSpecSet(b, A3, _method, +_classStaticPrivateMethodGet(b, A3, method) + 1); } diff --git a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.1.normal.js index 9d250889ce9..0932f1806c5 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.1.normal.js @@ -1,18 +1,57 @@ +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} function _classStaticPrivateMethodGet(receiver, classConstructor, method1) { _classCheckPrivateStaticAccess(receiver, classConstructor); return method1; @@ -34,7 +73,7 @@ var A3 = function A3(a, b) { var ref; ref = { x: function() {} - }, _classStaticPrivateMethodGet(A3, A3, method) = ref.x, ref; //Error, not writable + }, _classStaticPrivateFieldDestructureSet(A3, _method).value = ref.x, ref; //Error, not writable var x = _classStaticPrivateMethodGet(A3, A3, method); _classStaticPrivateFieldSpecSet(b, A3, _method, (_this_method = +_classStaticPrivateMethodGet(b, A3, method)) + 1), _this_method; //Error, not writable }; diff --git a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.2.minified.js index 6ce5cd3086f..d7f31509591 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticMethodAssignment_es5.2.minified.js @@ -1,7 +1,14 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method1) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method1; @@ -11,11 +18,19 @@ function _classCheckPrivateStaticAccess(receiver, classConstructor) { } var A3 = function(a, b) { "use strict"; - var ref; + var receiver, classConstructor, descriptor, ref; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); }(this, A3), _classStaticPrivateFieldSpecSet(A3, A3, _method, function() {}), _classStaticPrivateFieldSpecSet(a, A3, _method, function() {}), _classStaticPrivateFieldSpecSet(b, A3, _method, function() {}), ref = { x: function() {} - }, _classStaticPrivateMethodGet(A3, A3, method) = ref.x, _classStaticPrivateMethodGet(A3, A3, method), _classStaticPrivateFieldSpecSet(b, A3, _method, +_classStaticPrivateMethodGet(b, A3, method) + 1); + }, (_classCheckPrivateStaticAccess(receiver = A3, classConstructor = _method), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor)).value = ref.x, _classStaticPrivateMethodGet(A3, A3, method), _classStaticPrivateFieldSpecSet(b, A3, _method, +_classStaticPrivateMethodGet(b, A3, method) + 1); }; function method() {} diff --git a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.1.normal.js index 9d2fe5ffb1e..c7f0bbd0dd2 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.1.normal.js @@ -111,20 +111,36 @@ function _asyncToGenerator(fn) { function _AwaitValue(value) { this.wrapped = value; } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } return descriptor.value; } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _wrapAsyncGenerator(fn) { @@ -159,6 +175,10 @@ var __quux = { writable: true, value: void 0 }; +var _quux = { + get: get_quux, + set: set_quux +}; function foo(a) {} function bar(a) { return _bar.apply(this, arguments); @@ -176,10 +196,10 @@ function _baz() { }); return _baz.apply(this, arguments); } -function quux() { +function get_quux() { return _classStaticPrivateFieldSpecGet(this, A, __quux); } -function quux(val) { +function set_quux(val) { _classStaticPrivateFieldSpecSet(this, A, __quux, val); } class B extends A { diff --git a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.2.minified.js index ea27ea241e6..ec539b5a605 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es2015.2.minified.js @@ -61,10 +61,17 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { function _AwaitValue(value) { this.wrapped = value; } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method; @@ -89,6 +96,14 @@ class A { var __quux = { writable: !0, value: void 0 +}, _quux = { + get: function() { + var receiver, classConstructor, descriptor, receiver, descriptor; + return receiver = this, classConstructor = A, descriptor = __quux, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; + }, + set: function(val) { + _classStaticPrivateFieldSpecSet(this, A, __quux, val); + } }; function bar(a) { return _bar.apply(this, arguments); @@ -110,15 +125,6 @@ function _bar() { }; })(function*(a) {})).apply(this, arguments); } -function quux() { - return (function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - })(this, A, __quux); -} -function quux(val) { - _classStaticPrivateFieldSpecSet(this, A, __quux, val); -} class B extends A { constructor(){ super(), _classStaticPrivateMethodGet(B, B, function(a) {}).call(B, "str"); diff --git a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.1.normal.js index 0e6c2653678..fcfe9594c53 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.1.normal.js @@ -117,25 +117,41 @@ function _asyncToGenerator(fn) { function _AwaitValue(value) { this.wrapped = value; } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _getPrototypeOf(o) { @@ -227,6 +243,10 @@ var __quux = { writable: true, value: void 0 }; +var _quux = { + get: get_quux, + set: set_quux +}; function foo(a) {} function bar(a) { return _bar.apply(this, arguments); @@ -260,10 +280,10 @@ function _baz() { })); return _baz.apply(this, arguments); } -function quux() { +function get_quux() { return _classStaticPrivateFieldSpecGet(this, A, __quux); } -function quux(val) { +function set_quux(val) { _classStaticPrivateFieldSpecSet(this, A, __quux, val); } var B = /*#__PURE__*/ function(A) { diff --git a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.2.minified.js index 00781d38a56..1c7e3eb487b 100644 --- a/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameStaticsAndStaticMethods_es5.2.minified.js @@ -64,10 +64,17 @@ function _AwaitValue(value) { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _getPrototypeOf(o) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { @@ -101,6 +108,14 @@ var A = function() { }, __quux = { writable: !0, value: void 0 +}, _quux = { + get: function() { + var receiver, classConstructor, descriptor, receiver, descriptor; + return receiver = this, classConstructor = A, descriptor = __quux, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; + }, + set: function(val) { + _classStaticPrivateFieldSpecSet(this, A, __quux, val); + } }; function foo(a) {} function bar(a) { @@ -130,15 +145,6 @@ function _bar() { }); })).apply(this, arguments); } -function quux() { - return (function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - })(this, A, __quux); -} -function quux(val) { - _classStaticPrivateFieldSpecSet(this, A, __quux, val); -} var B = function(A1) { "use strict"; !function(subClass, superClass) { diff --git a/crates/swc/tests/tsc-references/privateNameUnused_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNameUnused_es2015.1.normal.js index ca1faa81510..0f9423d8288 100644 --- a/crates/swc/tests/tsc-references/privateNameUnused_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameUnused_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -23,6 +33,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } +var _used = new WeakMap(), _unused = new WeakMap(); // @noUnusedLocals:true // @noEmit: true // @target: es2015 @@ -39,33 +50,35 @@ export class A { console.log(_classPrivateFieldGet(this, _used)); } } -var _used = new WeakMap(); -var _unused = new WeakMap(); var _used1 = new WeakSet(), _unused1 = new WeakSet(); export class A2 { constructor(){ _classPrivateMethodInit(this, _used1); _classPrivateMethodInit(this, _unused1); - console.log(_classPrivateMethodGet(this, _used1, used).call(this)); + console.log(_classPrivateMethodGet(this, _used1, used1).call(this)); } } -function used() {} +function used1() {} function unused() {} -var _used2 = new WeakSet(), _used2 = new WeakSet(), _unused2 = new WeakSet(), _unused2 = new WeakSet(); +var _used2 = new WeakMap(), _unused2 = new WeakMap(); export class A3 { constructor(){ - _classPrivateMethodInit(this, _used2); - _classPrivateMethodInit(this, _used2); - _classPrivateMethodInit(this, _unused2); - _classPrivateMethodInit(this, _unused2); - console.log(_classPrivateMethodGet(this, _used2, used1)); + _classPrivateFieldInit(this, _used2, { + get: get_used, + set: set_used + }); + _classPrivateFieldInit(this, _unused2, { + get: get_unused, + set: set_unused + }); + console.log(_classPrivateMethodGet(this, _used2, used)); } } -function used1() { +function get_used() { return 0; } -function used1(value) {} -function unused1() { +function set_used(value) {} +function get_unused() { return 0; } -function unused1(value) {} +function set_unused(value) {} diff --git a/crates/swc/tests/tsc-references/privateNameUnused_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNameUnused_es2015.2.minified.js index b56f6a80206..c2b871f7cf8 100644 --- a/crates/swc/tests/tsc-references/privateNameUnused_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameUnused_es2015.2.minified.js @@ -11,6 +11,7 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); } +var _used = new WeakMap(), _unused = new WeakMap(); export class A { constructor(){ _classPrivateFieldInit(this, _used, { @@ -20,24 +21,33 @@ export class A { writable: !0, value: "unused" }), console.log(function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; }(this, _used)); } } -var _used = new WeakMap(), _unused = new WeakMap(), _used1 = new WeakSet(), _unused1 = new WeakSet(); +var _used1 = new WeakSet(), _unused1 = new WeakSet(); export class A2 { constructor(){ _classPrivateMethodInit(this, _used1), _classPrivateMethodInit(this, _unused1), console.log(_classPrivateMethodGet(this, _used1, function() {}).call(this)); } } -var _used2 = new WeakSet(), _used2 = new WeakSet(), _unused2 = new WeakSet(), _unused2 = new WeakSet(); +var _used2 = new WeakMap(), _unused2 = new WeakMap(); export class A3 { constructor(){ - _classPrivateMethodInit(this, _used2), _classPrivateMethodInit(this, _used2), _classPrivateMethodInit(this, _unused2), _classPrivateMethodInit(this, _unused2), console.log(_classPrivateMethodGet(this, _used2, used)); + _classPrivateFieldInit(this, _used2, { + get: function() { + return 0; + }, + set: function(value) {} + }), _classPrivateFieldInit(this, _unused2, { + get: function() { + return 0; + }, + set: function(value) {} + }), console.log(_classPrivateMethodGet(this, _used2, used)); } } -function used() { - return 0; -} -function used(value) {} diff --git a/crates/swc/tests/tsc-references/privateNameUnused_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNameUnused_es5.1.normal.js index 0ea11111431..60b40ba24d3 100644 --- a/crates/swc/tests/tsc-references/privateNameUnused_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNameUnused_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -28,6 +38,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } +var _used = new WeakMap(), _unused = new WeakMap(); // @noUnusedLocals:true // @noEmit: true // @target: es2015 @@ -44,33 +55,35 @@ export var A = function A() { }); console.log(_classPrivateFieldGet(this, _used)); }; -var _used = new WeakMap(); -var _unused = new WeakMap(); var _used1 = new WeakSet(), _unused1 = new WeakSet(); export var A2 = function A2() { "use strict"; _classCallCheck(this, A2); _classPrivateMethodInit(this, _used1); _classPrivateMethodInit(this, _unused1); - console.log(_classPrivateMethodGet(this, _used1, used).call(this)); + console.log(_classPrivateMethodGet(this, _used1, used1).call(this)); }; -function used() {} +function used1() {} function unused() {} -var _used2 = new WeakSet(), _used2 = new WeakSet(), _unused2 = new WeakSet(), _unused2 = new WeakSet(); +var _used2 = new WeakMap(), _unused2 = new WeakMap(); export var A3 = function A3() { "use strict"; _classCallCheck(this, A3); - _classPrivateMethodInit(this, _used2); - _classPrivateMethodInit(this, _used2); - _classPrivateMethodInit(this, _unused2); - _classPrivateMethodInit(this, _unused2); - console.log(_classPrivateMethodGet(this, _used2, used1)); + _classPrivateFieldInit(this, _used2, { + get: get_used, + set: set_used + }); + _classPrivateFieldInit(this, _unused2, { + get: get_unused, + set: set_unused + }); + console.log(_classPrivateMethodGet(this, _used2, used)); }; -function used1() { +function get_used() { return 0; } -function used1(value) {} -function unused1() { +function set_used(value) {} +function get_unused() { return 0; } -function unused1(value) {} +function set_unused(value) {} diff --git a/crates/swc/tests/tsc-references/privateNameUnused_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNameUnused_es5.2.minified.js index 159ae0bcdc5..d4213023da1 100644 --- a/crates/swc/tests/tsc-references/privateNameUnused_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNameUnused_es5.2.minified.js @@ -14,31 +14,41 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); } +var _used = new WeakMap(), _unused = new WeakMap(); export var A = function() { "use strict"; + var receiver, privateMap, descriptor, receiver, descriptor; _classCallCheck(this, A), _classPrivateFieldInit(this, _used, { writable: !0, value: "used" }), _classPrivateFieldInit(this, _unused, { writable: !0, value: "unused" - }), console.log(function(receiver, privateMap) { + }), console.log((receiver = this, (descriptor = descriptor = function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(this, _used)); + return privateMap.get(receiver); + }(receiver, privateMap = _used, "get")).get ? descriptor.get.call(receiver) : descriptor.value)); }; -var _used = new WeakMap(), _unused = new WeakMap(), _used1 = new WeakSet(), _unused1 = new WeakSet(); +var _used1 = new WeakSet(), _unused1 = new WeakSet(); export var A2 = function() { "use strict"; - _classCallCheck(this, A2), _classPrivateMethodInit(this, _used1), _classPrivateMethodInit(this, _unused1), console.log(_classPrivateMethodGet(this, _used1, used).call(this)); + _classCallCheck(this, A2), _classPrivateMethodInit(this, _used1), _classPrivateMethodInit(this, _unused1), console.log(_classPrivateMethodGet(this, _used1, used1).call(this)); }; -function used() {} -var _used2 = new WeakSet(), _used2 = new WeakSet(), _unused2 = new WeakSet(), _unused2 = new WeakSet(); +function used1() {} +var _used2 = new WeakMap(), _unused2 = new WeakMap(); export var A3 = function() { "use strict"; - _classCallCheck(this, A3), _classPrivateMethodInit(this, _used2), _classPrivateMethodInit(this, _used2), _classPrivateMethodInit(this, _unused2), _classPrivateMethodInit(this, _unused2), console.log(_classPrivateMethodGet(this, _used2, used1)); + _classCallCheck(this, A3), _classPrivateFieldInit(this, _used2, { + get: function() { + return 0; + }, + set: set_used + }), _classPrivateFieldInit(this, _unused2, { + get: function() { + return 0; + }, + set: set_unused + }), console.log(_classPrivateMethodGet(this, _used2, used)); }; -function used1() { - return 0; -} -function used1(value) {} +function set_used(value) {} +function set_unused(value) {} diff --git a/crates/swc/tests/tsc-references/privateNamesAndFields_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndFields_es2015.1.normal.js index 2488d42c0cb..8e19b3b38f8 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndFields_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndFields_es2015.1.normal.js @@ -3,21 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _foo = new WeakMap(); // @strict: true // @target: es6 class A { @@ -29,7 +40,7 @@ class A { _classPrivateFieldSet(this, _foo, 3); } } -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); class B extends A { constructor(){ super(); @@ -40,4 +51,3 @@ class B extends A { _classPrivateFieldSet(this, _foo1, "some string"); } } -var _foo1 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndFields_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndFields_es5.1.normal.js index 0563db4cb29..0c1ebd1023b 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndFields_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndFields_es5.1.normal.js @@ -9,24 +9,34 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _getPrototypeOf(o) { @@ -89,6 +99,7 @@ function _createSuper(Derived) { return _possibleConstructorReturn(this, result); }; } +var _foo = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -98,7 +109,7 @@ var A = function A() { }); _classPrivateFieldSet(this, _foo, 3); }; -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); var B = /*#__PURE__*/ function(A) { "use strict"; _inherits(B, A); @@ -116,4 +127,3 @@ var B = /*#__PURE__*/ function(A) { } return B; }(A); -var _foo1 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndFields_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndFields_es5.2.minified.js index 85ceaf07234..8fb9c548aa6 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndFields_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndFields_es5.2.minified.js @@ -11,10 +11,17 @@ function _classPrivateFieldInit(obj, privateMap, value) { }(obj, privateMap), privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _getPrototypeOf(o) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { @@ -26,13 +33,13 @@ function _setPrototypeOf(o, p) { return o.__proto__ = p, o; }, _setPrototypeOf(o, p); } -var A = function() { +var _foo = new WeakMap(), A = function() { "use strict"; _classCallCheck(this, A), _classPrivateFieldInit(this, _foo, { writable: !0, value: void 0 }), _classPrivateFieldSet(this, _foo, 3); -}, _foo = new WeakMap(), B = function(A1) { +}, _foo1 = new WeakMap(), B = function(A1) { "use strict"; !function(subClass, superClass) { if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function"); @@ -69,4 +76,4 @@ var A = function() { }), _classPrivateFieldSet(_assertThisInitialized(_this), _foo1, "some string"), _this; } return B; -}(A), _foo1 = new WeakMap(); +}(A); diff --git a/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.1.normal.js index 8262c1a3155..441e62fe488 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.1.normal.js @@ -116,25 +116,39 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -152,7 +166,7 @@ function _wrapAsyncGenerator(fn) { return new AsyncGenerator(fn.apply(this, arguments)); }; } -var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), _quux = new WeakSet(), _quux = new WeakSet(); +var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), __quux = new WeakMap(), _quux = new WeakMap(); // @target: esnext, es2022 // @lib: esnext, es2022 // @useDefineForClassFields: false @@ -166,8 +180,10 @@ class A { writable: true, value: void 0 }); - _classPrivateMethodInit(this, _quux); - _classPrivateMethodInit(this, _quux); + _classPrivateFieldInit(this, _quux, { + get: get_quux, + set: set_quux + }); _classPrivateMethodGet(this, _foo, foo).call(this, 30); _classPrivateMethodGet(this, _bar, bar).call(this, 30); _classPrivateMethodGet(this, _baz, baz).call(this, 30); @@ -175,7 +191,6 @@ class A { _classPrivateFieldSet(this, _quux, (_this_quux = +_classPrivateMethodGet(this, _quux, quux)) + 1), _this_quux; } } -var __quux = new WeakMap(); function foo(a) {} function bar(a) { return _bar1.apply(this, arguments); @@ -193,10 +208,10 @@ function _baz1() { }); return _baz1.apply(this, arguments); } -function quux() { +function get_quux() { return _classPrivateFieldGet(this, __quux); } -function quux(val) { +function set_quux(val) { _classPrivateFieldSet(this, __quux, val); } var _foo1 = new WeakSet(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.2.minified.js index 0436e5b9a07..169d7e6aa77 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndMethods_es2015.2.minified.js @@ -60,4 +60,4 @@ function _AwaitValue(value) { return this._invoke("throw", arg); }, AsyncGenerator.prototype.return = function(arg) { return this._invoke("return", arg); -}, new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(), new WeakSet(); +}, new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(), new WeakMap(), new WeakSet(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.1.normal.js index 5fbefa39685..31fc60e91bb 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.1.normal.js @@ -122,30 +122,44 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { @@ -224,7 +238,7 @@ function _createSuper(Derived) { }; } import regeneratorRuntime from "regenerator-runtime"; -var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), _quux = new WeakSet(), _quux = new WeakSet(); +var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), __quux = new WeakMap(), _quux = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -236,15 +250,16 @@ var A = function A() { writable: true, value: void 0 }); - _classPrivateMethodInit(this, _quux); - _classPrivateMethodInit(this, _quux); + _classPrivateFieldInit(this, _quux, { + get: get_quux, + set: set_quux + }); _classPrivateMethodGet(this, _foo, foo).call(this, 30); _classPrivateMethodGet(this, _bar, bar).call(this, 30); _classPrivateMethodGet(this, _baz, baz).call(this, 30); _classPrivateFieldSet(this, _quux, _classPrivateMethodGet(this, _quux, quux) + 1); _classPrivateFieldSet(this, _quux, (_this_quux = +_classPrivateMethodGet(this, _quux, quux)) + 1), _this_quux; }; -var __quux = new WeakMap(); function foo(a) {} function bar(a) { return _bar1.apply(this, arguments); @@ -278,10 +293,10 @@ function _baz1() { })); return _baz1.apply(this, arguments); } -function quux() { +function get_quux() { return _classPrivateFieldGet(this, __quux); } -function quux(val) { +function set_quux(val) { _classPrivateFieldSet(this, __quux, val); } var _foo1 = new WeakSet(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.2.minified.js index f9d5fde1de6..af7e06f381e 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndMethods_es5.2.minified.js @@ -71,11 +71,22 @@ function _checkPrivateRedeclaration(obj, privateCollection) { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); +} function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); @@ -104,14 +115,16 @@ function _setPrototypeOf(o, p) { return this._invoke("return", arg); }; import regeneratorRuntime from "regenerator-runtime"; -var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), _quux = new WeakSet(), _quux = new WeakSet(), A = function() { +var _foo = new WeakSet(), _bar = new WeakSet(), _baz = new WeakSet(), __quux = new WeakMap(), _quux = new WeakMap(), A = function() { "use strict"; - var obj, privateMap, value; - _classCallCheck(this, A), _classPrivateMethodInit(this, _foo), _classPrivateMethodInit(this, _bar), _classPrivateMethodInit(this, _baz), obj = this, value = { + _classCallCheck(this, A), _classPrivateMethodInit(this, _foo), _classPrivateMethodInit(this, _bar), _classPrivateMethodInit(this, _baz), _classPrivateFieldInit(this, __quux, { writable: !0, value: void 0 - }, _checkPrivateRedeclaration(obj, privateMap = __quux), privateMap.set(obj, value), _classPrivateMethodInit(this, _quux), _classPrivateMethodInit(this, _quux), _classPrivateMethodGet(this, _foo, foo).call(this, 30), _classPrivateMethodGet(this, _bar, bar).call(this, 30), _classPrivateMethodGet(this, _baz, baz).call(this, 30), _classPrivateFieldSet(this, _quux, _classPrivateMethodGet(this, _quux, quux) + 1), _classPrivateFieldSet(this, _quux, +_classPrivateMethodGet(this, _quux, quux) + 1); -}, __quux = new WeakMap(); + }), _classPrivateFieldInit(this, _quux, { + get: get_quux, + set: set_quux + }), _classPrivateMethodGet(this, _foo, foo).call(this, 30), _classPrivateMethodGet(this, _bar, bar).call(this, 30), _classPrivateMethodGet(this, _baz, baz).call(this, 30), _classPrivateFieldSet(this, _quux, _classPrivateMethodGet(this, _quux, quux) + 1), _classPrivateFieldSet(this, _quux, +_classPrivateMethodGet(this, _quux, quux) + 1); +}; function foo(a) {} function bar(a) { return _bar1.apply(this, arguments); @@ -159,13 +172,11 @@ function _baz1() { return new AsyncGenerator(fn.apply(this, arguments)); })).apply(this, arguments); } -function quux() { - return (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, __quux); +function get_quux() { + var receiver, privateMap, descriptor, receiver, descriptor; + return receiver = this, (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = __quux, "get")).get ? descriptor.get.call(receiver) : descriptor.value; } -function quux(val) { +function set_quux(val) { _classPrivateFieldSet(this, __quux, val); } var _foo1 = new WeakSet(), B = function(A1) { diff --git a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.1.normal.js index 9d2fe5ffb1e..c7f0bbd0dd2 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.1.normal.js @@ -111,20 +111,36 @@ function _asyncToGenerator(fn) { function _AwaitValue(value) { this.wrapped = value; } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } return descriptor.value; } +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _wrapAsyncGenerator(fn) { @@ -159,6 +175,10 @@ var __quux = { writable: true, value: void 0 }; +var _quux = { + get: get_quux, + set: set_quux +}; function foo(a) {} function bar(a) { return _bar.apply(this, arguments); @@ -176,10 +196,10 @@ function _baz() { }); return _baz.apply(this, arguments); } -function quux() { +function get_quux() { return _classStaticPrivateFieldSpecGet(this, A, __quux); } -function quux(val) { +function set_quux(val) { _classStaticPrivateFieldSpecSet(this, A, __quux, val); } class B extends A { diff --git a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.2.minified.js index ea27ea241e6..ec539b5a605 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es2015.2.minified.js @@ -61,10 +61,17 @@ function asyncGeneratorStep(gen, resolve, reject, _next, _throw, key, arg) { function _AwaitValue(value) { this.wrapped = value; } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classStaticPrivateMethodGet(receiver, classConstructor, method) { return _classCheckPrivateStaticAccess(receiver, classConstructor), method; @@ -89,6 +96,14 @@ class A { var __quux = { writable: !0, value: void 0 +}, _quux = { + get: function() { + var receiver, classConstructor, descriptor, receiver, descriptor; + return receiver = this, classConstructor = A, descriptor = __quux, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; + }, + set: function(val) { + _classStaticPrivateFieldSpecSet(this, A, __quux, val); + } }; function bar(a) { return _bar.apply(this, arguments); @@ -110,15 +125,6 @@ function _bar() { }; })(function*(a) {})).apply(this, arguments); } -function quux() { - return (function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - })(this, A, __quux); -} -function quux(val) { - _classStaticPrivateFieldSpecSet(this, A, __quux, val); -} class B extends A { constructor(){ super(), _classStaticPrivateMethodGet(B, B, function(a) {}).call(B, "str"); diff --git a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.1.normal.js index 0e6c2653678..fcfe9594c53 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.1.normal.js @@ -117,25 +117,41 @@ function _asyncToGenerator(fn) { function _AwaitValue(value) { this.wrapped = value; } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return descriptor.value; +} +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } function _getPrototypeOf(o) { @@ -227,6 +243,10 @@ var __quux = { writable: true, value: void 0 }; +var _quux = { + get: get_quux, + set: set_quux +}; function foo(a) {} function bar(a) { return _bar.apply(this, arguments); @@ -260,10 +280,10 @@ function _baz() { })); return _baz.apply(this, arguments); } -function quux() { +function get_quux() { return _classStaticPrivateFieldSpecGet(this, A, __quux); } -function quux(val) { +function set_quux(val) { _classStaticPrivateFieldSpecSet(this, A, __quux, val); } var B = /*#__PURE__*/ function(A) { diff --git a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.2.minified.js index 00781d38a56..1c7e3eb487b 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndStaticMethods_es5.2.minified.js @@ -64,10 +64,17 @@ function _AwaitValue(value) { function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); +} function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + return _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "set"), !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _getPrototypeOf(o) { return _getPrototypeOf = Object.setPrototypeOf ? Object.getPrototypeOf : function _getPrototypeOf(o) { @@ -101,6 +108,14 @@ var A = function() { }, __quux = { writable: !0, value: void 0 +}, _quux = { + get: function() { + var receiver, classConstructor, descriptor, receiver, descriptor; + return receiver = this, classConstructor = A, descriptor = __quux, _classCheckPrivateStaticAccess(receiver, classConstructor), _classCheckPrivateStaticFieldDescriptor(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; + }, + set: function(val) { + _classStaticPrivateFieldSpecSet(this, A, __quux, val); + } }; function foo(a) {} function bar(a) { @@ -130,15 +145,6 @@ function _bar() { }); })).apply(this, arguments); } -function quux() { - return (function(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - })(this, A, __quux); -} -function quux(val) { - _classStaticPrivateFieldSpecSet(this, A, __quux, val); -} var B = function(A1) { "use strict"; !function(subClass, superClass) { diff --git a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.1.normal.js index 26ce4b80403..519c87f3b58 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.1.normal.js @@ -11,7 +11,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _fooMethod = new WeakSet(), _fooProp = new WeakSet(), _fooProp = new WeakSet(); +var _fooField = new WeakMap(), _fooMethod = new WeakSet(), _fooProp = new WeakMap(); // @strict: true // @target: es6 class A { @@ -21,18 +21,19 @@ class A { value: 3 }); _classPrivateMethodInit(this, _fooMethod); - _classPrivateMethodInit(this, _fooProp); - _classPrivateMethodInit(this, _fooProp); + _classPrivateFieldInit(this, _fooProp, { + get: get_fooProp, + set: set_fooProp + }); this.bar = 3; this.baz = 3; } } -var _fooField = new WeakMap(); function fooMethod() {} -function fooProp() { +function get_fooProp() { return 1; } -function fooProp(value) {} +function set_fooProp(value) {} // `keyof A` should not include '#foo*' let k = "bar"; // OK k = "baz"; // OK diff --git a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.2.minified.js index fa627425d84..08a513adefa 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es2015.2.minified.js @@ -1 +1 @@ -new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(); +new WeakMap(), new WeakSet(), new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.1.normal.js index 6fe4652060b..e2423451373 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.1.normal.js @@ -16,7 +16,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } -var _fooMethod = new WeakSet(), _fooProp = new WeakSet(), _fooProp = new WeakSet(); +var _fooField = new WeakMap(), _fooMethod = new WeakSet(), _fooProp = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -25,19 +25,20 @@ var A = function A() { value: 3 }); _classPrivateMethodInit(this, _fooMethod); - _classPrivateMethodInit(this, _fooProp); - _classPrivateMethodInit(this, _fooProp); + _classPrivateFieldInit(this, _fooProp, { + get: get_fooProp, + set: set_fooProp + }); // @strict: true // @target: es6 this.bar = 3; this.baz = 3; }; -var _fooField = new WeakMap(); function fooMethod() {} -function fooProp() { +function get_fooProp() { return 1; } -function fooProp(value) {} +function set_fooProp(value) {} // `keyof A` should not include '#foo*' var k = "bar"; // OK k = "baz"; // OK diff --git a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.2.minified.js index d14dea5e74c..0d6217fdd65 100644 --- a/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAndkeyof_es5.2.minified.js @@ -1,16 +1,22 @@ function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); } -var _fooMethod = new WeakSet(), _fooProp = new WeakSet(), _fooProp = new WeakSet(), A = function() { +var _fooField = new WeakMap(), _fooMethod = new WeakSet(), _fooProp = new WeakMap(), A = function() { "use strict"; - var obj, privateMap, value; + var obj, privateSet; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, A), obj = this, value = { + }(this, A), _classPrivateFieldInit(this, _fooField, { writable: !0, value: 3 - }, _checkPrivateRedeclaration(obj, privateMap = _fooField), privateMap.set(obj, value), _classPrivateMethodInit(this, _fooMethod), _classPrivateMethodInit(this, _fooProp), _classPrivateMethodInit(this, _fooProp), this.bar = 3, this.baz = 3; -}, _fooField = new WeakMap(); + }), obj = this, _checkPrivateRedeclaration(obj, privateSet = _fooMethod), privateSet.add(obj), _classPrivateFieldInit(this, _fooProp, { + get: function() { + return 1; + }, + set: set_fooProp + }), this.bar = 3, this.baz = 3; +}; +function set_fooProp(value) {} diff --git a/crates/swc/tests/tsc-references/privateNamesAssertion_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAssertion_es2015.1.normal.js index b50dd4b8701..8561bde8d5c 100644 --- a/crates/swc/tests/tsc-references/privateNamesAssertion_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAssertion_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -23,6 +33,7 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } +var _p1 = new WeakMap(); // @strict: true // @target: esnext, es2022 // @useDefineForClassFields: false @@ -42,7 +53,6 @@ class Foo { }); } } -var _p1 = new WeakMap(); var _p11 = new WeakSet(); class Foo2 { m1(v) { diff --git a/crates/swc/tests/tsc-references/privateNamesAssertion_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesAssertion_es5.1.normal.js index 0e648d808dc..dbdadbbb3f2 100644 --- a/crates/swc/tests/tsc-references/privateNamesAssertion_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesAssertion_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -42,6 +52,7 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +var _p1 = new WeakMap(); var Foo = // @strict: true // @target: esnext, es2022 // @useDefineForClassFields: false @@ -69,7 +80,6 @@ var Foo = // @strict: true ]); return Foo; }(); -var _p1 = new WeakMap(); var _p11 = new WeakSet(); var Foo2 = /*#__PURE__*/ function() { "use strict"; diff --git a/crates/swc/tests/tsc-references/privateNamesAssertion_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesAssertion_es5.2.minified.js index 1e5480436d5..6b8af7a5109 100644 --- a/crates/swc/tests/tsc-references/privateNamesAssertion_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesAssertion_es5.2.minified.js @@ -13,7 +13,7 @@ function _defineProperties(target, props) { function _createClass(Constructor, protoProps, staticProps) { return protoProps && _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Constructor; } -var Foo = function() { +var _p1 = new WeakMap(), Foo = function() { "use strict"; function Foo() { var obj, privateMap, value; @@ -28,14 +28,15 @@ var Foo = function() { { key: "m1", value: function(v) { - (function(receiver, privateMap) { + var receiver, privateMap, descriptor, receiver, descriptor; + (receiver = this, (descriptor = descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _p1).call(this, v); + return privateMap.get(receiver); + })(receiver, privateMap = _p1, "get")).get ? descriptor.get.call(receiver) : descriptor.value).call(this, v); } } ]), Foo; -}(), _p1 = new WeakMap(), _p11 = new WeakSet(), Foo2 = function() { +}(), _p11 = new WeakSet(), Foo2 = function() { "use strict"; function Foo2() { var obj, privateSet; diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.1.normal.js index 2e203cac4b6..d46b2c49c58 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.1.normal.js @@ -3,22 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } +var _foo = new WeakMap(); // @target: es2015 class Parent { accessChildProps() { @@ -33,23 +53,23 @@ class Parent { }); } } -var _foo = new WeakMap(); var _bar = { writable: true, value: 5 }; +var _foo1 = new WeakMap(), _bar1 = new WeakMap(); class Child extends Parent { constructor(...args){ super(...args); _classPrivateFieldInit(this, _foo1, { writable: true, value: "foo" - }); + }) // OK (Child's #foo does not conflict, as `Parent`'s `#foo` is not accessible) + ; _classPrivateFieldInit(this, _bar1, { writable: true, value: "bar" - }); + }) // OK + ; } } -var _foo1 = new WeakMap(); -var _bar1 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.2.minified.js index 66984f1c13a..60855229567 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es2015.2.minified.js @@ -1,17 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } +var _foo = new WeakMap(); class Parent { accessChildProps() { - !function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(new Child(), _foo), (function(receiver, classConstructor, descriptor) { + var receiver, privateMap, descriptor, receiver, classConstructor, descriptor; + descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + })(receiver = new Child(), privateMap = _foo, "get"), _classApplyDescriptorGet(receiver, descriptor), receiver = Child, classConstructor = Parent, descriptor = _bar, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - descriptor.value; - })(Child, Parent, _bar); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + })(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } constructor(){ _classPrivateFieldInit(this, _foo, { @@ -20,10 +26,10 @@ class Parent { }); } } -var _foo = new WeakMap(), _bar = { +var _bar = { writable: !0, value: 5 -}; +}, _foo1 = new WeakMap(), _bar1 = new WeakMap(); class Child extends Parent { constructor(...args){ super(...args), _classPrivateFieldInit(this, _foo1, { @@ -35,4 +41,3 @@ class Child extends Parent { }); } } -var _foo1 = new WeakMap(), _bar1 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.1.normal.js index d5ce2d4d39b..0617e83a26c 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.1.normal.js @@ -9,26 +9,40 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return privateMap.get(receiver).value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -80,6 +94,11 @@ var _typeof = function(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; @@ -104,6 +123,7 @@ function _createSuper(Derived) { return _possibleConstructorReturn(this, result); }; } +var _foo = new WeakMap(); var Parent = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -126,11 +146,11 @@ var Parent = // @target: es2015 ]); return Parent; }(); -var _foo = new WeakMap(); var _bar = { writable: true, value: 5 }; +var _foo1 = new WeakMap(), _bar1 = new WeakMap(); var Child = /*#__PURE__*/ function(Parent) { "use strict"; _inherits(Child, Parent); @@ -142,14 +162,14 @@ var Child = /*#__PURE__*/ function(Parent) { _classPrivateFieldInit(_assertThisInitialized(_this), _foo1, { writable: true, value: "foo" - }); + }) // OK (Child's #foo does not conflict, as `Parent`'s `#foo` is not accessible) + ; _classPrivateFieldInit(_assertThisInitialized(_this), _bar1, { writable: true, value: "bar" - }); + }) // OK + ; return _this; } return Child; }(Parent); -var _foo1 = new WeakMap(); -var _bar1 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.2.minified.js index 4903d548cf4..ddfc80df832 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-1_es5.2.minified.js @@ -2,6 +2,9 @@ function _assertThisInitialized(self) { if (void 0 === self) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return self; } +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } @@ -26,7 +29,7 @@ function _setPrototypeOf(o, p) { return o.__proto__ = p, o; }, _setPrototypeOf(o, p); } -var Parent = function() { +var _foo = new WeakMap(), Parent = function() { "use strict"; var Constructor, protoProps, staticProps; function Parent() { @@ -39,20 +42,22 @@ var Parent = function() { { key: "accessChildProps", value: function() { - (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - privateMap.get(receiver).value; - })(new Child(), _foo), (function(receiver, classConstructor, descriptor) { + var receiver, privateMap, descriptor, receiver, classConstructor, descriptor; + descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + })(receiver = new Child(), privateMap = _foo, "get"), _classApplyDescriptorGet(receiver, descriptor), receiver = Child, classConstructor = Parent, descriptor = _bar, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - descriptor.value; - })(Child, Parent, _bar); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + })(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Parent; -}(), _foo = new WeakMap(), _bar = { +}(), _bar = { writable: !0, value: 5 -}, Child = function(Parent) { +}, _foo1 = new WeakMap(), _bar1 = new WeakMap(), Child = function(Parent) { "use strict"; !function(subClass, superClass) { if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function"); @@ -92,4 +97,4 @@ var Parent = function() { }), _this; } return Child; -}(Parent), _foo1 = new WeakMap(), _bar1 = new WeakMap(); +}(Parent); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.1.normal.js index 6c3adab3fef..9254a6d0faf 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.1.normal.js @@ -3,22 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } +var _foo = new WeakMap(); // @target: es2015 class Parent { accessChildProps() { @@ -33,24 +53,24 @@ class Parent { }); } } -var _foo = new WeakMap(); var _bar = { writable: true, value: 5 }; +var _foo1 = new WeakMap(), _bar1 = new WeakMap(); class Child extends Parent { constructor(...args){ super(...args); _classPrivateFieldInit(this, _foo1, { writable: true, value: "foo" - }); + }) // OK (Child's #foo does not conflict, as `Parent`'s `#foo` is not accessible) + ; _classPrivateFieldInit(this, _bar1, { writable: true, value: "bar" - }); + }) // OK + ; } } -var _foo1 = new WeakMap(); -var _bar1 = new WeakMap(); new Parent().accessChildProps(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.2.minified.js index 2fb5c9d1bd2..07302ec2e79 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es2015.2.minified.js @@ -1,17 +1,23 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } +var _foo = new WeakMap(); class Parent { accessChildProps() { - !function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(new Child(), _foo), (function(receiver, classConstructor, descriptor) { + var receiver, privateMap, descriptor, receiver, classConstructor, descriptor; + descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + })(receiver = new Child(), privateMap = _foo, "get"), _classApplyDescriptorGet(receiver, descriptor), receiver = Child, classConstructor = Parent, descriptor = _bar, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - descriptor.value; - })(Child, Parent, _bar); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + })(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } constructor(){ _classPrivateFieldInit(this, _foo, { @@ -20,10 +26,10 @@ class Parent { }); } } -var _foo = new WeakMap(), _bar = { +var _bar = { writable: !0, value: 5 -}; +}, _foo1 = new WeakMap(), _bar1 = new WeakMap(); class Child extends Parent { constructor(...args){ super(...args), _classPrivateFieldInit(this, _foo1, { @@ -35,5 +41,4 @@ class Child extends Parent { }); } } -var _foo1 = new WeakMap(), _bar1 = new WeakMap(); new Parent().accessChildProps(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.1.normal.js index 1ec989de9b0..35418e95ec2 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.1.normal.js @@ -9,26 +9,40 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); } - return privateMap.get(receiver).value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -80,6 +94,11 @@ var _typeof = function(obj) { "@swc/helpers - typeof"; return obj && typeof Symbol !== "undefined" && obj.constructor === Symbol ? "symbol" : typeof obj; }; +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} function _isNativeReflectConstruct() { if (typeof Reflect === "undefined" || !Reflect.construct) return false; if (Reflect.construct.sham) return false; @@ -104,6 +123,7 @@ function _createSuper(Derived) { return _possibleConstructorReturn(this, result); }; } +var _foo = new WeakMap(); var Parent = // @target: es2015 /*#__PURE__*/ function() { "use strict"; @@ -126,11 +146,11 @@ var Parent = // @target: es2015 ]); return Parent; }(); -var _foo = new WeakMap(); var _bar = { writable: true, value: 5 }; +var _foo1 = new WeakMap(), _bar1 = new WeakMap(); var Child = /*#__PURE__*/ function(Parent) { "use strict"; _inherits(Child, Parent); @@ -142,15 +162,15 @@ var Child = /*#__PURE__*/ function(Parent) { _classPrivateFieldInit(_assertThisInitialized(_this), _foo1, { writable: true, value: "foo" - }); + }) // OK (Child's #foo does not conflict, as `Parent`'s `#foo` is not accessible) + ; _classPrivateFieldInit(_assertThisInitialized(_this), _bar1, { writable: true, value: "bar" - }); + }) // OK + ; return _this; } return Child; }(Parent); -var _foo1 = new WeakMap(); -var _bar1 = new WeakMap(); new Parent().accessChildProps(); diff --git a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.2.minified.js index afbede2b101..9c244cf26ba 100644 --- a/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesConstructorChain-2_es5.2.minified.js @@ -2,6 +2,9 @@ function _assertThisInitialized(self) { if (void 0 === self) throw new ReferenceError("this hasn't been initialised - super() hasn't been called"); return self; } +function _classApplyDescriptorGet(receiver, descriptor) { + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } @@ -26,7 +29,7 @@ function _setPrototypeOf(o, p) { return o.__proto__ = p, o; }, _setPrototypeOf(o, p); } -var Parent = function() { +var _foo = new WeakMap(), Parent = function() { "use strict"; var Constructor, protoProps, staticProps; function Parent() { @@ -39,20 +42,22 @@ var Parent = function() { { key: "accessChildProps", value: function() { - (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - privateMap.get(receiver).value; - })(new Child(), _foo), (function(receiver, classConstructor, descriptor) { + var receiver, privateMap, descriptor, receiver, classConstructor, descriptor; + descriptor = (function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); + })(receiver = new Child(), privateMap = _foo, "get"), _classApplyDescriptorGet(receiver, descriptor), receiver = Child, classConstructor = Parent, descriptor = _bar, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - descriptor.value; - })(Child, Parent, _bar); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to " + action + " private static field before its declaration"); + })(descriptor, "get"), _classApplyDescriptorGet(receiver, descriptor); } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Parent; -}(), _foo = new WeakMap(), _bar = { +}(), _bar = { writable: !0, value: 5 -}, Child = function(Parent) { +}, _foo1 = new WeakMap(), _bar1 = new WeakMap(), Child = function(Parent) { "use strict"; !function(subClass, superClass) { if ("function" != typeof superClass && null !== superClass) throw new TypeError("Super expression must either be null or a function"); @@ -92,5 +97,5 @@ var Parent = function() { }), _this; } return Child; -}(Parent), _foo1 = new WeakMap(), _bar1 = new WeakMap(); +}(Parent); new Parent().accessChildProps(); diff --git a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.1.normal.js index 3e9c1ceaa02..507cf8cfc4b 100644 --- a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.1.normal.js @@ -12,12 +12,18 @@ function _classPrivateMethodInit(obj, privateSet) { privateSet.add(obj); } var /** + * @public + */ _a = new WeakMap(), /** + * @private + */ _b = new WeakMap(), /** + * @protected + */ _c = new WeakMap(), /** * @public */ _aMethod = new WeakSet(), /** * @private */ _bMethod = new WeakSet(), /** * @protected - */ _cMethod = new WeakSet(), _aProp = new WeakSet(), _aProp = new WeakSet(), _bProp = new WeakSet(), _bProp = new WeakSet(), _cProp = new WeakSet(), _cProp = new WeakSet(); + */ _cMethod = new WeakSet(), _aProp = new WeakMap(), _bProp = new WeakMap(), _cProp = new WeakMap(); // @allowJs: true // @checkJs: true // @strict: true @@ -26,21 +32,15 @@ var /** // @filename: privateNamesIncompatibleModifiersJs.js class A { constructor(){ - _classPrivateFieldInit(this, /** - * @public - */ _a, { + _classPrivateFieldInit(this, _a, { writable: true, value: 1 }); - _classPrivateFieldInit(this, /** - * @private - */ _b, { + _classPrivateFieldInit(this, _b, { writable: true, value: 1 }); - _classPrivateFieldInit(this, /** - * @protected - */ _c, { + _classPrivateFieldInit(this, _c, { writable: true, value: 1 }); @@ -49,27 +49,30 @@ class A { _classPrivateMethodInit(this, _cMethod); /** * @public - */ _classPrivateMethodInit(this, _aProp); - /** + */ _classPrivateFieldInit(this, _aProp, { + get: get_aProp, + set: /** * @public - */ _classPrivateMethodInit(this, _aProp); + */ set_aProp + }); /** * @private - */ _classPrivateMethodInit(this, _bProp); - /** + */ _classPrivateFieldInit(this, _bProp, { + get: get_bProp, + set: /** * @private - */ _classPrivateMethodInit(this, _bProp); + */ set_bProp + }); /** * @protected - */ _classPrivateMethodInit(this, _cProp); - /** + */ _classPrivateFieldInit(this, _cProp, { + get: get_cProp, + set: /** * @protected - */ _classPrivateMethodInit(this, _cProp); + */ set_cProp + }); } } -var _a = new WeakMap(); -var _b = new WeakMap(); -var _c = new WeakMap(); function aMethod() { return 1; } @@ -79,15 +82,15 @@ function bMethod() { function cMethod() { return 1; } -function aProp() { +function get_aProp() { return 1; } -function aProp(value) {} -function bProp() { +function set_aProp(value) {} +function get_bProp() { return 1; } -function bProp(value) {} -function cProp() { +function set_bProp(value) {} +function get_cProp() { return 1; } -function cProp(value) {} +function set_cProp(value) {} diff --git a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.2.minified.js index e2512fc5877..39b1b9fe755 100644 --- a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es2015.2.minified.js @@ -1 +1 @@ -new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(), new WeakMap(), new WeakMap(); +new WeakMap(), new WeakMap(), new WeakMap(), new WeakSet(), new WeakSet(), new WeakSet(), new WeakMap(), new WeakMap(), new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.1.normal.js index f050de94e5b..0b638d66afe 100644 --- a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.1.normal.js @@ -17,30 +17,30 @@ function _classPrivateMethodInit(obj, privateSet) { privateSet.add(obj); } var /** + * @public + */ _a = new WeakMap(), /** + * @private + */ _b = new WeakMap(), /** + * @protected + */ _c = new WeakMap(), /** * @public */ _aMethod = new WeakSet(), /** * @private */ _bMethod = new WeakSet(), /** * @protected - */ _cMethod = new WeakSet(), _aProp = new WeakSet(), _aProp = new WeakSet(), _bProp = new WeakSet(), _bProp = new WeakSet(), _cProp = new WeakSet(), _cProp = new WeakSet(); + */ _cMethod = new WeakSet(), _aProp = new WeakMap(), _bProp = new WeakMap(), _cProp = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); - _classPrivateFieldInit(this, /** - * @public - */ _a, { + _classPrivateFieldInit(this, _a, { writable: true, value: 1 }); - _classPrivateFieldInit(this, /** - * @private - */ _b, { + _classPrivateFieldInit(this, _b, { writable: true, value: 1 }); - _classPrivateFieldInit(this, /** - * @protected - */ _c, { + _classPrivateFieldInit(this, _c, { writable: true, value: 1 }); @@ -49,26 +49,29 @@ var A = function A() { _classPrivateMethodInit(this, _cMethod); /** * @public - */ _classPrivateMethodInit(this, _aProp); - /** + */ _classPrivateFieldInit(this, _aProp, { + get: get_aProp, + set: /** * @public - */ _classPrivateMethodInit(this, _aProp); + */ set_aProp + }); /** * @private - */ _classPrivateMethodInit(this, _bProp); - /** + */ _classPrivateFieldInit(this, _bProp, { + get: get_bProp, + set: /** * @private - */ _classPrivateMethodInit(this, _bProp); + */ set_bProp + }); /** * @protected - */ _classPrivateMethodInit(this, _cProp); - /** + */ _classPrivateFieldInit(this, _cProp, { + get: get_cProp, + set: /** * @protected - */ _classPrivateMethodInit(this, _cProp); + */ set_cProp + }); }; -var _a = new WeakMap(); -var _b = new WeakMap(); -var _c = new WeakMap(); function aMethod() { return 1; } @@ -78,15 +81,15 @@ function bMethod() { function cMethod() { return 1; } -function aProp() { +function get_aProp() { return 1; } -function aProp(value) {} -function bProp() { +function set_aProp(value) {} +function get_bProp() { return 1; } -function bProp(value) {} -function cProp() { +function set_bProp(value) {} +function get_cProp() { return 1; } -function cProp(value) {} +function set_cProp(value) {} diff --git a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.2.minified.js index 438be4224be..7510c49d220 100644 --- a/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesIncompatibleModifiersJs_es5.2.minified.js @@ -7,7 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); } -var _aMethod = new WeakSet(), _bMethod = new WeakSet(), _cMethod = new WeakSet(), _aProp = new WeakSet(), _aProp = new WeakSet(), _bProp = new WeakSet(), _bProp = new WeakSet(), _cProp = new WeakSet(), _cProp = new WeakSet(), A = function() { +var _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(), _aMethod = new WeakSet(), _bMethod = new WeakSet(), _cMethod = new WeakSet(), _aProp = new WeakMap(), _bProp = new WeakMap(), _cProp = new WeakMap(), A = function() { "use strict"; !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); @@ -20,5 +20,23 @@ var _aMethod = new WeakSet(), _bMethod = new WeakSet(), _cMethod = new WeakSet() }), _classPrivateFieldInit(this, _c, { writable: !0, value: 1 - }), _classPrivateMethodInit(this, _aMethod), _classPrivateMethodInit(this, _bMethod), _classPrivateMethodInit(this, _cMethod), _classPrivateMethodInit(this, _aProp), _classPrivateMethodInit(this, _aProp), _classPrivateMethodInit(this, _bProp), _classPrivateMethodInit(this, _bProp), _classPrivateMethodInit(this, _cProp), _classPrivateMethodInit(this, _cProp); -}, _a = new WeakMap(), _b = new WeakMap(), _c = new WeakMap(); + }), _classPrivateMethodInit(this, _aMethod), _classPrivateMethodInit(this, _bMethod), _classPrivateMethodInit(this, _cMethod), _classPrivateFieldInit(this, _aProp, { + get: function() { + return 1; + }, + set: set_aProp + }), _classPrivateFieldInit(this, _bProp, { + get: function() { + return 1; + }, + set: set_bProp + }), _classPrivateFieldInit(this, _cProp, { + get: function() { + return 1; + }, + set: set_cProp + }); +}; +function set_aProp(value) {} +function set_bProp(value) {} +function set_cProp(value) {} diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-1_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-1_es2015.1.normal.js index 4008a66547c..f3671c5e0d5 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-1_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-1_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); // @strict: true // @target: es6 // @strictPropertyInitialization: false @@ -18,7 +19,7 @@ class A { }); } } -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); class B { constructor(){ _classPrivateFieldInit(this, _foo1, { @@ -27,5 +28,4 @@ class B { }); } } -var _foo1 = new WeakMap(); const b = new B(); // Error: Property #foo is missing diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.1.normal.js index bf75543c9b8..cf58912ef6b 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -20,7 +21,7 @@ var A = function A() { value: void 0 }); }; -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); var B = function B() { "use strict"; _classCallCheck(this, B); @@ -29,5 +30,4 @@ var B = function B() { value: void 0 }); }; -var _foo1 = new WeakMap(); var b = new B(); // Error: Property #foo is missing diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.2.minified.js index 5f81e268402..c2d92d7fd4d 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-1_es5.2.minified.js @@ -6,17 +6,17 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var A = function() { +var _foo = new WeakMap(), A = function() { "use strict"; _classCallCheck(this, A), _classPrivateFieldInit(this, _foo, { writable: !0, value: void 0 }); -}, _foo = new WeakMap(), B = function() { +}, _foo1 = new WeakMap(), B = function() { "use strict"; _classCallCheck(this, B), _classPrivateFieldInit(this, _foo1, { writable: !0, value: void 0 }); -}, _foo1 = new WeakMap(); +}; new B(); diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.1.normal.js index 883f3878fcb..4e4883429e4 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -16,6 +26,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { // @filename: main.ts import { Foo as A } from "./a"; import { Foo as B } from "./b"; +var _x = new WeakMap(); // @target: es2015 // @filename: a.ts export class Foo { @@ -29,7 +40,7 @@ export class Foo { }); } } -var _x = new WeakMap(); +var _x1 = new WeakMap(); // @filename: b.ts export class Foo { constructor(){ @@ -39,7 +50,6 @@ export class Foo { }); } } -var _x1 = new WeakMap(); const a = new A(); const b = new B(); a.copy(b); // error diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.2.minified.js index fb003978749..8a0a0d2b616 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-2_es2015.2.minified.js @@ -5,12 +5,14 @@ function _classPrivateFieldInit(obj, privateMap, value) { } import { Foo as A } from "./a"; import { Foo as B } from "./b"; +var _x = new WeakMap(); export class Foo { copy(other) { - !function(receiver, privateMap) { + var receiver, privateMap, descriptor, receiver, descriptor; + (descriptor = descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(other, _x); + return privateMap.get(receiver); + })(receiver = other, privateMap = _x, "get")).get ? descriptor.get.call(receiver) : descriptor.value; } constructor(){ _classPrivateFieldInit(this, _x, { @@ -19,7 +21,7 @@ export class Foo { }); } } -var _x = new WeakMap(); +var _x1 = new WeakMap(); export class Foo { constructor(){ _classPrivateFieldInit(this, _x1, { @@ -28,6 +30,5 @@ export class Foo { }); } } -var _x1 = new WeakMap(); const a = new A(), b = new B(); a.copy(b); // error diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.1.normal.js index f24aff52a56..02ec51361d9 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -35,6 +45,7 @@ function _createClass(Constructor, protoProps, staticProps) { // @filename: main.ts import { Foo as A } from "./a"; import { Foo as B } from "./b"; +var _x = new WeakMap(); // @target: es2015 // @filename: a.ts export var Foo = /*#__PURE__*/ function() { @@ -56,7 +67,7 @@ export var Foo = /*#__PURE__*/ function() { ]); return Foo; }(); -var _x = new WeakMap(); +var _x1 = new WeakMap(); // @filename: b.ts export var Foo = function Foo() { "use strict"; @@ -66,7 +77,6 @@ export var Foo = function Foo() { value: void 0 }); }; -var _x1 = new WeakMap(); var a = new A(); var b = new B(); a.copy(b); // error diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.2.minified.js index 79ab5511682..f1d203a6436 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-2_es5.2.minified.js @@ -14,6 +14,7 @@ function _defineProperties(target, props) { } import { Foo as A } from "./a"; import { Foo as B } from "./b"; +var _x = new WeakMap(); export var Foo = function() { "use strict"; var Constructor, protoProps, staticProps; @@ -27,15 +28,16 @@ export var Foo = function() { { key: "copy", value: function(other) { - !function(receiver, privateMap) { + var receiver, privateMap, descriptor, receiver, descriptor; + (descriptor = descriptor = (function(receiver, privateMap, action) { if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - }(other, _x); + return privateMap.get(receiver); + })(receiver = other, privateMap = _x, "get")).get ? descriptor.get.call(receiver) : descriptor.value; } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Foo; }(); -var _x = new WeakMap(); +var _x1 = new WeakMap(); export var Foo = function() { "use strict"; _classCallCheck(this, Foo), _classPrivateFieldInit(this, _x1, { @@ -43,5 +45,5 @@ export var Foo = function() { value: void 0 }); }; -var _x1 = new WeakMap(), a = new A(), b = new B(); +var a = new A(), b = new B(); a.copy(b); // error diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.1.normal.js index 12e1a5a3dbd..337cf8a665e 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.1.normal.js @@ -3,16 +3,32 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); +} +function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } - return descriptor.value; } +var _foo = new WeakMap(); // @target: es2015 class A { constructor(){ @@ -22,11 +38,11 @@ class A { }); } } -var _foo = new WeakMap(); var _foo = { writable: true, value: true -}; +} // error (duplicate) +; class B { test(x) { _classStaticPrivateFieldSpecGet(x, B, _foo1); // error (#foo is a static property on B, not an instance property) diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.2.minified.js index a52bcb09ddb..d49633ef8ee 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-3_es2015.2.minified.js @@ -1,10 +1,12 @@ new WeakMap(); class B { test(x) { - !function(receiver, classConstructor, descriptor) { + var receiver, classConstructor, descriptor, receiver, descriptor; + receiver = x, classConstructor = B, descriptor = _foo, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - }(x, B, _foo); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + })(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; } } var _foo = { diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.1.normal.js index 420e4e7ed53..8456d6d8136 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.1.normal.js @@ -3,20 +3,30 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ @@ -32,6 +42,12 @@ function _createClass(Constructor, protoProps, staticProps) { if (staticProps) _defineProperties(Constructor, staticProps); return Constructor; } +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} +var _foo = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -40,11 +56,11 @@ var A = function A() { value: 1 }); }; -var _foo = new WeakMap(); var _foo = { writable: true, value: true -}; +} // error (duplicate) +; var B = /*#__PURE__*/ function() { "use strict"; function B() { diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.2.minified.js index 2a60610cdea..0f86ff97911 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-3_es5.2.minified.js @@ -7,7 +7,7 @@ function _defineProperties(target, props) { descriptor.enumerable = descriptor.enumerable || !1, descriptor.configurable = !0, "value" in descriptor && (descriptor.writable = !0), Object.defineProperty(target, descriptor.key, descriptor); } } -var A = function() { +var _foo = new WeakMap(), A = function() { "use strict"; var obj, privateMap, value; _classCallCheck(this, A), obj = this, value = { @@ -16,7 +16,7 @@ var A = function() { }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); })(obj, privateMap = _foo), privateMap.set(obj, value); -}, _foo = new WeakMap(), _foo = { +}, _foo = { writable: !0, value: !0 }, B = function() { @@ -29,10 +29,12 @@ var A = function() { { key: "test", value: function(x) { - !function(receiver, classConstructor, descriptor) { + var receiver, classConstructor, descriptor, receiver, descriptor; + receiver = x, classConstructor = B, descriptor = _foo1, (function(receiver, classConstructor) { if (receiver !== classConstructor) throw new TypeError("Private static access of wrong provenance"); - return descriptor.value; - }(x, B, _foo1); + })(receiver, classConstructor), (function(descriptor, action) { + if (void 0 === descriptor) throw new TypeError("attempted to get private static field before its declaration"); + })(descriptor, "get"), (descriptor = descriptor).get ? descriptor.get.call(receiver) : descriptor.value; } } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), B; diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-4_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-4_es2015.1.normal.js index f2a3400b28c..b406fa672c7 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-4_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-4_es2015.1.normal.js @@ -10,6 +10,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { // @target: es2015 class A1 { } +var _something = new WeakMap(); class C { constructor(){ _classPrivateFieldInit(this, _something, { @@ -18,5 +19,4 @@ class C { }); } } -var _something = new WeakMap(); const c = a; diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.1.normal.js index 61b9dd60baf..4e53ca9ebe6 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.1.normal.js @@ -16,6 +16,7 @@ var A1 = function A1() { "use strict"; _classCallCheck(this, A1); }; +var _something = new WeakMap(); var C = function C() { "use strict"; _classCallCheck(this, C); @@ -24,5 +25,4 @@ var C = function C() { value: void 0 }); }; -var _something = new WeakMap(); var c = a; diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.2.minified.js index 55948987dd1..49cba3a3dd0 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-4_es5.2.minified.js @@ -4,7 +4,7 @@ function _classCallCheck(instance, Constructor) { var A1 = function() { "use strict"; _classCallCheck(this, A1); -}, C = function() { +}, _something = new WeakMap(), C = function() { "use strict"; var obj, privateMap, value; _classCallCheck(this, C), obj = this, value = { @@ -13,4 +13,4 @@ var A1 = function() { }, (function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); })(obj, privateMap = _something), privateMap.set(obj, value); -}, _something = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-5_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-5_es2015.1.normal.js index fe274910830..f60ff77b705 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-5_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-5_es2015.1.normal.js @@ -7,6 +7,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); // @strict: true // @target: es6 // @strictPropertyInitialization: false @@ -19,7 +20,7 @@ class A { }); } } -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); class B { constructor(){ _classPrivateFieldInit(this, _foo1, { @@ -28,5 +29,4 @@ class B { }); } } -var _foo1 = new WeakMap(); const b = new B(); diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.1.normal.js index 9fb8b1d791e..dd9a84c163a 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.1.normal.js @@ -12,6 +12,7 @@ function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } +var _foo = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); @@ -20,7 +21,7 @@ var A = function A() { value: void 0 }); }; -var _foo = new WeakMap(); +var _foo1 = new WeakMap(); var B = function B() { "use strict"; _classCallCheck(this, B); @@ -29,5 +30,4 @@ var B = function B() { value: void 0 }); }; -var _foo1 = new WeakMap(); var b = new B(); diff --git a/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.2.minified.js index 5f81e268402..c2d92d7fd4d 100644 --- a/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUnique-5_es5.2.minified.js @@ -6,17 +6,17 @@ function _classPrivateFieldInit(obj, privateMap, value) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } -var A = function() { +var _foo = new WeakMap(), A = function() { "use strict"; _classCallCheck(this, A), _classPrivateFieldInit(this, _foo, { writable: !0, value: void 0 }); -}, _foo = new WeakMap(), B = function() { +}, _foo1 = new WeakMap(), B = function() { "use strict"; _classCallCheck(this, B), _classPrivateFieldInit(this, _foo1, { writable: !0, value: void 0 }); -}, _foo1 = new WeakMap(); +}; new B(); diff --git a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.1.normal.js index de327443205..399cfdab4ee 100644 --- a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.1.normal.js @@ -3,11 +3,21 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -23,60 +33,63 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } +var _foo = new WeakMap(), _bar = new WeakMap(); // @target: es2015 class A { constructor(){ _classPrivateFieldInit(this, _foo, { writable: true, value: _classPrivateFieldGet(this, _bar) - }); + }) // Error + ; _classPrivateFieldInit(this, _bar, { writable: true, value: 3 }); } } -var _foo = new WeakMap(); -var _bar = new WeakMap(); -var _bar1 = new WeakSet(); +var _foo1 = new WeakMap(), _bar1 = new WeakSet(); class A2 { constructor(){ _classPrivateFieldInit(this, _foo1, { writable: true, - value: _classPrivateMethodGet(this, _bar1, bar).call(this) - }); + value: _classPrivateMethodGet(this, _bar1, bar1).call(this) + }) // No Error + ; _classPrivateMethodInit(this, _bar1); } } -var _foo1 = new WeakMap(); -function bar() { +function bar1() { return 3; } -var _bar2 = new WeakSet(); +var _foo2 = new WeakMap(), _bar2 = new WeakMap(); class A3 { constructor(){ _classPrivateFieldInit(this, _foo2, { writable: true, - value: _classPrivateMethodGet(this, _bar2, bar1) + value: _classPrivateMethodGet(this, _bar2, bar) + }) // No Error + ; + _classPrivateFieldInit(this, _bar2, { + get: get_bar, + set: void 0 }); - _classPrivateMethodInit(this, _bar2); } } -var _foo2 = new WeakMap(); -function bar1() { +function get_bar() { return 3; } +var _foo3 = new WeakMap(), _bar3 = new WeakMap(); class B { constructor(){ _classPrivateFieldInit(this, _foo3, { writable: true, value: _classPrivateFieldGet(this, _bar3) - }); + }) // Error + ; _classPrivateFieldInit(this, _bar3, { writable: true, value: _classPrivateFieldGet(this, _foo3) }); } } -var _foo3 = new WeakMap(); -var _bar3 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.2.minified.js index c36102b2b4e..79f328676d1 100644 --- a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es2015.2.minified.js @@ -1 +1 @@ -new WeakMap(), new WeakMap(), new WeakSet(), new WeakMap(), new WeakSet(), new WeakMap(), new WeakMap(), new WeakMap(); +new WeakMap(), new WeakMap(), new WeakMap(), new WeakSet(), new WeakMap(), new WeakMap(), new WeakMap(), new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.1.normal.js b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.1.normal.js index e365578e9e3..20f3e1e2039 100644 --- a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.1.normal.js @@ -3,16 +3,26 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); @@ -28,59 +38,62 @@ function _classPrivateMethodInit(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); } +var _foo = new WeakMap(), _bar = new WeakMap(); var A = function A() { "use strict"; _classCallCheck(this, A); _classPrivateFieldInit(this, _foo, { writable: true, value: _classPrivateFieldGet(this, _bar) - }); + }) // Error + ; _classPrivateFieldInit(this, _bar, { writable: true, value: 3 }); }; -var _foo = new WeakMap(); -var _bar = new WeakMap(); -var _bar1 = new WeakSet(); +var _foo1 = new WeakMap(), _bar1 = new WeakSet(); var A2 = function A2() { "use strict"; _classCallCheck(this, A2); _classPrivateFieldInit(this, _foo1, { writable: true, - value: _classPrivateMethodGet(this, _bar1, bar).call(this) - }); + value: _classPrivateMethodGet(this, _bar1, bar1).call(this) + }) // No Error + ; _classPrivateMethodInit(this, _bar1); }; -var _foo1 = new WeakMap(); -function bar() { +function bar1() { return 3; } -var _bar2 = new WeakSet(); +var _foo2 = new WeakMap(), _bar2 = new WeakMap(); var A3 = function A3() { "use strict"; _classCallCheck(this, A3); _classPrivateFieldInit(this, _foo2, { writable: true, - value: _classPrivateMethodGet(this, _bar2, bar1) + value: _classPrivateMethodGet(this, _bar2, bar) + }) // No Error + ; + _classPrivateFieldInit(this, _bar2, { + get: get_bar, + set: void 0 }); - _classPrivateMethodInit(this, _bar2); }; -var _foo2 = new WeakMap(); -function bar1() { +function get_bar() { return 3; } +var _foo3 = new WeakMap(), _bar3 = new WeakMap(); var B = function B() { "use strict"; _classCallCheck(this, B); _classPrivateFieldInit(this, _foo3, { writable: true, value: _classPrivateFieldGet(this, _bar3) - }); + }) // Error + ; _classPrivateFieldInit(this, _bar3, { writable: true, value: _classPrivateFieldGet(this, _foo3) }); }; -var _foo3 = new WeakMap(); -var _bar3 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.2.minified.js b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.2.minified.js index e6e70c68910..3a6b6b482d2 100644 --- a/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateNamesUseBeforeDef_es5.2.minified.js @@ -5,8 +5,11 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; + var receiver, descriptor, descriptor = function(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); + return privateMap.get(receiver); + }(receiver, privateMap, "get"); + return descriptor.get ? descriptor.get.call(receiver) : descriptor.value; } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap), privateMap.set(obj, value); @@ -15,10 +18,7 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet), privateSet.add(obj); -} -var A = function() { +var _foo = new WeakMap(), _bar = new WeakMap(), A = function() { "use strict"; _classCallCheck(this, A), _classPrivateFieldInit(this, _foo, { writable: !0, @@ -27,23 +27,27 @@ var A = function() { writable: !0, value: 3 }); -}, _foo = new WeakMap(), _bar = new WeakMap(), _bar1 = new WeakSet(), A2 = function() { +}, _foo1 = new WeakMap(), _bar1 = new WeakSet(), A2 = function() { "use strict"; + var obj, privateSet; _classCallCheck(this, A2), _classPrivateFieldInit(this, _foo1, { writable: !0, value: _classPrivateMethodGet(this, _bar1, function() { return 3; }).call(this) - }), _classPrivateMethodInit(this, _bar1); -}, _foo1 = new WeakMap(), _bar2 = new WeakSet(), A3 = function() { + }), obj = this, _checkPrivateRedeclaration(obj, privateSet = _bar1), privateSet.add(obj); +}, _foo2 = new WeakMap(), _bar2 = new WeakMap(), A3 = function() { "use strict"; _classCallCheck(this, A3), _classPrivateFieldInit(this, _foo2, { writable: !0, - value: _classPrivateMethodGet(this, _bar2, function() { + value: _classPrivateMethodGet(this, _bar2, bar) + }), _classPrivateFieldInit(this, _bar2, { + get: function() { return 3; - }) - }), _classPrivateMethodInit(this, _bar2); -}, _foo2 = new WeakMap(), B = function() { + }, + set: void 0 + }); +}, _foo3 = new WeakMap(), _bar3 = new WeakMap(), B = function() { "use strict"; _classCallCheck(this, B), _classPrivateFieldInit(this, _foo3, { writable: !0, @@ -52,4 +56,4 @@ var A = function() { writable: !0, value: _classPrivateFieldGet(this, _foo3) }); -}, _foo3 = new WeakMap(), _bar3 = new WeakMap(); +}; diff --git a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.1.normal.js b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.1.normal.js index ca51ecadf66..b078359ad00 100644 --- a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.1.normal.js @@ -3,46 +3,7 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldSet(receiver, privateMap, value1) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value1; - return value1; -} -function _classPrivateMethodGet(receiver, privateSet, fn) { - if (!privateSet.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); - } - return fn; -} -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} -function _extends() { - _extends = Object.assign || function(target) { - for(var i = 1; i < arguments.length; i++){ - var source = arguments[i]; - for(var key in source){ - if (Object.prototype.hasOwnProperty.call(source, key)) { - target[key] = source[key]; - } - } - } - return target; - }; - return _extends.apply(this, arguments); -} -function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); +function _classApplyDescriptorDestructureSet(receiver, descriptor) { if (descriptor.set) { if (!("__destrObj" in descriptor)) { descriptor.__destrObj = { @@ -59,7 +20,56 @@ function _classPrivateFieldDestructureSet(receiver, privateMap) { return descriptor; } } -var _value = new WeakSet(), _valueRest = new WeakSet(), _valueOne = new WeakSet(), _valueCompound = new WeakSet(); +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; +} +function _classPrivateMethodGet(receiver, privateSet, fn) { + if (!privateSet.has(receiver)) { + throw new TypeError("attempted to get private field on non-instance"); + } + return fn; +} +function _extends() { + _extends = Object.assign || function(target) { + for(var i = 1; i < arguments.length; i++){ + var source = arguments[i]; + for(var key in source){ + if (Object.prototype.hasOwnProperty.call(source, key)) { + target[key] = source[key]; + } + } + } + return target; + }; + return _extends.apply(this, arguments); +} +function _classPrivateFieldDestructureSet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} +var _value = new WeakMap(), _valueRest = new WeakMap(), _valueOne = new WeakMap(), _valueCompound = new WeakMap(); // @target: es2015 class Test { m() { @@ -105,14 +115,26 @@ class Test { _classPrivateFieldSet(this, _valueCompound, _classPrivateMethodGet(this, _valueCompound, valueCompound) + 3); } constructor(){ - _classPrivateMethodInit(this, _value); - _classPrivateMethodInit(this, _valueRest); - _classPrivateMethodInit(this, _valueOne); - _classPrivateMethodInit(this, _valueCompound); + _classPrivateFieldInit(this, _value, { + get: void 0, + set: set_value + }); + _classPrivateFieldInit(this, _valueRest, { + get: void 0, + set: set_valueRest + }); + _classPrivateFieldInit(this, _valueOne, { + get: void 0, + set: set_valueOne + }); + _classPrivateFieldInit(this, _valueCompound, { + get: void 0, + set: set_valueCompound + }); } } -function value(v) {} -function valueRest(v) {} -function valueOne(v) {} -function valueCompound(v) {} +function set_value(v) {} +function set_valueRest(v) {} +function set_valueOne(v) {} +function set_valueCompound(v) {} new Test().m(); diff --git a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.2.minified.js b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.2.minified.js index e1d9e5b9839..4dcf2d4ebf2 100644 --- a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.2.minified.js +++ b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es2015.2.minified.js @@ -1,18 +1,26 @@ -function _classPrivateFieldSet(receiver, privateMap, value1) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value1, value1; +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + !function(obj, privateCollection) { + if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + }(obj, privateMap), privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - !function(obj, privateCollection) { - if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); -} function _extends() { return (_extends = Object.assign || function(target) { for(var i = 1; i < arguments.length; i++){ @@ -23,21 +31,18 @@ function _extends() { }).apply(this, arguments); } function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { - set value (v){ - descriptor.set.call(receiver, v); - } - }), descriptor.__destrObj; - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor); } -var _value = new WeakSet(), _valueRest = new WeakSet(), _valueOne = new WeakSet(), _valueCompound = new WeakSet(); -function value(v) {} -function valueRest(v) {} -function valueOne(v) {} -function valueCompound(v) {} +var _value = new WeakMap(), _valueRest = new WeakMap(), _valueOne = new WeakMap(), _valueCompound = new WeakMap(); new class { m() { var _tmp, _tmp1; @@ -65,6 +70,18 @@ new class { ], _classPrivateMethodGet(this, _valueOne, valueOne), _classPrivateMethodGet(this, _valueRest, valueRest), _classPrivateFieldSet(this, _valueCompound, _classPrivateMethodGet(this, _valueCompound, valueCompound) + 3); } constructor(){ - _classPrivateMethodInit(this, _value), _classPrivateMethodInit(this, _valueRest), _classPrivateMethodInit(this, _valueOne), _classPrivateMethodInit(this, _valueCompound); + _classPrivateFieldInit(this, _value, { + get: void 0, + set: function(v) {} + }), _classPrivateFieldInit(this, _valueRest, { + get: void 0, + set: function(v) {} + }), _classPrivateFieldInit(this, _valueOne, { + get: void 0, + set: function(v) {} + }), _classPrivateFieldInit(this, _valueCompound, { + get: void 0, + set: function(v) {} + }); } }().m(); diff --git a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.1.normal.js b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.1.normal.js index 10ac35ff0d4..0cb12f144cd 100644 --- a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.1.normal.js @@ -11,21 +11,52 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldSet(receiver, privateMap, value1) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value1; - return value1; + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + _checkPrivateRedeclaration(obj, privateMap); + privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); + return value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { @@ -33,10 +64,6 @@ function _classPrivateMethodGet(receiver, privateSet, fn) { } return fn; } -function _classPrivateMethodInit(obj, privateSet) { - _checkPrivateRedeclaration(obj, privateSet); - privateSet.add(obj); -} function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; @@ -83,36 +110,31 @@ function _unsupportedIterableToArray(o, minLen) { if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); } function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (descriptor.set) { - if (!("__destrObj" in descriptor)) { - descriptor.__destrObj = { - set value (v){ - descriptor.set.call(receiver, v); - } - }; - } - return descriptor.__destrObj; - } else { - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - return descriptor; - } + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); } -var _value = new WeakSet(), _valueRest = new WeakSet(), _valueOne = new WeakSet(), _valueCompound = new WeakSet(); +var _value = new WeakMap(), _valueRest = new WeakMap(), _valueOne = new WeakMap(), _valueCompound = new WeakMap(); var Test = // @target: es2015 /*#__PURE__*/ function() { "use strict"; function Test() { _classCallCheck(this, Test); - _classPrivateMethodInit(this, _value); - _classPrivateMethodInit(this, _valueRest); - _classPrivateMethodInit(this, _valueOne); - _classPrivateMethodInit(this, _valueCompound); + _classPrivateFieldInit(this, _value, { + get: void 0, + set: set_value + }); + _classPrivateFieldInit(this, _valueRest, { + get: void 0, + set: set_valueRest + }); + _classPrivateFieldInit(this, _valueOne, { + get: void 0, + set: set_valueOne + }); + _classPrivateFieldInit(this, _valueCompound, { + get: void 0, + set: set_valueCompound + }); } _createClass(Test, [ { @@ -164,8 +186,8 @@ var Test = // @target: es2015 ]); return Test; }(); -function value(v) {} -function valueRest(v) {} -function valueOne(v) {} -function valueCompound(v) {} +function set_value(v) {} +function set_valueRest(v) {} +function set_valueOne(v) {} +function set_valueCompound(v) {} new Test().m(); diff --git a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.2.minified.js b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.2.minified.js index 163d0e7c746..3cfbd687936 100644 --- a/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/privateWriteOnlyAccessorRead_es5.2.minified.js @@ -3,21 +3,29 @@ function _arrayLikeToArray(arr, len) { for(var i = 0, arr2 = new Array(len); i < len; i++)arr2[i] = arr[i]; return arr2; } -function _classPrivateFieldSet(receiver, privateMap, value1) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value1, value1; +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} +function _classPrivateFieldInit(obj, privateMap, value) { + !function(obj, privateCollection) { + if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); + }(obj, privateMap), privateMap.set(obj, value); +} +function _classPrivateFieldSet(receiver, privateMap, value) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); return fn; } -function _classPrivateMethodInit(obj, privateSet) { - !function(obj, privateCollection) { - if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); - }(obj, privateSet), privateSet.add(obj); -} function _defineProperties(target, props) { for(var i = 0; i < props.length; i++){ var descriptor = props[i]; @@ -34,23 +42,36 @@ function _extends() { }).apply(this, arguments); } function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { - set value (v){ - descriptor.set.call(receiver, v); - } - }), descriptor.__destrObj; - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return (function(receiver, descriptor) { + if (descriptor.set) return "__destrObj" in descriptor || (descriptor.__destrObj = { + set value (v){ + descriptor.set.call(receiver, v); + } + }), descriptor.__destrObj; + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + return descriptor; + })(receiver, descriptor); } -var _value = new WeakSet(), _valueRest = new WeakSet(), _valueOne = new WeakSet(), _valueCompound = new WeakSet(), Test = function() { +var _value = new WeakMap(), _valueRest = new WeakMap(), _valueOne = new WeakMap(), _valueCompound = new WeakMap(), Test = function() { "use strict"; var Constructor, protoProps, staticProps; function Test() { !function(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); - }(this, Test), _classPrivateMethodInit(this, _value), _classPrivateMethodInit(this, _valueRest), _classPrivateMethodInit(this, _valueOne), _classPrivateMethodInit(this, _valueCompound); + }(this, Test), _classPrivateFieldInit(this, _value, { + get: void 0, + set: set_value + }), _classPrivateFieldInit(this, _valueRest, { + get: void 0, + set: set_valueRest + }), _classPrivateFieldInit(this, _valueOne, { + get: void 0, + set: set_valueOne + }), _classPrivateFieldInit(this, _valueCompound, { + get: void 0, + set: set_valueCompound + }); } return Constructor = Test, protoProps = [ { @@ -96,8 +117,8 @@ var _value = new WeakSet(), _valueRest = new WeakSet(), _valueOne = new WeakSet( } ], _defineProperties(Constructor.prototype, protoProps), staticProps && _defineProperties(Constructor, staticProps), Test; }(); -function value(v) {} -function valueRest(v) {} -function valueOne(v) {} -function valueCompound(v) {} +function set_value(v) {} +function set_valueRest(v) {} +function set_valueOne(v) {} +function set_valueCompound(v) {} new Test().m(); diff --git a/crates/swc/tests/tsc-references/strictPropertyInitialization_es2015.1.normal.js b/crates/swc/tests/tsc-references/strictPropertyInitialization_es2015.1.normal.js index e8af47f1fdf..2766d2efef1 100644 --- a/crates/swc/tests/tsc-references/strictPropertyInitialization_es2015.1.normal.js +++ b/crates/swc/tests/tsc-references/strictPropertyInitialization_es2015.1.normal.js @@ -3,27 +3,42 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } -function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); } - return privateMap.get(receiver).value; + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _f = new WeakMap(), _g = new WeakMap(), _h = new WeakMap(), _i = new WeakMap(); // @strict: true // @target:es2015 // @declaration: true @@ -48,13 +63,10 @@ class C1 { }); } } -var _f = new WeakMap(); -var _g = new WeakMap(); -var _h = new WeakMap(); -var _i = new WeakMap(); // No strict initialization checks for static members class C3 { } +var _d = new WeakMap(), _e = new WeakMap(), _f1 = new WeakMap(); // Initializer satisfies strict initialization check class C4 { constructor(){ @@ -75,9 +87,7 @@ class C4 { this.c = "abc"; } } -var _d = new WeakMap(); -var _e = new WeakMap(); -var _f1 = new WeakMap(); +var _b = new WeakMap(); // Assignment in constructor satisfies strict initialization check class C5 { constructor(){ @@ -89,7 +99,7 @@ class C5 { _classPrivateFieldSet(this, _b, 0); } } -var _b = new WeakMap(); +var _b1 = new WeakMap(); // All code paths must contain assignment class C6 { constructor(cond){ @@ -104,7 +114,7 @@ class C6 { _classPrivateFieldSet(this, _b1, 0); } } -var _b1 = new WeakMap(); +var _b2 = new WeakMap(); class C7 { constructor(cond){ _classPrivateFieldInit(this, _b2, { @@ -120,13 +130,13 @@ class C7 { _classPrivateFieldSet(this, _b2, 1); } } -var _b2 = new WeakMap(); // Properties with string literal names aren't checked class C8 { } // No strict initialization checks for abstract members class C9 { } +var _d1 = new WeakMap(); // Properties with non-undefined types must be assigned before they can be accessed // within their constructor class C10 { @@ -144,7 +154,7 @@ class C10 { let y = this.c; } } -var _d1 = new WeakMap(); +var _b3 = new WeakMap(); class C11 { constructor(){ _classPrivateFieldInit(this, _b3, { @@ -155,4 +165,3 @@ class C11 { _classPrivateFieldSet(this, _b3, someValue()); } } -var _b3 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.1.normal.js b/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.1.normal.js index 201e37f8f17..88d6a0e0aa8 100644 --- a/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.1.normal.js +++ b/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.1.normal.js @@ -3,32 +3,47 @@ function _checkPrivateRedeclaration(obj, privateCollection) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } } +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } } -function _classPrivateFieldGet(receiver, privateMap) { +function _classExtractFieldDescriptor(receiver, privateMap, action) { if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); + throw new TypeError("attempted to " + action + " private field on non-instance"); } - return privateMap.get(receiver).value; + return privateMap.get(receiver); +} +function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } function _classPrivateFieldInit(obj, privateMap, value) { _checkPrivateRedeclaration(obj, privateMap); privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } +var _f = new WeakMap(), _g = new WeakMap(), _h = new WeakMap(), _i = new WeakMap(); var C1 = function C1() { "use strict"; _classCallCheck(this, C1); @@ -49,14 +64,11 @@ var C1 = function C1() { value: void 0 }); }; -var _f = new WeakMap(); -var _g = new WeakMap(); -var _h = new WeakMap(); -var _i = new WeakMap(); var C3 = function C3() { "use strict"; _classCallCheck(this, C3); }; +var _d = new WeakMap(), _e = new WeakMap(), _f1 = new WeakMap(); var C4 = function C4() { "use strict"; _classCallCheck(this, C4); @@ -77,9 +89,7 @@ var C4 = function C4() { this.b = 0; this.c = "abc"; }; -var _d = new WeakMap(); -var _e = new WeakMap(); -var _f1 = new WeakMap(); +var _b = new WeakMap(); var C5 = function C5() { "use strict"; _classCallCheck(this, C5); @@ -90,7 +100,7 @@ var C5 = function C5() { this.a = 0; _classPrivateFieldSet(this, _b, 0); }; -var _b = new WeakMap(); +var _b1 = new WeakMap(); var C6 = function C6(cond) { "use strict"; _classCallCheck(this, C6); @@ -104,7 +114,7 @@ var C6 = function C6(cond) { this.a = 0; _classPrivateFieldSet(this, _b1, 0); }; -var _b1 = new WeakMap(); +var _b2 = new WeakMap(); var C7 = function C7(cond) { "use strict"; _classCallCheck(this, C7); @@ -120,7 +130,6 @@ var C7 = function C7(cond) { this.a = 0; _classPrivateFieldSet(this, _b2, 1); }; -var _b2 = new WeakMap(); var C8 = function C8() { "use strict"; _classCallCheck(this, C8); @@ -129,6 +138,7 @@ var C9 = function C9() { "use strict"; _classCallCheck(this, C9); }; +var _d1 = new WeakMap(); var C10 = function C10() { "use strict"; _classCallCheck(this, C10); @@ -144,7 +154,7 @@ var C10 = function C10() { _classPrivateFieldSet(this, _d1, x); var y = this.c; }; -var _d1 = new WeakMap(); +var _b3 = new WeakMap(); var C11 = function C11() { "use strict"; _classCallCheck(this, C11); @@ -155,4 +165,3 @@ var C11 = function C11() { this.a = someValue(); _classPrivateFieldSet(this, _b3, someValue()); }; -var _b3 = new WeakMap(); diff --git a/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.2.minified.js b/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.2.minified.js index 698eb70f6d7..78d696f97b4 100644 --- a/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.2.minified.js +++ b/crates/swc/tests/tsc-references/strictPropertyInitialization_es5.2.minified.js @@ -1,18 +1,26 @@ function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) throw new TypeError("Cannot call a class as a function"); } +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) throw new TypeError("attempted to " + action + " private field on non-instance"); + return privateMap.get(receiver); +} function _classPrivateFieldInit(obj, privateMap, value) { !function(obj, privateCollection) { if (privateCollection.has(obj)) throw new TypeError("Cannot initialize the same private elements twice on an object"); }(obj, privateMap), privateMap.set(obj, value); } function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to set private field on non-instance"); - var descriptor = privateMap.get(receiver); - if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); - return descriptor.value = value, value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return !function(receiver, descriptor, value) { + if (descriptor.set) descriptor.set.call(receiver, value); + else { + if (!descriptor.writable) throw new TypeError("attempted to set read only private field"); + descriptor.value = value; + } + }(receiver, descriptor, value), value; } -var C1 = function() { +var _f = new WeakMap(), _g = new WeakMap(), _h = new WeakMap(), _i = new WeakMap(), C1 = function() { "use strict"; _classCallCheck(this, C1), _classPrivateFieldInit(this, _f, { writable: !0, @@ -27,10 +35,10 @@ var C1 = function() { writable: !0, value: void 0 }); -}, _f = new WeakMap(), _g = new WeakMap(), _h = new WeakMap(), _i = new WeakMap(), C3 = function() { +}, C3 = function() { "use strict"; _classCallCheck(this, C3); -}, C4 = function() { +}, _d = new WeakMap(), _e = new WeakMap(), _f1 = new WeakMap(), C4 = function() { "use strict"; _classCallCheck(this, C4), _classPrivateFieldInit(this, _d, { writable: !0, @@ -42,19 +50,19 @@ var C1 = function() { writable: !0, value: "abc" }), this.a = 0, this.b = 0, this.c = "abc"; -}, _d = new WeakMap(), _e = new WeakMap(), _f1 = new WeakMap(), C5 = function() { +}, _b = new WeakMap(), C5 = function() { "use strict"; _classCallCheck(this, C5), _classPrivateFieldInit(this, _b, { writable: !0, value: void 0 }), this.a = 0, _classPrivateFieldSet(this, _b, 0); -}, _b = new WeakMap(), C6 = function(cond) { +}, _b1 = new WeakMap(), C6 = function(cond) { "use strict"; _classCallCheck(this, C6), _classPrivateFieldInit(this, _b1, { writable: !0, value: void 0 }), cond || (this.a = 0, _classPrivateFieldSet(this, _b1, 0)); -}, _b1 = new WeakMap(), C7 = function(cond) { +}, _b2 = new WeakMap(), C7 = function(cond) { "use strict"; if (_classCallCheck(this, C7), _classPrivateFieldInit(this, _b2, { writable: !0, @@ -64,27 +72,24 @@ var C1 = function() { return; } this.a = 0, _classPrivateFieldSet(this, _b2, 1); -}, _b2 = new WeakMap(), C8 = function() { +}, C8 = function() { "use strict"; _classCallCheck(this, C8); }, C9 = function() { "use strict"; _classCallCheck(this, C9); -}, C10 = function() { +}, _d1 = new WeakMap(), C10 = function() { "use strict"; _classCallCheck(this, C10), _classPrivateFieldInit(this, _d1, { writable: !0, value: void 0 }); - var x = this.a; - this.a = this.b, this.b = (function(receiver, privateMap) { - if (!privateMap.has(receiver)) throw new TypeError("attempted to get private field on non-instance"); - return privateMap.get(receiver).value; - })(this, _d1), this.b = x, _classPrivateFieldSet(this, _d1, x), this.c; -}, _d1 = new WeakMap(), C11 = function() { + var receiver, privateMap, descriptor, receiver, descriptor, x = this.a; + this.a = this.b, receiver = this, this.b = (descriptor = descriptor = _classExtractFieldDescriptor(receiver, privateMap = _d1, "get")).get ? descriptor.get.call(receiver) : descriptor.value, this.b = x, _classPrivateFieldSet(this, _d1, x), this.c; +}, _b3 = new WeakMap(), C11 = function() { "use strict"; _classCallCheck(this, C11), _classPrivateFieldInit(this, _b3, { writable: !0, value: void 0 }), this.a = someValue(), _classPrivateFieldSet(this, _b3, someValue()); -}, _b3 = new WeakMap(); +}; diff --git a/crates/swc_ecma_parser/src/error.rs b/crates/swc_ecma_parser/src/error.rs index b1579cc1c60..f85c7b7f968 100644 --- a/crates/swc_ecma_parser/src/error.rs +++ b/crates/swc_ecma_parser/src/error.rs @@ -49,6 +49,8 @@ pub enum SyntaxError { ArrowNotAllowed, ExportNotAllowed, GetterSetterCannotBeReadonly, + GetterParam, + SetterParam, TopLevelAwait, TopLevelAwaitInScript, @@ -220,7 +222,6 @@ pub enum SyntaxError { TS1183, TS1184, TS1093, - TS1094, TS1196, TS1242, TS1243(JsWord, JsWord), @@ -469,6 +470,8 @@ impl SyntaxError { SyntaxError::GetterSetterCannotBeReadonly => { "A getter or a setter cannot be readonly".into() } + SyntaxError::GetterParam => "A `get` accessor cannot have parameters".into(), + SyntaxError::SetterParam => "A `set` accessor must have exactly one parameter".into(), SyntaxError::RestPatInSetter => "Rest pattern is not allowed in setter".into(), SyntaxError::GeneratorConstructor => "A constructor cannot be generator".into(), @@ -548,9 +551,6 @@ impl SyntaxError { SyntaxError::TS1093 => { "Type annotation cannot appear on a constructor declaration".into() } - SyntaxError::TS1094 => { - "A `set` accessor must have a corresponding `get` accessor".into() - } SyntaxError::TS1196 => "Catch clause variable cannot have a type annotation".into(), SyntaxError::TS1242 => { "`abstract` modifier can only appear on a class or method declaration".into() diff --git a/crates/swc_ecma_parser/src/parser/class_and_fn.rs b/crates/swc_ecma_parser/src/parser/class_and_fn.rs index 6509cb0efdc..20fb218daab 100644 --- a/crates/swc_ecma_parser/src/parser/class_and_fn.rs +++ b/crates/swc_ecma_parser/src/parser/class_and_fn.rs @@ -835,7 +835,7 @@ impl<'a, I: Tokens> Parser { let params = p.parse_formal_params()?; if params.iter().filter(|p| is_not_this(p)).count() != 0 { - p.emit_err(key_span, SyntaxError::TS1094); + p.emit_err(key_span, SyntaxError::GetterParam); } Ok(params) @@ -859,7 +859,7 @@ impl<'a, I: Tokens> Parser { let params = p.parse_formal_params()?; if params.iter().filter(|p| is_not_this(p)).count() != 1 { - p.emit_err(key_span, SyntaxError::TS1094); + p.emit_err(key_span, SyntaxError::SetterParam); } if !params.is_empty() { diff --git a/crates/swc_ecma_parser/src/parser/object.rs b/crates/swc_ecma_parser/src/parser/object.rs index 8c77b4728ce..332cceef095 100644 --- a/crates/swc_ecma_parser/src/parser/object.rs +++ b/crates/swc_ecma_parser/src/parser/object.rs @@ -287,7 +287,7 @@ impl ParseObject> for Parser { let params = p.parse_formal_params()?; if params.iter().filter(|p| is_not_this(p)).count() != 0 { - p.emit_err(key_span, SyntaxError::TS1094); + p.emit_err(key_span, SyntaxError::GetterParam); } Ok(params) @@ -297,15 +297,8 @@ impl ParseObject> for Parser { ) .map( |Function { - body, - type_params, - return_type, - .. + body, return_type, .. }| { - if let Some(type_params) = type_params { - parser.emit_err(type_params.span(), SyntaxError::TS1094); - } - if parser.input.syntax().typescript() && parser.input.target() == EsVersion::Es3 { @@ -329,7 +322,7 @@ impl ParseObject> for Parser { let params = p.parse_formal_params()?; if params.iter().filter(|p| is_not_this(p)).count() != 1 { - p.emit_err(key_span, SyntaxError::TS1094); + p.emit_err(key_span, SyntaxError::SetterParam); } if !params.is_empty() { @@ -352,30 +345,19 @@ impl ParseObject> for Parser { false, false, ) - .map( - |Function { - params, - body, - type_params, - .. - }| { - if let Some(type_params) = type_params { - parser.emit_err(type_params.span(), SyntaxError::TS1094); - } - - // debug_assert_eq!(params.len(), 1); - PropOrSpread::Prop(Box::new(Prop::Setter(SetterProp { - span: span!(parser, start), - key, - body, - param: params - .into_iter() - .map(|p| p.pat) - .next() - .unwrap_or(Pat::Invalid(Invalid { span: key_span })), - }))) - }, - ), + .map(|Function { params, body, .. }| { + // debug_assert_eq!(params.len(), 1); + PropOrSpread::Prop(Box::new(Prop::Setter(SetterProp { + span: span!(parser, start), + key, + body, + param: params + .into_iter() + .map(|p| p.pat) + .next() + .unwrap_or(Pat::Invalid(Invalid { span: key_span })), + }))) + }), js_word!("async") => parser .parse_fn_args_body( // no decorator in an object literal diff --git a/crates/swc_ecma_parser/src/parser/pat.rs b/crates/swc_ecma_parser/src/parser/pat.rs index 80381cdd4b7..48157be1b09 100644 --- a/crates/swc_ecma_parser/src/parser/pat.rs +++ b/crates/swc_ecma_parser/src/parser/pat.rs @@ -359,7 +359,7 @@ impl<'a, I: Tokens> Parser { let cnt = params.iter().filter(|p| is_not_this(p)).count(); if cnt != 1 { - self.emit_err(key_span, SyntaxError::TS1094); + self.emit_err(key_span, SyntaxError::SetterParam); } if !params.is_empty() { diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/44dda972051e652d.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/44dda972051e652d.js.stderr index 8fd02e7a022..66e53f5c77f 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/44dda972051e652d.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/44dda972051e652d.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `get` accessor cannot have parameters --> $DIR/tests/test262-parser/fail/44dda972051e652d.js:1:11 | 1 | class A { get prop(x) {} } diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/45cb305cf7a07edd.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/45cb305cf7a07edd.js.stderr index 95dc361da53..f0fdc981098 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/45cb305cf7a07edd.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/45cb305cf7a07edd.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `set` accessor must have exactly one parameter --> $DIR/tests/test262-parser/fail/45cb305cf7a07edd.js:1:8 | 1 | ({ set prop() {} }) diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/576b2243fb8c3b54.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/576b2243fb8c3b54.js.stderr index a0259551694..5a6457fdbdd 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/576b2243fb8c3b54.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/576b2243fb8c3b54.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `get` accessor cannot have parameters --> $DIR/tests/test262-parser/fail/576b2243fb8c3b54.js:1:8 | 1 | ({ get prop(x) {} }) diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/8b6b9c72c875f0d8.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/8b6b9c72c875f0d8.js.stderr index 6cc9a49ae88..29155c38059 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/8b6b9c72c875f0d8.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/8b6b9c72c875f0d8.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `set` accessor must have exactly one parameter --> $DIR/tests/test262-parser/fail/8b6b9c72c875f0d8.js:1:8 | 1 | ({ set prop(x, y) {} }) diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/9cbce7d794e8d679.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/9cbce7d794e8d679.js.stderr index 33b4cf2238a..fc72a0c7327 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/9cbce7d794e8d679.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/9cbce7d794e8d679.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `set` accessor must have exactly one parameter --> $DIR/tests/test262-parser/fail/9cbce7d794e8d679.js:1:11 | 1 | class A { set prop() {} } diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/b757d5d1b6a40259.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/b757d5d1b6a40259.js.stderr index bb1941b385a..401ca58ea1d 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/b757d5d1b6a40259.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/b757d5d1b6a40259.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `set` accessor must have exactly one parameter --> $DIR/tests/test262-parser/fail/b757d5d1b6a40259.js:1:11 | 1 | class A { set prop(x, y) {} } diff --git a/crates/swc_ecma_parser/tests/test262-error-references/fail/d594faccae1187d7.js.stderr b/crates/swc_ecma_parser/tests/test262-error-references/fail/d594faccae1187d7.js.stderr index d5a4ecb6d73..9d509c53adc 100644 --- a/crates/swc_ecma_parser/tests/test262-error-references/fail/d594faccae1187d7.js.stderr +++ b/crates/swc_ecma_parser/tests/test262-error-references/fail/d594faccae1187d7.js.stderr @@ -1,4 +1,4 @@ -error: A `set` accessor must have a corresponding `get` accessor +error: A `set` accessor must have exactly one parameter --> $DIR/tests/test262-parser/fail/d594faccae1187d7.js:1:8 | 1 | ({ set s() { } }) diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_destructure.js b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_destructure.js new file mode 100644 index 00000000000..f351c741d1d --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_destructure.js @@ -0,0 +1,20 @@ +function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value(v) { + descriptor.set.call(receiver, v) + }, + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + // This should only throw in strict mode, but class bodies are + // always strict and private fields can only be used inside + // class bodies. + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_get.js b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_get.js new file mode 100644 index 00000000000..b252fa29742 --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_get.js @@ -0,0 +1,6 @@ +function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_set.js b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_set.js new file mode 100644 index 00000000000..41d672ce35a --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_apply_descriptor_set.js @@ -0,0 +1,13 @@ +function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + // This should only throw in strict mode, but class bodies are + // always strict and private fields can only be used inside + // class bodies. + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_access.js b/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_access.js index 03723f56bc0..2184f3c3e9c 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_access.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_access.js @@ -1 +1,5 @@ -function _classCheckPrivateStaticAccess(receiver, classConstructor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } } \ No newline at end of file +function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_field_descriptor.js b/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_field_descriptor.js new file mode 100644 index 00000000000..70b385a4ea3 --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_check_private_static_field_descriptor.js @@ -0,0 +1,5 @@ +function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_extract_field_descriptor.js b/crates/swc_ecma_transforms_base/src/helpers/_class_extract_field_descriptor.js new file mode 100644 index 00000000000..397f360df3d --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_extract_field_descriptor.js @@ -0,0 +1,6 @@ +function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_destructure.js b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_destructure.js index 4006dc1ef46..621d8ba6eb3 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_destructure.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_destructure.js @@ -1,25 +1,4 @@ function _classPrivateFieldDestructureSet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - var descriptor = privateMap.get(receiver); - if (descriptor.set) { - if (!("__destrObj" in descriptor)) { - descriptor.__destrObj = { - set value(v) { - descriptor.set.call(receiver, v) - }, - }; - } - return descriptor.__destrObj; - } else { - if (!descriptor.writable) { - // This should only throw in strict mode, but class bodies are - // always strict and private fields can only be used inside - // class bodies. - throw new TypeError("attempted to set read only private field"); - } - - return descriptor; - } - } \ No newline at end of file + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_get.js b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_get.js index f2a07cd9ab9..e8c329b24e8 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_get.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_get.js @@ -1,7 +1,4 @@ function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); - } - - return privateMap.get(receiver).value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_set.js b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_set.js index 13e624028c5..fd6d4a853af 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_set.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_private_field_set.js @@ -1,14 +1,5 @@ function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - - var descriptor = privateMap.get(receiver); - - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - - descriptor.value = value; + var descriptor = _classExtractFieldDescriptor(receiver, privateMap, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_destructure.js b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_destructure.js new file mode 100644 index 00000000000..4f40277fd30 --- /dev/null +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_destructure.js @@ -0,0 +1,5 @@ +function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return _classApplyDescriptorDestructureSet(receiver, descriptor); +} diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_get.js b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_get.js index 916d521f3e6..b2b25bd16ba 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_get.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_get.js @@ -1,7 +1,5 @@ function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - - return descriptor.value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return _classApplyDescriptorGet(receiver, descriptor); } diff --git a/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_set.js b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_set.js index 8e51b36b7a6..b6013ea9e6e 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_set.js +++ b/crates/swc_ecma_transforms_base/src/helpers/_class_static_private_field_spec_set.js @@ -1,12 +1,6 @@ function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - - descriptor.value = value; + _classCheckPrivateStaticAccess(receiver, classConstructor); + _classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + _classApplyDescriptorSet(receiver, descriptor, value); return value; } diff --git a/crates/swc_ecma_transforms_base/src/helpers/mod.rs b/crates/swc_ecma_transforms_base/src/helpers/mod.rs index 50a26ebf142..2c8e6ad3862 100644 --- a/crates/swc_ecma_transforms_base/src/helpers/mod.rs +++ b/crates/swc_ecma_transforms_base/src/helpers/mod.rs @@ -176,18 +176,31 @@ define_helpers!(Helpers { await_async_generator: (await_value), await_value: (), check_private_redeclaration: (), + class_apply_descriptor_destructure: (), + class_apply_descriptor_get: (), + class_apply_descriptor_set: (), class_call_check: (), + class_check_private_static_field_descriptor: (), + class_extract_field_descriptor: (), class_name_tdz_error: (), - class_private_field_get: (), + class_private_field_get: (class_extract_field_descriptor, class_apply_descriptor_get), class_private_field_init: (check_private_redeclaration), class_private_field_loose_base: (), // class_private_field_loose_key: (), - class_private_field_set: (), + class_private_field_set: (class_extract_field_descriptor, class_apply_descriptor_set), class_private_method_get: (), class_private_method_init: (check_private_redeclaration), class_private_method_set: (), - class_static_private_field_spec_get: (), - class_static_private_field_spec_set: (), + class_static_private_field_spec_get: ( + class_check_private_static_access, + class_check_private_static_field_descriptor, + class_apply_descriptor_get + ), + class_static_private_field_spec_set: ( + class_check_private_static_access, + class_check_private_static_field_descriptor, + class_apply_descriptor_set + ), construct: (set_prototype_of), create_class: (), decorate: (to_array, to_property_key), @@ -262,7 +275,15 @@ define_helpers!(Helpers { is_native_function ), - class_private_field_destructure: (), + class_private_field_destructure: ( + class_extract_field_descriptor, + class_apply_descriptor_destructure + ), + class_static_private_field_destructure: ( + class_check_private_static_access, + class_extract_field_descriptor, + class_apply_descriptor_destructure + ), class_static_private_method_get: (class_check_private_static_access), class_check_private_static_access: (), diff --git a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs index c9654e12fc8..15156c43a11 100644 --- a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs +++ b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/mod.rs @@ -1,9 +1,10 @@ #![allow(dead_code)] +use indexmap::IndexMap; use swc_common::{ collections::{AHashMap, AHashSet}, util::take::Take, - Mark, Spanned, SyntaxContext, DUMMY_SP, + Mark, Span, Spanned, SyntaxContext, DUMMY_SP, }; use swc_ecma_ast::*; use swc_ecma_transforms_base::{helper, perf::Check}; @@ -56,6 +57,15 @@ struct ClassProperties { private: PrivateRecord, } +enum MemberInit { + Prop(Span, Box), + Private(Span, Box), + PrivMethod(Span), + PrivAccessor(Span, Option, Option), +} + +type MemberInitMap = IndexMap; + #[fast_path(ShouldWork)] impl VisitMut for ClassProperties { noop_visit_mut_type!(); @@ -384,20 +394,19 @@ impl ClassProperties { let has_super = class.super_class.is_some(); - let mut typescript_constructor_properties = vec![]; - - let mut constructor_exprs = vec![]; + // we need a hash map to avoid generate two init for corresponding getter/setter + let mut constructor_inits = MemberInitMap::default(); let mut vars = vec![]; - let mut extra_stmts = vec![]; + let mut extra_inits = MemberInitMap::default(); + // same here let mut private_method_fn_decls = vec![]; let mut members = vec![]; let mut constructor = None; let mut used_names = vec![]; let mut used_key_names = vec![]; - let mut names_used_for_brand_checks = AHashSet::default(); class.body.visit_mut_with(&mut BrandCheckHandler { - names: &mut names_used_for_brand_checks, + names: &mut AHashSet::default(), private: &self.private, }); @@ -454,17 +463,17 @@ impl ClassProperties { } let key = match prop.key { - PropName::Ident(i) => Box::new(Expr::from(Lit::Str(Str { + PropName::Ident(i) => Expr::from(Lit::Str(Str { span: i.span, value: i.sym, has_escape: false, kind: StrKind::Normal { contains_quote: false, }, - }))), - PropName::Num(num) => Box::new(Expr::from(num)), - PropName::Str(s) => Box::new(Expr::from(s)), - PropName::BigInt(big_int) => Box::new(Expr::from(big_int)), + })), + PropName::Num(num) => Expr::from(num), + PropName::Str(s) => Expr::from(s), + PropName::BigInt(big_int) => Expr::from(big_int), PropName::Computed(mut key) => { vars.extend(visit_private_in_expr(&mut key.expr, &self.private)); @@ -487,7 +496,7 @@ impl ClassProperties { definite: false, }); } - Box::new(Expr::from(ident)) + Expr::from(ident) } }; @@ -516,33 +525,10 @@ impl ClassProperties { }); } - let callee = helper!(define_property, "defineProperty"); - if prop.is_static { - extra_stmts.push( - CallExpr { - span: DUMMY_SP, - callee, - args: vec![ - class_ident.clone().as_arg(), - key.as_arg(), - value.as_arg(), - ], - type_args: Default::default(), - } - .into_stmt(), - ) + extra_inits.insert(key, MemberInit::Prop(prop_span, value)); } else { - constructor_exprs.push(Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee, - args: vec![ - ThisExpr { span: DUMMY_SP }.as_arg(), - key.as_arg(), - value.as_arg(), - ], - type_args: Default::default(), - }))); + constructor_inits.insert(key, MemberInit::Prop(prop_span, value)); } } ClassMember::PrivateProp(mut prop) => { @@ -570,76 +556,24 @@ impl ClassProperties { let value = prop.value.unwrap_or_else(|| undefined(prop_span)); - let extra_init = if prop.is_static { - Box::new(Expr::Object(ObjectLit { - span: DUMMY_SP, - props: vec![ - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: PropName::Ident(quote_ident!("writable")), - value: Box::new(Expr::Lit(Lit::Bool(Bool { - span: DUMMY_SP, - value: true, - }))), - }))), - PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: PropName::Ident(quote_ident!("value")), - value, - }))), - ], - })) + if prop.is_static { + extra_inits.insert(ident.into(), MemberInit::Private(prop_span, value)); } else { - constructor_exprs.push(Box::new(Expr::Call(CallExpr { - span: DUMMY_SP, - callee: helper!(class_private_field_init, "classPrivateFieldInit"), - args: vec![ - ThisExpr { span: DUMMY_SP }.as_arg(), - ident.clone().as_arg(), - ObjectLit { - span: DUMMY_SP, - props: vec![ - // writeable: true - PropOrSpread::Prop(Box::new(Prop::KeyValue( - KeyValueProp { - key: PropName::Ident(quote_ident!("writable")), - value: Box::new(Expr::Lit(Lit::Bool(Bool { - value: true, - span: DUMMY_SP, - }))), - }, - ))), - // value: value, - PropOrSpread::Prop(Box::new(Prop::KeyValue( - KeyValueProp { - key: PropName::Ident(quote_ident!("value")), - value, - }, - ))), - ], - } - .as_arg(), - ], - type_args: Default::default(), - }))); + constructor_inits + .insert(ident.clone().into(), MemberInit::Private(prop_span, value)); - Box::new(Expr::New(NewExpr { - span: DUMMY_SP, - callee: Box::new(Expr::Ident(quote_ident!("WeakMap"))), - args: Some(vec![]), - type_args: Default::default(), - })) - }; - - extra_stmts.push(Stmt::Decl(Decl::Var(VarDecl { - span: DUMMY_SP, - kind: VarDeclKind::Var, - declare: false, - decls: vec![VarDeclarator { + vars.push(VarDeclarator { span: DUMMY_SP, definite: false, - name: ident.clone().into(), - init: Some(extra_init), - }], - }))); + name: ident.into(), + init: Some(Box::new(Expr::from(NewExpr { + span: DUMMY_SP, + callee: Box::new(Expr::Ident(quote_ident!("WeakMap"))), + args: Some(Default::default()), + type_args: Default::default(), + }))), + }); + }; } ClassMember::Constructor(c) => { @@ -649,19 +583,19 @@ impl ClassProperties { ClassMember::PrivateMethod(method) => { let is_static = method.is_static; let prop_span = method.span; + let fn_name = Ident::new( - method.key.id.sym.clone(), + match method.kind { + MethodKind::Getter => format!("get_{}", method.key.id.sym).into(), + MethodKind::Setter => format!("set_{}", method.key.id.sym).into(), + MethodKind::Method => method.key.id.sym.clone(), + }, method .span .with_ctxt(SyntaxContext::empty()) .apply_mark(self.private.cur_mark()), ); - let should_use_map = - matches!(method.kind, MethodKind::Getter | MethodKind::Setter) - && names_used_for_brand_checks.contains(&method.key.id.sym) - && !is_static; - let weak_coll_var = Ident::new( format!("_{}", method.key.id.sym).into(), // We use `self.mark` for private variables. @@ -671,76 +605,69 @@ impl ClassProperties { used_names: &mut used_names, }); - if should_use_map || !is_static { + let extra_collect = match (method.kind, is_static) { + (MethodKind::Getter | MethodKind::Setter, false) => { + let mut inserted = false; + let mut key = weak_coll_var.clone(); + key.span = DUMMY_SP.with_ctxt(key.span.ctxt); + let mut entry = + constructor_inits.entry(key.into()).or_insert_with(|| { + inserted = true; + MemberInit::PrivAccessor(prop_span, None, None) + }); + if let MemberInit::PrivAccessor(_, getter, setter) = &mut entry { + if method.kind == MethodKind::Getter { + *getter = Some(fn_name.clone()) + } else { + *setter = Some(fn_name.clone()) + } + }; + + if inserted { + Some(quote_ident!("WeakMap")) + } else { + None + } + } + (MethodKind::Getter | MethodKind::Setter, true) => { + let mut key = weak_coll_var.clone(); + key.span = DUMMY_SP.with_ctxt(key.span.ctxt); + let mut entry = extra_inits + .entry(key.into()) + .or_insert(MemberInit::PrivAccessor(prop_span, None, None)); + if let MemberInit::PrivAccessor(_, getter, setter) = &mut entry { + if method.kind == MethodKind::Getter { + *getter = Some(fn_name.clone()) + } else { + *setter = Some(fn_name.clone()) + } + } + None + } + + (MethodKind::Method, false) => { + constructor_inits.insert( + weak_coll_var.clone().into(), + MemberInit::PrivMethod(prop_span), + ); + Some(quote_ident!("WeakSet")) + } + (MethodKind::Method, true) => None, + }; + + if let Some(extra) = extra_collect { vars.push(VarDeclarator { span: DUMMY_SP, definite: false, name: weak_coll_var.clone().into(), init: Some(Box::new(Expr::from(NewExpr { span: DUMMY_SP, - callee: if should_use_map { - Box::new(Expr::Ident(quote_ident!("WeakMap"))) - } else { - Box::new(Expr::Ident(quote_ident!("WeakSet"))) - }, + callee: Box::new(Expr::Ident(extra)), args: Some(Default::default()), type_args: Default::default(), }))), - }); - } - - if should_use_map { - let get = PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: PropName::Ident(quote_ident!("get")), - value: Box::new(Expr::Ident(fn_name.clone())), - }))); - let set = PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { - key: PropName::Ident(quote_ident!("set")), - value: undefined(DUMMY_SP), - }))); - - let obj = ObjectLit { - span: DUMMY_SP, - props: vec![get, set], - }; - - let obj = if is_static { - let var_name = private_ident!("static_method"); - - vars.push(VarDeclarator { - span: DUMMY_SP, - name: var_name.clone().into(), - init: Some(Box::new(Expr::Object(obj))), - definite: Default::default(), - }); - - var_name.as_arg() - } else { - obj.as_arg() - }; - - constructor_exprs.push(Box::new(Expr::Call(CallExpr { - span: prop_span, - callee: weak_coll_var - .clone() - .make_member(quote_ident!("set")) - .as_callee(), - args: vec![ThisExpr { span: DUMMY_SP }.as_arg(), obj], - type_args: Default::default(), - }))); - } else if !is_static { - // Add `_get.add(this);` to the constructor where `_get` is the name of the - // weak set. - constructor_exprs.push(Box::new(Expr::Call(CallExpr { - span: prop_span, - callee: helper!(class_private_method_init, "classPrivateMethodInit"), - args: vec![ - ThisExpr { span: DUMMY_SP }.as_arg(), - weak_coll_var.clone().as_arg(), - ], - type_args: Default::default(), - }))); - } + }) + }; private_method_fn_decls.push(Stmt::Decl(Decl::Fn(FnDecl { ident: fn_name, @@ -755,12 +682,7 @@ impl ClassProperties { } } - let constructor_exprs = { - typescript_constructor_properties.extend(constructor_exprs); - typescript_constructor_properties - }; - - let constructor = self.process_constructor(constructor, has_super, constructor_exprs); + let constructor = self.process_constructor(constructor, has_super, constructor_inits); if let Some(c) = constructor { members.push(ClassMember::Constructor(c)); } @@ -771,7 +693,82 @@ impl ClassProperties { in_assign_pat: false, }); - extra_stmts.extend(private_method_fn_decls); + let extra_stmts = extra_inits + .into_iter() + .map(|(key, value)| match value { + MemberInit::Prop(span, value) => Stmt::Expr(ExprStmt { + span, + expr: Expr::Call(CallExpr { + span, + callee: helper!(define_property, "defineProperty"), + args: vec![class_ident.clone().as_arg(), key.as_arg(), value.as_arg()], + type_args: Default::default(), + }) + .into(), + }), + MemberInit::Private(span, value) => Stmt::Decl(Decl::Var(VarDecl { + span, + kind: VarDeclKind::Var, + decls: vec![VarDeclarator { + span, + name: key.expect_ident().into(), + init: Some( + Expr::Object(ObjectLit { + span, + props: vec![ + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("writable")), + value: Box::new(Expr::Lit(Lit::Bool(Bool { + span: DUMMY_SP, + value: true, + }))), + }))), + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("value")), + value, + }))), + ], + }) + .into(), + ), + definite: false, + }], + declare: false, + })), + MemberInit::PrivAccessor(span, getter, setter) => Stmt::Decl(Decl::Var(VarDecl { + span, + kind: VarDeclKind::Var, + decls: vec![VarDeclarator { + span, + name: key.expect_ident().into(), + init: Some( + Expr::Object(ObjectLit { + span, + props: vec![ + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("get")), + value: getter + .map(|id| Box::new(id.into())) + .unwrap_or_else(|| undefined(DUMMY_SP)), + }))), + PropOrSpread::Prop(Box::new(Prop::KeyValue(KeyValueProp { + key: PropName::Ident(quote_ident!("set")), + value: setter + .map(|id| Box::new(id.into())) + .unwrap_or_else(|| undefined(DUMMY_SP)), + }))), + ], + }) + .into(), + ), + definite: false, + }], + declare: false, + })), + MemberInit::PrivMethod(_) => unreachable!(), + }) + .chain(private_method_fn_decls) + .collect(); members.visit_mut_with(&mut PrivateAccessVisitor { private: &self.private, @@ -829,7 +826,7 @@ impl ClassProperties { &mut self, constructor: Option, has_super: bool, - constructor_exprs: Vec>, + constructor_exprs: MemberInitMap, ) -> Option { let constructor = constructor.or_else(|| { if constructor_exprs.is_empty() { @@ -840,6 +837,94 @@ impl ClassProperties { }); if let Some(mut c) = constructor { + let constructor_exprs = constructor_exprs + .into_iter() + .map(|(key, value)| { + let (span, callee, args) = match value { + MemberInit::PrivMethod(span) => ( + span, + helper!(class_private_method_init, "classPrivateMethodInit"), + vec![ThisExpr { span: DUMMY_SP }.as_arg(), key.as_arg()], + ), + MemberInit::Private(span, value) => ( + span, + helper!(class_private_field_init, "classPrivateFieldInit"), + vec![ + ThisExpr { span: DUMMY_SP }.as_arg(), + key.as_arg(), + ObjectLit { + span: DUMMY_SP, + props: vec![ + // writeable: true + PropOrSpread::Prop(Box::new(Prop::KeyValue( + KeyValueProp { + key: PropName::Ident(quote_ident!("writable")), + value: Box::new(Expr::Lit(Lit::Bool(Bool { + value: true, + span: DUMMY_SP, + }))), + }, + ))), + // value: value, + PropOrSpread::Prop(Box::new(Prop::KeyValue( + KeyValueProp { + key: PropName::Ident(quote_ident!("value")), + value, + }, + ))), + ], + } + .as_arg(), + ], + ), + MemberInit::PrivAccessor(span, getter, setter) => ( + span, + helper!(class_private_field_init, "classPrivateFieldInit"), + vec![ + ThisExpr { span: DUMMY_SP }.as_arg(), + key.as_arg(), + ObjectLit { + span: DUMMY_SP, + props: vec![ + PropOrSpread::Prop(Box::new(Prop::KeyValue( + KeyValueProp { + key: PropName::Ident(quote_ident!("get")), + value: getter + .map(|id| Box::new(id.into())) + .unwrap_or_else(|| undefined(DUMMY_SP)), + }, + ))), + PropOrSpread::Prop(Box::new(Prop::KeyValue( + KeyValueProp { + key: PropName::Ident(quote_ident!("set")), + value: setter + .map(|id| Box::new(id.into())) + .unwrap_or_else(|| undefined(DUMMY_SP)), + }, + ))), + ], + } + .as_arg(), + ], + ), + MemberInit::Prop(span, value) => ( + span, + helper!(define_property, "defineProperty"), + vec![ + ThisExpr { span: DUMMY_SP }.as_arg(), + key.as_arg(), + value.as_arg(), + ], + ), + }; + Box::new(Expr::Call(CallExpr { + span, + callee, + args, + type_args: Default::default(), + })) + }) + .collect(); // Prepend properties inject_after_super(&mut c, constructor_exprs); Some(c) diff --git a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/private_field.rs b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/private_field.rs index 7bcbf840fe4..d384283c383 100644 --- a/crates/swc_ecma_transforms_compat/src/es2022/class_properties/private_field.rs +++ b/crates/swc_ecma_transforms_compat/src/es2022/class_properties/private_field.rs @@ -627,6 +627,25 @@ impl<'a> PrivateAccessVisitor<'a> { let ident = Ident::new(format!("_{}", n.id.sym).into(), n.id.span.apply_mark(mark)); if kind.is_static { + if self.in_assign_pat { + let set = helper!( + class_static_private_field_destructure, + "classStaticPrivateFieldDestructureSet" + ); + + return ( + CallExpr { + span: DUMMY_SP, + callee: set, + args: vec![obj.clone().as_arg(), ident.as_arg()], + + type_args: Default::default(), + } + .make_member(quote_ident!("value")), + Some(*obj), + ); + } + if kind.is_method { let h = helper!( class_static_private_method_get, @@ -669,20 +688,17 @@ impl<'a> PrivateAccessVisitor<'a> { "classPrivateFieldDestructureSet" ); - return match &*obj { - Expr::This(this) => ( - CallExpr { - span: DUMMY_SP, - callee: set, - args: vec![this.as_arg(), ident.as_arg()], + return ( + CallExpr { + span: DUMMY_SP, + callee: set, + args: vec![obj.clone().as_arg(), ident.as_arg()], - type_args: Default::default(), - } - .make_member(quote_ident!("value")), - Some(Expr::This(*this)), - ), - _ => unimplemented!("destructuring set for object except this"), - }; + type_args: Default::default(), + } + .make_member(quote_ident!("value")), + Some(*obj), + ); } let get = if kind.is_method { diff --git a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs index 2937312124c..274c3acd718 100644 --- a/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs +++ b/crates/swc_ecma_transforms_compat/tests/es2022_class_properties.rs @@ -201,6 +201,7 @@ class Child extends Parent { "#, r#" +var _scopedFunctionWithThis = new WeakMap(); var Child = /*#__PURE__*/ function (Parent) { @@ -226,9 +227,6 @@ function (Parent) { return Child; }(Parent); - -var _scopedFunctionWithThis = new WeakMap(); - "# ); @@ -542,6 +540,7 @@ expect(() => { "#, r#" +var _x = new WeakMap(); var C = function C() { 'use strict'; @@ -554,8 +553,6 @@ var C = function C() { }); }; -var _x = new WeakMap(); - expect(() => { new C(); }).toThrow(); @@ -672,6 +669,7 @@ class Foo extends Bar { "#, r#" +var _bar = new WeakMap(); var Foo = /*#__PURE__*/ function (Bar) { @@ -705,9 +703,6 @@ function (Bar) { return Foo; }(Bar); - -var _bar = new WeakMap(); - "# ); @@ -803,6 +798,7 @@ class Foo { "#, r#" +var _bar = new WeakMap(); var Foo = function Foo() { 'use strict'; @@ -813,9 +809,6 @@ var Foo = function Foo() { value: void 0 }); }; - -var _bar = new WeakMap(); - "# ); @@ -955,6 +948,7 @@ class Foo { "#, r#" var foo = "bar"; +var _bar = new WeakMap(); var Foo = function Foo() { 'use strict'; @@ -968,9 +962,6 @@ var Foo = function Foo() { var foo1 = "foo"; }; - -var _bar = new WeakMap(); - "# ); @@ -1410,6 +1401,7 @@ class Foo extends Bar { "#, r#" +var _bar = new WeakMap(); var Foo = /*#__PURE__*/ function (Bar) { @@ -1433,9 +1425,6 @@ function (Bar) { return Foo; }(Bar); - -var _bar = new WeakMap(); - "# ); @@ -1455,6 +1444,8 @@ class Outer { "#, r#" +var _outer = new WeakMap(); + var Outer = function Outer() { 'use strict'; _classCallCheck(this, Outer); @@ -1476,9 +1467,6 @@ var Outer = function Outer() { return Test; }(_classPrivateFieldGet(_this, _outer)); }; - -var _outer = new WeakMap(); - "# ); @@ -1500,6 +1488,7 @@ class Foo { "#, r#" +var _foo = new WeakMap(); var Foo = /*#__PURE__*/ function () { @@ -1527,9 +1516,6 @@ function () { }]); return Foo; }(); - -var _foo = new WeakMap(); - "# ); @@ -1727,6 +1713,7 @@ class Bar extends Foo { "#, r#" +var _prop = new WeakMap(); var Foo = function Foo() { 'use strict'; @@ -1738,7 +1725,7 @@ var Foo = function Foo() { }); }; -var _prop = new WeakMap(); +var _prop1 = new WeakMap(); var Bar = /*#__PURE__*/ @@ -1763,9 +1750,6 @@ function (Foo) { return Bar; }(Foo); - -var _prop1 = new WeakMap(); - "# ); @@ -1802,6 +1786,7 @@ var A = function () { return A; }(); +var _foo = new WeakMap(); var B = /*#__PURE__*/ function (A) { @@ -1825,9 +1810,6 @@ function (A) { return B; }(A); - -var _foo = new WeakMap(); - "# ); @@ -1846,6 +1828,8 @@ class Foo { "#, r#" +var _two = new WeakMap(), _private = new WeakMap(), _four = new WeakMap(); + var Foo = function Foo() { 'use strict'; @@ -1869,13 +1853,6 @@ var Foo = function Foo() { value: _classPrivateFieldGet(this, _private) }); }; - -var _two = new WeakMap(); - -var _private = new WeakMap(); - -var _four = new WeakMap(); - "# ); @@ -2050,6 +2027,7 @@ class Foo { "#, r#" +var _foo = new WeakMap(); var Foo = /*#__PURE__*/ function () { @@ -2077,9 +2055,6 @@ function () { }]); return Foo; }(); - -var _foo = new WeakMap(); - "# ); @@ -2153,6 +2128,7 @@ class Foo { "#, r#" +var _x = new WeakMap(), _y = new WeakMap(); var Foo = function Foo() { 'use strict'; @@ -2168,11 +2144,6 @@ var Foo = function Foo() { value: _classPrivateFieldGet(this, _x) }); }; - -var _x = new WeakMap(); - -var _y = new WeakMap(); - "# ); @@ -2370,6 +2341,7 @@ class Foo { "#, r#" +var _foo = new WeakMap(); var Foo = function () { 'use strict'; @@ -2395,9 +2367,6 @@ var Foo = function () { }]); return Foo; }(); - -var _foo = new WeakMap(); - "# ); @@ -2451,6 +2420,8 @@ class Foo { r#" var foo = "bar"; +var _bar = new WeakMap(), _baz = new WeakMap(); + var Foo = function Foo(foo1) { 'use strict'; @@ -2466,11 +2437,6 @@ var Foo = function Foo(foo1) { value: foo }); }; - -var _bar = new WeakMap(); - -var _baz = new WeakMap(); - "# ); @@ -2677,6 +2643,7 @@ class Foo extends Bar { "#, r#" +var _bar = new WeakMap(); var Foo = /*#__PURE__*/ function (Bar) { @@ -2698,9 +2665,6 @@ function (Bar) { return Foo; }(Bar); - -var _bar = new WeakMap(); - "# ); @@ -2766,6 +2730,7 @@ class Foo { } ", " +var _x = new WeakMap(); var Foo = function () { 'use strict'; function Foo() { @@ -2790,8 +2755,6 @@ var Foo = function () { return Foo; }(); - -var _x = new WeakMap(); " ); @@ -3110,6 +3073,7 @@ function classFactory() { r#" function classFactory() { return (function() { + var _foo = new WeakMap(); class Foo{ instance() { return _classPrivateFieldGet(this, _foo); @@ -3130,7 +3094,6 @@ function classFactory() { }); } } - var _foo = new WeakMap(); var _bar = { writable: true, value: 'bar' @@ -3210,6 +3173,7 @@ class Foo { } "#, r#" +var _client = new WeakMap(); var Foo = function Foo(props) { "use strict"; @@ -3227,9 +3191,6 @@ var Foo = function Foo(props) { z: this.z = _classPrivateFieldGet(this, _client) } = props); }; - -var _client = new WeakMap(); - "# ); @@ -3404,6 +3365,7 @@ class Foo { "#, r#" +var _client = new WeakMap(); var Foo = function Foo(props) { "use strict"; @@ -3416,9 +3378,6 @@ var Foo = function Foo(props) { [_classPrivateFieldDestructureSet(this, _client).value] = props; }; - -var _client = new WeakMap(); - "# ); @@ -3492,6 +3451,7 @@ export default class MyClass3 { "#, r#" +var _myAsyncMethod = new WeakMap(); class MyClass { constructor(){ var _this = this; @@ -3503,8 +3463,8 @@ class MyClass { }); } } -var _myAsyncMethod = new WeakMap(); (function() { + var _myAsyncMethod2 = new WeakMap(); class MyClass2 { constructor(){ var _this = this; @@ -3516,9 +3476,9 @@ var _myAsyncMethod = new WeakMap(); }); } } - var _myAsyncMethod2 = new WeakMap(); return MyClass2; })(); +var _myAsyncMethod1 = new WeakMap(); class MyClass3 { constructor(){ var _this = this; @@ -3530,10 +3490,7 @@ class MyClass3 { }); } } -var _myAsyncMethod1 = new WeakMap(); export { MyClass3 as default }; - - "# ); @@ -3557,6 +3514,7 @@ class Foo { } "#, r#" +var _client = new WeakMap(); var Foo = function Foo(props) { "use strict"; @@ -3570,9 +3528,6 @@ var Foo = function Foo(props) { _classPrivateFieldSet(this, _client, 1); [this.x = _classPrivateFieldGet(this, _client), _classPrivateFieldDestructureSet(this, _client).value, this.y = _classPrivateFieldGet(this, _client)] = props; }; - -var _client = new WeakMap(); - "# ); @@ -4155,6 +4110,7 @@ class A { } "#, r#" +var _fieldFunc = new WeakMap(); class A { test() { _classPrivateFieldGet(this, _fieldFunc)?.call(this); @@ -4167,7 +4123,6 @@ class A { _defineProperty(this, "x", 1); } } -var _fieldFunc = new WeakMap(); "# ); @@ -4184,6 +4139,7 @@ class MyClass { } "#, r#" +var _a = new WeakMap(); class MyClass { foo(o) { o === null || o === void 0 ? void 0 : _classPrivateFieldGet(o, _a); @@ -4195,7 +4151,6 @@ class MyClass { }); } } -var _a = new WeakMap(); "# ); @@ -4232,6 +4187,7 @@ class Point { "#, r#" +var _x = new WeakMap(), _y = new WeakMap(); var Point = /*#__PURE__*/ function () { @@ -4283,11 +4239,6 @@ function () { }]); return Point; }(); - -var _x = new WeakMap(); - -var _y = new WeakMap(); - "# ); @@ -4362,6 +4313,7 @@ class Foo { } "#, r#" +var _client = new WeakMap(); var Foo = function Foo(props) { "use strict"; @@ -4374,9 +4326,35 @@ var Foo = function Foo(props) { [_classPrivateFieldDestructureSet(this, _client).value = 5] = props; }; +"# +); -var _client = new WeakMap(); +test!( + syntax(), + |_| class_properties(class_properties::Config { loose: false }), + staic_private_destructuring_array_pattern, + r#" +class A { + #a = 123 + foo() { + [a().#a] = [] + } +} +"#, + r#" +var _a = /*#__PURE__*/ new WeakMap(); +class A { + foo() { + [_classPrivateFieldDestructureSet(a(), _a).value] = []; + } + constructor() { + _classPrivateFieldInit(this, _a, { + writable: true, + value: 123 + }); + } +} "# ); @@ -4424,6 +4402,7 @@ class Foo { } "#, r#" +var _client = new WeakMap(); var Foo = function Foo(props) { "use strict"; @@ -4436,9 +4415,6 @@ var Foo = function Foo(props) { [x, ..._classPrivateFieldDestructureSet(this, _client).value] = props; }; - -var _client = new WeakMap(); - "# ); @@ -4542,6 +4518,7 @@ class Foo { "#, r#" +var _bar = new WeakMap(); var Foo = function Foo() { "use strict"; @@ -4552,9 +4529,6 @@ var Foo = function Foo() { value: "foo" }); }; - -var _bar = new WeakMap(); - "# ); @@ -4617,7 +4591,6 @@ _defineProperty(Foo, "foo", "foo"); // public_arrow_static_this_without_transform test!( // Emitting class properties is not supported yet. - ignore, syntax(), |_| arrow(), public_arrow_static_this_without_transform, @@ -4719,6 +4692,7 @@ class Foo { "#, r#" +var _bar = new WeakMap(); class Foo { static test() { @@ -4741,8 +4715,6 @@ var _foo = { writable: true, value: "foo" }; -var _bar = new WeakMap(); - "# ); @@ -4952,6 +4924,7 @@ test!( } "#, " + var _name = new WeakMap(); class Animal { noise() { return _classPrivateFieldGet(this, _name); @@ -4964,7 +4937,6 @@ test!( _classPrivateFieldSet(this, _name, name); } } - var _name = new WeakMap(); " ); @@ -4986,6 +4958,7 @@ class Animal { } "#, " + var _name = new WeakMap(); class Animal { noise() { return _classPrivateFieldGet(this, _name).toUpperCase(); @@ -4998,7 +4971,6 @@ class Animal { _classPrivateFieldSet(this, _name, name); } } -var _name = new WeakMap(); " ); @@ -5016,6 +4988,7 @@ test!( } ", " + var _ws = new WeakMap(), _ws2 = new WeakMap(); class Foo { get connected() { return _classPrivateFieldGet(this, _ws2) && _classPrivateFieldGet(this, _ws).readyState \ @@ -5032,8 +5005,6 @@ test!( }); } } - var _ws = new WeakMap(); - var _ws2 = new WeakMap(); " ); @@ -5131,6 +5102,7 @@ test!( } ", " + var _ws = new WeakMap(), _serialization = new WeakMap(), _seq = new WeakMap(); class Test { _packet(raw) { let pak; @@ -5216,9 +5188,6 @@ test!( }); } } - var _ws = new WeakMap(); - var _serialization = new WeakMap(); - var _seq = new WeakMap(); " ); @@ -5250,6 +5219,7 @@ test!( } ", " + var _ws = new WeakMap(), _serialization = new WeakMap(); class Test { _packet(raw) { let pak; @@ -5276,8 +5246,6 @@ test!( }); } } - var _ws = new WeakMap(); - var _serialization = new WeakMap(); " ); @@ -5302,6 +5270,7 @@ test!( } ", " + var _ws = new WeakMap(), _serialization = new WeakMap(); class Test { _packet(raw) { let pak; @@ -5322,8 +5291,6 @@ test!( }); } } - var _ws = new WeakMap(); - var _serialization = new WeakMap(); " ); @@ -5340,6 +5307,7 @@ test!( } ", " + var _serialization = new WeakMap(); class Test { _packet(raw) { pak = _classPrivateFieldGet(this, _serialization).decode(raw); @@ -5351,7 +5319,6 @@ test!( }); } } - var _serialization = new WeakMap(); " ); @@ -5368,6 +5335,7 @@ test!( } ", " + var _serialization = new WeakMap(); class Test { _packet(raw) { _classPrivateFieldGet(this, _serialization).decode(raw); @@ -5379,7 +5347,6 @@ test!( }); } } - var _serialization = new WeakMap(); " ); @@ -5437,6 +5404,41 @@ function baz(child) {} " ); +test!( + syntax(), + |_| class_properties(class_properties::Config { loose: false }), + issue_3618, + " +class MyClass { + get #a() {} + set #a(x) {} + static get #b() {} + static set #b(x) {} +} +", + " +var _a = /*#__PURE__*/ new WeakMap(); + +class MyClass { + constructor() { + _classPrivateFieldInit(this, _a, { + get: get_a, + set: set_a + }); + } +} + +var _b = { + get: get_b, + set: set_b +}; +function get_a() {} +function set_a(x) {} +function get_b() {} +function set_b(x) {} +" +); + test!( syntax(), |_| chain!( @@ -5523,7 +5525,7 @@ test!( const instance = new Foo(); ", " - var _sssss = new WeakSet(); + var _y = new WeakMap(), _sssss = new WeakSet(); class Foo { constructor(){ _classPrivateFieldInit(this, _y, { @@ -5536,7 +5538,6 @@ test!( _classPrivateMethodGet(this, _sssss, sssss).call(this); } } - var _y = new WeakMap(); var _z = { writable: true, value: 3 @@ -5672,7 +5673,7 @@ test!( new Foo(); ", " - var _tag = new WeakSet(); + var _tag = new WeakSet(), _tag2 = new WeakMap(); class Foo { constructor(){ _classPrivateMethodInit(this, _tag); @@ -5686,7 +5687,6 @@ test!( expect(receiver2).toBe(this); } } - var _tag2 = new WeakMap(); function tag() { return this; } @@ -5795,6 +5795,7 @@ class A { } ", " +var _D = new WeakMap(); class A { B() { var _C, _this_D; @@ -5809,7 +5810,6 @@ class A { }); } } -var _D = new WeakMap(); " ); @@ -5829,6 +5829,7 @@ class A { } ", " +var _b = new WeakMap(); class A { foo() { var _A; @@ -5844,7 +5845,6 @@ class A { }); } } -var _b = new WeakMap(); " ); @@ -5867,7 +5867,7 @@ class A { } ", " -var _bar = new WeakSet(); +var _a = new WeakMap(), _bar = new WeakSet(); class A { foo() { return class B { @@ -5885,7 +5885,6 @@ class A { _classPrivateMethodInit(this, _bar); } } -var _a = new WeakMap(); var _b = { writable: true, value: 123 diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/accessor/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/accessor/output.js index 46a465d34d7..2bebb622308 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/accessor/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/accessor/output.js @@ -3,8 +3,8 @@ let Foo = function() { "use strict"; function Foo() { _classCallCheck(this, Foo); - _foo.set(this, { - get: foo, + _classPrivateFieldInit(this, _foo, { + get: get_foo, set: void 0 }); } @@ -18,5 +18,4 @@ let Foo = function() { ]); return Foo; }(); -function foo() { -} +function get_foo() {} diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/field/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/field/output.js index 63ee699bc9f..2957f9cf338 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/field/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/field/output.js @@ -1,3 +1,4 @@ +var _foo = new WeakMap(); let Foo = function() { "use strict"; function Foo() { @@ -17,4 +18,3 @@ let Foo = function() { ]); return Foo; }(); -var _foo = new WeakMap(); diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/native-classes/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/native-classes/output.js index e41ad246659..6bd004d8643 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/native-classes/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/native-classes/output.js @@ -1,3 +1,4 @@ +var _bar = new WeakMap(); class Foo { static test() { return Foo === Foo; @@ -16,4 +17,3 @@ var _foo = { writable: true, value: "foo" }; -var _bar = new WeakMap(); diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-other-redeclared/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-other-redeclared/output.js index 80873e6384a..cf8b0e49079 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-other-redeclared/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-other-redeclared/output.js @@ -1,3 +1,4 @@ +var _foo = new WeakMap(), _bar = new WeakMap(); let Foo = function() { "use strict"; function Foo() { @@ -15,6 +16,7 @@ let Foo = function() { { key: "test", value: function test() { + var _bar1 = new WeakMap(); let Nested = function() { function Nested() { _classCallCheck(this, Nested); @@ -34,7 +36,6 @@ let Foo = function() { ]); return Nested; }(); - var _bar1 = new WeakMap(); _foo.has(this); _bar.has(this); } @@ -42,5 +43,3 @@ let Foo = function() { ]); return Foo; }(); -var _foo = new WeakMap(); -var _bar = new WeakMap(); diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-redeclared/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-redeclared/output.js index 91c8f153aa0..b5ed319541f 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-redeclared/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class-redeclared/output.js @@ -1,3 +1,4 @@ +var _foo = new WeakMap(); let Foo = function() { "use strict"; function Foo() { @@ -11,6 +12,7 @@ let Foo = function() { { key: "test", value: function test() { + var _foo1 = new WeakMap(); let Nested = function() { function Nested() { _classCallCheck(this, Nested); @@ -29,11 +31,9 @@ let Foo = function() { ]); return Nested; }(); - var _foo1 = new WeakMap(); _foo.has(this); } } ]); return Foo; }(); -var _foo = new WeakMap(); diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class/output.js index 4f7f72f2ede..d234f4651da 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/nested-class/output.js @@ -1,3 +1,4 @@ +var _foo = new WeakMap(); let Foo = function() { "use strict"; function Foo() { @@ -31,4 +32,3 @@ let Foo = function() { ]); return Foo; }(); -var _foo = new WeakMap(); diff --git a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/static-accessor/output.js b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/static-accessor/output.js index ad67bc487a0..29bbbe1955b 100644 --- a/crates/swc_ecma_transforms_compat/tests/private-in-object/private/static-accessor/output.js +++ b/crates/swc_ecma_transforms_compat/tests/private-in-object/private/static-accessor/output.js @@ -13,5 +13,8 @@ let Foo = function() { ]); return Foo; }(); -function foo() { -} +var _foo = { + get: get_foo, + set: void 0 +}; +function get_foo() {} diff --git a/packages/swc-helpers/src/_class_apply_descriptor_destructure.js b/packages/swc-helpers/src/_class_apply_descriptor_destructure.js new file mode 100644 index 00000000000..5bf4e9a6b15 --- /dev/null +++ b/packages/swc-helpers/src/_class_apply_descriptor_destructure.js @@ -0,0 +1,20 @@ +export default function _classApplyDescriptorDestructureSet(receiver, descriptor) { + if (descriptor.set) { + if (!("__destrObj" in descriptor)) { + descriptor.__destrObj = { + set value(v) { + descriptor.set.call(receiver, v) + }, + }; + } + return descriptor.__destrObj; + } else { + if (!descriptor.writable) { + // This should only throw in strict mode, but class bodies are + // always strict and private fields can only be used inside + // class bodies. + throw new TypeError("attempted to set read only private field"); + } + return descriptor; + } +} diff --git a/packages/swc-helpers/src/_class_apply_descriptor_get.js b/packages/swc-helpers/src/_class_apply_descriptor_get.js new file mode 100644 index 00000000000..1d0a791d6d0 --- /dev/null +++ b/packages/swc-helpers/src/_class_apply_descriptor_get.js @@ -0,0 +1,6 @@ +export default function _classApplyDescriptorGet(receiver, descriptor) { + if (descriptor.get) { + return descriptor.get.call(receiver); + } + return descriptor.value; +} diff --git a/packages/swc-helpers/src/_class_apply_descriptor_set.js b/packages/swc-helpers/src/_class_apply_descriptor_set.js new file mode 100644 index 00000000000..f1020bda8c6 --- /dev/null +++ b/packages/swc-helpers/src/_class_apply_descriptor_set.js @@ -0,0 +1,13 @@ +export default function _classApplyDescriptorSet(receiver, descriptor, value) { + if (descriptor.set) { + descriptor.set.call(receiver, value); + } else { + if (!descriptor.writable) { + // This should only throw in strict mode, but class bodies are + // always strict and private fields can only be used inside + // class bodies. + throw new TypeError("attempted to set read only private field"); + } + descriptor.value = value; + } +} diff --git a/packages/swc-helpers/src/_class_check_private_static_access.js b/packages/swc-helpers/src/_class_check_private_static_access.js new file mode 100644 index 00000000000..649c4750c37 --- /dev/null +++ b/packages/swc-helpers/src/_class_check_private_static_access.js @@ -0,0 +1,5 @@ +export default function _classCheckPrivateStaticAccess(receiver, classConstructor) { + if (receiver !== classConstructor) { + throw new TypeError("Private static access of wrong provenance"); + } +} diff --git a/packages/swc-helpers/src/_class_check_private_static_field_descriptor.js b/packages/swc-helpers/src/_class_check_private_static_field_descriptor.js new file mode 100644 index 00000000000..d21ae3b8359 --- /dev/null +++ b/packages/swc-helpers/src/_class_check_private_static_field_descriptor.js @@ -0,0 +1,5 @@ +export default function _classCheckPrivateStaticFieldDescriptor(descriptor, action) { + if (descriptor === undefined) { + throw new TypeError("attempted to " + action + " private static field before its declaration"); + } +} diff --git a/packages/swc-helpers/src/_class_extract_field_descriptor.js b/packages/swc-helpers/src/_class_extract_field_descriptor.js new file mode 100644 index 00000000000..c61ad97c845 --- /dev/null +++ b/packages/swc-helpers/src/_class_extract_field_descriptor.js @@ -0,0 +1,6 @@ +export default function _classExtractFieldDescriptor(receiver, privateMap, action) { + if (!privateMap.has(receiver)) { + throw new TypeError("attempted to " + action + " private field on non-instance"); + } + return privateMap.get(receiver); +} diff --git a/packages/swc-helpers/src/_class_private_field_destructure.js b/packages/swc-helpers/src/_class_private_field_destructure.js new file mode 100644 index 00000000000..d744b3ad70b --- /dev/null +++ b/packages/swc-helpers/src/_class_private_field_destructure.js @@ -0,0 +1,7 @@ +import classExtractFieldDescriptor from './_class_extract_field_descriptor'; +import classApplyDescriptorDestructureSet from './_class_apply_descriptor_destructure'; + +export default function _classPrivateFieldDestructureSet(receiver, privateMap) { + var descriptor = classExtractFieldDescriptor(receiver, privateMap, "set"); + return classApplyDescriptorDestructureSet(receiver, descriptor); +} diff --git a/packages/swc-helpers/src/_class_private_field_get.js b/packages/swc-helpers/src/_class_private_field_get.js index 00c2bd65c17..69305640583 100644 --- a/packages/swc-helpers/src/_class_private_field_get.js +++ b/packages/swc-helpers/src/_class_private_field_get.js @@ -1,7 +1,7 @@ -export default function _classPrivateFieldGet(receiver, privateMap) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to get private field on non-instance"); - } +import classExtractFieldDescriptor from './_class_extract_field_descriptor'; +import classApplyDescriptorGet from './_class_apply_descriptor_get'; - return privateMap.get(receiver).value; +export default function _classPrivateFieldGet(receiver, privateMap) { + var descriptor = classExtractFieldDescriptor(receiver, privateMap, "get"); + return classApplyDescriptorGet(receiver, descriptor); } diff --git a/packages/swc-helpers/src/_class_private_field_set.js b/packages/swc-helpers/src/_class_private_field_set.js index d341bbd82c7..e8d441ec12b 100644 --- a/packages/swc-helpers/src/_class_private_field_set.js +++ b/packages/swc-helpers/src/_class_private_field_set.js @@ -1,14 +1,8 @@ +import classExtractFieldDescriptor from './_class_extract_field_descriptor'; +import classApplyDescriptorSet from './_class_apply_descriptor_set'; + export default function _classPrivateFieldSet(receiver, privateMap, value) { - if (!privateMap.has(receiver)) { - throw new TypeError("attempted to set private field on non-instance"); - } - - var descriptor = privateMap.get(receiver); - - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - - descriptor.value = value; + var descriptor = classExtractFieldDescriptor(receiver, privateMap, "set"); + classApplyDescriptorSet(receiver, descriptor, value); return value; } diff --git a/packages/swc-helpers/src/_class_static_private_field_destructure.js b/packages/swc-helpers/src/_class_static_private_field_destructure.js new file mode 100644 index 00000000000..7f0e360ae0b --- /dev/null +++ b/packages/swc-helpers/src/_class_static_private_field_destructure.js @@ -0,0 +1,9 @@ +import classCheckPrivateStaticAccess from './_class_check_private_static_access'; +import classCheckPrivateStaticFieldDescriptor from './_class_check_private_static_access'; +import classApplyDescriptorDestructureSet from './_class_apply_descriptor_destructure'; + +export default function _classStaticPrivateFieldDestructureSet(receiver, classConstructor, descriptor) { + classCheckPrivateStaticAccess(receiver, classConstructor); + classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + return classApplyDescriptorDestructureSet(receiver, descriptor); +} diff --git a/packages/swc-helpers/src/_class_static_private_field_spec_get.js b/packages/swc-helpers/src/_class_static_private_field_spec_get.js index fe06ad2c1fa..c6ddde6f4b1 100644 --- a/packages/swc-helpers/src/_class_static_private_field_spec_get.js +++ b/packages/swc-helpers/src/_class_static_private_field_spec_get.js @@ -1,7 +1,9 @@ -export default function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } +import classCheckPrivateStaticAccess from './_class_check_private_static_access'; +import classCheckPrivateStaticFieldDescriptor from './_class_check_private_static_access'; +import classApplyDescriptorGet from './_class_apply_descriptor_get'; - return descriptor.value; +export default function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { + classCheckPrivateStaticAccess(receiver, classConstructor); + classCheckPrivateStaticFieldDescriptor(descriptor, "get"); + return classApplyDescriptorGet(receiver, descriptor); } diff --git a/packages/swc-helpers/src/_class_static_private_field_spec_set.js b/packages/swc-helpers/src/_class_static_private_field_spec_set.js index 7daecdf3d79..3b05ab4f9b8 100644 --- a/packages/swc-helpers/src/_class_static_private_field_spec_set.js +++ b/packages/swc-helpers/src/_class_static_private_field_spec_set.js @@ -1,12 +1,10 @@ +import classCheckPrivateStaticAccess from './_class_check_private_static_access'; +import classCheckPrivateStaticFieldDescriptor from './_class_check_private_static_access'; +import classApplyDescriptorSet from './_class_apply_descriptor_set'; + export default function _classStaticPrivateFieldSpecSet(receiver, classConstructor, descriptor, value) { - if (receiver !== classConstructor) { - throw new TypeError("Private static access of wrong provenance"); - } - - if (!descriptor.writable) { - throw new TypeError("attempted to set read only private field"); - } - - descriptor.value = value; + classCheckPrivateStaticAccess(receiver, classConstructor); + classCheckPrivateStaticFieldDescriptor(descriptor, "set"); + classApplyDescriptorSet(receiver, descriptor, value); return value; } diff --git a/packages/swc-helpers/src/index.js b/packages/swc-helpers/src/index.js index 960118f867e..da190a76b03 100644 --- a/packages/swc-helpers/src/index.js +++ b/packages/swc-helpers/src/index.js @@ -10,8 +10,14 @@ export { default as asyncToGenerator } from './_async_to_generator'; export { default as awaitAsyncGenerator } from './_await_async_generator'; export { default as awaitValue } from './_await_value'; export { default as checkPrivateRedeclaration } from './_check_private_redeclaration'; +export { default as classApplyDescriptorDestructureSet } from './_class_apply_descriptor_destructure'; +export { default as classApplyDescriptorGet } from './_class_apply_descriptor_get'; +export { default as classApplyDescriptorSet } from './_class_apply_descriptor_set'; export { default as classCallCheck } from './_class_call_check'; +export { default as classCheckPrivateStaticFieldDescriptor } from './_class_check_private_static_field_descriptor' +export { default as classCheckPrivateStaticAccess } from './_class_check_private_static_access'; export { default as classNameTDZError } from './_class_name_tdz_error'; +export { default as classPrivateFieldDestructureSet } from './_class_private_field_destructure'; export { default as classPrivateFieldGet } from './_class_private_field_get'; export { default as classPrivateFieldInit } from './_class_private_field_init'; export { default as classPrivateFieldLooseBase } from './_class_private_field_loose_base'; @@ -19,6 +25,7 @@ export { default as classPrivateFieldSet } from './_class_private_field_set'; export { default as classPrivateMethodGet } from './_class_private_method_get'; export { default as classPrivateMethodInit } from './_class_private_method_init'; export { default as classPrivateMethodSet } from './_class_private_method_set'; +export { default as classStaticPrivateFieldDestructureSet } from './_class_static_private_field_destructure'; export { default as classStaticPrivateFieldSpecGet } from './_class_static_private_field_spec_get'; export { default as classStaticPrivateFieldSpecSet } from './_class_static_private_field_spec_set'; export { default as construct } from './_construct';