Skip to content

Commit

Permalink
refactor(lint/useEnumInitializers): make code fix safe (#393)
Browse files Browse the repository at this point in the history
  • Loading branch information
Conaclos authored Sep 23, 2023
1 parent c8ff3d9 commit b0f47cc
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 8 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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",
│ +++++++++
Expand Down Expand Up @@ -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 {
Expand All @@ -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,
│ ++++
```


1 change: 1 addition & 0 deletions website/src/content/docs/internals/changelog.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ enum Version {

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.</span>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Suggested fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>
<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Safe fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>

<strong> </strong><strong> 2 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span>V1<span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">0</span>,
<strong> </strong><strong> │ </strong> <span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span>
Expand Down Expand Up @@ -80,7 +80,7 @@ enum Status {

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.</span>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Suggested fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>
<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Safe fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>

<strong> </strong><strong> 3 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span>Close<span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">2</span>,
<strong> </strong><strong> │ </strong> <span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span>
Expand Down Expand Up @@ -114,7 +114,7 @@ enum Color {

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Allowing implicit initializations for enum members can cause bugs if enum declarations are modified over time.</span>

<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Suggested fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>
<strong><span style="color: rgb(38, 148, 255);"> </span></strong><strong><span style="color: rgb(38, 148, 255);">ℹ</span></strong> <span style="color: rgb(38, 148, 255);">Safe fix</span><span style="color: rgb(38, 148, 255);">: </span><span style="color: rgb(38, 148, 255);">Initialize all enum members.</span>

<strong> </strong><strong> 4 │ </strong><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span><span style="opacity: 0.8;">·</span>Blue<span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">=</span><span style="opacity: 0.8;"><span style="color: MediumSeaGreen;">·</span></span><span style="color: MediumSeaGreen;">&quot;</span><span style="color: MediumSeaGreen;">B</span><span style="color: MediumSeaGreen;">l</span><span style="color: MediumSeaGreen;">u</span><span style="color: MediumSeaGreen;">e</span><span style="color: MediumSeaGreen;">&quot;</span>,
<strong> </strong><strong> │ </strong> <span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span><span style="color: MediumSeaGreen;">+</span>
Expand Down

0 comments on commit b0f47cc

Please sign in to comment.