Repository navigation
Fix: allow reserved words as property identifiers after optional chaining - #378
rbonestell wants to merge 3 commits into
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. |
|
Hi @rbonestell and @SegaraRai, Since this repository isn't actively maintained, I have set up a fork of it in the grammar-orchard, a space for collaborative maintenance of tree-sitter grammars. I have integrated your patch there. If you have any other PRs to submit, I would welcome them there and would be happy to onboard you as well. The fork is available on Crates.io, NPM, PyPI and as a GitHub repo as well. |
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 />.