Skip to content

Commit fd837e1

Browse files
aspeddroclaude
andcommitted
Fix multiple consecutive block comments in switch expressions
When two or more block comments appeared after a switch_match body, the scanner emitted a NEWLINE between each pair (/* is not a continuation character). These extra NEWLINEs had nowhere to fit in the grammar, causing tree-sitter error recovery to produce a MISSING node. Fix by introducing _switch_body (same as _one_or_more_statements but without the trailing optional delimiter) and moving delimiter consumption into switch_expression and try_expression via repeat(seq(switch_match, repeat(_statement_delimeter))). Trailing NEWLINEs — including those between consecutive block comments — are now absorbed at the switch/try level instead of being orphaned. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent 33be075 commit fd837e1

4 files changed

Lines changed: 134650 additions & 134434 deletions

File tree

grammar.js

Lines changed: 24 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ module.exports = grammar({
100100
[$._record_pun_field, $._record_single_pun_field],
101101
[$._record_field_name, $.record_pattern],
102102
[$._statement, $._one_or_more_statements],
103+
[$._statement, $._switch_body],
103104
[$._inline_type, $.function_type_parameters],
104105
[$.primary_expression, $.parameter, $._pattern],
105106
[$.parameter, $._pattern],
@@ -134,6 +135,13 @@ module.exports = grammar({
134135
_one_or_more_statements: ($) =>
135136
seq(repeat($._statement), $.statement, optional($._statement_delimeter)),
136137

138+
// Like _one_or_more_statements but without a trailing delimiter.
139+
// Used as switch_match / try-catch bodies so that trailing NEWLINEs
140+
// (including those emitted between consecutive block comments) are
141+
// absorbed by the enclosing switch/try rule instead of being orphaned.
142+
_switch_body: ($) =>
143+
seq(repeat($._statement), $.statement),
144+
137145
statement: ($) =>
138146
choice(
139147
$.expression_statement,
@@ -589,7 +597,13 @@ module.exports = grammar({
589597
else_clause: ($) => seq("else", $.block),
590598

591599
switch_expression: ($) =>
592-
seq("switch", $.expression, "{", repeat($.switch_match), "}"),
600+
seq(
601+
"switch",
602+
$.expression,
603+
"{",
604+
repeat(seq($.switch_match, repeat($._statement_delimeter))),
605+
"}",
606+
),
593607

594608
switch_match: ($) =>
595609
prec.dynamic(
@@ -601,7 +615,7 @@ module.exports = grammar({
601615
"=>",
602616
field(
603617
"body",
604-
alias($._one_or_more_statements, $.sequence_expression),
618+
alias($._switch_body, $.sequence_expression),
605619
),
606620
),
607621
),
@@ -616,7 +630,14 @@ module.exports = grammar({
616630
seq($.variant_type_pattern, optional($.as_aliasing)),
617631

618632
try_expression: ($) =>
619-
seq("try", $.expression, "catch", "{", repeat($.switch_match), "}"),
633+
seq(
634+
"try",
635+
$.expression,
636+
"catch",
637+
"{",
638+
repeat(seq($.switch_match, repeat($._statement_delimeter))),
639+
"}",
640+
),
620641

621642
as_aliasing: ($) =>
622643
prec.left(seq("as", $._pattern, optional($.type_annotation))),

src/grammar.json

Lines changed: 49 additions & 5 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)