From b0f47ccc9976d44649d6b6e60ea4e1a36883d77a Mon Sep 17 00:00:00 2001 From: Victorien Elvinger Date: Sat, 23 Sep 2023 16:33:14 +0200 Subject: [PATCH] refactor(lint/useEnumInitializers): make code fix safe (#393) --- CHANGELOG.md | 1 + .../analyzers/style/use_enum_initializers.rs | 2 +- .../style/useEnumInitializers/invalid.ts | 20 +++ .../style/useEnumInitializers/invalid.ts.snap | 130 +++++++++++++++++- .../src/content/docs/internals/changelog.mdx | 1 + .../linter/rules/use-enum-initializers.md | 6 +- 6 files changed, 152 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 6d8c3ac484dd..758687b61bf5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -54,6 +54,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - [noUselessLabel](https://biomejs.dev/linter/rules/no-useless-label) - [noUselessTypeConstraint](https://biomejs.dev/linter/rules/no-useless-type-constraint) + - [useEnumInitializers](https://biomejs.dev/linter/rules/use-enum-initializers) #### Bug fixes diff --git a/crates/biome_js_analyze/src/analyzers/style/use_enum_initializers.rs b/crates/biome_js_analyze/src/analyzers/style/use_enum_initializers.rs index 8daf364768b5..3520ba3a97a1 100644 --- a/crates/biome_js_analyze/src/analyzers/style/use_enum_initializers.rs +++ b/crates/biome_js_analyze/src/analyzers/style/use_enum_initializers.rs @@ -179,7 +179,7 @@ impl Rule for UseEnumInitializers { if has_mutations { return Some(JsRuleAction { category: ActionCategory::QuickFix, - applicability: Applicability::MaybeIncorrect, + applicability: Applicability::Always, message: markup! { "Initialize all enum members." }.to_owned(), mutation, }); diff --git a/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts b/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts index 9708b512ce3d..5ddca1ab9919 100644 --- a/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts +++ b/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts @@ -45,3 +45,23 @@ export namespace A { } } } + +const RED = 0; +export enum RgbColor { + Red = RED, + Green, + Blue, +} + +export enum RgbColor2 { + Red = RED, + Green = 5, + Blue, +} + +const GREEN = 0; +export enum RgbColor3 { + Red, + Green = GREEN, + Blue, +} diff --git a/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts.snap b/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts.snap index 6f4ac2a4e9dd..c28af434af57 100644 --- a/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts.snap +++ b/crates/biome_js_analyze/tests/specs/style/useEnumInitializers/invalid.ts.snap @@ -52,6 +52,26 @@ export namespace A { } } +const RED = 0; +export enum RgbColor { + Red = RED, + Green, + Blue, +} + +export enum RgbColor2 { + Red = RED, + Green = 5, + Blue, +} + +const GREEN = 0; +export enum RgbColor3 { + Red, + Green = GREEN, + Blue, +} + ``` # Diagnostics @@ -84,7 +104,7 @@ invalid.ts:1:13 lint/style/useEnumInitializers FIXABLE ━━━━━━━ i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - i Suggested fix: Initialize all enum members. + i Safe fix: Initialize all enum members. 1 1 │ export enum Status { 2 │ - → Close, @@ -174,7 +194,7 @@ invalid.ts:14:13 lint/style/useEnumInitializers FIXABLE ━━━━━━━ i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - i Suggested fix: Initialize all enum members. + i Safe fix: Initialize all enum members. 13 13 │ 14 14 │ export enum Direction { @@ -215,7 +235,7 @@ invalid.ts:21:13 lint/style/useEnumInitializers FIXABLE ━━━━━━━ i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - i Suggested fix: Initialize all enum members. + i Safe fix: Initialize all enum members. 24 │ → Blue·=·"Blue", │ +++++++++ @@ -315,7 +335,7 @@ invalid.ts:42:21 lint/style/useEnumInitializers FIXABLE ━━━━━━━ i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - i Suggested fix: Initialize all enum members. + i Safe fix: Initialize all enum members. 41 41 │ export namespace B { 42 42 │ export enum Enum { @@ -329,4 +349,106 @@ invalid.ts:42:21 lint/style/useEnumInitializers FIXABLE ━━━━━━━ ``` +``` +invalid.ts:50:13 lint/style/useEnumInitializers ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! This enum declaration contains members that are implicitly initialized. + + 49 │ const RED = 0; + > 50 │ export enum RgbColor { + │ ^^^^^^^^ + 51 │ Red = RED, + 52 │ Green, + + i This enum member should be explicitly initialized. + + 50 │ export enum RgbColor { + 51 │ Red = RED, + > 52 │ Green, + │ ^^^^^ + 53 │ Blue, + 54 │ } + + i This enum member should be explicitly initialized. + + 51 │ Red = RED, + 52 │ Green, + > 53 │ Blue, + │ ^^^^ + 54 │ } + 55 │ + + i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. + + +``` + +``` +invalid.ts:56:13 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! This enum declaration contains members that are implicitly initialized. + + 54 │ } + 55 │ + > 56 │ export enum RgbColor2 { + │ ^^^^^^^^^ + 57 │ Red = RED, + 58 │ Green = 5, + + i This enum member should be explicitly initialized. + + 57 │ Red = RED, + 58 │ Green = 5, + > 59 │ Blue, + │ ^^^^ + 60 │ } + 61 │ + + i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. + + i Safe fix: Initialize all enum members. + + 59 │ → Blue·=·6, + │ ++++ + +``` + +``` +invalid.ts:63:13 lint/style/useEnumInitializers FIXABLE ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ + + ! This enum declaration contains members that are implicitly initialized. + + 62 │ const GREEN = 0; + > 63 │ export enum RgbColor3 { + │ ^^^^^^^^^ + 64 │ Red, + 65 │ Green = GREEN, + + i This enum member should be explicitly initialized. + + 62 │ const GREEN = 0; + 63 │ export enum RgbColor3 { + > 64 │ Red, + │ ^^^ + 65 │ Green = GREEN, + 66 │ Blue, + + i This enum member should be explicitly initialized. + + 64 │ Red, + 65 │ Green = GREEN, + > 66 │ Blue, + │ ^^^^ + 67 │ } + 68 │ + + i Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. + + i Safe fix: Initialize all enum members. + + 64 │ → Red·=·0, + │ ++++ + +``` + diff --git a/website/src/content/docs/internals/changelog.mdx b/website/src/content/docs/internals/changelog.mdx index e69bbe99b07b..7e0cd3ef7769 100644 --- a/website/src/content/docs/internals/changelog.mdx +++ b/website/src/content/docs/internals/changelog.mdx @@ -60,6 +60,7 @@ Read our [guidelines for writing a good changelog entry](https://github.com/biom - [noUselessLabel](https://biomejs.dev/linter/rules/no-useless-label) - [noUselessTypeConstraint](https://biomejs.dev/linter/rules/no-useless-type-constraint) + - [useEnumInitializers](https://biomejs.dev/linter/rules/use-enum-initializers) #### Bug fixes diff --git a/website/src/content/docs/linter/rules/use-enum-initializers.md b/website/src/content/docs/linter/rules/use-enum-initializers.md index 212bcb3fef44..111819312ddd 100644 --- a/website/src/content/docs/linter/rules/use-enum-initializers.md +++ b/website/src/content/docs/linter/rules/use-enum-initializers.md @@ -47,7 +47,7 @@ enum Version { Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - Suggested fix: Initialize all enum members. + Safe fix: Initialize all enum members. 2 │ ····V1·=·0, ++++ @@ -80,7 +80,7 @@ enum Status { Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - Suggested fix: Initialize all enum members. + Safe fix: Initialize all enum members. 3 │ ····Close·=·2, ++++ @@ -114,7 +114,7 @@ enum Color { Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time. - Suggested fix: Initialize all enum members. + Safe fix: Initialize all enum members. 4 │ ····Blue·=·"Blue", +++++++++