Skip to content

Commit 6565dbe

Browse files
committed
fix(linter/switch-case-braces): skip comments when searching for : token (#15425)
Related to #14833
1 parent 85bd19a commit 6565dbe

File tree

1 file changed

+19
-7
lines changed

1 file changed

+19
-7
lines changed

crates/oxc_linter/src/rules/unicorn/switch_case_braces.rs

Lines changed: 19 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -142,13 +142,11 @@ impl Rule for SwitchCaseBraces {
142142
};
143143

144144
if self.always_braces && missing_braces {
145-
let colon = u32::try_from(
146-
ctx.source_range(Span::new(case.span.start, case.consequent[0].span().start))
147-
.rfind(':')
148-
.unwrap(),
149-
)
150-
.unwrap();
151-
let span = Span::sized(case.span.start, colon + 1);
145+
let test_end = case.test.as_ref().map_or(case.span.start, |t| t.span().end);
146+
let colon_offset = ctx.find_next_token_from(test_end, ":").unwrap();
147+
let colon_pos = test_end + colon_offset;
148+
let span = Span::new(case.span.start, colon_pos + 1);
149+
152150
ctx.diagnostic_with_fix(
153151
switch_case_braces_diagnostic_missing_braces(span),
154152
|fixer| {
@@ -271,6 +269,20 @@ fn test() {
271269
"switch(something) { case 'scope:type': { doSomething();}}",
272270
None,
273271
),
272+
(
273+
"switch(something) {
274+
case 'scope:type':
275+
// HACK: this is a hack
276+
doSomething();
277+
}",
278+
"switch(something) {
279+
case 'scope:type': {
280+
// HACK: this is a hack
281+
doSomething();
282+
}
283+
}",
284+
None,
285+
),
274286
];
275287

276288
Tester::new(SwitchCaseBraces::NAME, SwitchCaseBraces::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)