From 785b6e8bde1419b3ea7d219cd8caf9dab043edfb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Hu=C3=A1ng=20J=C3=B9nli=C3=A0ng?= Date: Fri, 22 May 2020 16:08:27 -0400 Subject: [PATCH] feat: support ?.#x and ?.#m() --- .../src/index.js | 15 ++- .../exec.js | 0 .../input.js | 0 .../options.json | 0 .../output.js | 0 .../optional-chain-before-member-call/exec.js | 122 +++++++++++++++++ .../input.js | 0 .../options.json | 0 .../output.js | 0 .../exec.js | 0 .../input.js | 0 .../options.json | 0 .../output.js | 0 .../optional-chain-before-property/exec.js | 121 +++++++++++++++++ .../input.js | 0 .../options.json | 0 .../output.js | 0 .../exec.js | 126 ++++++++++++++++++ .../input.js | 70 ++++++++++ .../options.json | 3 + .../output.js | 92 +++++++++++++ .../exec.js | 126 ++++++++++++++++++ .../input.js | 70 ++++++++++ .../options.json | 3 + .../output.js | 92 +++++++++++++ .../exec.js | 125 +++++++++++++++++ .../input.js | 69 ++++++++++ .../options.json | 3 + .../output.js | 86 ++++++++++++ .../optional-chain-optional-property/exec.js | 125 +++++++++++++++++ .../optional-chain-optional-property/input.js | 70 ++++++++++ .../options.json | 3 + .../output.js | 86 ++++++++++++ 33 files changed, 1400 insertions(+), 7 deletions(-) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call-with-transform => optional-chain-before-member-call-with-transform}/exec.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call-with-transform => optional-chain-before-member-call-with-transform}/input.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call-with-transform => optional-chain-before-member-call-with-transform}/options.json (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call-with-transform => optional-chain-before-member-call-with-transform}/output.js (100%) create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/exec.js rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call => optional-chain-before-member-call}/input.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call => optional-chain-before-member-call}/options.json (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-private-call => optional-chain-before-member-call}/output.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-with-transform => optional-chain-before-property-with-transform}/exec.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-with-transform => optional-chain-before-property-with-transform}/input.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-with-transform => optional-chain-before-property-with-transform}/options.json (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain-with-transform => optional-chain-before-property-with-transform}/output.js (100%) create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/exec.js rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain => optional-chain-before-property}/input.js (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain => optional-chain-before-property}/options.json (100%) rename packages/babel-plugin-proposal-class-properties/test/fixtures/private/{optional-chain => optional-chain-before-property}/output.js (100%) create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/output.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/exec.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/input.js create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/options.json create mode 100644 packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/output.js diff --git a/packages/babel-helper-member-expression-to-functions/src/index.js b/packages/babel-helper-member-expression-to-functions/src/index.js index b7482bcdd46c..fe3d22cadbd4 100644 --- a/packages/babel-helper-member-expression-to-functions/src/index.js +++ b/packages/babel-helper-member-expression-to-functions/src/index.js @@ -125,12 +125,6 @@ const handle = { throw member.buildCodeFrameError(`can't handle delete`); } - if (node.optional) { - throw member.buildCodeFrameError( - `can't handle '?.' directly before ${node.property.type}`, - ); - } - // Now, we're looking for the start of this optional chain, which is // optional to the left of this member. // @@ -150,6 +144,8 @@ const handle = { startingOptional = startingOptional.get("callee"); continue; } + // prevent infinite loop: unreachable if the AST is well-formed + throw new Error("Internal error"); } const { scope } = member; @@ -160,8 +156,13 @@ const handle = { const baseRef = scope.generateUidIdentifierBasedOnNode(startingNode); scope.push({ id: baseRef }); + // Compute parentIsOptionalCall before `startingOptional` is replaced + // as `node` may refer to `startingOptional.node` before replaced. + const parentIsOptionalCall = parentPath.isOptionalCallExpression({ + callee: node, + }); startingOptional.replaceWith(toNonOptional(startingOptional, baseRef)); - if (parentPath.isOptionalCallExpression({ callee: node })) { + if (parentIsOptionalCall) { parentPath.replaceWith(this.call(member, parent.arguments)); } else { member.replaceWith(this.get(member)); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/exec.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/exec.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/exec.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/input.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/input.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/input.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/options.json similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/options.json rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/options.json diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/output.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call-with-transform/output.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call-with-transform/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/exec.js new file mode 100644 index 000000000000..13cc95662d95 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/exec.js @@ -0,0 +1,122 @@ +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; + } + + expect(o?.Foo.#m()).toEqual(1); + expect(o?.Foo.#m().toString).toEqual(1..toString); + expect(o?.Foo.#m().toString()).toEqual('1'); + + expect(deep?.very.o?.Foo.#m()).toEqual(1); + expect(deep?.very.o?.Foo.#m().toString).toEqual(1..toString); + expect(deep?.very.o?.Foo.#m().toString()).toEqual('1'); + + expect(o?.Foo.#self.#m()).toEqual(1); + expect(o?.Foo.#self.self.#m()).toEqual(1); + expect(o?.Foo.#self?.self.#m()).toEqual(1); + expect(o?.Foo.#self.self?.self.#m()).toEqual(1); + expect(o?.Foo.#self?.self?.self.#m()).toEqual(1); + + expect(o?.Foo.#self.getSelf().#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.().#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf().#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.().#m()).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self.#m()).toEqual(1); + + expect(fn?.().Foo.#m()).toEqual(1); + expect(fn?.().Foo.#m().toString).toEqual(1..toString); + expect(fn?.().Foo.#m().toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo.#m()).toEqual(1); + expect(fnDeep?.().very.o?.Foo.#m().toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo.#m().toString()).toEqual('1'); + + expect(fn?.().Foo.#self.#m()).toEqual(1); + expect(fn?.().Foo.#self.self.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self.#m()).toEqual(1); + expect(fn?.().Foo.#self.self?.self.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self?.self.#m()).toEqual(1); + + expect(fn?.().Foo.#self.getSelf().#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.().#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf().#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.().#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self.#m()).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo.#m()).toEqual(undefined); + expect(o?.Foo.#m().toString).toEqual(undefined); + expect(o?.Foo.#m().toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo.#m()).toEqual(undefined); + expect(deep?.very.o?.Foo.#m().toString).toEqual(undefined); + expect(deep?.very.o?.Foo.#m().toString()).toEqual(undefined); + + expect(o?.Foo.#self.#m()).toEqual(undefined); + expect(o?.Foo.#self.self.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self.#m()).toEqual(undefined); + expect(o?.Foo.#self.self?.self.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self?.self.#m()).toEqual(undefined); + + expect(o?.Foo.#self.getSelf().#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.().#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf().#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.().#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self.#m()).toEqual(undefined); + + expect(fn?.().Foo.#m()).toEqual(undefined); + expect(fn?.().Foo.#m().toString).toEqual(undefined); + expect(fn?.().Foo.#m().toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo.#m()).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo.#m().toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo.#m().toString()).toEqual(undefined); + + expect(fn?.().Foo.#self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self.#m()).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf().#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.().#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf().#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.().#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self.#m()).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/input.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/input.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/input.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/options.json similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/options.json rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/options.json diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/output.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-private-call/output.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-member-call/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/exec.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/exec.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/exec.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/input.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/input.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/input.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/options.json similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/options.json rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/options.json diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/output.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-with-transform/output.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property-with-transform/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/exec.js new file mode 100644 index 000000000000..98ac8e7a7e12 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/exec.js @@ -0,0 +1,121 @@ +class Foo { + static #x = 1; + 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; + } + + expect(o?.Foo.#x).toEqual(1); + expect(o?.Foo.#x.toString).toEqual(1..toString); + expect(o?.Foo.#x.toString()).toEqual('1'); + + expect(deep?.very.o?.Foo.#x).toEqual(1); + expect(deep?.very.o?.Foo.#x.toString).toEqual(1..toString); + expect(deep?.very.o?.Foo.#x.toString()).toEqual('1'); + + expect(o?.Foo.#self.#x).toEqual(1); + expect(o?.Foo.#self.self.#x).toEqual(1); + expect(o?.Foo.#self?.self.#x).toEqual(1); + expect(o?.Foo.#self.self?.self.#x).toEqual(1); + expect(o?.Foo.#self?.self?.self.#x).toEqual(1); + + expect(o?.Foo.#self.getSelf().#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.().#x).toEqual(1); + expect(o?.Foo.#self?.getSelf().#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.().#x).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self.#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self.#x).toEqual(1); + + expect(fn?.().Foo.#x).toEqual(1); + expect(fn?.().Foo.#x.toString).toEqual(1..toString); + expect(fn?.().Foo.#x.toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo.#x).toEqual(1); + expect(fnDeep?.().very.o?.Foo.#x.toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo.#x.toString()).toEqual('1'); + + expect(fn?.().Foo.#self.#x).toEqual(1); + expect(fn?.().Foo.#self.self.#x).toEqual(1); + expect(fn?.().Foo.#self?.self.#x).toEqual(1); + expect(fn?.().Foo.#self.self?.self.#x).toEqual(1); + expect(fn?.().Foo.#self?.self?.self.#x).toEqual(1); + + expect(fn?.().Foo.#self.getSelf().#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.().#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf().#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.().#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self.#x).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo.#x).toEqual(undefined); + expect(o?.Foo.#x.toString).toEqual(undefined); + expect(o?.Foo.#x.toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo.#x).toEqual(undefined); + expect(deep?.very.o?.Foo.#x.toString).toEqual(undefined); + expect(deep?.very.o?.Foo.#x.toString()).toEqual(undefined); + + expect(o?.Foo.#self.#x).toEqual(undefined); + expect(o?.Foo.#self.self.#x).toEqual(undefined); + expect(o?.Foo.#self?.self.#x).toEqual(undefined); + expect(o?.Foo.#self.self?.self.#x).toEqual(undefined); + expect(o?.Foo.#self?.self?.self.#x).toEqual(undefined); + + expect(o?.Foo.#self.getSelf().#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.().#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf().#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.().#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self.#x).toEqual(undefined); + + expect(fn?.().Foo.#x).toEqual(undefined); + expect(fn?.().Foo.#x.toString).toEqual(undefined); + expect(fn?.().Foo.#x.toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo.#x).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo.#x.toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo.#x.toString()).toEqual(undefined); + + expect(fn?.().Foo.#self.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self.#x).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf().#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.().#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf().#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.().#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self.#x).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/input.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/input.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/input.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/options.json similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/options.json rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/options.json diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/output.js similarity index 100% rename from packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain/output.js rename to packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-before-property/output.js diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/exec.js new file mode 100644 index 000000000000..df4319aff69b --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/exec.js @@ -0,0 +1,126 @@ +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; + } + + 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(deep?.very.o?.Foo?.#m()).toEqual(1); + expect(deep?.very.o?.Foo?.#m().toString).toEqual(1..toString); + expect(deep?.very.o?.Foo?.#m().toString()).toEqual('1'); + + expect(o?.Foo.#self?.#m()).toEqual(1); + expect(o?.Foo.#self.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.self?.#m()).toEqual(1); + expect(o?.Foo.#self.self?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.self?.self?.#m()).toEqual(1); + + expect(o?.Foo.#self.getSelf()?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self?.#m()).toEqual(1); + + expect(fn?.().Foo?.#m()).toEqual(1); + expect(fn?.().Foo?.#m().toString).toEqual(1..toString); + expect(fn?.().Foo?.#m().toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo?.#m()).toEqual(1); + expect(fnDeep?.().very.o?.Foo?.#m().toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo?.#m().toString()).toEqual('1'); + + expect(fn?.().Foo.#self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.self?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self?.self?.#m()).toEqual(1); + + expect(fn?.().Foo.#self.getSelf()?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#m()).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo?.#m()).toEqual(undefined); + expect(o?.Foo?.#m().toString).toEqual(undefined); + expect(o?.Foo?.#m().toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo?.#m()).toEqual(undefined); + expect(deep?.very.o?.Foo?.#m().toString).toEqual(undefined); + expect(deep?.very.o?.Foo?.#m().toString()).toEqual(undefined); + + expect(o?.Foo.#self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.self?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self?.self?.#m()).toEqual(undefined); + + expect(o?.Foo.#self.getSelf()?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self?.#m()).toEqual(undefined); + + expect(fn?.().Foo?.#m()).toEqual(undefined); + expect(fn?.().Foo?.#m().toString).toEqual(undefined); + expect(fn?.().Foo?.#m().toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo?.#m()).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#m().toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#m().toString()).toEqual(undefined); + + expect(fn?.().Foo.#self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self?.#m()).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#m()).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/input.js new file mode 100644 index 000000000000..80f689342109 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/input.js @@ -0,0 +1,70 @@ +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; + } + + Foo?.#m(); + Foo?.#m().toString; + Foo?.#m().toString(); + + o?.Foo?.#m(); + o?.Foo?.#m().toString; + o?.Foo?.#m().toString(); + + deep?.very.o?.Foo?.#m(); + deep?.very.o?.Foo?.#m().toString; + deep?.very.o?.Foo?.#m().toString(); + + o?.Foo.#self?.#m(); + o?.Foo.#self.self?.#m(); + o?.Foo.#self?.self?.#m(); + o?.Foo.#self.self?.self?.#m(); + o?.Foo.#self?.self?.self?.#m(); + + o?.Foo.#self.getSelf()?.#m(); + o?.Foo.#self.getSelf?.()?.#m(); + o?.Foo.#self?.getSelf()?.#m(); + o?.Foo.#self?.getSelf?.()?.#m(); + o?.Foo.#self.getSelf()?.self?.#m(); + o?.Foo.#self.getSelf?.()?.self?.#m(); + o?.Foo.#self?.getSelf()?.self?.#m(); + o?.Foo.#self?.getSelf?.()?.self?.#m(); + + fn?.().Foo?.#m(); + fn?.().Foo?.#m().toString; + fn?.().Foo?.#m().toString(); + + fnDeep?.().very.o?.Foo?.#m(); + fnDeep?.().very.o?.Foo?.#m().toString; + fnDeep?.().very.o?.Foo?.#m().toString(); + + fn?.().Foo.#self?.#m(); + fn?.().Foo.#self.self?.#m(); + fn?.().Foo.#self?.self?.#m(); + fn?.().Foo.#self.self?.self?.#m(); + fn?.().Foo.#self?.self?.self?.#m(); + + fn?.().Foo.#self.getSelf()?.#m(); + fn?.().Foo.#self.getSelf?.()?.#m(); + fn?.().Foo.#self?.getSelf()?.#m(); + fn?.().Foo.#self?.getSelf?.()?.#m(); + fn?.().Foo.#self.getSelf()?.self?.#m(); + fn?.().Foo.#self.getSelf?.()?.self?.#m(); + fn?.().Foo.#self?.getSelf()?.self?.#m(); + fn?.().Foo.#self?.getSelf?.()?.self?.#m(); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/options.json new file mode 100644 index 000000000000..63b4c77cc8e8 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-optional-chaining", "proposal-class-properties"] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/output.js new file mode 100644 index 000000000000..22ae2753c5d1 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call-with-transform/output.js @@ -0,0 +1,92 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } + +class Foo { + static getSelf() { + return this; + } + + static test() { + var _Foo, _Foo2, _Foo3, _o$Foo, _o$Foo2, _o$Foo3, _deep$very$o$Foo, _deep$very$o$Foo2, _deep$very$o$Foo3, _o, _ref, _o2, _ref2, _o3, _self2, _o4, _self3, _o5, _self$self, _o6, _ref3, _o7, _classStaticPrivateFi, _call, _o8, _getSelf, _o9, _getSelf2, _o10, _self4, _o11, _classStaticPrivateFi2, _call$self, _o12, _getSelf$self, _o13, _getSelf$self2, _fn$Foo, _fn$Foo2, _fn$Foo3, _fnDeep$very$o$Foo, _fnDeep$very$o$Foo2, _fnDeep$very$o$Foo3, _fn, _ref4, _fn2, _ref5, _fn3, _self5, _fn4, _self6, _fn5, _self$self2, _fn6, _ref6, _fn7, _classStaticPrivateFi3, _call2, _fn8, _getSelf3, _fn9, _getSelf4, _fn10, _self7, _fn11, _classStaticPrivateFi4, _call$self2, _fn12, _getSelf$self3, _fn13, _getSelf$self4, _deep$very$o, _deep$very$o2, _deep$very$o3, _ref7, _ref8, _ref9, _ref9$self, _ref10, _ref11, _ref12, _ref12$getSelf, _ref13, _ref14, _ref14$call, _ref15, _ref15$getSelf, _ref16, _ref16$getSelf, _ref16$getSelf$call, _fnDeep$very$o, _fnDeep$very$o2, _fnDeep$very$o3, _ref17, _ref18, _ref19, _ref19$self, _ref20, _ref21, _ref22, _ref22$getSelf, _ref23, _ref24, _ref24$call, _ref25, _ref25$getSelf, _ref26, _ref26$getSelf, _ref26$getSelf$call; + + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + (_Foo = Foo) === null || _Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo, Foo, _m).call(_Foo); + (_Foo2 = Foo) === null || _Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo2, Foo, _m).call(_Foo2).toString; + (_Foo3 = Foo) === null || _Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo3, Foo, _m).call(_Foo3).toString(); + (_o$Foo = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo, Foo, _m).call(_o$Foo); + (_o$Foo2 = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo2, Foo, _m).call(_o$Foo2).toString; + (_o$Foo3 = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo3, Foo, _m).call(_o$Foo3).toString(); + (_deep$very$o$Foo = deep === null || deep === void 0 ? void 0 : (_deep$very$o = deep.very.o) === null || _deep$very$o === void 0 ? void 0 : _deep$very$o.Foo) === null || _deep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo, Foo, _m).call(_deep$very$o$Foo); + (_deep$very$o$Foo2 = deep === null || deep === void 0 ? void 0 : (_deep$very$o2 = deep.very.o) === null || _deep$very$o2 === void 0 ? void 0 : _deep$very$o2.Foo) === null || _deep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo2, Foo, _m).call(_deep$very$o$Foo2).toString; + (_deep$very$o$Foo3 = deep === null || deep === void 0 ? void 0 : (_deep$very$o3 = deep.very.o) === null || _deep$very$o3 === void 0 ? void 0 : _deep$very$o3.Foo) === null || _deep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo3, Foo, _m).call(_deep$very$o$Foo3).toString(); + (_ref = (_o = o) === null || _o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o.Foo, Foo, _self)) === null || _ref === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref, Foo, _m).call(_ref); + (_ref2 = (_o2 = o) === null || _o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o2.Foo, Foo, _self).self) === null || _ref2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref2, Foo, _m).call(_ref2); + (_self2 = (_ref7 = (_o3 = o) === null || _o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o3.Foo, Foo, _self)) === null || _ref7 === void 0 ? void 0 : _ref7.self) === null || _self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self2, Foo, _m).call(_self2); + (_self3 = (_ref8 = (_o4 = o) === null || _o4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o4.Foo, Foo, _self).self) === null || _ref8 === void 0 ? void 0 : _ref8.self) === null || _self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self3, Foo, _m).call(_self3); + (_self$self = (_ref9 = (_o5 = o) === null || _o5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o5.Foo, Foo, _self)) === null || _ref9 === void 0 ? void 0 : (_ref9$self = _ref9.self) === null || _ref9$self === void 0 ? void 0 : _ref9$self.self) === null || _self$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self, Foo, _m).call(_self$self); + (_ref3 = (_o6 = o) === null || _o6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o6.Foo, Foo, _self).getSelf()) === null || _ref3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref3, Foo, _m).call(_ref3); + (_call = (_ref10 = (_o7 = o) === null || _o7 === void 0 ? void 0 : (_classStaticPrivateFi = _classStaticPrivateFieldSpecGet(_o7.Foo, Foo, _self)).getSelf) === null || _ref10 === void 0 ? void 0 : _ref10.call(_classStaticPrivateFi)) === null || _call === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call, Foo, _m).call(_call); + (_getSelf = (_ref11 = (_o8 = o) === null || _o8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o8.Foo, Foo, _self)) === null || _ref11 === void 0 ? void 0 : _ref11.getSelf()) === null || _getSelf === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf, Foo, _m).call(_getSelf); + (_getSelf2 = (_ref12 = (_o9 = o) === null || _o9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o9.Foo, Foo, _self)) === null || _ref12 === void 0 ? void 0 : (_ref12$getSelf = _ref12.getSelf) === null || _ref12$getSelf === void 0 ? void 0 : _ref12$getSelf.call(_ref12)) === null || _getSelf2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf2, Foo, _m).call(_getSelf2); + (_self4 = (_ref13 = (_o10 = o) === null || _o10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o10.Foo, Foo, _self).getSelf()) === null || _ref13 === void 0 ? void 0 : _ref13.self) === null || _self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self4, Foo, _m).call(_self4); + (_call$self = (_ref14 = (_o11 = o) === null || _o11 === void 0 ? void 0 : (_classStaticPrivateFi2 = _classStaticPrivateFieldSpecGet(_o11.Foo, Foo, _self)).getSelf) === null || _ref14 === void 0 ? void 0 : (_ref14$call = _ref14.call(_classStaticPrivateFi2)) === null || _ref14$call === void 0 ? void 0 : _ref14$call.self) === null || _call$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self, Foo, _m).call(_call$self); + (_getSelf$self = (_ref15 = (_o12 = o) === null || _o12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o12.Foo, Foo, _self)) === null || _ref15 === void 0 ? void 0 : (_ref15$getSelf = _ref15.getSelf()) === null || _ref15$getSelf === void 0 ? void 0 : _ref15$getSelf.self) === null || _getSelf$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self, Foo, _m).call(_getSelf$self); + (_getSelf$self2 = (_ref16 = (_o13 = o) === null || _o13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o13.Foo, Foo, _self)) === null || _ref16 === void 0 ? void 0 : (_ref16$getSelf = _ref16.getSelf) === null || _ref16$getSelf === void 0 ? void 0 : (_ref16$getSelf$call = _ref16$getSelf.call(_ref16)) === null || _ref16$getSelf$call === void 0 ? void 0 : _ref16$getSelf$call.self) === null || _getSelf$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self2, Foo, _m).call(_getSelf$self2); + (_fn$Foo = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo, Foo, _m).call(_fn$Foo); + (_fn$Foo2 = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo2, Foo, _m).call(_fn$Foo2).toString; + (_fn$Foo3 = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo3, Foo, _m).call(_fn$Foo3).toString(); + (_fnDeep$very$o$Foo = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o = fnDeep().very.o) === null || _fnDeep$very$o === void 0 ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _m).call(_fnDeep$very$o$Foo); + (_fnDeep$very$o$Foo2 = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o2 = fnDeep().very.o) === null || _fnDeep$very$o2 === void 0 ? void 0 : _fnDeep$very$o2.Foo) === null || _fnDeep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo2, Foo, _m).call(_fnDeep$very$o$Foo2).toString; + (_fnDeep$very$o$Foo3 = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o3 = fnDeep().very.o) === null || _fnDeep$very$o3 === void 0 ? void 0 : _fnDeep$very$o3.Foo) === null || _fnDeep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo3, Foo, _m).call(_fnDeep$very$o$Foo3).toString(); + (_ref4 = (_fn = fn) === null || _fn === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn().Foo, Foo, _self)) === null || _ref4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref4, Foo, _m).call(_ref4); + (_ref5 = (_fn2 = fn) === null || _fn2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn2().Foo, Foo, _self).self) === null || _ref5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref5, Foo, _m).call(_ref5); + (_self5 = (_ref17 = (_fn3 = fn) === null || _fn3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn3().Foo, Foo, _self)) === null || _ref17 === void 0 ? void 0 : _ref17.self) === null || _self5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self5, Foo, _m).call(_self5); + (_self6 = (_ref18 = (_fn4 = fn) === null || _fn4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn4().Foo, Foo, _self).self) === null || _ref18 === void 0 ? void 0 : _ref18.self) === null || _self6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self6, Foo, _m).call(_self6); + (_self$self2 = (_ref19 = (_fn5 = fn) === null || _fn5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn5().Foo, Foo, _self)) === null || _ref19 === void 0 ? void 0 : (_ref19$self = _ref19.self) === null || _ref19$self === void 0 ? void 0 : _ref19$self.self) === null || _self$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self2, Foo, _m).call(_self$self2); + (_ref6 = (_fn6 = fn) === null || _fn6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn6().Foo, Foo, _self).getSelf()) === null || _ref6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref6, Foo, _m).call(_ref6); + (_call2 = (_ref20 = (_fn7 = fn) === null || _fn7 === void 0 ? void 0 : (_classStaticPrivateFi3 = _classStaticPrivateFieldSpecGet(_fn7().Foo, Foo, _self)).getSelf) === null || _ref20 === void 0 ? void 0 : _ref20.call(_classStaticPrivateFi3)) === null || _call2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call2, Foo, _m).call(_call2); + (_getSelf3 = (_ref21 = (_fn8 = fn) === null || _fn8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn8().Foo, Foo, _self)) === null || _ref21 === void 0 ? void 0 : _ref21.getSelf()) === null || _getSelf3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf3, Foo, _m).call(_getSelf3); + (_getSelf4 = (_ref22 = (_fn9 = fn) === null || _fn9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn9().Foo, Foo, _self)) === null || _ref22 === void 0 ? void 0 : (_ref22$getSelf = _ref22.getSelf) === null || _ref22$getSelf === void 0 ? void 0 : _ref22$getSelf.call(_ref22)) === null || _getSelf4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf4, Foo, _m).call(_getSelf4); + (_self7 = (_ref23 = (_fn10 = fn) === null || _fn10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn10().Foo, Foo, _self).getSelf()) === null || _ref23 === void 0 ? void 0 : _ref23.self) === null || _self7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self7, Foo, _m).call(_self7); + (_call$self2 = (_ref24 = (_fn11 = fn) === null || _fn11 === void 0 ? void 0 : (_classStaticPrivateFi4 = _classStaticPrivateFieldSpecGet(_fn11().Foo, Foo, _self)).getSelf) === null || _ref24 === void 0 ? void 0 : (_ref24$call = _ref24.call(_classStaticPrivateFi4)) === null || _ref24$call === void 0 ? void 0 : _ref24$call.self) === null || _call$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self2, Foo, _m).call(_call$self2); + (_getSelf$self3 = (_ref25 = (_fn12 = fn) === null || _fn12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn12().Foo, Foo, _self)) === null || _ref25 === void 0 ? void 0 : (_ref25$getSelf = _ref25.getSelf()) === null || _ref25$getSelf === void 0 ? void 0 : _ref25$getSelf.self) === null || _getSelf$self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self3, Foo, _m).call(_getSelf$self3); + (_getSelf$self4 = (_ref26 = (_fn13 = fn) === null || _fn13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn13().Foo, Foo, _self)) === null || _ref26 === void 0 ? void 0 : (_ref26$getSelf = _ref26.getSelf) === null || _ref26$getSelf === void 0 ? void 0 : (_ref26$getSelf$call = _ref26$getSelf.call(_ref26)) === null || _ref26$getSelf$call === void 0 ? void 0 : _ref26$getSelf$call.self) === null || _getSelf$self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self4, Foo, _m).call(_getSelf$self4); + } + +} + +var _x = { + writable: true, + value: 1 +}; +var _m = { + writable: true, + value: function () { + return _classStaticPrivateFieldSpecGet(this, Foo, _x); + } +}; +var _self = { + writable: true, + value: Foo +}; + +_defineProperty(Foo, "self", Foo); + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/exec.js new file mode 100644 index 000000000000..df4319aff69b --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/exec.js @@ -0,0 +1,126 @@ +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; + } + + 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(deep?.very.o?.Foo?.#m()).toEqual(1); + expect(deep?.very.o?.Foo?.#m().toString).toEqual(1..toString); + expect(deep?.very.o?.Foo?.#m().toString()).toEqual('1'); + + expect(o?.Foo.#self?.#m()).toEqual(1); + expect(o?.Foo.#self.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.self?.#m()).toEqual(1); + expect(o?.Foo.#self.self?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.self?.self?.#m()).toEqual(1); + + expect(o?.Foo.#self.getSelf()?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self?.#m()).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self?.#m()).toEqual(1); + + expect(fn?.().Foo?.#m()).toEqual(1); + expect(fn?.().Foo?.#m().toString).toEqual(1..toString); + expect(fn?.().Foo?.#m().toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo?.#m()).toEqual(1); + expect(fnDeep?.().very.o?.Foo?.#m().toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo?.#m().toString()).toEqual('1'); + + expect(fn?.().Foo.#self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.self?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.self?.self?.#m()).toEqual(1); + + expect(fn?.().Foo.#self.getSelf()?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self?.#m()).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#m()).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo?.#m()).toEqual(undefined); + expect(o?.Foo?.#m().toString).toEqual(undefined); + expect(o?.Foo?.#m().toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo?.#m()).toEqual(undefined); + expect(deep?.very.o?.Foo?.#m().toString).toEqual(undefined); + expect(deep?.very.o?.Foo?.#m().toString()).toEqual(undefined); + + expect(o?.Foo.#self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.self?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.self?.self?.#m()).toEqual(undefined); + + expect(o?.Foo.#self.getSelf()?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self?.#m()).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self?.#m()).toEqual(undefined); + + expect(fn?.().Foo?.#m()).toEqual(undefined); + expect(fn?.().Foo?.#m().toString).toEqual(undefined); + expect(fn?.().Foo?.#m().toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo?.#m()).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#m().toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#m().toString()).toEqual(undefined); + + expect(fn?.().Foo.#self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self?.#m()).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self?.#m()).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#m()).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/input.js new file mode 100644 index 000000000000..a5c222687ee8 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/input.js @@ -0,0 +1,70 @@ +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; + } + + Foo?.#m(); + Foo?.#m().toString; + Foo?.#m().toString(); + + o?.Foo.#m(); + o?.Foo.#m().toString; + o?.Foo.#m().toString(); + + deep?.very.o?.Foo.#m(); + deep?.very.o?.Foo.#m().toString; + deep?.very.o?.Foo.#m().toString(); + + o?.Foo.#self.#m(); + o?.Foo.#self.self.#m(); + o?.Foo.#self?.self.#m(); + o?.Foo.#self.self?.self.#m(); + o?.Foo.#self?.self?.self.#m(); + + o?.Foo.#self.getSelf().#m(); + o?.Foo.#self.getSelf?.().#m(); + o?.Foo.#self?.getSelf().#m(); + o?.Foo.#self?.getSelf?.().#m(); + o?.Foo.#self.getSelf()?.self.#m(); + o?.Foo.#self.getSelf?.()?.self.#m(); + o?.Foo.#self?.getSelf()?.self.#m(); + o?.Foo.#self?.getSelf?.()?.self.#m(); + + fn?.().Foo.#m(); + fn?.().Foo.#m().toString; + fn?.().Foo.#m().toString(); + + fnDeep?.().very.o?.Foo.#m(); + fnDeep?.().very.o?.Foo.#m().toString; + fnDeep?.().very.o?.Foo.#m().toString(); + + fn?.().Foo.#self.#m(); + fn?.().Foo.#self.self.#m(); + fn?.().Foo.#self?.self.#m(); + fn?.().Foo.#self.self?.self.#m(); + fn?.().Foo.#self?.self?.self.#m(); + + fn?.().Foo.#self.getSelf().#m(); + fn?.().Foo.#self.getSelf?.().#m(); + fn?.().Foo.#self?.getSelf().#m(); + fn?.().Foo.#self?.getSelf?.().#m(); + fn?.().Foo.#self.getSelf()?.self.#m(); + fn?.().Foo.#self.getSelf?.()?.self.#m(); + fn?.().Foo.#self?.getSelf()?.self.#m(); + fn?.().Foo.#self?.getSelf?.()?.self.#m(); + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/options.json new file mode 100644 index 000000000000..19ed5174f545 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-class-properties"] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/output.js new file mode 100644 index 000000000000..bce199186f44 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-member-call/output.js @@ -0,0 +1,92 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } + +class Foo { + static getSelf() { + return this; + } + + static test() { + var _Foo, _Foo2, _Foo3, _o, _o$Foo, _o2, _o2$Foo, _o3, _o3$Foo, _deep$very$o, _deep$very$o$Foo, _deep$very$o2, _deep$very$o2$Foo, _deep$very$o3, _deep$very$o3$Foo, _o4, _classStaticPrivateFi, _o5, _classStaticPrivateFi2, _o6, _ref, _ref$self, _o7, _ref2, _ref2$self, _o8, _self2, _self2$self, _o9, _classStaticPrivateFi3, _o10, _classStaticPrivateFi4, _ref3, _ref3$call, _o11, _ref4, _ref4$getSelf, _o12, _getSelf, _ref5, _getSelf$call, _o13, _ref6, _ref6$self, _o14, _classStaticPrivateFi5, _call, _call$self, _o15, _getSelf2, _getSelf2$self, _o16, _getSelf3, _getSelf3$self, _fn, _fn$Foo, _fn2, _fn2$Foo, _fn3, _fn3$Foo, _fnDeep$very$o, _fnDeep$very$o$Foo, _fnDeep$very$o2, _fnDeep$very$o2$Foo, _fnDeep$very$o3, _fnDeep$very$o3$Foo, _fn4, _classStaticPrivateFi6, _fn5, _classStaticPrivateFi7, _fn6, _ref7, _ref7$self, _fn7, _ref8, _ref8$self, _fn8, _self3, _self3$self, _fn9, _classStaticPrivateFi8, _fn10, _classStaticPrivateFi9, _ref9, _ref9$call, _fn11, _ref10, _ref10$getSelf, _fn12, _getSelf4, _ref11, _getSelf4$call, _fn13, _ref12, _ref12$self, _fn14, _classStaticPrivateFi10, _call2, _call2$self, _fn15, _getSelf5, _getSelf5$self, _fn16, _getSelf6, _getSelf6$self; + + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + (_Foo = Foo) === null || _Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo, Foo, _m).call(_Foo); + (_Foo2 = Foo) === null || _Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo2, Foo, _m).call(_Foo2).toString; + (_Foo3 = Foo) === null || _Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo3, Foo, _m).call(_Foo3).toString(); + (_o = o) === null || _o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo = _o.Foo, Foo, _m).call(_o$Foo); + (_o2 = o) === null || _o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o2$Foo = _o2.Foo, Foo, _m).call(_o2$Foo).toString; + (_o3 = o) === null || _o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o3$Foo = _o3.Foo, Foo, _m).call(_o3$Foo).toString(); + (_deep$very$o = deep?.very.o) === null || _deep$very$o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo = _deep$very$o.Foo, Foo, _m).call(_deep$very$o$Foo); + (_deep$very$o2 = deep?.very.o) === null || _deep$very$o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o2$Foo = _deep$very$o2.Foo, Foo, _m).call(_deep$very$o2$Foo).toString; + (_deep$very$o3 = deep?.very.o) === null || _deep$very$o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o3$Foo = _deep$very$o3.Foo, Foo, _m).call(_deep$very$o3$Foo).toString(); + (_o4 = o) === null || _o4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi = _classStaticPrivateFieldSpecGet(_o4.Foo, Foo, _self), Foo, _m).call(_classStaticPrivateFi); + (_o5 = o) === null || _o5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi2 = _classStaticPrivateFieldSpecGet(_o5.Foo, Foo, _self).self, Foo, _m).call(_classStaticPrivateFi2); + (_ref = (_o6 = o) === null || _o6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o6.Foo, Foo, _self)) === null || _ref === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref$self = _ref.self, Foo, _m).call(_ref$self); + (_ref2 = (_o7 = o) === null || _o7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o7.Foo, Foo, _self).self) === null || _ref2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref2$self = _ref2.self, Foo, _m).call(_ref2$self); + (_self2 = ((_o8 = o) === null || _o8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o8.Foo, Foo, _self))?.self) === null || _self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self2$self = _self2.self, Foo, _m).call(_self2$self); + (_o9 = o) === null || _o9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi3 = _classStaticPrivateFieldSpecGet(_o9.Foo, Foo, _self).getSelf(), Foo, _m).call(_classStaticPrivateFi3); + (_ref3 = (_o10 = o) === null || _o10 === void 0 ? void 0 : (_classStaticPrivateFi4 = _classStaticPrivateFieldSpecGet(_o10.Foo, Foo, _self)).getSelf) === null || _ref3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref3$call = _ref3.call(_classStaticPrivateFi4), Foo, _m).call(_ref3$call); + (_ref4 = (_o11 = o) === null || _o11 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o11.Foo, Foo, _self)) === null || _ref4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref4$getSelf = _ref4.getSelf(), Foo, _m).call(_ref4$getSelf); + (_getSelf = (_ref5 = (_o12 = o) === null || _o12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o12.Foo, Foo, _self))?.getSelf) === null || _getSelf === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$call = _getSelf.call(_ref5), Foo, _m).call(_getSelf$call); + (_ref6 = (_o13 = o) === null || _o13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o13.Foo, Foo, _self).getSelf()) === null || _ref6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref6$self = _ref6.self, Foo, _m).call(_ref6$self); + (_call = ((_o14 = o) === null || _o14 === void 0 ? void 0 : (_classStaticPrivateFi5 = _classStaticPrivateFieldSpecGet(_o14.Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi5)) === null || _call === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self = _call.self, Foo, _m).call(_call$self); + (_getSelf2 = ((_o15 = o) === null || _o15 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o15.Foo, Foo, _self))?.getSelf()) === null || _getSelf2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf2$self = _getSelf2.self, Foo, _m).call(_getSelf2$self); + (_getSelf3 = ((_o16 = o) === null || _o16 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o16.Foo, Foo, _self))?.getSelf?.()) === null || _getSelf3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf3$self = _getSelf3.self, Foo, _m).call(_getSelf3$self); + (_fn = fn) === null || _fn === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo = _fn().Foo, Foo, _m).call(_fn$Foo); + (_fn2 = fn) === null || _fn2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn2$Foo = _fn2().Foo, Foo, _m).call(_fn2$Foo).toString; + (_fn3 = fn) === null || _fn3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn3$Foo = _fn3().Foo, Foo, _m).call(_fn3$Foo).toString(); + (_fnDeep$very$o = fnDeep?.().very.o) === null || _fnDeep$very$o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo = _fnDeep$very$o.Foo, Foo, _m).call(_fnDeep$very$o$Foo); + (_fnDeep$very$o2 = fnDeep?.().very.o) === null || _fnDeep$very$o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o2$Foo = _fnDeep$very$o2.Foo, Foo, _m).call(_fnDeep$very$o2$Foo).toString; + (_fnDeep$very$o3 = fnDeep?.().very.o) === null || _fnDeep$very$o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o3$Foo = _fnDeep$very$o3.Foo, Foo, _m).call(_fnDeep$very$o3$Foo).toString(); + (_fn4 = fn) === null || _fn4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi6 = _classStaticPrivateFieldSpecGet(_fn4().Foo, Foo, _self), Foo, _m).call(_classStaticPrivateFi6); + (_fn5 = fn) === null || _fn5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi7 = _classStaticPrivateFieldSpecGet(_fn5().Foo, Foo, _self).self, Foo, _m).call(_classStaticPrivateFi7); + (_ref7 = (_fn6 = fn) === null || _fn6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn6().Foo, Foo, _self)) === null || _ref7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref7$self = _ref7.self, Foo, _m).call(_ref7$self); + (_ref8 = (_fn7 = fn) === null || _fn7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn7().Foo, Foo, _self).self) === null || _ref8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref8$self = _ref8.self, Foo, _m).call(_ref8$self); + (_self3 = ((_fn8 = fn) === null || _fn8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn8().Foo, Foo, _self))?.self) === null || _self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self3$self = _self3.self, Foo, _m).call(_self3$self); + (_fn9 = fn) === null || _fn9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_classStaticPrivateFi8 = _classStaticPrivateFieldSpecGet(_fn9().Foo, Foo, _self).getSelf(), Foo, _m).call(_classStaticPrivateFi8); + (_ref9 = (_fn10 = fn) === null || _fn10 === void 0 ? void 0 : (_classStaticPrivateFi9 = _classStaticPrivateFieldSpecGet(_fn10().Foo, Foo, _self)).getSelf) === null || _ref9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref9$call = _ref9.call(_classStaticPrivateFi9), Foo, _m).call(_ref9$call); + (_ref10 = (_fn11 = fn) === null || _fn11 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn11().Foo, Foo, _self)) === null || _ref10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref10$getSelf = _ref10.getSelf(), Foo, _m).call(_ref10$getSelf); + (_getSelf4 = (_ref11 = (_fn12 = fn) === null || _fn12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn12().Foo, Foo, _self))?.getSelf) === null || _getSelf4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf4$call = _getSelf4.call(_ref11), Foo, _m).call(_getSelf4$call); + (_ref12 = (_fn13 = fn) === null || _fn13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn13().Foo, Foo, _self).getSelf()) === null || _ref12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref12$self = _ref12.self, Foo, _m).call(_ref12$self); + (_call2 = ((_fn14 = fn) === null || _fn14 === void 0 ? void 0 : (_classStaticPrivateFi10 = _classStaticPrivateFieldSpecGet(_fn14().Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi10)) === null || _call2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call2$self = _call2.self, Foo, _m).call(_call2$self); + (_getSelf5 = ((_fn15 = fn) === null || _fn15 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn15().Foo, Foo, _self))?.getSelf()) === null || _getSelf5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf5$self = _getSelf5.self, Foo, _m).call(_getSelf5$self); + (_getSelf6 = ((_fn16 = fn) === null || _fn16 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn16().Foo, Foo, _self))?.getSelf?.()) === null || _getSelf6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf6$self = _getSelf6.self, Foo, _m).call(_getSelf6$self); + } + +} + +var _x = { + writable: true, + value: 1 +}; +var _m = { + writable: true, + value: function () { + return _classStaticPrivateFieldSpecGet(this, Foo, _x); + } +}; +var _self = { + writable: true, + value: Foo +}; + +_defineProperty(Foo, "self", Foo); + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/exec.js new file mode 100644 index 000000000000..0d627ac9e0d4 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/exec.js @@ -0,0 +1,125 @@ +class Foo { + static #x = 1; + 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; + } + + expect(Foo?.#x).toEqual(1); + expect(Foo?.#x.toString).toEqual(1..toString); + expect(Foo?.#x.toString()).toEqual('1'); + + expect(o?.Foo?.#x).toEqual(1); + expect(o?.Foo?.#x.toString).toEqual(1..toString); + expect(o?.Foo?.#x.toString()).toEqual('1'); + + expect(deep?.very.o?.Foo?.#x).toEqual(1); + expect(deep?.very.o?.Foo?.#x.toString).toEqual(1..toString); + expect(deep?.very.o?.Foo?.#x.toString()).toEqual('1'); + + expect(o?.Foo.#self?.#x).toEqual(1); + expect(o?.Foo.#self.self?.#x).toEqual(1); + expect(o?.Foo.#self?.self?.#x).toEqual(1); + expect(o?.Foo.#self.self?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.self?.self?.#x).toEqual(1); + + expect(o?.Foo.#self.getSelf()?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self?.#x).toEqual(1); + + expect(fn?.().Foo?.#x).toEqual(1); + expect(fn?.().Foo?.#x.toString).toEqual(1..toString); + expect(fn?.().Foo?.#x.toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo?.#x).toEqual(1); + expect(fnDeep?.().very.o?.Foo?.#x.toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo?.#x.toString()).toEqual('1'); + + expect(fn?.().Foo.#self?.#x).toEqual(1); + expect(fn?.().Foo.#self.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self.self?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.self?.self?.#x).toEqual(1); + + expect(fn?.().Foo.#self.getSelf()?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#x).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo?.#x).toEqual(undefined); + expect(o?.Foo?.#x.toString).toEqual(undefined); + expect(o?.Foo?.#x.toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo?.#x).toEqual(undefined); + expect(deep?.very.o?.Foo?.#x.toString).toEqual(undefined); + expect(deep?.very.o?.Foo?.#x.toString()).toEqual(undefined); + + expect(o?.Foo.#self?.#x).toEqual(undefined); + expect(o?.Foo.#self.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self.self?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.self?.self?.#x).toEqual(undefined); + + expect(o?.Foo.#self.getSelf()?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self?.#x).toEqual(undefined); + + expect(fn?.().Foo?.#x).toEqual(undefined); + expect(fn?.().Foo?.#x.toString).toEqual(undefined); + expect(fn?.().Foo?.#x.toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo?.#x).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#x.toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#x.toString()).toEqual(undefined); + + expect(fn?.().Foo.#self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self?.#x).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#x).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/input.js new file mode 100644 index 000000000000..968eeaf3cb44 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/input.js @@ -0,0 +1,69 @@ +class Foo { + static #x = 1; + 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; + } + + Foo?.#x; + Foo?.#x.toString; + Foo?.#x.toString(); + + o?.Foo?.#x; + o?.Foo?.#x.toString; + o?.Foo?.#x.toString(); + + deep?.very.o?.Foo?.#x; + deep?.very.o?.Foo?.#x.toString; + deep?.very.o?.Foo?.#x.toString(); + + o?.Foo.#self?.#x; + o?.Foo.#self.self?.#x; + o?.Foo.#self?.self?.#x; + o?.Foo.#self.self?.self?.#x; + o?.Foo.#self?.self?.self?.#x; + + o?.Foo.#self.getSelf()?.#x; + o?.Foo.#self.getSelf?.()?.#x; + o?.Foo.#self?.getSelf()?.#x; + o?.Foo.#self?.getSelf?.()?.#x; + o?.Foo.#self.getSelf()?.self?.#x; + o?.Foo.#self.getSelf?.()?.self?.#x; + o?.Foo.#self?.getSelf()?.self?.#x; + o?.Foo.#self?.getSelf?.()?.self?.#x; + + fn?.().Foo?.#x; + fn?.().Foo?.#x.toString; + fn?.().Foo?.#x.toString(); + + fnDeep?.().very.o?.Foo?.#x; + fnDeep?.().very.o?.Foo?.#x.toString; + fnDeep?.().very.o?.Foo?.#x.toString(); + + fn?.().Foo.#self?.#x; + fn?.().Foo.#self.self?.#x; + fn?.().Foo.#self?.self?.#x; + fn?.().Foo.#self.self?.self?.#x; + fn?.().Foo.#self?.self?.self?.#x; + + fn?.().Foo.#self.getSelf()?.#x; + fn?.().Foo.#self.getSelf?.()?.#x; + fn?.().Foo.#self?.getSelf()?.#x; + fn?.().Foo.#self?.getSelf?.()?.#x; + fn?.().Foo.#self.getSelf()?.self?.#x; + fn?.().Foo.#self.getSelf?.()?.self?.#x; + fn?.().Foo.#self?.getSelf()?.self?.#x; + fn?.().Foo.#self?.getSelf?.()?.self?.#x; + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/options.json new file mode 100644 index 000000000000..63b4c77cc8e8 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-optional-chaining", "proposal-class-properties"] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/output.js new file mode 100644 index 000000000000..71d124ad5550 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property-with-transform/output.js @@ -0,0 +1,86 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } + +class Foo { + static getSelf() { + return this; + } + + static test() { + var _Foo, _Foo2, _Foo3, _o$Foo, _o$Foo2, _o$Foo3, _deep$very$o$Foo, _deep$very$o$Foo2, _deep$very$o$Foo3, _o, _ref, _o2, _ref2, _o3, _self2, _o4, _self3, _o5, _self$self, _o6, _ref3, _o7, _classStaticPrivateFi, _call, _o8, _getSelf, _o9, _getSelf2, _o10, _self4, _o11, _classStaticPrivateFi2, _call$self, _o12, _getSelf$self, _o13, _getSelf$self2, _fn$Foo, _fn$Foo2, _fn$Foo3, _fnDeep$very$o$Foo, _fnDeep$very$o$Foo2, _fnDeep$very$o$Foo3, _fn, _ref4, _fn2, _ref5, _fn3, _self5, _fn4, _self6, _fn5, _self$self2, _fn6, _ref6, _fn7, _classStaticPrivateFi3, _call2, _fn8, _getSelf3, _fn9, _getSelf4, _fn10, _self7, _fn11, _classStaticPrivateFi4, _call$self2, _fn12, _getSelf$self3, _fn13, _getSelf$self4, _deep$very$o, _deep$very$o2, _deep$very$o3, _ref7, _ref8, _ref9, _ref9$self, _ref10, _ref11, _ref12, _ref12$getSelf, _ref13, _ref14, _ref14$call, _ref15, _ref15$getSelf, _ref16, _ref16$getSelf, _ref16$getSelf$call, _fnDeep$very$o, _fnDeep$very$o2, _fnDeep$very$o3, _ref17, _ref18, _ref19, _ref19$self, _ref20, _ref21, _ref22, _ref22$getSelf, _ref23, _ref24, _ref24$call, _ref25, _ref25$getSelf, _ref26, _ref26$getSelf, _ref26$getSelf$call; + + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + (_Foo = Foo) === null || _Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo, Foo, _x); + (_Foo2 = Foo) === null || _Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo2, Foo, _x).toString; + (_Foo3 = Foo) === null || _Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo3, Foo, _x).toString(); + (_o$Foo = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo, Foo, _x); + (_o$Foo2 = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo2, Foo, _x).toString; + (_o$Foo3 = o === null || o === void 0 ? void 0 : o.Foo) === null || _o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo3, Foo, _x).toString(); + (_deep$very$o$Foo = deep === null || deep === void 0 ? void 0 : (_deep$very$o = deep.very.o) === null || _deep$very$o === void 0 ? void 0 : _deep$very$o.Foo) === null || _deep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo, Foo, _x); + (_deep$very$o$Foo2 = deep === null || deep === void 0 ? void 0 : (_deep$very$o2 = deep.very.o) === null || _deep$very$o2 === void 0 ? void 0 : _deep$very$o2.Foo) === null || _deep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo2, Foo, _x).toString; + (_deep$very$o$Foo3 = deep === null || deep === void 0 ? void 0 : (_deep$very$o3 = deep.very.o) === null || _deep$very$o3 === void 0 ? void 0 : _deep$very$o3.Foo) === null || _deep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo3, Foo, _x).toString(); + (_ref = (_o = o) === null || _o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o.Foo, Foo, _self)) === null || _ref === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref, Foo, _x); + (_ref2 = (_o2 = o) === null || _o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o2.Foo, Foo, _self).self) === null || _ref2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref2, Foo, _x); + (_self2 = (_ref7 = (_o3 = o) === null || _o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o3.Foo, Foo, _self)) === null || _ref7 === void 0 ? void 0 : _ref7.self) === null || _self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self2, Foo, _x); + (_self3 = (_ref8 = (_o4 = o) === null || _o4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o4.Foo, Foo, _self).self) === null || _ref8 === void 0 ? void 0 : _ref8.self) === null || _self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self3, Foo, _x); + (_self$self = (_ref9 = (_o5 = o) === null || _o5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o5.Foo, Foo, _self)) === null || _ref9 === void 0 ? void 0 : (_ref9$self = _ref9.self) === null || _ref9$self === void 0 ? void 0 : _ref9$self.self) === null || _self$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self, Foo, _x); + (_ref3 = (_o6 = o) === null || _o6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o6.Foo, Foo, _self).getSelf()) === null || _ref3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref3, Foo, _x); + (_call = (_ref10 = (_o7 = o) === null || _o7 === void 0 ? void 0 : (_classStaticPrivateFi = _classStaticPrivateFieldSpecGet(_o7.Foo, Foo, _self)).getSelf) === null || _ref10 === void 0 ? void 0 : _ref10.call(_classStaticPrivateFi)) === null || _call === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call, Foo, _x); + (_getSelf = (_ref11 = (_o8 = o) === null || _o8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o8.Foo, Foo, _self)) === null || _ref11 === void 0 ? void 0 : _ref11.getSelf()) === null || _getSelf === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf, Foo, _x); + (_getSelf2 = (_ref12 = (_o9 = o) === null || _o9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o9.Foo, Foo, _self)) === null || _ref12 === void 0 ? void 0 : (_ref12$getSelf = _ref12.getSelf) === null || _ref12$getSelf === void 0 ? void 0 : _ref12$getSelf.call(_ref12)) === null || _getSelf2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf2, Foo, _x); + (_self4 = (_ref13 = (_o10 = o) === null || _o10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o10.Foo, Foo, _self).getSelf()) === null || _ref13 === void 0 ? void 0 : _ref13.self) === null || _self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self4, Foo, _x); + (_call$self = (_ref14 = (_o11 = o) === null || _o11 === void 0 ? void 0 : (_classStaticPrivateFi2 = _classStaticPrivateFieldSpecGet(_o11.Foo, Foo, _self)).getSelf) === null || _ref14 === void 0 ? void 0 : (_ref14$call = _ref14.call(_classStaticPrivateFi2)) === null || _ref14$call === void 0 ? void 0 : _ref14$call.self) === null || _call$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self, Foo, _x); + (_getSelf$self = (_ref15 = (_o12 = o) === null || _o12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o12.Foo, Foo, _self)) === null || _ref15 === void 0 ? void 0 : (_ref15$getSelf = _ref15.getSelf()) === null || _ref15$getSelf === void 0 ? void 0 : _ref15$getSelf.self) === null || _getSelf$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self, Foo, _x); + (_getSelf$self2 = (_ref16 = (_o13 = o) === null || _o13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o13.Foo, Foo, _self)) === null || _ref16 === void 0 ? void 0 : (_ref16$getSelf = _ref16.getSelf) === null || _ref16$getSelf === void 0 ? void 0 : (_ref16$getSelf$call = _ref16$getSelf.call(_ref16)) === null || _ref16$getSelf$call === void 0 ? void 0 : _ref16$getSelf$call.self) === null || _getSelf$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self2, Foo, _x); + (_fn$Foo = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo, Foo, _x); + (_fn$Foo2 = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo2, Foo, _x).toString; + (_fn$Foo3 = fn === null || fn === void 0 ? void 0 : fn().Foo) === null || _fn$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo3, Foo, _x).toString(); + (_fnDeep$very$o$Foo = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o = fnDeep().very.o) === null || _fnDeep$very$o === void 0 ? void 0 : _fnDeep$very$o.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _x); + (_fnDeep$very$o$Foo2 = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o2 = fnDeep().very.o) === null || _fnDeep$very$o2 === void 0 ? void 0 : _fnDeep$very$o2.Foo) === null || _fnDeep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo2, Foo, _x).toString; + (_fnDeep$very$o$Foo3 = fnDeep === null || fnDeep === void 0 ? void 0 : (_fnDeep$very$o3 = fnDeep().very.o) === null || _fnDeep$very$o3 === void 0 ? void 0 : _fnDeep$very$o3.Foo) === null || _fnDeep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo3, Foo, _x).toString(); + (_ref4 = (_fn = fn) === null || _fn === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn().Foo, Foo, _self)) === null || _ref4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref4, Foo, _x); + (_ref5 = (_fn2 = fn) === null || _fn2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn2().Foo, Foo, _self).self) === null || _ref5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref5, Foo, _x); + (_self5 = (_ref17 = (_fn3 = fn) === null || _fn3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn3().Foo, Foo, _self)) === null || _ref17 === void 0 ? void 0 : _ref17.self) === null || _self5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self5, Foo, _x); + (_self6 = (_ref18 = (_fn4 = fn) === null || _fn4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn4().Foo, Foo, _self).self) === null || _ref18 === void 0 ? void 0 : _ref18.self) === null || _self6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self6, Foo, _x); + (_self$self2 = (_ref19 = (_fn5 = fn) === null || _fn5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn5().Foo, Foo, _self)) === null || _ref19 === void 0 ? void 0 : (_ref19$self = _ref19.self) === null || _ref19$self === void 0 ? void 0 : _ref19$self.self) === null || _self$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self2, Foo, _x); + (_ref6 = (_fn6 = fn) === null || _fn6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn6().Foo, Foo, _self).getSelf()) === null || _ref6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref6, Foo, _x); + (_call2 = (_ref20 = (_fn7 = fn) === null || _fn7 === void 0 ? void 0 : (_classStaticPrivateFi3 = _classStaticPrivateFieldSpecGet(_fn7().Foo, Foo, _self)).getSelf) === null || _ref20 === void 0 ? void 0 : _ref20.call(_classStaticPrivateFi3)) === null || _call2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call2, Foo, _x); + (_getSelf3 = (_ref21 = (_fn8 = fn) === null || _fn8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn8().Foo, Foo, _self)) === null || _ref21 === void 0 ? void 0 : _ref21.getSelf()) === null || _getSelf3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf3, Foo, _x); + (_getSelf4 = (_ref22 = (_fn9 = fn) === null || _fn9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn9().Foo, Foo, _self)) === null || _ref22 === void 0 ? void 0 : (_ref22$getSelf = _ref22.getSelf) === null || _ref22$getSelf === void 0 ? void 0 : _ref22$getSelf.call(_ref22)) === null || _getSelf4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf4, Foo, _x); + (_self7 = (_ref23 = (_fn10 = fn) === null || _fn10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn10().Foo, Foo, _self).getSelf()) === null || _ref23 === void 0 ? void 0 : _ref23.self) === null || _self7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self7, Foo, _x); + (_call$self2 = (_ref24 = (_fn11 = fn) === null || _fn11 === void 0 ? void 0 : (_classStaticPrivateFi4 = _classStaticPrivateFieldSpecGet(_fn11().Foo, Foo, _self)).getSelf) === null || _ref24 === void 0 ? void 0 : (_ref24$call = _ref24.call(_classStaticPrivateFi4)) === null || _ref24$call === void 0 ? void 0 : _ref24$call.self) === null || _call$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self2, Foo, _x); + (_getSelf$self3 = (_ref25 = (_fn12 = fn) === null || _fn12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn12().Foo, Foo, _self)) === null || _ref25 === void 0 ? void 0 : (_ref25$getSelf = _ref25.getSelf()) === null || _ref25$getSelf === void 0 ? void 0 : _ref25$getSelf.self) === null || _getSelf$self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self3, Foo, _x); + (_getSelf$self4 = (_ref26 = (_fn13 = fn) === null || _fn13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn13().Foo, Foo, _self)) === null || _ref26 === void 0 ? void 0 : (_ref26$getSelf = _ref26.getSelf) === null || _ref26$getSelf === void 0 ? void 0 : (_ref26$getSelf$call = _ref26$getSelf.call(_ref26)) === null || _ref26$getSelf$call === void 0 ? void 0 : _ref26$getSelf$call.self) === null || _getSelf$self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self4, Foo, _x); + } + +} + +var _x = { + writable: true, + value: 1 +}; +var _self = { + writable: true, + value: Foo +}; + +_defineProperty(Foo, "self", Foo); + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/exec.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/exec.js new file mode 100644 index 000000000000..0d627ac9e0d4 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/exec.js @@ -0,0 +1,125 @@ +class Foo { + static #x = 1; + 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; + } + + expect(Foo?.#x).toEqual(1); + expect(Foo?.#x.toString).toEqual(1..toString); + expect(Foo?.#x.toString()).toEqual('1'); + + expect(o?.Foo?.#x).toEqual(1); + expect(o?.Foo?.#x.toString).toEqual(1..toString); + expect(o?.Foo?.#x.toString()).toEqual('1'); + + expect(deep?.very.o?.Foo?.#x).toEqual(1); + expect(deep?.very.o?.Foo?.#x.toString).toEqual(1..toString); + expect(deep?.very.o?.Foo?.#x.toString()).toEqual('1'); + + expect(o?.Foo.#self?.#x).toEqual(1); + expect(o?.Foo.#self.self?.#x).toEqual(1); + expect(o?.Foo.#self?.self?.#x).toEqual(1); + expect(o?.Foo.#self.self?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.self?.self?.#x).toEqual(1); + + expect(o?.Foo.#self.getSelf()?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf()?.self?.#x).toEqual(1); + expect(o?.Foo.#self.getSelf?.()?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf()?.self?.#x).toEqual(1); + expect(o?.Foo.#self?.getSelf?.()?.self?.#x).toEqual(1); + + expect(fn?.().Foo?.#x).toEqual(1); + expect(fn?.().Foo?.#x.toString).toEqual(1..toString); + expect(fn?.().Foo?.#x.toString()).toEqual('1'); + + expect(fnDeep?.().very.o?.Foo?.#x).toEqual(1); + expect(fnDeep?.().very.o?.Foo?.#x.toString).toEqual(1..toString); + expect(fnDeep?.().very.o?.Foo?.#x.toString()).toEqual('1'); + + expect(fn?.().Foo.#self?.#x).toEqual(1); + expect(fn?.().Foo.#self.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self.self?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.self?.self?.#x).toEqual(1); + + expect(fn?.().Foo.#self.getSelf()?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf()?.self?.#x).toEqual(1); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#x).toEqual(1); + } + + static testNull() { + const o = null;; + const deep = { very: { o } }; + const fn = null; + function fnDeep() { + return deep; + } + + expect(o?.Foo?.#x).toEqual(undefined); + expect(o?.Foo?.#x.toString).toEqual(undefined); + expect(o?.Foo?.#x.toString()).toEqual(undefined); + + expect(deep?.very.o?.Foo?.#x).toEqual(undefined); + expect(deep?.very.o?.Foo?.#x.toString).toEqual(undefined); + expect(deep?.very.o?.Foo?.#x.toString()).toEqual(undefined); + + expect(o?.Foo.#self?.#x).toEqual(undefined); + expect(o?.Foo.#self.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self.self?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.self?.self?.#x).toEqual(undefined); + + expect(o?.Foo.#self.getSelf()?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self.getSelf?.()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf()?.self?.#x).toEqual(undefined); + expect(o?.Foo.#self?.getSelf?.()?.self?.#x).toEqual(undefined); + + expect(fn?.().Foo?.#x).toEqual(undefined); + expect(fn?.().Foo?.#x.toString).toEqual(undefined); + expect(fn?.().Foo?.#x.toString()).toEqual(undefined); + + expect(fnDeep?.().very.o?.Foo?.#x).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#x.toString).toEqual(undefined); + expect(fnDeep?.().very.o?.Foo?.#x.toString()).toEqual(undefined); + + expect(fn?.().Foo.#self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.self?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.self?.self?.#x).toEqual(undefined); + + expect(fn?.().Foo.#self.getSelf()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self.getSelf?.()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf()?.self?.#x).toEqual(undefined); + expect(fn?.().Foo.#self?.getSelf?.()?.self?.#x).toEqual(undefined); + } +} + +Foo.test(); +Foo.testNull(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/input.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/input.js new file mode 100644 index 000000000000..aecba329a27a --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/input.js @@ -0,0 +1,70 @@ +class Foo { + static #x = 1; + 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; + } + + Foo?.#x; + Foo?.#x.toString; + Foo?.#x.toString(); + + o?.Foo?.#x; + o?.Foo?.#x.toString; + o?.Foo?.#x.toString(); + + deep?.very.o?.Foo?.#x; + deep?.very.o?.Foo?.#x.toString; + deep?.very.o?.Foo?.#x.toString(); + + o?.Foo.#self?.#x; + o?.Foo.#self.self?.#x; + o?.Foo.#self?.self?.#x; + o?.Foo.#self.self?.self?.#x; + o?.Foo.#self?.self?.self?.#x; + + o?.Foo.#self.getSelf()?.#x; + o?.Foo.#self.getSelf?.()?.#x; + o?.Foo.#self?.getSelf()?.#x; + o?.Foo.#self?.getSelf?.()?.#x; + o?.Foo.#self.getSelf()?.self?.#x; + o?.Foo.#self.getSelf?.()?.self?.#x; + o?.Foo.#self?.getSelf()?.self?.#x; + o?.Foo.#self?.getSelf?.()?.self?.#x; + + fn?.().Foo?.#x; + fn?.().Foo?.#x.toString; + fn?.().Foo?.#x.toString(); + + fnDeep?.().very.o?.Foo?.#x; + fnDeep?.().very.o?.Foo?.#x.toString; + fnDeep?.().very.o?.Foo?.#x.toString(); + + fn?.().Foo.#self?.#x; + fn?.().Foo.#self.self?.#x; + fn?.().Foo.#self?.self?.#x; + fn?.().Foo.#self.self?.self?.#x; + fn?.().Foo.#self?.self?.self?.#x; + + fn?.().Foo.#self.getSelf()?.#x; + fn?.().Foo.#self.getSelf?.()?.#x; + fn?.().Foo.#self?.getSelf()?.#x; + fn?.().Foo.#self?.getSelf?.()?.#x; + fn?.().Foo.#self.getSelf()?.self?.#x; + fn?.().Foo.#self.getSelf?.()?.self?.#x; + fn?.().Foo.#self?.getSelf()?.self?.#x; + fn?.().Foo.#self?.getSelf?.()?.self?.#x; + + } +} + +Foo.test(); diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/options.json b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/options.json new file mode 100644 index 000000000000..19ed5174f545 --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/options.json @@ -0,0 +1,3 @@ +{ + "plugins": ["proposal-class-properties"] +} diff --git a/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/output.js b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/output.js new file mode 100644 index 000000000000..b095f6df78af --- /dev/null +++ b/packages/babel-plugin-proposal-class-properties/test/fixtures/private/optional-chain-optional-property/output.js @@ -0,0 +1,86 @@ +function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; } + +function _classStaticPrivateFieldSpecGet(receiver, classConstructor, descriptor) { if (receiver !== classConstructor) { throw new TypeError("Private static access of wrong provenance"); } if (descriptor.get) { return descriptor.get.call(receiver); } return descriptor.value; } + +class Foo { + static getSelf() { + return this; + } + + static test() { + var _Foo, _Foo2, _Foo3, _o$Foo, _o$Foo2, _o$Foo3, _deep$very$o$Foo, _deep$very$o$Foo2, _deep$very$o$Foo3, _o, _ref, _o2, _ref2, _o3, _self2, _o4, _self3, _o5, _self$self, _o6, _ref3, _o7, _classStaticPrivateFi, _call, _o8, _getSelf, _o9, _getSelf2, _o10, _self4, _o11, _classStaticPrivateFi2, _call$self, _o12, _getSelf$self, _o13, _getSelf$self2, _fn$Foo, _fn$Foo2, _fn$Foo3, _fnDeep$very$o$Foo, _fnDeep$very$o$Foo2, _fnDeep$very$o$Foo3, _fn, _ref4, _fn2, _ref5, _fn3, _self5, _fn4, _self6, _fn5, _self$self2, _fn6, _ref6, _fn7, _classStaticPrivateFi3, _call2, _fn8, _getSelf3, _fn9, _getSelf4, _fn10, _self7, _fn11, _classStaticPrivateFi4, _call$self2, _fn12, _getSelf$self3, _fn13, _getSelf$self4; + + const o = { + Foo: Foo + }; + const deep = { + very: { + o + } + }; + + function fn() { + return o; + } + + function fnDeep() { + return deep; + } + + (_Foo = Foo) === null || _Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo, Foo, _x); + (_Foo2 = Foo) === null || _Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo2, Foo, _x).toString; + (_Foo3 = Foo) === null || _Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_Foo3, Foo, _x).toString(); + (_o$Foo = o?.Foo) === null || _o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo, Foo, _x); + (_o$Foo2 = o?.Foo) === null || _o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo2, Foo, _x).toString; + (_o$Foo3 = o?.Foo) === null || _o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o$Foo3, Foo, _x).toString(); + (_deep$very$o$Foo = deep?.very.o?.Foo) === null || _deep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo, Foo, _x); + (_deep$very$o$Foo2 = deep?.very.o?.Foo) === null || _deep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo2, Foo, _x).toString; + (_deep$very$o$Foo3 = deep?.very.o?.Foo) === null || _deep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_deep$very$o$Foo3, Foo, _x).toString(); + (_ref = (_o = o) === null || _o === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o.Foo, Foo, _self)) === null || _ref === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref, Foo, _x); + (_ref2 = (_o2 = o) === null || _o2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o2.Foo, Foo, _self).self) === null || _ref2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref2, Foo, _x); + (_self2 = ((_o3 = o) === null || _o3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o3.Foo, Foo, _self))?.self) === null || _self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self2, Foo, _x); + (_self3 = ((_o4 = o) === null || _o4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o4.Foo, Foo, _self).self)?.self) === null || _self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self3, Foo, _x); + (_self$self = ((_o5 = o) === null || _o5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o5.Foo, Foo, _self))?.self?.self) === null || _self$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self, Foo, _x); + (_ref3 = (_o6 = o) === null || _o6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o6.Foo, Foo, _self).getSelf()) === null || _ref3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref3, Foo, _x); + (_call = ((_o7 = o) === null || _o7 === void 0 ? void 0 : (_classStaticPrivateFi = _classStaticPrivateFieldSpecGet(_o7.Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi)) === null || _call === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call, Foo, _x); + (_getSelf = ((_o8 = o) === null || _o8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o8.Foo, Foo, _self))?.getSelf()) === null || _getSelf === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf, Foo, _x); + (_getSelf2 = ((_o9 = o) === null || _o9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o9.Foo, Foo, _self))?.getSelf?.()) === null || _getSelf2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf2, Foo, _x); + (_self4 = ((_o10 = o) === null || _o10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o10.Foo, Foo, _self).getSelf())?.self) === null || _self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self4, Foo, _x); + (_call$self = ((_o11 = o) === null || _o11 === void 0 ? void 0 : (_classStaticPrivateFi2 = _classStaticPrivateFieldSpecGet(_o11.Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi2)?.self) === null || _call$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self, Foo, _x); + (_getSelf$self = ((_o12 = o) === null || _o12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o12.Foo, Foo, _self))?.getSelf()?.self) === null || _getSelf$self === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self, Foo, _x); + (_getSelf$self2 = ((_o13 = o) === null || _o13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_o13.Foo, Foo, _self))?.getSelf?.()?.self) === null || _getSelf$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self2, Foo, _x); + (_fn$Foo = fn?.().Foo) === null || _fn$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo, Foo, _x); + (_fn$Foo2 = fn?.().Foo) === null || _fn$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo2, Foo, _x).toString; + (_fn$Foo3 = fn?.().Foo) === null || _fn$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn$Foo3, Foo, _x).toString(); + (_fnDeep$very$o$Foo = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo, Foo, _x); + (_fnDeep$very$o$Foo2 = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo2, Foo, _x).toString; + (_fnDeep$very$o$Foo3 = fnDeep?.().very.o?.Foo) === null || _fnDeep$very$o$Foo3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fnDeep$very$o$Foo3, Foo, _x).toString(); + (_ref4 = (_fn = fn) === null || _fn === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn().Foo, Foo, _self)) === null || _ref4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref4, Foo, _x); + (_ref5 = (_fn2 = fn) === null || _fn2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn2().Foo, Foo, _self).self) === null || _ref5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref5, Foo, _x); + (_self5 = ((_fn3 = fn) === null || _fn3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn3().Foo, Foo, _self))?.self) === null || _self5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self5, Foo, _x); + (_self6 = ((_fn4 = fn) === null || _fn4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn4().Foo, Foo, _self).self)?.self) === null || _self6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self6, Foo, _x); + (_self$self2 = ((_fn5 = fn) === null || _fn5 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn5().Foo, Foo, _self))?.self?.self) === null || _self$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self$self2, Foo, _x); + (_ref6 = (_fn6 = fn) === null || _fn6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn6().Foo, Foo, _self).getSelf()) === null || _ref6 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_ref6, Foo, _x); + (_call2 = ((_fn7 = fn) === null || _fn7 === void 0 ? void 0 : (_classStaticPrivateFi3 = _classStaticPrivateFieldSpecGet(_fn7().Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi3)) === null || _call2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call2, Foo, _x); + (_getSelf3 = ((_fn8 = fn) === null || _fn8 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn8().Foo, Foo, _self))?.getSelf()) === null || _getSelf3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf3, Foo, _x); + (_getSelf4 = ((_fn9 = fn) === null || _fn9 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn9().Foo, Foo, _self))?.getSelf?.()) === null || _getSelf4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf4, Foo, _x); + (_self7 = ((_fn10 = fn) === null || _fn10 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn10().Foo, Foo, _self).getSelf())?.self) === null || _self7 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_self7, Foo, _x); + (_call$self2 = ((_fn11 = fn) === null || _fn11 === void 0 ? void 0 : (_classStaticPrivateFi4 = _classStaticPrivateFieldSpecGet(_fn11().Foo, Foo, _self)).getSelf)?.call(_classStaticPrivateFi4)?.self) === null || _call$self2 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_call$self2, Foo, _x); + (_getSelf$self3 = ((_fn12 = fn) === null || _fn12 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn12().Foo, Foo, _self))?.getSelf()?.self) === null || _getSelf$self3 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self3, Foo, _x); + (_getSelf$self4 = ((_fn13 = fn) === null || _fn13 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_fn13().Foo, Foo, _self))?.getSelf?.()?.self) === null || _getSelf$self4 === void 0 ? void 0 : _classStaticPrivateFieldSpecGet(_getSelf$self4, Foo, _x); + } + +} + +var _x = { + writable: true, + value: 1 +}; +var _self = { + writable: true, + value: Foo +}; + +_defineProperty(Foo, "self", Foo); + +Foo.test();