Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optional chaining on super.method() breaks #10673

Closed
jridgewell opened this issue Nov 7, 2019 · 5 comments · Fixed by #10694

Comments

@jridgewell
Copy link
Member

@jridgewell jridgewell commented Nov 7, 2019

Bug Report

Current Behavior
Optional call of a super method (super.method?.()) attempts to use super as an identifier, which is invalid. This might only happen in strict mode?

Input Code
REPL

class Base {
    method() { }
}

class Sub extends Base {
    method() {
        return super.method?.()
    }
}

Expected behavior/code
Super method must always be invoked as super.method(), not method.call(super)

Babel Configuration (.babelrc, package.json, cli command)

{
  "plugins": [ "@babel/plugin-proposal-optional-chaining" ]
}

Environment

  • Babel version(s): 7.7.2
  • Node/npm version: any
  • OS: any
  • Monorepo: any
  • How you are using Babel: REPL

Possible Solution

Two different possible solutions:

  1. Access super.method twice (Don't do this, see #10673 (comment))
  2. Call the cached method with this instead of super
Solution 1 (ignore this)
// Solution 1
// Input
super.method?.();

// Output
((_super$method = super.method) === null || _super$method === undefined)
  ? undefined
  : super.method();
// Solution 2
// Input
super.method?.();

// Output
((_super$method = super.method) === null || _super$method === undefined)
  ? undefined
  : _super$method.call(this)

Additional context/Screenshots

This should be a beginner level fix.

@nicolo-ribaudo

This comment has been minimized.

Copy link
Member

@nicolo-ribaudo nicolo-ribaudo commented Nov 7, 2019

Solution 1 is not correct if method is a getter.

@jridgewell

This comment has been minimized.

Copy link
Member Author

@jridgewell jridgewell commented Nov 7, 2019

You're right. We can go the second solution.

Are there any meta properties that can be methods? Eg new.target?.()? We might need a new issue to track those.

@jridgewell

This comment has been minimized.

Copy link
Member Author

@jridgewell jridgewell commented Nov 7, 2019

To make this appropriate for a Good First Issue, the needed changes are in

// Otherwise, we need to memoize the context object, and change the call into a Function#call.
// `a.?b.?()` translates roughly to `(_b = _a.b) != null && _b.call(_a)`
const { object } = chain;
let context = scope.maybeGenerateMemoised(object);
if (context) {
chain.object = t.assignmentExpression("=", context, object);
} else {
context = object;
}
node.arguments.unshift(t.cloneNode(context));
node.callee = t.memberExpression(
node.callee,
t.identifier("call"),
);

We need to check if the context is super, and if so, use this instead.

@Shriram-Balaji

This comment has been minimized.

Copy link
Contributor

@Shriram-Balaji Shriram-Balaji commented Nov 7, 2019

@jridgewell I'd like to take a stab at this.

@jridgewell

This comment has been minimized.

Copy link
Member Author

@jridgewell jridgewell commented Nov 7, 2019

Excellent! It's yours.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
4 participants
You can’t perform that action at this time.