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
- Loading branch information
1 parent
ec7449d
commit e0ee9ae
Showing
6 changed files
with
415 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
116 changes: 116 additions & 0 deletions
116
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,116 @@ | ||
import { 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' | ||
>; | ||
|
||
type KeywordFilter = (type: ts.Type) => boolean; | ||
|
||
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, | ||
requiresTypeChecking: true, | ||
suggestion: true, | ||
}, | ||
fixable: 'code', | ||
messages: { | ||
unnecessaryConstraint: | ||
'Constraining a generic type to {{constraint}} does nothing and is unnecessary.', | ||
}, | ||
schema: [], | ||
type: 'suggestion', | ||
}, | ||
defaultOptions: [], | ||
create(context) { | ||
const parserServices = util.getParserServices(context); | ||
const checker = parserServices.program.getTypeChecker(); | ||
|
||
const keywordFilters: [KeywordFilter, string][] = []; | ||
if (is3dot5) { | ||
keywordFilters.push([util.isTypeUnknownType, 'unknown']); | ||
|
||
if (is3dot9) { | ||
keywordFilters.push([util.isTypeAnyType, 'any']); | ||
} | ||
} | ||
|
||
if (!keywordFilters.length) { | ||
return {}; | ||
} | ||
|
||
const inJsx = context.getFilename().toLowerCase().endsWith('tsx'); | ||
|
||
const report = ( | ||
node: TypeParameterWithConstraint, | ||
constraint: string, | ||
inArrowFunction: boolean, | ||
): void => { | ||
context.report({ | ||
data: { constraint }, | ||
fix(fixer) { | ||
return fixer.replaceTextRange( | ||
[node.name.range[1], node.constraint.range[1]], | ||
inArrowFunction && inJsx ? ',' : '', | ||
); | ||
}, | ||
messageId: 'unnecessaryConstraint', | ||
node, | ||
}); | ||
}; | ||
|
||
const checkNode = ( | ||
node: TypeParameterWithConstraint, | ||
inArrowFunction: boolean, | ||
): void => { | ||
const constraint = parserServices.esTreeNodeToTSNodeMap.get( | ||
node.constraint, | ||
); | ||
const constraintType = checker.getTypeAtLocation(constraint); | ||
|
||
for (const [filter, type] of keywordFilters) { | ||
if (filter(constraintType)) { | ||
report(node, type, inArrowFunction); | ||
return; | ||
} | ||
} | ||
}; | ||
|
||
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.