-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [no-duplicate-enum-values] add rule #4833
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
JoshuaKGoldberg
merged 5 commits into
typescript-eslint:main
from
aifreedom:no-duplicate-enum-members
Apr 27, 2022
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b3b4343
feat(eslint-plugin): create a new rule to disallow duplicate enum values
aifreedom 2b80aa8
fix(eslint-plugin): remove unused imported variable from no-duplicate…
aifreedom 74b9c2c
fix(eslint-plugin): test falsy values and fix some metadata
aifreedom 5484161
fix(eslint-plugin): make Enums in the falsy test valid
aifreedom 42515fd
Merge branch 'main' into no-duplicate-enum-members
JoshuaKGoldberg File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
51 changes: 51 additions & 0 deletions
51
packages/eslint-plugin/docs/rules/no-duplicate-enum-values.md
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
# `no-duplicate-enum-values` | ||
|
||
Disallow duplicate enum member values. | ||
|
||
Although TypeScript supports duplicate enum member values, people usually expect members to have unique values within the same enum. Duplicate values can lead to bugs that are hard to track down. | ||
|
||
## Rule Details | ||
|
||
This rule disallows defining an enum with multiple members initialized to the same value. Now it only enforces on enum members initialized with String or Number literals. Members without initializer or initialized with an expression are not checked by this rule. | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
enum E { | ||
A = 0, | ||
B = 0, | ||
} | ||
``` | ||
|
||
```ts | ||
enum E { | ||
A = 'A' | ||
B = 'A' | ||
} | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
enum E { | ||
A = 0, | ||
B = 1, | ||
} | ||
``` | ||
|
||
```ts | ||
enum E { | ||
A = 'A' | ||
B = 'B' | ||
} | ||
``` | ||
|
||
This rule is not configurable. | ||
|
||
## Attributes | ||
|
||
- [ ] ✅ Recommended | ||
- [ ] 🔧 Fixable | ||
- [ ] 💭 Requires type information |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
72 changes: 72 additions & 0 deletions
72
packages/eslint-plugin/src/rules/no-duplicate-enum-values.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
import * as util from '../util'; | ||
|
||
export default util.createRule({ | ||
name: 'no-duplicate-enum-values', | ||
meta: { | ||
type: 'problem', | ||
docs: { | ||
description: 'Disallow duplicate enum member values', | ||
recommended: false, | ||
}, | ||
hasSuggestions: true, | ||
messages: { | ||
duplicateValue: 'Duplicate enum member value {{value}}.', | ||
}, | ||
schema: [], | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
function isStringLiteral( | ||
node: TSESTree.Expression, | ||
): node is TSESTree.StringLiteral { | ||
return ( | ||
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'string' | ||
); | ||
} | ||
|
||
function isNumberLiteral( | ||
node: TSESTree.Expression, | ||
): node is TSESTree.NumberLiteral { | ||
return ( | ||
node.type === AST_NODE_TYPES.Literal && typeof node.value === 'number' | ||
); | ||
} | ||
|
||
return { | ||
TSEnumDeclaration(node: TSESTree.TSEnumDeclaration): void { | ||
const enumMembers = node.members; | ||
const seenValues = new Set<number | string>(); | ||
|
||
enumMembers.forEach(member => { | ||
if (member.initializer === undefined) { | ||
return; | ||
} | ||
|
||
let value: string | number | undefined; | ||
if (isStringLiteral(member.initializer)) { | ||
value = String(member.initializer.value); | ||
} else if (isNumberLiteral(member.initializer)) { | ||
value = Number(member.initializer.value); | ||
} | ||
|
||
if (value === undefined) { | ||
return; | ||
} | ||
|
||
if (seenValues.has(value)) { | ||
context.report({ | ||
node: member, | ||
messageId: 'duplicateValue', | ||
data: { | ||
value, | ||
}, | ||
}); | ||
} else { | ||
seenValues.add(value); | ||
} | ||
}); | ||
}, | ||
}; | ||
}, | ||
}); |
136 changes: 136 additions & 0 deletions
136
packages/eslint-plugin/tests/rules/no-duplicate-enum-values.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,136 @@ | ||
import rule from '../../src/rules/no-duplicate-enum-values'; | ||
import { RuleTester } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('no-duplicate-enum-values', rule, { | ||
valid: [ | ||
` | ||
enum E { | ||
A, | ||
B, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 1, | ||
B, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 1, | ||
B = 2, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 'A', | ||
B = 'B', | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 'A', | ||
B = 'B', | ||
C, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 'A', | ||
B = 'B', | ||
C = 2, | ||
D = 1 + 1, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 3, | ||
B = 2, | ||
C, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 'A', | ||
B = 'B', | ||
C = 2, | ||
D = foo(), | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = '', | ||
B = 0, | ||
} | ||
`, | ||
` | ||
enum E { | ||
A = 0, | ||
B = -0, | ||
C = NaN, | ||
} | ||
`, | ||
], | ||
invalid: [ | ||
{ | ||
code: ` | ||
enum E { | ||
A = 1, | ||
B = 1, | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
column: 3, | ||
messageId: 'duplicateValue', | ||
data: { value: 1 }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
enum E { | ||
A = 'A', | ||
B = 'A', | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
column: 3, | ||
messageId: 'duplicateValue', | ||
data: { value: 'A' }, | ||
}, | ||
], | ||
}, | ||
{ | ||
code: ` | ||
enum E { | ||
A = 'A', | ||
B = 'A', | ||
C = 1, | ||
D = 1, | ||
} | ||
`, | ||
errors: [ | ||
{ | ||
line: 4, | ||
column: 3, | ||
messageId: 'duplicateValue', | ||
data: { value: 'A' }, | ||
}, | ||
{ | ||
line: 6, | ||
column: 3, | ||
messageId: 'duplicateValue', | ||
data: { value: 1 }, | ||
}, | ||
], | ||
}, | ||
], | ||
JoshuaKGoldberg marked this conversation as resolved.
Show resolved
Hide resolved
|
||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[Nitpicking] Is there a need to separate these two functions out? They both check that
node.type === AST_NODE_TYPES.Literal
.I'd suggest either making a single separate function:
Just suggestions, feel free to ignore if you don't like either of these changes 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I had to do this because typescript doesn't know the type of
member.initializer
without the util functions, and would complain when I cast itsvalue
toString
orNumber
.