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

Determining if the LHS of an AssignmentExpression is parenthesized #194

Open
TimothyGu opened this issue Dec 29, 2018 · 5 comments
Open

Comments

@TimothyGu
Copy link

TimothyGu commented Dec 29, 2018

Or, distinguishing between

fn = function () {};

and

(fn) = function () {};

Spec-wise, the first would have a left-hand side of PrimaryExpression : IdentifierReference, while the second would have PrimaryExpression : CoverParenthesizedExpressionAndArrowParameterList. This then leads to the IsIdentifierRef static semantic returning true for the first, and false for the second.

Through IsIdentifierRef, the distinction has real implications during run-time. The first would automatically name the newly created function using SetFunctionName to "fn", while the second would not. Indeed, this is tested in tc39/test262 language/expressions/assignment/fn-name-lhs-cover.js.

However as of now, ESTree does not distinguish between the two variants, making it impossible to write a compliant JavaScript engine with an ESTree AST alone. Is there any guidance one could follow when implementing this?

@TimothyGu
Copy link
Author

I should point out that V8 currently implements this wrongly, but ChakraCore and SpiderMonkey both do so correctly.

@mysticatea
Copy link
Contributor

Oh. Indeed, this sounds that ESTree should be updated.

@TimothyGu
Copy link
Author

For cowpath-paving information, Acorn provides a preserveParens option that fixes exactly this:

If this option is true, parenthesized expressions are represented by (non-standard) ParenthesizedExpression nodes that have a single expression property containing the expression inside parentheses.

It would be great to be able to standardize that.

@nzakas
Copy link
Contributor

nzakas commented Jan 2, 2019

I actually love the Acorn extension because I think it's incredibly useful. However, I believe that this proposal would be a breaking change, and we generally not accepted breaking changes because it would require every tool that currently produces ESTree to change along with it. For instance, ESLint supports a number of ESTree-based parsers and it would be difficult to know if we could rely on a ParenthesizedExpression to be present or not. (Further discussion on breaking changes is in #65.)

You can definitely copy what Acorn does (or just use Acorn) if you want to create an implementation that has this extension, but I'm not sure it can be part of ESTree formally without a lot of coordination and effort.

@RReverser
Copy link
Member

I wonder if it would be possible instead to define this as a boolean field in contexts where it matters in runtime (like Identifier in example above). Then it would become a backward-compatible extension.

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

No branches or pull requests

4 participants