diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/exec.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/exec.js new file mode 100644 index 000000000000..bfb53e693b08 --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/exec.js @@ -0,0 +1,45 @@ +class Foo { + constructor() { + this.x = 1; + this.self = this; + } + m() { return this.x; }; + getSelf() { return this } + + test() { + const Foo = this; + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + expect((Foo?.["m"])()).toEqual(1); + expect((Foo?.["m"])().toString).toEqual(1..toString); + expect((Foo?.["m"])().toString()).toEqual('1'); + + expect((o?.Foo.m)()).toEqual(1); + expect((o?.Foo.m)().toString).toEqual(1..toString); + expect((o?.Foo.m)().toString()).toEqual('1'); + + expect((((o.Foo?.self.getSelf)())?.m)()).toEqual(1); + expect((((o.Foo.self?.getSelf)())?.m)()).toEqual(1); + } + + testNull() { + const o = null; + + expect(() => { (o?.Foo.m)() }).toThrow(); + expect(() => { (o?.Foo.m)().toString }).toThrow(); + expect(() => { (o?.Foo.m)().toString() }).toThrow(); + + expect(() => { (((o.Foo?.self.getSelf)())?.m)() }).toThrow(); + expect(() => { (((o.Foo.self?.getSelf)())?.m)() }).toThrow(); + } +} + +(new Foo).test(); +(new Foo).testNull(); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/options.json b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/options.json new file mode 100644 index 000000000000..fe05dbf3f58c --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-expression-member-call-loose/options.json @@ -0,0 +1,6 @@ +{ + "plugins": [["proposal-optional-chaining", { "loose": true }]], + "parserOpts": { + "createParenthesizedExpressions": true + } +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/exec.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/exec.js new file mode 100644 index 000000000000..bfb53e693b08 --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/exec.js @@ -0,0 +1,45 @@ +class Foo { + constructor() { + this.x = 1; + this.self = this; + } + m() { return this.x; }; + getSelf() { return this } + + test() { + const Foo = this; + const o = { Foo: Foo }; + const deep = { very: { o } }; + function fn() { + return o; + } + function fnDeep() { + return deep; + } + + expect((Foo?.["m"])()).toEqual(1); + expect((Foo?.["m"])().toString).toEqual(1..toString); + expect((Foo?.["m"])().toString()).toEqual('1'); + + expect((o?.Foo.m)()).toEqual(1); + expect((o?.Foo.m)().toString).toEqual(1..toString); + expect((o?.Foo.m)().toString()).toEqual('1'); + + expect((((o.Foo?.self.getSelf)())?.m)()).toEqual(1); + expect((((o.Foo.self?.getSelf)())?.m)()).toEqual(1); + } + + testNull() { + const o = null; + + expect(() => { (o?.Foo.m)() }).toThrow(); + expect(() => { (o?.Foo.m)().toString }).toThrow(); + expect(() => { (o?.Foo.m)().toString() }).toThrow(); + + expect(() => { (((o.Foo?.self.getSelf)())?.m)() }).toThrow(); + expect(() => { (((o.Foo.self?.getSelf)())?.m)() }).toThrow(); + } +} + +(new Foo).test(); +(new Foo).testNull(); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/input.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/input.js new file mode 100644 index 000000000000..1bdce5676802 --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/input.js @@ -0,0 +1,26 @@ +class Foo { + constructor() { + this.x = 1; + this.self = this; + } + m() { return this.x; }; + getSelf() { return this } + + test() { + const Foo = this; + const o = { Foo: Foo }; + + (Foo?.["m"])(); + (Foo?.["m"])().toString; + (Foo?.["m"])().toString(); + + (o?.Foo.m)(); + (o?.Foo.m)().toString; + (o?.Foo.m)().toString(); + + (((o.Foo?.self.getSelf)())?.m)(); + (((o.Foo.self?.getSelf)())?.m)(); + } +} + +(new Foo).test(); diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/options.json b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/options.json new file mode 100644 index 000000000000..39ea3f99c7ff --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-optional-chaining", { "loose": true }]] +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/output.js new file mode 100644 index 000000000000..3f79a4007881 --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/parenthesized-member-call-loose/output.js @@ -0,0 +1,34 @@ +class Foo { + constructor() { + this.x = 1; + this.self = this; + } + + m() { + return this.x; + } + + getSelf() { + return this; + } + + test() { + var _o$Foo$self$getSelf, _o$Foo, _o$Foo$self$getSelf2, _o$Foo$self; + + const Foo = this; + const o = { + Foo: Foo + }; + (Foo == null ? void 0 : Foo["m"].bind(Foo))(); + (Foo == null ? void 0 : Foo["m"].bind(Foo))().toString; + (Foo == null ? void 0 : Foo["m"].bind(Foo))().toString(); + (o == null ? void 0 : o.Foo.m.bind(o.Foo))(); + (o == null ? void 0 : o.Foo.m.bind(o.Foo))().toString; + (o == null ? void 0 : o.Foo.m.bind(o.Foo))().toString(); + ((_o$Foo$self$getSelf = ((_o$Foo = o.Foo) == null ? void 0 : _o$Foo.self.getSelf.bind(_o$Foo.self))()) == null ? void 0 : _o$Foo$self$getSelf.m.bind(_o$Foo$self$getSelf))(); + ((_o$Foo$self$getSelf2 = ((_o$Foo$self = o.Foo.self) == null ? void 0 : _o$Foo$self.getSelf.bind(_o$Foo$self))()) == null ? void 0 : _o$Foo$self$getSelf2.m.bind(_o$Foo$self$getSelf2))(); + } + +} + +new Foo().test();