-
Notifications
You must be signed in to change notification settings - Fork 849
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
Reuse of Identifier when parsing ObjectPattern with AssignmentPattern #928
Comments
Mutation of AST nodes wasn't really something we accounted for when writing this, but I agree it's a reasonable thing to do. I'd be happy with a pull request that copies the identifier node in this case. (It'd be a good idea to use a |
Counter-point: a plugin might want to put some properties only on one side of a shorthand expression (e.g. set |
Yeah, our plugin approach remains a wild, dangerous ride. I think adding a designated 'copyIdentifierNode` method for this would be overkill, since it's relatively unlikely for this to actually become a real problem. What do you think? |
I think for now we can just copy nodes as raw |
I don't care too much either way, really. |
Implemented in 85f4150. I did go for a by-property copy, since even estree's own type-annotation doc extends Identifier to add another property. |
It would be nice to describe it in the CHANGELOG.md |
Context: typescript-eslint/typescript-eslint#1686
cc @kaicataldo
When parsing an assignment within an object pattern, acorn reuses the identifier object for both the
Property.key
, and theAssignmentPattern.left
. Best shown by the example below.This is a problem because it means that any mutations applied to one node also apply to the other node.
For example, ESLint adds a
parent
property as it traverses the AST. This node reuse means that theparent
will be wrong for one of the two locations (whichever one is traversed first).It has exposed a subtle bug in ESLint's camelcase rule.
When the code is parsed by espree (and thus acorn), the rule does not fire an error, because it does not detect the correct lineage for the node.
However, when the code is parsed by a parser that does not reuse objects (like typescript-estree), the rule fires an error, because the parent pointers are correct.
Is this intentional? To me it seems like a bug in acorn, as I would assume that every AST node is a brand new object, no matter how similar to an existing node they might be. It seems like ESLint makes the same assumption as well.
The text was updated successfully, but these errors were encountered: