Repository navigation
Commit 9674aec
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
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1078 | 1078 | | |
1079 | 1079 | | |
1080 | 1080 | | |
1081 | | - | |
1082 | | - | |
1083 | | - | |
1084 | | - | |
1085 | | - | |
1086 | | - | |
1087 | 1081 | | |
1088 | 1082 | | |
1089 | | - | |
1090 | 1083 | | |
1091 | | - | |
1092 | | - | |
1093 | | - | |
1094 | | - | |
1095 | | - | |
1096 | | - | |
1097 | | - | |
1098 | | - | |
1099 | | - | |
1100 | | - | |
1101 | | - | |
1102 | | - | |
1103 | | - | |
1104 | | - | |
1105 | | - | |
1106 | | - | |
1107 | | - | |
1108 | | - | |
1109 | | - | |
1110 | | - | |
1111 | | - | |
1112 | | - | |
1113 | | - | |
1114 | | - | |
1115 | | - | |
1116 | | - | |
1117 | | - | |
1118 | | - | |
1119 | | - | |
1120 | | - | |
1121 | | - | |
1122 | | - | |
1123 | | - | |
1124 | | - | |
1125 | | - | |
1126 | | - | |
1127 | | - | |
1128 | | - | |
1129 | | - | |
1130 | | - | |
1131 | | - | |
1132 | | - | |
1133 | | - | |
1134 | | - | |
1135 | | - | |
1136 | | - | |
1137 | | - | |
1138 | | - | |
1139 | | - | |
1140 | | - | |
1141 | | - | |
1142 | | - | |
1143 | | - | |
1144 | | - | |
1145 | | - | |
1146 | | - | |
1147 | | - | |
1148 | | - | |
1149 | | - | |
1150 | | - | |
1151 | | - | |
1152 | | - | |
1153 | | - | |
1154 | | - | |
1155 | | - | |
1156 | | - | |
1157 | | - | |
| 1084 | + | |
| 1085 | + | |
| 1086 | + | |
| 1087 | + | |
| 1088 | + | |
| 1089 | + | |
| 1090 | + | |
| 1091 | + | |
| 1092 | + | |
| 1093 | + | |
| 1094 | + | |
1158 | 1095 | | |
1159 | | - | |
| 1096 | + | |
1160 | 1097 | | |
1161 | | - | |
1162 | | - | |
1163 | 1098 | | |
1164 | | - | |
1165 | | - | |
1166 | 1099 | | |
1167 | 1100 | | |
1168 | 1101 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
309 | 309 | | |
310 | 310 | | |
311 | 311 | | |
| 312 | + | |
| 313 | + | |
| 314 | + | |
| 315 | + | |
312 | 316 | | |
313 | 317 | | |
314 | 318 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
693 | 693 | | |
694 | 694 | | |
695 | 695 | | |
| 696 | + | |
| 697 | + | |
| 698 | + | |
| 699 | + | |
| 700 | + | |
| 701 | + | |
| 702 | + | |
| 703 | + | |
696 | 704 | | |
697 | 705 | | |
698 | 706 | | |
| |||
0 commit comments