Skip to content

Commit 9674aec

Browse files
shuxiachaiclaudecamc314
authored
fix(linter): don't skip code between comments when checking for an ASI hazard (#29601)
`could_be_asi_hazard` decides whether a fix that starts with `[` or `(` needs a `;` in front. It calls `find_last_meaningful_char` to find the character before the statement, skipping whitespace and comments. When two comments are separated only by whitespace, that function jumps from the start of the later one to the start of the one before it, skipping the code in between: ```js const list = []; // Build the list list.push(0) // trailing note // then reset it new Array(1, 2).forEach((x) => console.log(x)); ``` `oxlint -A all -D eslint/no-array-constructor --fix` skips `list.push(0)`, finds nothing before it, and writes `[1, 2].forEach(...)` without a `;`. That parses as `list.push(0)[1, 2].forEach(...)`, so the fixer's syntax check doesn't catch it, and running the file throws a `TypeError` instead of printing `1` and `2`. With `unicorn/prefer-spread`, `Array.from(set)` in the same position becomes `[...set]`, which doesn't parse; debug builds stop at the fixer's "produced invalid syntax" assertion. It only takes one comment anywhere before, plus a trailing comment followed by a comment line above the statement. This PR rewrites `find_last_meaningful_char` as a loop that trims whitespace (ASCII and irregular, as before), then skips the comment that the text ends with, if any. The function is shared by `eslint/no-array-constructor`, `eslint/no-negated-condition`, `unicorn/no-negated-condition`, `unicorn/no-negation-in-equality-check`, `unicorn/no-new-array`, `unicorn/no-useless-undefined`, `unicorn/prefer-response-static-json` and `unicorn/prefer-spread`. The example is added as a fix case for `eslint/no-array-constructor` and `unicorn/prefer-spread`, plus a block-comment variant for `unicorn/prefer-spread`. Both rules' tests fail without this change. `cargo test --all-features -p oxc_linter` and `just lint -p oxc_linter` pass. --- I used AI assistance (Claude Code) to find this bug and write this change. I reviewed the code and ran the tests. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Additional review and comment cleanup used OpenAI Codex assistance. --------- Co-authored-by: Claude Opus 5.5 <[email protected]> Co-authored-by: Cameron <[email protected]>
1 parent 5e74d21 commit 9674aec

3 files changed

Lines changed: 24 additions & 79 deletions

File tree

‎crates/oxc_linter/src/ast_util.rs‎

Lines changed: 12 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,91 +1078,24 @@ pub fn could_be_asi_hazard(node: &AstNode, ctx: &LintContext) -> bool {
10781078
|| last_char == '$'
10791079
}
10801080

1081-
#[inline]
1082-
#[expect(clippy::cast_possible_wrap)]
1083-
fn is_utf8_char_boundary(b: u8) -> bool {
1084-
(b as i8) >= -0x40
1085-
}
1086-
10871081
/// Find the last meaningful (non-whitespace, non-comment) character before `end_pos`.
10881082
fn find_last_meaningful_char(source_text: &str, end_pos: u32, ctx: &LintContext) -> Option<char> {
1089-
let bytes = source_text.as_bytes();
10901083
let comments = ctx.semantic().comments();
1091-
1092-
let mut comment_idx = comments.partition_point(|c| c.span.start < end_pos);
1093-
let mut current_comment_end: u32 = 0;
1094-
let mut i = end_pos;
1095-
1096-
// Handle case where end_pos is inside a comment
1097-
if comment_idx > 0 {
1098-
let prev_comment = &comments[comment_idx - 1];
1099-
if end_pos <= prev_comment.span.end {
1100-
i = prev_comment.span.start;
1101-
comment_idx -= 1;
1102-
if comment_idx > 0 {
1103-
current_comment_end = comments[comment_idx - 1].span.end;
1104-
}
1105-
}
1106-
}
1107-
1108-
while i > 0 {
1109-
if i <= current_comment_end && comment_idx > 0 {
1110-
comment_idx -= 1;
1111-
current_comment_end = comments[comment_idx].span.start;
1112-
i = current_comment_end;
1113-
continue;
1114-
}
1115-
1116-
i -= 1;
1117-
1118-
let byte = bytes[i as usize];
1119-
1120-
if byte.is_ascii_whitespace() {
1121-
continue;
1122-
}
1123-
1124-
// Check if we're entering a comment from the end
1125-
if comment_idx > 0 {
1126-
let comment = &comments[comment_idx - 1];
1127-
if i >= comment.span.start && i < comment.span.end {
1128-
i = comment.span.start;
1129-
comment_idx -= 1;
1130-
if comment_idx > 0 {
1131-
current_comment_end = comments[comment_idx - 1].span.end;
1132-
}
1133-
continue;
1134-
}
1135-
}
1136-
1137-
if byte.is_ascii() {
1138-
return Some(byte as char);
1139-
}
1140-
1141-
// Multi-byte UTF-8: find the start byte (max 4 bytes per char)
1142-
let i_usize = i as usize;
1143-
let char_start = if is_utf8_char_boundary(bytes[i_usize.saturating_sub(1)]) {
1144-
i_usize - 1
1145-
} else if is_utf8_char_boundary(bytes[i_usize.saturating_sub(2)]) {
1146-
i_usize - 2
1147-
} else {
1148-
i_usize - 3
1149-
};
1150-
1151-
let c = source_text[char_start..].chars().next().unwrap();
1152-
1153-
// Skip irregular whitespace (NBSP, ZWNBSP, etc.)
1154-
if is_irregular_whitespace(c) {
1155-
#[expect(clippy::cast_possible_truncation)]
1156-
{
1157-
i = char_start as u32;
1084+
let mut idx = comments.partition_point(|c| c.span.start < end_pos);
1085+
let mut text = &source_text[..end_pos as usize];
1086+
loop {
1087+
text =
1088+
text.trim_end_matches(|c: char| c.is_ascii_whitespace() || is_irregular_whitespace(c));
1089+
#[expect(clippy::cast_possible_truncation)]
1090+
let text_end = text.len() as u32;
1091+
match idx.checked_sub(1).map(|i| &comments[i]) {
1092+
Some(comment) if comment.span.end >= text_end => {
1093+
text = &source_text[..comment.span.start as usize];
1094+
idx -= 1;
11581095
}
1159-
continue;
1096+
_ => return text.chars().next_back(),
11601097
}
1161-
1162-
return Some(c);
11631098
}
1164-
1165-
None
11661099
}
11671100

11681101
#[test]

‎crates/oxc_linter/src/rules/eslint/no_array_constructor.rs‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -309,6 +309,10 @@ fn test() {
309309
// [] // ";" not required
310310
// "#,
311311
// ),
312+
(
313+
"// leading\nfoo() // trailing\n// above\nnew Array(1, 2)",
314+
"// leading\nfoo() // trailing\n// above\n;[1, 2]",
315+
),
312316
];
313317

314318
Tester::new(NoArrayConstructor::NAME, NoArrayConstructor::PLUGIN, pass, fail)

‎crates/oxc_linter/src/rules/unicorn/prefer_spread.rs‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -693,6 +693,14 @@ str.split("")"#,
693693
"class A extends B { method() { return super.items.slice(); } }",
694694
"class A extends B { method() { return [...super.items]; } }",
695695
),
696+
(
697+
"// leading\nfoo() // trailing\n// above\nArray.from(set).map(() => {})",
698+
"// leading\nfoo() // trailing\n// above\n;[...set].map(() => {})",
699+
),
700+
(
701+
"foo(/* a */) /* b */ /* c */\nArray.from(set).map(() => {})",
702+
"foo(/* a */) /* b */ /* c */\n;[...set].map(() => {})",
703+
),
696704
];
697705

698706
Tester::new(PreferSpread::NAME, PreferSpread::PLUGIN, pass, fail)

0 commit comments

Comments
 (0)