-
-
Notifications
You must be signed in to change notification settings - Fork 2.8k
feat(eslint-plugin): [strict-enums] new rule #4864
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
Closed
Closed
Changes from all commits
Commits
Show all changes
74 commits
Select commit
Hold shift + click to select a range
481fb44
feat: adding enums to member-ordering rule
Zamiell 5776a01
Merge branch 'main' into strict-enums
Zamiell 094c90e
feat(eslint-plugin): [strict-enums] adding check for null/undefined
Zamiell 311b1f8
feat(eslint-plugin): [strict-enums] refactoring tests to separate files
Zamiell ac5e6d9
feat(eslint-plugin): [strict-enums] allowing more operators
Zamiell 7d55248
fix(eslint-plugin): [strict-enums] adding union test
Zamiell 74af3d1
fix(eslint-plugin): [strict-enums] refactoring + adding failing class
Zamiell 1eb70ee
fix(eslint-plugin): [strict-enums] adding constraint code
Zamiell cf006fd
fix(eslint-plugin): [strict-enums] better eslint messages
Zamiell 4d7f3fe
Merge branch 'main' into strict-enums
Zamiell 2bbc2b7
fix(eslint-plugin): [strict-enums] removing vscode setting changes
Zamiell 75c2252
Merge branch 'main' into strict-enums
Zamiell 3976fdc
fix: changing function definition to reflect reality
Zamiell 2406d03
fix: pass string enum literal into function that take string
Zamiell 4244376
fix: allow passing enums to functions with any/unknown
Zamiell 8b4dcdc
fix: using flags instead of names
Zamiell 12f8120
fix: adding test that breaks the rule
Zamiell e638718
fix: adding test for variadic functions
Zamiell aef971a
fix: adding isSymbolFlagSet internally
Zamiell 75f60b9
fix: adding ignoring for remaining lint failures
Zamiell d784aa0
fix: better comments
Zamiell 811de98
fix: broken test
Zamiell 9a71d45
fix: adding failing test for generic functions
Zamiell f93a02a
fix: refactoring tests + adding tests
Zamiell 2ced9f8
fix: refactoring enum helper function locations
Zamiell 0695c1b
fix: cleanup
Zamiell 7272a95
fix: refactoring + fixing tests
Zamiell 6ed1f66
fix: more tests
Zamiell 83c70e3
fix: refactoring and making tests pass
Zamiell 0ff3096
fix: adding array code, all tests pass now
Zamiell ddebded
fix: adding failing test
Zamiell c8b239b
fix: allow empty arrays
Zamiell 90981a9
fix: adding logic for arrays with no enums
Zamiell 32dec63
fix: adding more tests
Zamiell d0dbcbb
fix: fixing test
Zamiell c63c518
fix: fixing linter
Zamiell c5cf7f3
Merge branch 'main' into strict-enums
Zamiell 1b26d74
Merge branch 'main' into strict-enums
Zamiell 6e4f705
fix: reverting comment fixes
Zamiell a0a2ee6
fix: removing refactor
Zamiell dbae5db
fix: removing fixes to dot-notation
Zamiell 16965d9
fix: removing semi refactor
Zamiell 8b8f8f8
fix: removing jest logic
Zamiell 5f437cc
fix: removing comparison operators check
Zamiell b1dbeb6
fix: adding failing test
Zamiell 25caa05
fix: making test pass
Zamiell f42de61
fix: adding comment
Zamiell a866ddd
fix: adding back in bitwise operator checks since apparently they are
Zamiell 61cf107
fix: remove bad comment
Zamiell 304d796
fix: removing unnecessary comments
Zamiell 83c5f30
fix: remove types from error messages
Zamiell 23ac918
fix: removing isArray + refactoring
Zamiell 098d732
Merge branch 'main' into strict-enums
Zamiell 99424e1
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell 0de3b26
fix: removing strict-enums from recommended
Zamiell 2d488cc
fix: simplify
Zamiell 15dc3df
fix: undoing refactoring
Zamiell a3d0122
fix: undoing refactoring
Zamiell 5cd8fa7
fix: moving tests into subdirectory
Zamiell a236085
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell c357432
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell 2395998
fix: adding failing test
Zamiell 2851c3b
fix: making boolean tests pass
Zamiell 149c049
Merge branch 'main' into strict-enums
Zamiell cf7c9b7
fix: refactor tests + fix linter
Zamiell 3b2fa57
fix: adding brads tests
Zamiell 6c43f71
fix: brads tests now pass
Zamiell af7d5da
Update packages/eslint-plugin/docs/rules/strict-enums.md
Zamiell a50f6e1
Merge branch 'main' into strict-enums
Zamiell 85563be
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell ee96ce5
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell 943bf9f
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell 7679c4a
fix: make brads updates actually compile
Zamiell 871e9ca
Update strict-enums.ts
Zamiell 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
# `strict-enums` | ||
|
||
Disallows the usage of unsafe enum patterns. | ||
|
||
## Rule Details | ||
|
||
Horrifyingly, the TypeScript compiler will allow you to set any number to a variable containing a number enum, like this: | ||
|
||
```ts | ||
enum Fruit { | ||
Apple, | ||
Banana, | ||
} | ||
|
||
let fruit = Fruit.Apple; | ||
fruit = 999; // No error | ||
``` | ||
|
||
This has resulted in many TypeScript programmers avoiding the use of enums altogether. Instead, they should use this rule, which bans working with enums in potentially unsafe ways. | ||
|
||
See the examples below for the types of patterns that are prevented. | ||
|
||
## Goals | ||
|
||
The goal of this rule is to make enums work like they do in other languages. One of the main benefits of enums is that they allow you to write code that is future-safe, because enums are supposed to be resilient to reorganization. If you arbitrarily change the values of an enum (or change the ordering of an enum with computed values), the idea is that nothing in your code-base should break. | ||
|
||
## Banned Patterns | ||
|
||
This rule bans: | ||
|
||
1. Enum incrementing/decrementing - `incorrectIncrement` | ||
1. Mismatched enum declarations/assignments - `mismatchedAssignment` | ||
1. Mismatched enum comparisons - `mismatchedComparison` | ||
1. Mismatched enum function arguments - `mismatchedFunctionArgument` | ||
|
||
<!--tabs--> | ||
|
||
### ❌ Incorrect | ||
|
||
```ts | ||
let fruit = Fruit.Apple; | ||
fruit++; | ||
``` | ||
|
||
```ts | ||
const fruit: Fruit = 0; | ||
``` | ||
|
||
```ts | ||
if (fruit === 0) { | ||
} | ||
if (vegetable === 'lettuce') { | ||
} | ||
``` | ||
|
||
```ts | ||
function useFruit(fruit: Fruit) {} | ||
useFruit(0); | ||
``` | ||
|
||
### ✅ Correct | ||
|
||
```ts | ||
let fruit = Fruit.Apple; | ||
fruit = Fruit.Banana; | ||
``` | ||
|
||
```ts | ||
const fruit = Fruit.Apple; | ||
``` | ||
|
||
```ts | ||
let fruit = Fruit.Apple; | ||
fruit = Fruit.Banana; | ||
``` | ||
|
||
```ts | ||
if (fruit === Fruit.Apple) { | ||
} | ||
if (vegetable === Vegetable.Lettuce) { | ||
} | ||
``` | ||
|
||
```ts | ||
function useFruit(fruit: Fruit) {} | ||
useFruit(Fruit.Apple); | ||
``` | ||
|
||
## Error Information | ||
|
||
- `incorrectIncrement` - You cannot increment or decrement an enum type. | ||
- Enums are supposed to be resilient to reorganization, so you should explicitly assign a new value instead. For example, if someone someone reassigned/reordered the values of the enum, then it could potentially break your code. | ||
- `mismatchedAssignment` - The type of the assignment does not match the declared enum type of the variable. | ||
- In other words, you are trying to assign a `Foo` enum value to a variable with a `Bar` type. Enums are supposed to be resilient to reorganization, so these kinds of assignments can be dangerous. | ||
- `mismatchedComparison` - The two things in the comparison do not have a shared enum type. | ||
- You might be trying to compare using a number literal, like `Foo.Value1 === 1`. Or, you might be trying to compare use a disparate enum type, like `Foo.Value1 === Bar.Value1`. Either way, you need to use a value that corresponds to the correct enum, like `foo === Foo.Value1`, where `foo` is type `Foo`. Enums are supposed to be resilient to reorganization, so these types of comparisons can be dangerous. | ||
- `mismatchedFunctionArgument` - The argument in the function call does not match the declared enum type of the function signature. | ||
- You might be trying to use a number literal, like `useFoo(1)`. Or, you might be trying to use a disparate enum type, like `useFoo(Bar.Value1)`. Either way, you need to use a value that corresponds to the correct enum, like `useFoo(Foo.Value1)`. Enums are supposed to be resilient to reorganization, so non-exact function calls like this can be dangerous. | ||
|
||
## Number Enums vs String Enums | ||
|
||
Surprisingly, the TypeScript compiler deals with string enums in a safer way than it does with number enums. If we duplicate the first example above by using a string enum, the TypeScript compiler will correctly throw an error: | ||
|
||
```ts | ||
enum Vegetable { | ||
Lettuce = 'lettuce', | ||
Carrot = 'carrot', | ||
} | ||
|
||
let vegetable = Vegetable.Lettuce; | ||
vegetable = 'definitelyNotAVegetable'; // Type '"definitelyNotAVegetable"' is not assignable to type 'Vegetable'. | ||
|
||
// Even "valid" strings will not work, which is good! | ||
vegetable = 'carrot'; // Type '"carrot"' is not assignable to type 'Vegetable'. | ||
``` | ||
|
||
Thus, the `strict-enums` rule is mostly concerned with throwing errors for misused number enums. However, it still prevents mismatched comparison, which slips by the TypeScript compiler even for string enums: | ||
|
||
```ts | ||
// Bad | ||
if (vegetable === 'lettuce') { | ||
// The TypeScript compiler allows this, but the `strict-enums` rule does not | ||
} | ||
|
||
// Good | ||
if (vegetable === Vegetable.Lettuce) { | ||
} | ||
``` | ||
|
||
## Comparison Operators | ||
|
||
Since it is a common pattern, this rule allows using greater than or less than to compare numeric enums, like this: | ||
|
||
```ts | ||
if (fruit > Fruit.Banana) { | ||
} | ||
``` | ||
|
||
This pattern allows you to select a subset of enums. However, it can lead to bugs when enum values are arbitrarily changed, because the subset will also change. The TypeScript compiler cannot warn you about this, so you should use this pattern with care. | ||
|
||
## Options | ||
|
||
No options are provided. | ||
|
||
## Attributes | ||
|
||
- [ ] ✅ Recommended | ||
- [ ] 🔧 Fixable | ||
- [x] 💭 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
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
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
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
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 |
---|---|---|
|
@@ -328,7 +328,7 @@ export default createRule<Options, MessageId>({ | |
if (isStrictNullChecks) { | ||
const UNDEFINED = ts.TypeFlags.Undefined; | ||
const NULL = ts.TypeFlags.Null; | ||
const isComparable = (type: ts.Type, flag: ts.TypeFlags): boolean => { | ||
const isComparable = (type: ts.Type, flag: number): boolean => { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change makes the function parameter less precise. I don't think we'd want that. See my comment in |
||
// Allow comparison to `any`, `unknown` or a naked type parameter. | ||
flag |= | ||
ts.TypeFlags.Any | | ||
|
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
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
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
2 changes: 1 addition & 1 deletion
2
packages/eslint-plugin/src/rules/sort-type-union-intersection-members.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
Oops, something went wrong.
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.
this is just a less-verbose duplicate of "Error Information" below - so let's remove this.