From e74f2f5b333a47b8d1894f2e8cf7066c839a0b43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 4 Sep 2020 15:37:44 -0400 Subject: [PATCH] chore: add more testcases --- .../exec.js | 46 +++++++++ .../input.js | 46 +++++++++ .../output.js | 92 ++++++++++++++++++ .../optional-chain-in-function-param/exec.js | 46 +++++++++ .../optional-chain-in-function-param/input.js | 46 +++++++++ .../options.json | 4 + .../output.js | 88 +++++++++++++++++ .../exec.js | 46 +++++++++ .../input.js | 46 +++++++++ .../options.json | 6 ++ .../output.js | 96 +++++++++++++++++++ 11 files changed, 562 insertions(+) create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/exec.js new file mode 100644 index 000000000000..6eb26ab6e01a --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/exec.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + expect(f(o)).toBe(1); + expect(g(o)).toBe(1); + expect(h(fnDeep)).toBe(1); + expect(i(fn)).toBe(1); + expect(j(fn)).toBe(1); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/input.js new file mode 100644 index 000000000000..c5fe33636eca --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/input.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m?.()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js new file mode 100644 index 000000000000..fe7ddec253e6 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param-with-transform/output.js @@ -0,0 +1,92 @@ +var _x = babelHelpers.classPrivateFieldLooseKey("x"); + +var _m = babelHelpers.classPrivateFieldLooseKey("m"); + +var _self = babelHelpers.classPrivateFieldLooseKey("self"); + +var Foo = /*#__PURE__*/function () { + "use strict"; + + function Foo() { + babelHelpers.classCallCheck(this, Foo); + } + + babelHelpers.createClass(Foo, null, [{ + key: "getSelf", + value: function getSelf() { + return this; + } + }, { + key: "test", + value: function test() { + var o = { + Foo: Foo + }; + var deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + function f(o, r = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(o.Foo, _m)[_m]())()) { + return r; + } + + function g(o, r = (() => o === null || o === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()?.()) { + return r; + } + + function h(fnDeep, r = (() => { + var _fnDeep$very$o$Foo; + + return (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); + })()) { + return r; + } + + function i(fn, r = (() => { + var _getSelf; + + return (_getSelf = (() => fn === null || fn === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self])()?.getSelf()) === null || _getSelf === void 0 ? void 0 : babelHelpers.classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); + })()) { + return r; + } + + function j(fn, r = (() => babelHelpers.classPrivateFieldLooseBase(babelHelpers.classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m)[_m]?.())()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } + }]); + return Foo; +}(); + +Object.defineProperty(Foo, _x, { + writable: true, + value: 1 +}); +Object.defineProperty(Foo, _m, { + writable: true, + value: function () { + return babelHelpers.classPrivateFieldLooseBase(this, _x)[_x]; + } +}); +Object.defineProperty(Foo, _self, { + writable: true, + value: Foo +}); +Foo.self = Foo; +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/exec.js new file mode 100644 index 000000000000..6eb26ab6e01a --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/exec.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + expect(f(o)).toBe(1); + expect(g(o)).toBe(1); + expect(h(fnDeep)).toBe(1); + expect(i(fn)).toBe(1); + expect(j(fn)).toBe(1); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/input.js new file mode 100644 index 000000000000..c5fe33636eca --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/input.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m?.()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/options.json new file mode 100644 index 000000000000..2d5cfe8e8095 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/options.json @@ -0,0 +1,4 @@ +{ + "plugins": [["proposal-class-properties", { "loose": true }]], + "minNodeVersion": "14.0.0" +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js new file mode 100644 index 000000000000..2b7d864e3b04 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private-loose/optional-chain-in-function-param/output.js @@ -0,0 +1,88 @@ +function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } + +var id = 0; + +function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } + +var _x = _classPrivateFieldLooseKey("x"); + +var _m = _classPrivateFieldLooseKey("m"); + +var _self = _classPrivateFieldLooseKey("self"); + +class Foo { + static getSelf() { + return this; + } + + static test() { + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + function f(o, r = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(o.Foo, _m)[_m]())()) { + return r; + } + + function g(o, r = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()?.()) { + return r; + } + + function h(fnDeep, r = (() => { + var _fnDeep$very$o$Foo; + + return (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); + })()) { + return r; + } + + function i(fn, r = (() => { + var _getSelf; + + return (_getSelf = (() => fn === null || fn === void 0 ? void 0 : _classPrivateFieldLooseBase(fn().Foo, _self)[_self])()?.getSelf()) === null || _getSelf === void 0 ? void 0 : _classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); + })()) { + return r; + } + + function j(fn, r = (() => _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m)[_m]?.())()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } + +} + +Object.defineProperty(Foo, _x, { + writable: true, + value: 1 +}); +Object.defineProperty(Foo, _m, { + writable: true, + value: function () { + return _classPrivateFieldLooseBase(this, _x)[_x]; + } +}); +Object.defineProperty(Foo, _self, { + writable: true, + value: Foo +}); +Foo.self = Foo; +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/exec.js new file mode 100644 index 000000000000..6eb26ab6e01a --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/exec.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + expect(f(o)).toBe(1); + expect(g(o)).toBe(1); + expect(h(fnDeep)).toBe(1); + expect(i(fn)).toBe(1); + expect(j(fn)).toBe(1); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/input.js new file mode 100644 index 000000000000..c5fe33636eca --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/input.js @@ -0,0 +1,46 @@ +class Foo { + static #x = 1; + static #m = function() { return this.#x; }; + static #self = Foo; + static self = Foo; + static getSelf() { return this } + + static test() { + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + function f(o, r = o?.Foo.#m()) { + return r; + } + + function g(o, r = o?.Foo.#self.getSelf().#m?.()) { + return r; + } + + function h(fnDeep, r = fnDeep?.().very.o?.Foo?.#m()) { + return r; + } + + function i(fn, r = fn?.().Foo.#self?.getSelf()?.self.#m()) { + return r; + } + + function j(fn, r = (fn().Foo.#self.getSelf().self.#m)?.()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/options.json new file mode 100644 index 000000000000..124133b5af2b --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [ + ["proposal-optional-chaining", { "loose": true }], + ["proposal-class-properties", { "loose": true }] + ] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js new file mode 100644 index 000000000000..fb3516c4b811 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-in-function-param-with-transform/output.js @@ -0,0 +1,96 @@ +function _classPrivateFieldLooseBase(receiver, privateKey) { if (!Object.prototype.hasOwnProperty.call(receiver, privateKey)) { throw new TypeError("attempted to use private field on non-instance"); } return receiver; } + +var id = 0; + +function _classPrivateFieldLooseKey(name) { return "__private_" + id++ + "_" + name; } + +var _x = _classPrivateFieldLooseKey("x"); + +var _m = _classPrivateFieldLooseKey("m"); + +var _self = _classPrivateFieldLooseKey("self"); + +class Foo { + static getSelf() { + return this; + } + + static test() { + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + function f(o, r = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(o.Foo, _m)[_m]())()) { + return r; + } + + function g(o, r = (() => { + var _ref; + + return (_ref = (() => o === null || o === void 0 ? void 0 : _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(o.Foo, _self)[_self].getSelf(), _m)[_m])()) == null ? void 0 : _ref(); + })()) { + return r; + } + + function h(fnDeep, r = (() => { + var _fnDeep$very$o$Foo, _fnDeep$very$o; + + return (_fnDeep$very$o$Foo = fnDeep == null ? void 0 : (_fnDeep$very$o = fnDeep().very.o) == null ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classPrivateFieldLooseBase(_fnDeep$very$o$Foo, _m)[_m](); + })()) { + return r; + } + + function i(fn, r = (() => { + var _getSelf, _ref2; + + return (_getSelf = (_ref2 = (() => fn === null || fn === void 0 ? void 0 : _classPrivateFieldLooseBase(fn().Foo, _self)[_self])()) == null ? void 0 : _ref2.getSelf()) === null || _getSelf === void 0 ? void 0 : _classPrivateFieldLooseBase(_getSelf.self, _m)[_m](); + })()) { + return r; + } + + function j(fn, r = (() => { + var _classPrivateFieldLoo, _classPrivateFieldLoo2; + + return (_classPrivateFieldLoo = (_classPrivateFieldLoo2 = _classPrivateFieldLooseBase(_classPrivateFieldLooseBase(fn().Foo, _self)[_self].getSelf().self, _m))[_m]) == null ? void 0 : _classPrivateFieldLoo.call(_classPrivateFieldLoo2); + })()) { + return r; + } + + f(o); + g(o); + h(fnDeep); + i(fn); + j(fn); + } + +} + +Object.defineProperty(Foo, _x, { + writable: true, + value: 1 +}); +Object.defineProperty(Foo, _m, { + writable: true, + value: function () { + return _classPrivateFieldLooseBase(this, _x)[_x]; + } +}); +Object.defineProperty(Foo, _self, { + writable: true, + value: Foo +}); +Foo.self = Foo; +Foo.test();