Skip to content

Fix literal subscript expressions in array patterns in TypeScript. #485

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

bm-w
Copy link

@bm-w bm-w commented Apr 3, 2025

The Tree-sitter TypeScript grammar “overloads” the subscript_expression syntax node with use in LHS assignment patterns. This causes duplicate node & attribute creation in the stanza dealing with array_pattern syntax nodes, giving an error — e.g.:

0: Error executing statement node @pat.comp_index at (4275, 3)
   src/stack-graphs.tsg:4275:3:
   4275 |   node @pat.comp_index
        |   ^
   in stanza
   src/stack-graphs.tsg:4272:1:
   4272 | (array_pattern
        | ^
   matching (array_pattern) node
   test/statements/array-pattern.ts:2:1:
   2 | [items[0], items[1]] = [items[1], items[0]];
     | ^
 > and executing statement node @subscript_expr.comp_index at (3602, 3)
   src/stack-graphs.tsg:3602:3:
   3602 |   node @subscript_expr.comp_index
        |   ^
   in stanza
   src/stack-graphs.tsg:3598:1:
   3598 | (subscript_expression
        | ^
   matching (subscript_expression) node
   test/statements/array-pattern.ts:2:2:
   2 | [items[0], items[1]] = [items[1], items[0]];
     |  ^
1: Duplicate variable [syntax node subscript_expression (2, 2)].comp_index

A somewhat inelegant fix is to check whether the array pattern component is a subscript_expression with a number and if so avoid creating the node & attribute within the array_pattern stanza. Copious testing (on this repo’s TypeScript test suite and on all of microsoft/vscode) reveals no negative side effects.

@Copilot Copilot AI review requested due to automatic review settings April 3, 2025 18:51
@bm-w bm-w requested review from a team as code owners April 3, 2025 18:51
Copy link

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

This PR addresses an error in TypeScript's literal subscript expressions when used in array patterns by modifying how the Tree-sitter TypeScript grammar handles subscript expressions.

  • Fixes duplicate node and attribute creation for subscript expressions in array patterns.
  • Adds a new test case in the TypeScript test suite to validate the fix.
Files not reviewed (1)
  • languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg: Language not supported
Comments suppressed due to low confidence (1)

languages/tree-sitter-stack-graphs-typescript/test/statements/array-pattern.ts:1

  • [nitpick] Consider adding additional test cases to cover more complex scenarios or edge cases for subscript expressions in array patterns.
const items = [13, 37];

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

Successfully merging this pull request may close these issues.

1 participant