From 764d972b560d30f373740ba87e266bd9794c0fb2 Mon Sep 17 00:00:00 2001 From: Bastiaan Marinus van de Weerd Date: Thu, 3 Apr 2025 15:43:18 -0300 Subject: [PATCH] Fix literal subscript expressions in array patterns in TypeScript. --- .../src/stack-graphs.tsg | 19 ++++++++++++++++--- .../test/statements/array-pattern.ts | 2 ++ 2 files changed, 18 insertions(+), 3 deletions(-) create mode 100644 languages/tree-sitter-stack-graphs-typescript/test/statements/array-pattern.ts diff --git a/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg b/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg index 773abde7c..e7fbad310 100644 --- a/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg +++ b/languages/tree-sitter-stack-graphs-typescript/src/stack-graphs.tsg @@ -4270,9 +4270,20 @@ if none @is_async { } (array_pattern - (_)@pat + [ + (_) + (subscript_expression + index:(number)@subidx + ) + ]@pat )@array_pat { - node @pat.comp_index + ; There’s a `(subscript_expression … index:(number)@…)` stanza above that also + ; creates the `comp_index` and its `push_symbol` attribute. + let is_number_subscript = (not (is-null @subidx)) + + if (not is_number_subscript) { + node @pat.comp_index + } ; propagate lexical scope edge @pat.lexical_scope -> @array_pat.lexical_scope @@ -4280,7 +4291,9 @@ if none @is_async { ; propagate cotype components edge @pat.cotype -> @pat.comp_index ; - attr (@pat.comp_index) push_symbol = (named-child-index @pat) + if (not is_number_subscript) { + attr (@pat.comp_index) push_symbol = (named-child-index @pat) + } edge @pat.comp_index -> @array_pat.indexable ; propagate defs diff --git a/languages/tree-sitter-stack-graphs-typescript/test/statements/array-pattern.ts b/languages/tree-sitter-stack-graphs-typescript/test/statements/array-pattern.ts new file mode 100644 index 000000000..d9f0d8005 --- /dev/null +++ b/languages/tree-sitter-stack-graphs-typescript/test/statements/array-pattern.ts @@ -0,0 +1,2 @@ +const items = [13, 37]; +[items[0], items[1]] = [items[1], items[0]];