Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(eslint-plugin): add
no-unnecessary-type-constraint
rule (#2516)
* feat(eslint-plugin): add no-unnecessary-type-constraint rule * Inlined report * Improved message and removed type checker * Stop testing with type info
- Loading branch information
Josh Goldberg
committed
Oct 26, 2020
1 parent
ef8b5a7
commit 880ac75
Showing
6 changed files
with
392 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
55 changes: 55 additions & 0 deletions
55
packages/eslint-plugin/docs/rules/no-unnecessary-type-constraint.md
This file contains 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,55 @@ | ||
# Disallows unnecessary constraints on generic types (`no-unnecessary-type-constraint`) | ||
|
||
## Rule Details | ||
|
||
Type parameters (`<T>`) may be "constrained" with an `extends` keyword ([docs](https://www.typescriptlang.org/docs/handbook/generics.html#generic-constraints)). | ||
When not provided, type parameters happen to default to: | ||
|
||
- As of TypeScript 3.9: `unknown` ([docs](https://www.typescriptlang.org/docs/handbook/release-notes/typescript-3-9.html#type-parameters-that-extend-any-no-longer-act-as-any)) | ||
- Before that, as of 3.5: `any` ([docs](https://devblogs.microsoft.com/typescript/announcing-typescript-3-5/#breaking-changes)) | ||
|
||
It is therefore redundant to `extend` from these types in later versions of TypeScript. | ||
|
||
Examples of **incorrect** code for this rule: | ||
|
||
```ts | ||
interface FooAny<T extends any> {} | ||
interface FooUnknown<T extends unknown> {} | ||
|
||
type BarAny<T extends any> = {}; | ||
type BarUnknown<T extends unknown> = {}; | ||
|
||
class BazAny<T extends any> { | ||
quxUnknown<U extends unknown>() {} | ||
} | ||
|
||
class BazUnknown<T extends unknown> { | ||
quxUnknown<U extends unknown>() {} | ||
} | ||
|
||
const QuuxAny = <T extends any>() => {}; | ||
const QuuxUnknown = <T extends unknown>() => {}; | ||
|
||
function QuuzAny<T extends any>() {} | ||
function QuuzUnknown<T extends unknown>() {} | ||
``` | ||
|
||
Examples of **correct** code for this rule: | ||
|
||
```ts | ||
interface Foo<T> {} | ||
|
||
type Bar<T> = {}; | ||
|
||
class Baz<T> { | ||
qux<U> { } | ||
} | ||
|
||
const Quux = <T>() => {}; | ||
|
||
function Quuz<T>() {} | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
If you don't care about the specific styles of your type constraints, or never use them in the first place, then you will not need this rule. |
This file contains 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 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
103 changes: 103 additions & 0 deletions
103
packages/eslint-plugin/src/rules/no-unnecessary-type-constraint.ts
This file contains 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,103 @@ | ||
import { | ||
AST_NODE_TYPES, | ||
TSESTree, | ||
} from '@typescript-eslint/experimental-utils'; | ||
import * as semver from 'semver'; | ||
import * as ts from 'typescript'; | ||
import * as util from '../util'; | ||
|
||
type MakeRequired<Base, Key extends keyof Base> = Omit<Base, Key> & | ||
Required<Pick<Base, Key>>; | ||
|
||
type TypeParameterWithConstraint = MakeRequired< | ||
TSESTree.TSTypeParameter, | ||
'constraint' | ||
>; | ||
|
||
const is3dot5 = semver.satisfies( | ||
ts.version, | ||
`>= 3.5.0 || >= 3.5.1-rc || >= 3.5.0-beta`, | ||
{ | ||
includePrerelease: true, | ||
}, | ||
); | ||
|
||
const is3dot9 = | ||
is3dot5 && | ||
semver.satisfies(ts.version, `>= 3.9.0 || >= 3.9.1-rc || >= 3.9.0-beta`, { | ||
includePrerelease: true, | ||
}); | ||
|
||
export default util.createRule({ | ||
name: 'no-unnecessary-type-constraint', | ||
meta: { | ||
docs: { | ||
category: 'Best Practices', | ||
description: 'Disallows unnecessary constraints on generic types', | ||
recommended: false, | ||
suggestion: true, | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
unnecessaryConstraint: | ||
'Constraining the generic type `{{name}}` to `{{constraint}}` does nothing and is unnecessary.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
if (!is3dot5) { | ||
return {}; | ||
} | ||
|
||
// In theory, we could use the type checker for more advanced constraint types... | ||
// ...but in practice, these types are rare, and likely not worth requiring type info. | ||
// https://github.com/typescript-eslint/typescript-eslint/pull/2516#discussion_r495731858 | ||
const unnecessaryConstraints = is3dot9 | ||
? new Map([ | ||
[AST_NODE_TYPES.TSAnyKeyword, 'any'], | ||
[AST_NODE_TYPES.TSUnknownKeyword, 'unknown'], | ||
]) | ||
: new Map([[AST_NODE_TYPES.TSUnknownKeyword, 'unknown']]); | ||
|
||
const inJsx = context.getFilename().toLowerCase().endsWith('tsx'); | ||
|
||
const checkNode = ( | ||
node: TypeParameterWithConstraint, | ||
inArrowFunction: boolean, | ||
): void => { | ||
const constraint = unnecessaryConstraints.get(node.constraint.type); | ||
|
||
if (constraint) { | ||
context.report({ | ||
data: { | ||
constraint, | ||
name: node.name.name, | ||
}, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[node.name.range[1], node.constraint.range[1]], | ||
inArrowFunction && inJsx ? ',' : '', | ||
); | ||
}, | ||
messageId: 'unnecessaryConstraint', | ||
node, | ||
}); | ||
} | ||
}; | ||
|
||
return { | ||
':not(ArrowFunctionExpression) > TSTypeParameterDeclaration > TSTypeParameter[constraint]'( | ||
node: TypeParameterWithConstraint, | ||
): void { | ||
checkNode(node, false); | ||
}, | ||
'ArrowFunctionExpression > TSTypeParameterDeclaration > TSTypeParameter[constraint]'( | ||
node: TypeParameterWithConstraint, | ||
): void { | ||
checkNode(node, true); | ||
}, | ||
}; | ||
}, | ||
}); |
Oops, something went wrong.