Commit 18eb0a7
fix: prevent ASI hazard in
* fix: prevent ASI hazard in `no-unused-labels` autofix
When removing an unused label whose body starts with `(`, `[`, `` ` ``,
`/`, `+`, or `-`, the autofix could silently change program semantics by
merging the body with the preceding statement via ASI.
e.g. `foo()\nLABEL: [1,2,3].forEach(x => x)` was incorrectly fixed to
`foo()\n[1,2,3].forEach(x => x)`, which parses as `foo()[1,2,3].forEach(...)`.
The fix skips autofixing in those cases when the token before the
LabeledStatement is not a safe ASI boundary (`;`, `{`, `}`, `:`).
* fix: remove } from safe ASI boundary tokens in no-unused-labels
} is not always a safe ASI boundary — e.g. a function expression ending with } can still be continued by [ on the next line:
const foo = function() {}
LABEL: [1, 2, 3].forEach(x => x)
Removing LABEL: here would produce `function() {}[1, 2, 3]` which is a member access, not two separate statements.
* Apply suggestion from @mdjermanovic
getFirstToken(node.body) always returns a token.
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update tests/lib/rules/no-unused-labels.js
This test was already being caught by the isStaticTemplateLiteral check above
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
* Update tests/lib/rules/no-unused-labels.js
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>
---------
Co-authored-by: Milos Djermanovic <milos.djermanovic@gmail.com>no-unused-labels autofix (#21173)1 parent 0a14800 commit 18eb0a7
2 files changed
Lines changed: 71 additions & 0 deletions
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
97 | 97 | | |
98 | 98 | | |
99 | 99 | | |
| 100 | + | |
| 101 | + | |
| 102 | + | |
| 103 | + | |
| 104 | + | |
| 105 | + | |
| 106 | + | |
| 107 | + | |
| 108 | + | |
| 109 | + | |
| 110 | + | |
| 111 | + | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
100 | 119 | | |
101 | 120 | | |
102 | 121 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
146 | 146 | | |
147 | 147 | | |
148 | 148 | | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
| 164 | + | |
| 165 | + | |
| 166 | + | |
| 167 | + | |
| 168 | + | |
| 169 | + | |
| 170 | + | |
| 171 | + | |
| 172 | + | |
| 173 | + | |
| 174 | + | |
| 175 | + | |
| 176 | + | |
| 177 | + | |
| 178 | + | |
| 179 | + | |
| 180 | + | |
| 181 | + | |
| 182 | + | |
| 183 | + | |
| 184 | + | |
| 185 | + | |
| 186 | + | |
| 187 | + | |
| 188 | + | |
| 189 | + | |
| 190 | + | |
| 191 | + | |
| 192 | + | |
| 193 | + | |
| 194 | + | |
| 195 | + | |
| 196 | + | |
| 197 | + | |
| 198 | + | |
| 199 | + | |
| 200 | + | |
149 | 201 | | |
150 | 202 | | |
151 | 203 | | |
| |||
0 commit comments