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

Computed names for object getters and setters #2871

Open
nicolo-ribaudo opened this issue Feb 6, 2016 · 9 comments · May be fixed by #2993
Open

Computed names for object getters and setters #2871

nicolo-ribaudo opened this issue Feb 6, 2016 · 9 comments · May be fixed by #2993

Comments

@nicolo-ribaudo
Copy link
Contributor

var x = {
  get ["a"]() { return 1; }
};

JSHint throws Expected '(' and instead saw '['. and Expected an identifier and instead saw 'a'..

@sstern6
Copy link
Contributor

sstern6 commented Jul 18, 2016

@nicolo-ribaudo @jugglinmike would like to take this one once I have completed the PR for #2975. Is that ok with you two?

Thanks

@jugglinmike
Copy link
Member

Go for it!

@sstern6
Copy link
Contributor

sstern6 commented Jul 19, 2016

Thanks @jugglinmike was reading the MDN docs on getters and setters of computed names and saw this warning.... Note: Computed properties are experimental technology, part of the ECMAScript 6 proposal, and are not widely supported by browsers yet. This will trigger a syntax error in non-supporting environments. Is this a concern for how experimental this is? Should we provide a warning that summarizes this?

Also, this ideally would be run by a user with esversion: 6 at the top of the file correct? or would we do this for es5 and es6?

Thank you

@nicolo-ribaudo
Copy link
Contributor Author

nicolo-ribaudo commented Jul 19, 2016

This should always be parsed correctly, but if esversion < 6 JSHint should report W119

@sstern6
Copy link
Contributor

sstern6 commented Jul 19, 2016

Perfect, thanks, should have a PR open this week for it.

@sstern6
Copy link
Contributor

sstern6 commented Jul 28, 2016

@nicolo-ribaudo && @jugglinmike I believe I found a clean solution for the first part of the issue.
Inside of function functionparams I was noticing with the getter and setter, the function advanced was being called with a forced "(" value. This caused logic in the function advanced to throw the error Expected '(' and instead saw '['.. So what I did was inside of functionparams i created a second conditional on which values to call advance() with:

    if (!options || !options.parsedOpening) {
      if (state.tokens.curr.value === "get" || state.tokens.curr.value === "set") {
        advance(next.value);
      } else {
        advance("(");
      }
    }

I am now tackling the second error and part of the ticket for expected an identifier and instead saw 'a'.

Let me know if you have any feedback or suggestions and should have a PR open this weekend, thank you guys

@jugglinmike
Copy link
Member

I think your latest idea is closer to the solution, but still not quite there. As I see it, the problem is in the difference between how JSHint parses property names for ES2015 method definitions and accessor method definitions.

The code path for the former includes this logic:

  if (state.tokens.next.id === "[") {
    i = computedPropertyName();
    state.nameStack.set(i);
  } else {
    state.nameStack.set(state.tokens.next);
    i = propertyName();
    saveProperty(props, i, state.tokens.next);

    if (typeof i !== "string") {
      break;
    }
  }

...while the code path for the latter does not account for the possibility of a computed property:

  i = propertyName();

@sstern6
Copy link
Contributor

sstern6 commented Jul 31, 2016

Perfect thank you, will hopefully have a PR open for this and #2920 by monday.

@sstern6 sstern6 linked a pull request Aug 2, 2016 that will close this issue
@sstern6
Copy link
Contributor

sstern6 commented Aug 2, 2016

@jugglinmike PR Opened #2993

Let me know your thoughts, thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants