Skip to content

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 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
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
109 commits
Select commit Hold shift + click to select a range
481fb44
feat: adding enums to member-ordering rule
Zamiell Apr 25, 2022
5776a01
Merge branch 'main' into strict-enums
Zamiell Apr 28, 2022
094c90e
feat(eslint-plugin): [strict-enums] adding check for null/undefined
Zamiell Apr 28, 2022
311b1f8
feat(eslint-plugin): [strict-enums] refactoring tests to separate files
Zamiell Apr 28, 2022
ac5e6d9
feat(eslint-plugin): [strict-enums] allowing more operators
Zamiell Apr 28, 2022
7d55248
fix(eslint-plugin): [strict-enums] adding union test
Zamiell Apr 29, 2022
74af3d1
fix(eslint-plugin): [strict-enums] refactoring + adding failing class
Zamiell Apr 29, 2022
1eb70ee
fix(eslint-plugin): [strict-enums] adding constraint code
Zamiell Apr 29, 2022
cf006fd
fix(eslint-plugin): [strict-enums] better eslint messages
Zamiell Apr 29, 2022
4d7f3fe
Merge branch 'main' into strict-enums
Zamiell Apr 29, 2022
2bbc2b7
fix(eslint-plugin): [strict-enums] removing vscode setting changes
Zamiell Apr 29, 2022
75c2252
Merge branch 'main' into strict-enums
Zamiell Apr 29, 2022
3976fdc
fix: changing function definition to reflect reality
Zamiell Apr 29, 2022
2406d03
fix: pass string enum literal into function that take string
Zamiell Apr 29, 2022
4244376
fix: allow passing enums to functions with any/unknown
Zamiell Apr 29, 2022
8b4dcdc
fix: using flags instead of names
Zamiell Apr 29, 2022
12f8120
fix: adding test that breaks the rule
Zamiell Apr 29, 2022
e638718
fix: adding test for variadic functions
Zamiell Apr 29, 2022
aef971a
fix: adding isSymbolFlagSet internally
Zamiell Apr 29, 2022
75f60b9
fix: adding ignoring for remaining lint failures
Zamiell Apr 30, 2022
d784aa0
fix: better comments
Zamiell Apr 30, 2022
811de98
fix: broken test
Zamiell Apr 30, 2022
9a71d45
fix: adding failing test for generic functions
Zamiell Apr 30, 2022
f93a02a
fix: refactoring tests + adding tests
Zamiell Apr 30, 2022
2ced9f8
fix: refactoring enum helper function locations
Zamiell Apr 30, 2022
0695c1b
fix: cleanup
Zamiell Apr 30, 2022
7272a95
fix: refactoring + fixing tests
Zamiell May 1, 2022
6ed1f66
fix: more tests
Zamiell May 1, 2022
83c70e3
fix: refactoring and making tests pass
Zamiell May 1, 2022
0ff3096
fix: adding array code, all tests pass now
Zamiell May 2, 2022
ddebded
fix: adding failing test
Zamiell May 2, 2022
c8b239b
fix: allow empty arrays
Zamiell May 2, 2022
90981a9
fix: adding logic for arrays with no enums
Zamiell May 2, 2022
32dec63
fix: adding more tests
Zamiell May 2, 2022
d0dbcbb
fix: fixing test
Zamiell May 2, 2022
c63c518
fix: fixing linter
Zamiell May 2, 2022
c5cf7f3
Merge branch 'main' into strict-enums
Zamiell May 2, 2022
1b26d74
Merge branch 'main' into strict-enums
Zamiell May 2, 2022
6e4f705
fix: reverting comment fixes
Zamiell May 3, 2022
a0a2ee6
fix: removing refactor
Zamiell May 3, 2022
dbae5db
fix: removing fixes to dot-notation
Zamiell May 3, 2022
16965d9
fix: removing semi refactor
Zamiell May 3, 2022
8b8f8f8
fix: removing jest logic
Zamiell May 3, 2022
5f437cc
fix: removing comparison operators check
Zamiell May 3, 2022
b1dbeb6
fix: adding failing test
Zamiell May 3, 2022
25caa05
fix: making test pass
Zamiell May 3, 2022
f42de61
fix: adding comment
Zamiell May 3, 2022
a866ddd
fix: adding back in bitwise operator checks since apparently they are
Zamiell May 3, 2022
61cf107
fix: remove bad comment
Zamiell May 3, 2022
304d796
fix: removing unnecessary comments
Zamiell May 3, 2022
83c5f30
fix: remove types from error messages
Zamiell May 3, 2022
23ac918
fix: removing isArray + refactoring
Zamiell May 4, 2022
098d732
Merge branch 'main' into strict-enums
Zamiell May 4, 2022
99424e1
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 4, 2022
0de3b26
fix: removing strict-enums from recommended
Zamiell May 4, 2022
2d488cc
fix: simplify
Zamiell May 5, 2022
15dc3df
fix: undoing refactoring
Zamiell May 6, 2022
a3d0122
fix: undoing refactoring
Zamiell May 6, 2022
5cd8fa7
fix: moving tests into subdirectory
Zamiell May 6, 2022
a236085
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 6, 2022
c357432
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 6, 2022
2395998
fix: adding failing test
Zamiell May 6, 2022
2851c3b
fix: making boolean tests pass
Zamiell May 6, 2022
149c049
Merge branch 'main' into strict-enums
Zamiell May 6, 2022
cf7c9b7
fix: refactor tests + fix linter
Zamiell May 6, 2022
3b2fa57
fix: adding brads tests
Zamiell May 6, 2022
6c43f71
fix: brads tests now pass
Zamiell May 6, 2022
af7d5da
Update packages/eslint-plugin/docs/rules/strict-enums.md
Zamiell May 7, 2022
a50f6e1
Merge branch 'main' into strict-enums
Zamiell May 10, 2022
85563be
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 30, 2022
ee96ce5
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 30, 2022
943bf9f
Update packages/eslint-plugin/src/rules/strict-enums.ts
Zamiell May 30, 2022
7679c4a
fix: make brads updates actually compile
Zamiell Jun 1, 2022
871e9ca
Update strict-enums.ts
Zamiell Jun 1, 2022
f861606
Merge branch 'main'
JoshuaKGoldberg Nov 25, 2022
cbe9e90
Continued fixing merge conflicts
JoshuaKGoldberg Nov 25, 2022
c37165c
Continued fixing the build
JoshuaKGoldberg Nov 25, 2022
3f37c67
Passing build
JoshuaKGoldberg Nov 25, 2022
c942261
Update packages/eslint-plugin/src/rules/strict-enums.ts
JoshuaKGoldberg Nov 25, 2022
b2947f9
Update packages/eslint-plugin/src/rules/strict-enums.ts
JoshuaKGoldberg Nov 25, 2022
a7772d2
A few more reverts
JoshuaKGoldberg Nov 25, 2022
4204a60
Just a bit more changing typeFlagUtils
JoshuaKGoldberg Nov 25, 2022
89bcd73
Fixed strict-enums.md build
JoshuaKGoldberg Nov 25, 2022
4f13db4
Convert tests to not pushing
JoshuaKGoldberg Nov 25, 2022
4706e4f
Simplified the rule a whole bunch
JoshuaKGoldberg Nov 25, 2022
be144e3
Add back getEnumNames
JoshuaKGoldberg Nov 25, 2022
e4ac325
Even more trimming down
JoshuaKGoldberg Nov 25, 2022
62ccdb1
...and just a bit more
JoshuaKGoldberg Nov 25, 2022
da7501a
Undo some JSDoc changes
JoshuaKGoldberg Nov 25, 2022
83c33cb
Progress: no-unsafe-enum-assignment is tested
JoshuaKGoldberg Nov 26, 2022
a3e6236
A bit more testing for assignments, as requested
JoshuaKGoldberg Nov 27, 2022
f1071fa
Finished testing and sharing
JoshuaKGoldberg Nov 27, 2022
680293a
Added comparison operators
JoshuaKGoldberg Nov 27, 2022
51bc2b1
Merge branch 'main'
JoshuaKGoldberg Nov 27, 2022
a84a604
Removed no-unsafe-enum-comparison
JoshuaKGoldberg Nov 27, 2022
2dc7e2e
Merge branch 'main' into strict-enums
JoshuaKGoldberg Dec 16, 2022
2c5d7ee
Update packages/eslint-plugin/src/rules/no-unsafe-enum-assignment.ts
JoshuaKGoldberg Dec 16, 2022
7496653
Formatting
JoshuaKGoldberg Dec 16, 2022
3f59944
Performance optimization: AST_NODE_TYPES
JoshuaKGoldberg Dec 16, 2022
9666ae7
Abstract type a bit
JoshuaKGoldberg Dec 16, 2022
5a97f3c
Remove unreachable cases
JoshuaKGoldberg Dec 16, 2022
ef43f39
Switch to [id.typeAnnotation]
JoshuaKGoldberg Dec 16, 2022
6ea1573
Oop, just a bit simpler
JoshuaKGoldberg Dec 16, 2022
0e57675
Switched to tsx for everything except Docusaurus
JoshuaKGoldberg Dec 16, 2022
8e3083a
lil lint fix
JoshuaKGoldberg Dec 16, 2022
2d020b8
pin ts-node to 10.7.0
JoshuaKGoldberg Dec 18, 2022
986f1ba
Merge branch 'main'
JoshuaKGoldberg Feb 4, 2023
576e048
WIP: will need to refactor
JoshuaKGoldberg Feb 4, 2023
1649a51
More failing test cases
JoshuaKGoldberg Feb 4, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions packages/eslint-plugin/docs/rules/no-unsafe-enum-assignment.md
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 deletions packages/eslint-plugin/src/configs/all.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,7 @@ export = {
'@typescript-eslint/no-unsafe-assignment': 'error',
'@typescript-eslint/no-unsafe-call': 'error',
'@typescript-eslint/no-unsafe-declaration-merging': 'error',
'@typescript-eslint/no-unsafe-enum-assignment': 'error',
'@typescript-eslint/no-unsafe-member-access': 'error',
'@typescript-eslint/no-unsafe-return': 'error',
'no-unused-expressions': 'off',
Expand Down
1 change: 1 addition & 0 deletions packages/eslint-plugin/src/configs/strict.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ export = {
'@typescript-eslint/no-unnecessary-condition': 'warn',
'@typescript-eslint/no-unnecessary-type-arguments': 'warn',
'@typescript-eslint/no-unsafe-declaration-merging': 'warn',
'@typescript-eslint/no-unsafe-enum-assignment': 'warn',
'no-useless-constructor': 'off',
'@typescript-eslint/no-useless-constructor': 'warn',
'@typescript-eslint/non-nullable-type-assertion-style': 'warn',
Expand Down
46 changes: 46 additions & 0 deletions packages/eslint-plugin/src/rules/enum-utils/shared.ts
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 deletions packages/eslint-plugin/src/rules/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ import noUnsafeArgument from './no-unsafe-argument';
import noUnsafeAssignment from './no-unsafe-assignment';
import noUnsafeCall from './no-unsafe-call';
import noUnsafeDeclarationMerging from './no-unsafe-declaration-merging';
import noUnsafeEnumAssignment from './no-unsafe-enum-assignment';
import noUnsafeMemberAccess from './no-unsafe-member-access';
import noUnsafeReturn from './no-unsafe-return';
import noUnusedExpressions from './no-unused-expressions';
Expand Down Expand Up @@ -214,6 +215,7 @@ export default {
'no-unsafe-assignment': noUnsafeAssignment,
'no-unsafe-call': noUnsafeCall,
'no-unsafe-declaration-merging': noUnsafeDeclarationMerging,
'no-unsafe-enum-assignment': noUnsafeEnumAssignment,
'no-unsafe-member-access': noUnsafeMemberAccess,
'no-unsafe-return': noUnsafeReturn,
'no-unused-expressions': noUnusedExpressions,
Expand Down
3 changes: 2 additions & 1 deletion packages/eslint-plugin/src/rules/no-redeclare.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { ScopeType } from '@typescript-eslint/scope-manager';
import type { TSESLint, TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';

Expand Down Expand Up @@ -256,7 +257,7 @@ export default util.createRule<Options, MessageIds>({

// Node.js or ES modules has a special scope.
if (
scope.type === 'global' &&
scope.type === ScopeType.global &&
scope.childScopes[0] &&
// The special scope's block is the Program node.
scope.block === scope.childScopes[0].block
Expand Down
Loading