Fix: allow reserved words as property identifiers after optional chaining#378
Open
rbonestell wants to merge 3 commits intotree-sitter:masterfrom
Open
Fix: allow reserved words as property identifiers after optional chaining#378rbonestell wants to merge 3 commits intotree-sitter:masterfrom
rbonestell wants to merge 3 commits intotree-sitter:masterfrom
Conversation
…ning Reserved words like 'delete', 'class', 'return' were producing ERROR nodes when used as property identifiers after '?.' (optional chaining), while working correctly after '.'. This adds a '_keyword_identifier' rule that explicitly lists all reserved words as valid alternatives in the member_expression property field, working around a tree-sitter core issue where the 'reserved()' context wasn't propagated through the '?.' token path. Also extracts RESERVED_WORDS and CONTEXTUAL_KEYWORDS as shared constants to eliminate keyword list duplication across reserved.global, _reserved_identifier, and _keyword_identifier. Fixes tree-sitter#377
Add tree-sitter to devDependencies (required by bindings/node/binding_test.js) and pin node-version to 22 (tree-sitter@0.25.0 cannot compile with Node 24).
|
Thanks for putting this together. I tested this branch locally and noticed a follow-up issue on the JSX side: For example, cases like these no longer parse correctly in JSX:
I put together a small follow-up fix on top of your branch here: Please feel free to reuse or cherry-pick that commit if it is useful. I validated this locally against the affected JSX corpus cases, but I did not run the full CI matrix. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #377.
Reserved words (
delete,class,return, etc.) are correctly treated asproperty_identifiernodes after.but produceERRORnodes after?.(optional chaining). This is because thereserved('properties', ...)context is not propagated through the?.token path — likely a tree-sitter core issue.This PR works around it by explicitly listing all reserved words as valid alternatives in the
member_expressionproperty field via a new_keyword_identifierrule.Additionally, this PR fixes JSX parsing of reserved/contextual keywords as tag names, attribute names, and member expression properties (a pre-existing issue). Credit to @SegaraRai for identifying and implementing the JSX fix (commit).
Before:
After:
Changes
RESERVED_WORDSandCONTEXTUAL_KEYWORDSas shared constants to eliminate duplication acrossreserved.global,_reserved_identifier, and the new_keyword_identifierrule. Add_keyword_identifier(inlined) to themember_expressionproperty field alongside$.identifierinside the existingreserved('properties', ...)wrapper. Addjsx_keyword_identifierandjsx_member_expressionrules to support reserved words in JSX contexts.tree-sittertodevDependencies(required bybindings/node/binding_test.js).node-version: 22for Node binding tests (tree-sitter@0.25.0cannot compile with Node 24).tree-sitter generate.Acknowledgments
Thanks to @SegaraRai for testing this branch and contributing the JSX reserved word fix (#378 comment).
Notes
reserved('properties', ...)wrapper is retained for the.path and forward compatibility — when tree-sitter core fixes the propagation issue, the explicit alternatives become redundant but harmless.jsx_member_expressionrule (separate fromnested_identifier) so that_jsx_identifier(which includes reserved words) is used for property resolution in JSX member expressions like<Foo.class />.