From 3409d5bdd3754f48faa91df44c78d48c0956b9e0 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Wed, 29 Nov 2023 10:46:03 +0100 Subject: [PATCH 1/3] fix(lint): useSimpleNumberKeys now ignores comments --- .../src/analyzers/complexity/use_simple_number_keys.rs | 3 ++- .../tests/specs/complexity/useSimpleNumberKeys/valid.js | 4 ++++ .../tests/specs/complexity/useSimpleNumberKeys/valid.js.snap | 5 ++++- 3 files changed, 10 insertions(+), 2 deletions(-) diff --git a/crates/biome_js_analyze/src/analyzers/complexity/use_simple_number_keys.rs b/crates/biome_js_analyze/src/analyzers/complexity/use_simple_number_keys.rs index 23ea3279fd87..f7ac11540257 100644 --- a/crates/biome_js_analyze/src/analyzers/complexity/use_simple_number_keys.rs +++ b/crates/biome_js_analyze/src/analyzers/complexity/use_simple_number_keys.rs @@ -1,4 +1,5 @@ use crate::JsRuleAction; + use biome_analyze::{ context::RuleContext, declare_rule, ActionCategory, Ast, FixKind, Rule, RuleDiagnostic, }; @@ -101,7 +102,7 @@ impl TryFrom for NumberLiteral { let token = literal_member_name.value().unwrap(); match token.kind() { JsSyntaxKind::JS_NUMBER_LITERAL | JsSyntaxKind::JS_BIGINT_LITERAL => { - let chars: Vec = token.to_string().chars().collect(); + let chars: Vec = token.text_trimmed().chars().collect(); let mut value = String::new(); let mut is_first_char_zero: bool = false; diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js index 9f18c80e5286..ba3870d403e6 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js @@ -3,5 +3,9 @@ ({ 1.2: "12" }); ({ 3.1e12: "12" }); ({ 0.1e12: "ee" }); +({ + // n + 20: "ee" +}); ({ a }); ({ ...a }); diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap index 571da5e61df4..656d23484e4b 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap @@ -1,6 +1,5 @@ --- source: crates/biome_js_analyze/tests/spec_tests.rs -assertion_line: 96 expression: valid.js --- # Input @@ -10,6 +9,10 @@ expression: valid.js ({ 1.2: "12" }); ({ 3.1e12: "12" }); ({ 0.1e12: "ee" }); +({ + // n + 20: "ee" +}); ({ a }); ({ ...a }); From 00ef2a606ff4bccc79c1644122f809bf674d8226 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Wed, 29 Nov 2023 11:07:51 +0100 Subject: [PATCH 2/3] Update changelog --- CHANGELOG.md | 2 ++ website/src/content/docs/internals/changelog.mdx | 2 ++ 2 files changed, 4 insertions(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index dfe487759783..40e2cfe8447d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -48,6 +48,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom #### Bug fixes +- Fix [#918](https://github.com/biomejs/biome/issues/918), [useSimpleNumberKeys](https://biomejs.dev/linter/rules/use-simple-number-keys) no longer repports false positive on comments. Contributed by @kalleep + ### Parser ## 1.4.0 (2023-11-27) diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index dcc48acc2cdc..84a64c86bc6d 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -54,6 +54,8 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom #### Bug fixes +- Fix [#918](https://github.com/biomejs/biome/issues/918), [useSimpleNumberKeys](https://biomejs.dev/linter/rules/use-simple-number-keys) no longer repports false positive on comments. Contributed by @kalleep + ### Parser ## 1.4.0 (2023-11-27) From ddaaf76c55c318936eac3bf104383ee9d4a8b943 Mon Sep 17 00:00:00 2001 From: Karl Persson Date: Wed, 29 Nov 2023 12:29:22 +0100 Subject: [PATCH 3/3] Add test for multiline hex key --- .../complexity/useSimpleNumberKeys/invalid.js | 3 + .../useSimpleNumberKeys/invalid.js.snap | 257 ++++++++++-------- .../complexity/useSimpleNumberKeys/valid.js | 2 +- .../useSimpleNumberKeys/valid.js.snap | 2 +- 4 files changed, 147 insertions(+), 117 deletions(-) diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js index b8b85e5dbdb6..6a80df51a2be 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js @@ -1,5 +1,8 @@ ({ 1n: 1 }); ({ 0x1: 1 }); +({ + 0x1: 1 +}); ({ 0o12: 1 }); ({ 0b1: 1 }); ({ 0o1: 1 }); diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js.snap b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js.snap index 747c68c75521..86a65374af40 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/invalid.js.snap @@ -6,6 +6,9 @@ expression: invalid.js ```js ({ 1n: 1 }); ({ 0x1: 1 }); +({ + 0x1: 1 +}); ({ 0o12: 1 }); ({ 0b1: 1 }); ({ 0o1: 1 }); @@ -28,14 +31,14 @@ invalid.js:1:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━ > 1 │ ({ 1n: 1 }); │ ^^ 2 │ ({ 0x1: 1 }); - 3 │ ({ 0o12: 1 }); + 3 │ ({ i Safe fix: Replace 1n with 1 1 │ - ({·1n:·1·}); 1 │ + ({·1:·1·}); 2 2 │ ({ 0x1: 1 }); - 3 3 │ ({ 0o12: 1 }); + 3 3 │ ({ ``` @@ -48,230 +51,254 @@ invalid.js:2:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━ 1 │ ({ 1n: 1 }); > 2 │ ({ 0x1: 1 }); │ ^^^ - 3 │ ({ 0o12: 1 }); - 4 │ ({ 0b1: 1 }); + 3 │ ({ + 4 │ 0x1: 1 i Safe fix: Replace 0x1 with 1 1 1 │ ({ 1n: 1 }); 2 │ - ({·0x1:·1·}); 2 │ + ({·1:·1·}); - 3 3 │ ({ 0o12: 1 }); - 4 4 │ ({ 0b1: 1 }); + 3 3 │ ({ + 4 4 │ 0x1: 1 ``` ``` -invalid.js:3:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:4:2 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ - ! Octal number literal is not allowed here. + ! Hexadecimal number literal is not allowed here. - 1 │ ({ 1n: 1 }); 2 │ ({ 0x1: 1 }); - > 3 │ ({ 0o12: 1 }); + 3 │ ({ + > 4 │ 0x1: 1 + │ ^^^ + 5 │ }); + 6 │ ({ 0o12: 1 }); + + i Safe fix: Replace 0x1 with 1 + + 2 2 │ ({ 0x1: 1 }); + 3 3 │ ({ + 4 │ - → 0x1:·1 + 4 │ + → 1:·1 + 5 5 │ }); + 6 6 │ ({ 0o12: 1 }); + + +``` + +``` +invalid.js:6:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! Octal number literal is not allowed here. + + 4 │ 0x1: 1 + 5 │ }); + > 6 │ ({ 0o12: 1 }); │ ^^^^ - 4 │ ({ 0b1: 1 }); - 5 │ ({ 0o1: 1 }); + 7 │ ({ 0b1: 1 }); + 8 │ ({ 0o1: 1 }); i Safe fix: Replace 0o12 with 9 - 1 1 │ ({ 1n: 1 }); - 2 2 │ ({ 0x1: 1 }); - 3 │ - ({·0o12:·1·}); - 3 │ + ({·9:·1·}); - 4 4 │ ({ 0b1: 1 }); - 5 5 │ ({ 0o1: 1 }); + 4 4 │ 0x1: 1 + 5 5 │ }); + 6 │ - ({·0o12:·1·}); + 6 │ + ({·9:·1·}); + 7 7 │ ({ 0b1: 1 }); + 8 8 │ ({ 0o1: 1 }); ``` ``` -invalid.js:4:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:7:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Binary number literal in is not allowed here. - 2 │ ({ 0x1: 1 }); - 3 │ ({ 0o12: 1 }); - > 4 │ ({ 0b1: 1 }); + 5 │ }); + 6 │ ({ 0o12: 1 }); + > 7 │ ({ 0b1: 1 }); │ ^^^ - 5 │ ({ 0o1: 1 }); - 6 │ ({ 1_0: 1 }); + 8 │ ({ 0o1: 1 }); + 9 │ ({ 1_0: 1 }); i Safe fix: Replace 0b1 with 1 - 2 2 │ ({ 0x1: 1 }); - 3 3 │ ({ 0o12: 1 }); - 4 │ - ({·0b1:·1·}); - 4 │ + ({·1:·1·}); - 5 5 │ ({ 0o1: 1 }); - 6 6 │ ({ 1_0: 1 }); + 5 5 │ }); + 6 6 │ ({ 0o12: 1 }); + 7 │ - ({·0b1:·1·}); + 7 │ + ({·1:·1·}); + 8 8 │ ({ 0o1: 1 }); + 9 9 │ ({ 1_0: 1 }); ``` ``` -invalid.js:5:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:8:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Octal number literal is not allowed here. - 3 │ ({ 0o12: 1 }); - 4 │ ({ 0b1: 1 }); - > 5 │ ({ 0o1: 1 }); - │ ^^^ - 6 │ ({ 1_0: 1 }); - 7 │ ({ 0.1e1_2: "ed" }); + 6 │ ({ 0o12: 1 }); + 7 │ ({ 0b1: 1 }); + > 8 │ ({ 0o1: 1 }); + │ ^^^ + 9 │ ({ 1_0: 1 }); + 10 │ ({ 0.1e1_2: "ed" }); i Safe fix: Replace 0o1 with 1 - 3 3 │ ({ 0o12: 1 }); - 4 4 │ ({ 0b1: 1 }); - 5 │ - ({·0o1:·1·}); - 5 │ + ({·1:·1·}); - 6 6 │ ({ 1_0: 1 }); - 7 7 │ ({ 0.1e1_2: "ed" }); + 6 6 │ ({ 0o12: 1 }); + 7 7 │ ({ 0b1: 1 }); + 8 │ - ({·0o1:·1·}); + 8 │ + ({·1:·1·}); + 9 9 │ ({ 1_0: 1 }); + 10 10 │ ({ 0.1e1_2: "ed" }); ``` ``` -invalid.js:6:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:9:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Number literal with underscore is not allowed here. - 4 │ ({ 0b1: 1 }); - 5 │ ({ 0o1: 1 }); - > 6 │ ({ 1_0: 1 }); - │ ^^^ - 7 │ ({ 0.1e1_2: "ed" }); - 8 │ ({ 11_1.11: "ee" }); + 7 │ ({ 0b1: 1 }); + 8 │ ({ 0o1: 1 }); + > 9 │ ({ 1_0: 1 }); + │ ^^^ + 10 │ ({ 0.1e1_2: "ed" }); + 11 │ ({ 11_1.11: "ee" }); i Safe fix: Replace 1_0 with 10 - 4 4 │ ({ 0b1: 1 }); - 5 5 │ ({ 0o1: 1 }); - 6 │ - ({·1_0:·1·}); - 6 │ + ({·10:·1·}); - 7 7 │ ({ 0.1e1_2: "ed" }); - 8 8 │ ({ 11_1.11: "ee" }); + 7 7 │ ({ 0b1: 1 }); + 8 8 │ ({ 0o1: 1 }); + 9 │ - ({·1_0:·1·}); + 9 │ + ({·10:·1·}); + 10 10 │ ({ 0.1e1_2: "ed" }); + 11 11 │ ({ 11_1.11: "ee" }); ``` ``` -invalid.js:7:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:10:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Number literal with underscore is not allowed here. - 5 │ ({ 0o1: 1 }); - 6 │ ({ 1_0: 1 }); - > 7 │ ({ 0.1e1_2: "ed" }); - │ ^^^^^^^ - 8 │ ({ 11_1.11: "ee" }); - 9 │ ({ 0x1() {} }); + 8 │ ({ 0o1: 1 }); + 9 │ ({ 1_0: 1 }); + > 10 │ ({ 0.1e1_2: "ed" }); + │ ^^^^^^^ + 11 │ ({ 11_1.11: "ee" }); + 12 │ ({ 0x1() {} }); i Safe fix: Replace 0.1e1_2 with .1e12 - 5 5 │ ({ 0o1: 1 }); - 6 6 │ ({ 1_0: 1 }); - 7 │ - ({·0.1e1_2:·"ed"·}); - 7 │ + ({·.1e12:·"ed"·}); - 8 8 │ ({ 11_1.11: "ee" }); - 9 9 │ ({ 0x1() {} }); + 8 8 │ ({ 0o1: 1 }); + 9 9 │ ({ 1_0: 1 }); + 10 │ - ({·0.1e1_2:·"ed"·}); + 10 │ + ({·.1e12:·"ed"·}); + 11 11 │ ({ 11_1.11: "ee" }); + 12 12 │ ({ 0x1() {} }); ``` ``` -invalid.js:8:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:11:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Number literal with underscore is not allowed here. - 6 │ ({ 1_0: 1 }); - 7 │ ({ 0.1e1_2: "ed" }); - > 8 │ ({ 11_1.11: "ee" }); + 9 │ ({ 1_0: 1 }); + 10 │ ({ 0.1e1_2: "ed" }); + > 11 │ ({ 11_1.11: "ee" }); │ ^^^^^^^ - 9 │ ({ 0x1() {} }); - 10 │ ({ [0x1]() {} }); + 12 │ ({ 0x1() {} }); + 13 │ ({ [0x1]() {} }); i Safe fix: Replace 11_1.11 with 111.11 - 6 6 │ ({ 1_0: 1 }); - 7 7 │ ({ 0.1e1_2: "ed" }); - 8 │ - ({·11_1.11:·"ee"·}); - 8 │ + ({·111.11:·"ee"·}); - 9 9 │ ({ 0x1() {} }); - 10 10 │ ({ [0x1]() {} }); + 9 9 │ ({ 1_0: 1 }); + 10 10 │ ({ 0.1e1_2: "ed" }); + 11 │ - ({·11_1.11:·"ee"·}); + 11 │ + ({·111.11:·"ee"·}); + 12 12 │ ({ 0x1() {} }); + 13 13 │ ({ [0x1]() {} }); ``` ``` -invalid.js:9:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:12:4 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Hexadecimal number literal is not allowed here. - 7 │ ({ 0.1e1_2: "ed" }); - 8 │ ({ 11_1.11: "ee" }); - > 9 │ ({ 0x1() {} }); + 10 │ ({ 0.1e1_2: "ed" }); + 11 │ ({ 11_1.11: "ee" }); + > 12 │ ({ 0x1() {} }); │ ^^^ - 10 │ ({ [0x1]() {} }); - 11 │ ({ get 0x1() { return this.a } }); + 13 │ ({ [0x1]() {} }); + 14 │ ({ get 0x1() { return this.a } }); i Safe fix: Replace 0x1 with 1 - 7 7 │ ({ 0.1e1_2: "ed" }); - 8 8 │ ({ 11_1.11: "ee" }); - 9 │ - ({·0x1()·{}·}); - 9 │ + ({·1()·{}·}); - 10 10 │ ({ [0x1]() {} }); - 11 11 │ ({ get 0x1() { return this.a } }); + 10 10 │ ({ 0.1e1_2: "ed" }); + 11 11 │ ({ 11_1.11: "ee" }); + 12 │ - ({·0x1()·{}·}); + 12 │ + ({·1()·{}·}); + 13 13 │ ({ [0x1]() {} }); + 14 14 │ ({ get 0x1() { return this.a } }); ``` ``` -invalid.js:11:8 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:14:8 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Hexadecimal number literal is not allowed here. - 9 │ ({ 0x1() {} }); - 10 │ ({ [0x1]() {} }); - > 11 │ ({ get 0x1() { return this.a } }); + 12 │ ({ 0x1() {} }); + 13 │ ({ [0x1]() {} }); + > 14 │ ({ get 0x1() { return this.a } }); │ ^^^ - 12 │ ({ set 0x1(a) { this.a = a } }); - 13 │ + 15 │ ({ set 0x1(a) { this.a = a } }); + 16 │ i Safe fix: Replace 0x1 with 1 - 9 9 │ ({ 0x1() {} }); - 10 10 │ ({ [0x1]() {} }); - 11 │ - ({·get·0x1()·{·return·this.a·}·}); - 11 │ + ({·get·1()·{·return·this.a·}·}); - 12 12 │ ({ set 0x1(a) { this.a = a } }); - 13 13 │ + 12 12 │ ({ 0x1() {} }); + 13 13 │ ({ [0x1]() {} }); + 14 │ - ({·get·0x1()·{·return·this.a·}·}); + 14 │ + ({·get·1()·{·return·this.a·}·}); + 15 15 │ ({ set 0x1(a) { this.a = a } }); + 16 16 │ ``` ``` -invalid.js:12:8 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ +invalid.js:15:8 lint/complexity/useSimpleNumberKeys FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ ! Hexadecimal number literal is not allowed here. - 10 │ ({ [0x1]() {} }); - 11 │ ({ get 0x1() { return this.a } }); - > 12 │ ({ set 0x1(a) { this.a = a } }); + 13 │ ({ [0x1]() {} }); + 14 │ ({ get 0x1() { return this.a } }); + > 15 │ ({ set 0x1(a) { this.a = a } }); │ ^^^ - 13 │ + 16 │ i Safe fix: Replace 0x1 with 1 - 10 10 │ ({ [0x1]() {} }); - 11 11 │ ({ get 0x1() { return this.a } }); - 12 │ - ({·set·0x1(a)·{·this.a·=·a·}·}); - 12 │ + ({·set·1(a)·{·this.a·=·a·}·}); - 13 13 │ + 13 13 │ ({ [0x1]() {} }); + 14 14 │ ({ get 0x1() { return this.a } }); + 15 │ - ({·set·0x1(a)·{·this.a·=·a·}·}); + 15 │ + ({·set·1(a)·{·this.a·=·a·}·}); + 16 16 │ ``` diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js index ba3870d403e6..115c069605ac 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js @@ -5,7 +5,7 @@ ({ 0.1e12: "ee" }); ({ // n - 20: "ee" + 20: "20" }); ({ a }); ({ ...a }); diff --git a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap index 656d23484e4b..6f471959968f 100644 --- a/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap +++ b/crates/biome_js_analyze/tests/specs/complexity/useSimpleNumberKeys/valid.js.snap @@ -11,7 +11,7 @@ expression: valid.js ({ 0.1e12: "ee" }); ({ // n - 20: "ee" + 20: "20" }); ({ a }); ({ ...a });