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

Better error message for using super when not using an object method #6716

Closed
babel-bot opened this Issue Nov 1, 2016 · 7 comments

Comments

Projects
None yet
4 participants
@babel-bot
Collaborator

babel-bot commented Nov 1, 2016

Issue originally reported by @hzoo in babel/babylon#212

Object.create({}, {
  foo: {
    get: function(){
      return super.foo;
    }
  }
})

errors with

repl: 'super' outside of function or class (4:13)
  2 |   foo: {
  3 |     get: function(){
> 4 |       return super.foo;
    |              ^
  5 |     }
  6 |   }
  7 | })

since it needs to be a get() {}

We should make a better error message for this?

cc @jasonkarns @Kovensky

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 1, 2016

Comment originally made by @Kovensky

Would be better to say "method or class constructor", as only those have a [[HomeObject]] (and thus access to super).

Right now it is inside a function, it's just a "free-standing" function expression that happens to be assigned as the value of an object's key.

Worse, if it did try to just use the object where the expression appears, its [[HomeObject]] would be the property descriptor itself 😄

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 1, 2016

Comment originally made by @hzoo

Yeah "method or class constructor" sounds good to me?

https://github.com/babel/babylon/blob/01e984fd0f21bb284943a508d2acc7c458aab13e/src/parser/expression.js#L379-L392

Also interesting is allowSuperOutsideMethod - we should update the readme since it says TODO https://github.com/babel/babylon/blob/master/README.md#options - not sure why you would want that

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 1, 2016

Comment originally made by @Kovensky

allowSuperOutsideMethod would probably only make sense if super was dynamic and worked like Object.getPrototypeOf(Object.getPrototypeOf(this)).

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Nov 1, 2016

Comment originally made by @jasonkarns

No suggestions on the error message, but I'll say that from my perspective, I'm not sure that method or class constructor would have been any more illuminating than the current message. It's absolutely precise, but also not all that helpful to the majority of users. (if they're like me)

It would probably take more work (since the error message would need to be contextual) but if going for the most helpful message, one could print exactly what needs changed to make it valid. (At least for function expressions used within property descriptors.)

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Oct 27, 2017

Comment originally made by @rajzshkr

I can work on this issue. Seems to be good for first contribution.

@babel-bot

This comment has been minimized.

Collaborator

babel-bot commented Oct 27, 2017

Comment originally made by @nicolo-ribaudo

@rajzshkr Go ahead!

@debugpai

This comment has been minimized.

Contributor

debugpai commented Feb 21, 2018

Looks like this issue needs to be closed

@lock lock bot added the outdated label May 23, 2018

@lock lock bot locked as resolved and limited conversation to collaborators May 23, 2018

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.