diff --git a/packages/babel-plugin-proposal-optional-chaining/src/index.js b/packages/babel-plugin-proposal-optional-chaining/src/index.js index c3e324041520..918a59ade508 100644 --- a/packages/babel-plugin-proposal-optional-chaining/src/index.js +++ b/packages/babel-plugin-proposal-optional-chaining/src/index.js @@ -82,6 +82,8 @@ export default declare((api, options) => { let context = scope.maybeGenerateMemoised(object); if (context) { chain.object = t.assignmentExpression("=", context, object); + } else if (t.isSuper(object)) { + context = t.thisExpression(); } else { context = object; } diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/input.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/input.js new file mode 100644 index 000000000000..445c06ab259e --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/input.js @@ -0,0 +1,12 @@ +"use strict"; +class Base { + method() { + return 'Hello!'; + } +} + +class Derived extends Base { + method() { + return super.method?.() + } +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/options.json b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/options.json new file mode 100644 index 000000000000..39ea3f99c7ff --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/options.json @@ -0,0 +1,3 @@ +{ + "plugins": [["proposal-optional-chaining", { "loose": true }]] +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/output.js new file mode 100644 index 000000000000..6fd3890a61aa --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/output.js @@ -0,0 +1,15 @@ +"use strict"; + +class Base { + method() { + return 'Hello!'; + } + +} + +class Derived extends Base { + method() { + return super.method == null ? void 0 : super.method(); + } + +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/input.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/input.js new file mode 100644 index 000000000000..445c06ab259e --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/input.js @@ -0,0 +1,12 @@ +"use strict"; +class Base { + method() { + return 'Hello!'; + } +} + +class Derived extends Base { + method() { + return super.method?.() + } +} diff --git a/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/output.js b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/output.js new file mode 100644 index 000000000000..891da0508e27 --- /dev/null +++ b/packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/output.js @@ -0,0 +1,17 @@ +"use strict"; + +class Base { + method() { + return 'Hello!'; + } + +} + +class Derived extends Base { + method() { + var _super$method; + + return (_super$method = super.method) === null || _super$method === void 0 ? void 0 : _super$method.call(this); + } + +}