From 48850aecf9af61feb54d4c438183d4f578396b25 Mon Sep 17 00:00:00 2001 From: Shriram Balaji Date: Mon, 11 Nov 2019 01:17:39 +0530 Subject: [PATCH 1/4] Fix optional-chaining to work with super method call --- packages/babel-plugin-proposal-optional-chaining/src/index.js | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/packages/babel-plugin-proposal-optional-chaining/src/index.js b/packages/babel-plugin-proposal-optional-chaining/src/index.js index c3e324041520..0a6db9be99ad 100644 --- a/packages/babel-plugin-proposal-optional-chaining/src/index.js +++ b/packages/babel-plugin-proposal-optional-chaining/src/index.js @@ -86,6 +86,10 @@ export default declare((api, options) => { context = object; } + if (t.isSuper(context)) { + context = t.thisExpression(); + } + node.arguments.unshift(t.cloneNode(context)); node.callee = t.memberExpression( node.callee, From 76cb2010e350a1ef69966d92c0d706e6115efca3 Mon Sep 17 00:00:00 2001 From: Shriram Balaji Date: Mon, 11 Nov 2019 01:18:30 +0530 Subject: [PATCH 2/4] Add fixtures for optional-chaining super method call --- .../fixtures/general/super-method-call/input.js | 12 ++++++++++++ .../general/super-method-call/output.js | 17 +++++++++++++++++ 2 files changed, 29 insertions(+) create mode 100644 packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/input.js create mode 100644 packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call/output.js 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); + } + +} From 5f1210c9b17cd8cf27cd369f53acea6cbaf30fb8 Mon Sep 17 00:00:00 2001 From: Shriram Balaji Date: Mon, 11 Nov 2019 22:51:42 +0530 Subject: [PATCH 3/4] Added super-method-call-loose tests --- .../general/super-method-call-loose/input.js | 12 ++++++++++++ .../general/super-method-call-loose/options.json | 3 +++ .../general/super-method-call-loose/output.js | 15 +++++++++++++++ 3 files changed, 30 insertions(+) create mode 100644 packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/input.js create mode 100644 packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/options.json create mode 100644 packages/babel-plugin-proposal-optional-chaining/test/fixtures/general/super-method-call-loose/output.js 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(); + } + +} From ddc646642ebbac9bc69144f7ca840a4e85fa55d1 Mon Sep 17 00:00:00 2001 From: Shriram Balaji Date: Tue, 12 Nov 2019 15:35:26 +0000 Subject: [PATCH 4/4] Check for super context when not an AssignmentExpression --- .../babel-plugin-proposal-optional-chaining/src/index.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/packages/babel-plugin-proposal-optional-chaining/src/index.js b/packages/babel-plugin-proposal-optional-chaining/src/index.js index 0a6db9be99ad..918a59ade508 100644 --- a/packages/babel-plugin-proposal-optional-chaining/src/index.js +++ b/packages/babel-plugin-proposal-optional-chaining/src/index.js @@ -82,14 +82,12 @@ 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; } - if (t.isSuper(context)) { - context = t.thisExpression(); - } - node.arguments.unshift(t.cloneNode(context)); node.callee = t.memberExpression( node.callee,