Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork2.8k
feat(eslint-plugin): [no-unsafe-enum-assignment] add rule#6091
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Closed
JoshuaKGoldberg wants to merge109 commits intotypescript-eslint:mainfromJoshuaKGoldberg:strict-enums
Uh oh!
There was an error while loading.Please reload this page.
Closed
Changes fromall commits
Commits
Show all changes
109 commits Select commitHold shift + click to select a range
481fb44
feat: adding enums to member-ordering rule
Zamiell5776a01
Merge branch 'main' into strict-enums
Zamiell094c90e
feat(eslint-plugin): [strict-enums] adding check for null/undefined
Zamiell311b1f8
feat(eslint-plugin): [strict-enums] refactoring tests to separate files
Zamiellac5e6d9
feat(eslint-plugin): [strict-enums] allowing more operators
Zamiell7d55248
fix(eslint-plugin): [strict-enums] adding union test
Zamiell74af3d1
fix(eslint-plugin): [strict-enums] refactoring + adding failing class
Zamiell1eb70ee
fix(eslint-plugin): [strict-enums] adding constraint code
Zamiellcf006fd
fix(eslint-plugin): [strict-enums] better eslint messages
Zamiell4d7f3fe
Merge branch 'main' into strict-enums
Zamiell2bbc2b7
fix(eslint-plugin): [strict-enums] removing vscode setting changes
Zamiell75c2252
Merge branch 'main' into strict-enums
Zamiell3976fdc
fix: changing function definition to reflect reality
Zamiell2406d03
fix: pass string enum literal into function that take string
Zamiell4244376
fix: allow passing enums to functions with any/unknown
Zamiell8b4dcdc
fix: using flags instead of names
Zamiell12f8120
fix: adding test that breaks the rule
Zamielle638718
fix: adding test for variadic functions
Zamiellaef971a
fix: adding isSymbolFlagSet internally
Zamiell75f60b9
fix: adding ignoring for remaining lint failures
Zamielld784aa0
fix: better comments
Zamiell811de98
fix: broken test
Zamiell9a71d45
fix: adding failing test for generic functions
Zamiellf93a02a
fix: refactoring tests + adding tests
Zamiell2ced9f8
fix: refactoring enum helper function locations
Zamiell0695c1b
fix: cleanup
Zamiell7272a95
fix: refactoring + fixing tests
Zamiell6ed1f66
fix: more tests
Zamiell83c70e3
fix: refactoring and making tests pass
Zamiell0ff3096
fix: adding array code, all tests pass now
Zamiellddebded
fix: adding failing test
Zamiellc8b239b
fix: allow empty arrays
Zamiell90981a9
fix: adding logic for arrays with no enums
Zamiell32dec63
fix: adding more tests
Zamielld0dbcbb
fix: fixing test
Zamiellc63c518
fix: fixing linter
Zamiellc5cf7f3
Merge branch 'main' into strict-enums
Zamiell1b26d74
Merge branch 'main' into strict-enums
Zamiell6e4f705
fix: reverting comment fixes
Zamiella0a2ee6
fix: removing refactor
Zamielldbae5db
fix: removing fixes to dot-notation
Zamiell16965d9
fix: removing semi refactor
Zamiell8b8f8f8
fix: removing jest logic
Zamiell5f437cc
fix: removing comparison operators check
Zamiellb1dbeb6
fix: adding failing test
Zamiell25caa05
fix: making test pass
Zamiellf42de61
fix: adding comment
Zamiella866ddd
fix: adding back in bitwise operator checks since apparently they are
Zamiell61cf107
fix: remove bad comment
Zamiell304d796
fix: removing unnecessary comments
Zamiell83c5f30
fix: remove types from error messages
Zamiell23ac918
fix: removing isArray + refactoring
Zamiell098d732
Merge branch 'main' into strict-enums
Zamiell99424e1
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell0de3b26
fix: removing strict-enums from recommended
Zamiell2d488cc
fix: simplify
Zamiell15dc3df
fix: undoing refactoring
Zamiella3d0122
fix: undoing refactoring
Zamiell5cd8fa7
fix: moving tests into subdirectory
Zamiella236085
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiellc357432
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell2395998
fix: adding failing test
Zamiell2851c3b
fix: making boolean tests pass
Zamiell149c049
Merge branch 'main' into strict-enums
Zamiellcf7c9b7
fix: refactor tests + fix linter
Zamiell3b2fa57
fix: adding brads tests
Zamiell6c43f71
fix: brads tests now pass
Zamiellaf7d5da
Update packages/eslint-plugin/docs/rules/strict-enums.md
Zamiella50f6e1
Merge branch 'main' into strict-enums
Zamiell85563be
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiellee96ce5
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell943bf9f
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell7679c4a
fix: make brads updates actually compile
Zamiell871e9ca
Update strict-enums.ts
Zamiellf861606
Merge branch 'main'
JoshuaKGoldbergcbe9e90
Continued fixing merge conflicts
JoshuaKGoldbergc37165c
Continued fixing the build
JoshuaKGoldberg3f37c67
Passing build
JoshuaKGoldbergc942261
Update packages/eslint-plugin/src/rules/strict-enums.ts
JoshuaKGoldbergb2947f9
Update packages/eslint-plugin/src/rules/strict-enums.ts
JoshuaKGoldberga7772d2
A few more reverts
JoshuaKGoldberg4204a60
Just a bit more changing typeFlagUtils
JoshuaKGoldberg89bcd73
Fixed strict-enums.md build
JoshuaKGoldberg4f13db4
Convert tests to not pushing
JoshuaKGoldberg4706e4f
Simplified the rule a whole bunch
JoshuaKGoldbergbe144e3
Add back getEnumNames
JoshuaKGoldberge4ac325
Even more trimming down
JoshuaKGoldberg62ccdb1
...and just a bit more
JoshuaKGoldbergda7501a
Undo some JSDoc changes
JoshuaKGoldberg83c33cb
Progress: no-unsafe-enum-assignment is tested
JoshuaKGoldberga3e6236
A bit more testing for assignments, as requested
JoshuaKGoldbergf1071fa
Finished testing and sharing
JoshuaKGoldberg680293a
Added comparison operators
JoshuaKGoldberg51bc2b1
Merge branch 'main'
JoshuaKGoldberga84a604
Removed no-unsafe-enum-comparison
JoshuaKGoldberg2dc7e2e
Merge branch 'main' into strict-enums
JoshuaKGoldberg2c5d7ee
Update packages/eslint-plugin/src/rules/no-unsafe-enum-assignment.ts
JoshuaKGoldberg7496653
Formatting
JoshuaKGoldberg3f59944
Performance optimization: AST_NODE_TYPES
JoshuaKGoldberg9666ae7
Abstract type a bit
JoshuaKGoldberg5a97f3c
Remove unreachable cases
JoshuaKGoldbergef43f39
Switch to [id.typeAnnotation]
JoshuaKGoldberg6ea1573
Oop, just a bit simpler
JoshuaKGoldberg0e57675
Switched to tsx for everything except Docusaurus
JoshuaKGoldberg8e3083a
lil lint fix
JoshuaKGoldberg2d020b8
pin ts-node to 10.7.0
JoshuaKGoldberg986f1ba
Merge branch 'main'
JoshuaKGoldberg576e048
WIP: will need to refactor
JoshuaKGoldberg1649a51
More failing test cases
JoshuaKGoldbergFile filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
62 changes: 62 additions & 0 deletionspackages/eslint-plugin/docs/rules/no-unsafe-enum-assignment.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,62 @@ | ||
--- | ||
description: 'Disallow providing non-enum values to enum typed locations.' | ||
--- | ||
> 🛑 This file is source code, not the primary documentation location! 🛑 | ||
> | ||
> See **https://typescript-eslint.io/rules/no-unsafe-enum-assignment** for documentation. | ||
The TypeScript compiler can be surprisingly lenient when working with enums. | ||
For example, it will allow you to assign any `number` value to a variable containing a numeric enum: | ||
```ts | ||
enum Fruit { | ||
Apple, | ||
Banana, | ||
} | ||
let fruit = Fruit.Apple; | ||
fruit = 999; // No error | ||
``` | ||
This rule flags when a `number` value is provided in a location that expects an enum type. | ||
<!--tabs--> | ||
### ❌ Incorrect | ||
```ts | ||
let fruit = Fruit.Apple; | ||
fruit++; | ||
``` | ||
```ts | ||
const fruit: Fruit = 0; | ||
``` | ||
```ts | ||
function useFruit(fruit: Fruit) {} | ||
useFruit(0); | ||
``` | ||
### ✅ Correct | ||
```ts | ||
let fruit = Fruit.Apple; | ||
fruit = Fruit.Banana; | ||
``` | ||
```ts | ||
const fruit: Fruit = Fruit.Apple; | ||
``` | ||
```ts | ||
function useFruit(fruit: Fruit) {} | ||
useFruit(Fruit.Apple); | ||
``` | ||
<!--/tabs--> | ||
## When Not to Use It | ||
If you use enums as shorthands for numbers and don't mind potentially unsafe `number`-typed values assigned to them, you likely don't need this rule. |
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/all.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
1 change: 1 addition & 0 deletionspackages/eslint-plugin/src/configs/strict.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
46 changes: 46 additions & 0 deletionspackages/eslint-plugin/src/rules/enum-utils/shared.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,46 @@ | ||
import * as tsutils from 'tsutils'; | ||
import * as ts from 'typescript'; | ||
import * as util from '../../util'; | ||
/* | ||
* If passed an enum member, returns the type of the parent. Otherwise, | ||
* returns itself. | ||
* | ||
* For example: | ||
* - `Fruit` --> `Fruit` | ||
* - `Fruit.Apple` --> `Fruit` | ||
*/ | ||
export function getBaseEnumType( | ||
typeChecker: ts.TypeChecker, | ||
type: ts.Type, | ||
): ts.Type { | ||
const symbol = type.getSymbol(); | ||
if ( | ||
!symbol?.valueDeclaration?.parent || | ||
!tsutils.isSymbolFlagSet(symbol, ts.SymbolFlags.EnumMember) | ||
) { | ||
return type; | ||
} | ||
return typeChecker.getTypeAtLocation(symbol.valueDeclaration.parent); | ||
} | ||
/** | ||
* A type can have 0 or more enum types. For example: | ||
* - 123 --> [] | ||
* - {} --> [] | ||
* - Fruit.Apple --> [Fruit] | ||
* - Fruit.Apple | Vegetable.Lettuce --> [Fruit, Vegetable] | ||
* - Fruit.Apple | Vegetable.Lettuce | 123 --> [Fruit, Vegetable] | ||
* - T extends Fruit --> [Fruit] | ||
*/ | ||
export function getEnumTypes( | ||
typeChecker: ts.TypeChecker, | ||
type: ts.Type, | ||
): ts.Type[] { | ||
return tsutils | ||
.unionTypeParts(type) | ||
.filter(subType => util.isTypeFlagSet(subType, ts.TypeFlags.EnumLike)) | ||
.map(type => getBaseEnumType(typeChecker, type)); | ||
} |
2 changes: 2 additions & 0 deletionspackages/eslint-plugin/src/rules/index.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
3 changes: 2 additions & 1 deletionpackages/eslint-plugin/src/rules/no-redeclare.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.
Uh oh!
There was an error while loading.Please reload this page.
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.