Skip to content

Commit 2d25bd8

Browse files
committed
feat(linter): remove unicorn/no-for-loop over typescript/prefer-for-of (#11354)
closes #11311 See the performance comment on that ticket, but tldr is we no longer suggest using `Array.prototype.entries()` to use the index AND the value, instead just write ``` for (let i = 0; i < arr.length; i += 1) { const item = arr[i] } ``` https://x.com/Cameron_C2/status/1927761085924282753
1 parent adbb3a3 commit 2d25bd8

File tree

5 files changed

+90
-510
lines changed

5 files changed

+90
-510
lines changed

crates/oxc_linter/src/rules.rs

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,6 @@ mod unicorn {
351351
pub mod no_console_spaces;
352352
pub mod no_document_cookie;
353353
pub mod no_empty_file;
354-
pub mod no_for_loop;
355354
pub mod no_hex_escape;
356355
pub mod no_instanceof_array;
357356
pub mod no_instanceof_builtins;
@@ -995,7 +994,6 @@ oxc_macros::declare_all_lint_rules! {
995994
unicorn::no_instanceof_builtins,
996995
unicorn::no_array_method_this_argument,
997996
unicorn::no_unnecessary_array_flat_depth,
998-
unicorn::no_for_loop,
999997
unicorn::no_unnecessary_slice_end,
1000998
unicorn::no_accessor_recursion,
1001999
unicorn::no_invalid_fetch_options,

crates/oxc_linter/src/rules/typescript/prefer_for_of.rs

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,39 @@ fn test() {
274274
"for (let i = 0; i < arr.length; i++) { ({ foo: arr[i] } = { foo: 1 }); }",
275275
"for (let i = 0; i < arr.length; i++) { arr[i]++; }",
276276
"function* gen() { for (let i = 0; i < this.length; ++i) { yield this[i]; } }",
277+
// subsection of eslint-plugin-unicotn test cases
278+
"for (;;);",
279+
"for (;;) {}",
280+
"for (a;; c) { d }",
281+
"for (a; b;) { d }",
282+
"for (the; love; of) { god }",
283+
"for ([a] = b; f(c); d--) { arr[d] }",
284+
"for (var a = b; c < arr.length; d++) { arr[e] }",
285+
"for (const x of xs) {}",
286+
"for (var j = 0; j < 10; j++) {}",
287+
"for (i = 0; i < arr.length; i++) { el = arr[i]; console.log(i, el); }",
288+
"for (let i = 0, j = 0; i < arr.length; i++) { const el = arr[i]; console.log(i, el); }",
289+
"for (let {i} = 0; i < arr.length; i++) { const el = arr[i]; console.log(i, el); }",
290+
"for (let i = 0; f(i, arr.length); i++) { const el = arr[i]; console.log(i, el); }",
291+
"for (let i = 0; i < arr.size; i++) { const el = arr[i]; console.log(i, el); }",
292+
"for (let i = 0; j < arr.length; i++) { const el = arr[i]; console.log(i, el); }",
293+
"for (let i = 0; i <= arr.length; i++) { const el = arr[i]; console.log(i, el); }",
294+
"for (let i = 0; arr.length > i;) { let el = arr[i]; console.log(i, el); }",
295+
"for (let i = 0; arr.length > i; i--) { let el = arr[i]; console.log(i, el); }",
296+
"for (let i = 0; arr.length > i; f(i)) { let el = arr[i]; console.log(i, el); }",
297+
"for (let i = 0; arr.length > i; i = f(i)) { let el = arr[i]; console.log(i, el); }",
298+
"const arr = []; for (let i = 0; arr.length > i; i ++);",
299+
"const arr = []; for (let i = 0; arr.length > i; i ++) console.log(NaN)",
300+
"const arr = []; for (let i = 0; i < arr.length; ++i) { const el = f(i); console.log(i, el); }",
301+
"const arr = []; for (let i = 0; i < arr.length; i++) { console.log(i); }",
302+
"const input = []; for (let i = 0; i < input.length; i++) { const el = input[i]; i++; console.log(i, el); }",
303+
"const input = []; for (let i = 0; i < input.length; i++) { const el = input[i]; i = 4; console.log(i, el); }",
304+
"const arr = []; for (let i = 0; i < arr.length; i++) { arr[i] = i + 2; }",
305+
"for (;;);",
306+
"for (;;) {}",
307+
"for (var j = 0; j < 10; j++) {}",
308+
"const arr = [];
309+
for (i = 0; i < arr.length; i++) { el = arr[i]; console.log(i, el); }",
277310
];
278311

279312
let fail = vec![
@@ -297,6 +330,14 @@ fn test() {
297330
"for (let i = 0; i < arr.length; i++) { ({ foo: obj[arr[i]] } = { foo: 1 }); }",
298331
"for (let i = 0; i < this.item.length; ++i) { this.item[i]; }",
299332
"function* gen() { for (let i = 0; i < this.array.length; ++i) { yield this.array[i]; } }",
333+
// subsection of eslint-plugin-unicorn test cases
334+
"const positions = []; for (let i = 0; i < positions.length; i++) { let last: vscode.Position | vscode.Range = positions[i]; }",
335+
"const arr = []; for (let i = 0; i < arr.length; i += 1) { console.log(arr[i]) }",
336+
"const plugins = []; for (let i = 0; i < plugins.length; i++) { let plugin = plugins[i]; plugin = calculateSomeNewValue(); }",
337+
"const array = []; for (let i = 0; i < array.length; i++) { var foo = array[i]; foo = bar(); }",
338+
"const array = []; for (let i = 0; i < array.length; i++) { let foo = array[i]; }",
339+
"const array = []; for (let i = 0; i < array.length; i++) { const foo = array[i]; }",
340+
"const array = []; for (let i = 0; i < array.length; i++) { var foo = array[i], bar = 1; }",
300341
];
301342

302343
Tester::new(PreferForOf::NAME, PreferForOf::PLUGIN, pass, fail).test_and_snapshot();

0 commit comments

Comments
 (0)