forked from typescript-eslint/typescript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
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): [consistent-generic-constructors] add rule (type…
- Loading branch information
Showing
9 changed files
with
410 additions
and
1 deletion.
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
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
82 changes: 82 additions & 0 deletions
82
packages/eslint-plugin/docs/rules/consistent-generic-constructors.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,82 @@ | ||
# `consistent-generic-constructors` | ||
|
||
Enforces specifying generic type arguments on type annotation or constructor name of a constructor call. | ||
|
||
When constructing a generic class, you can specify the type arguments on either the left-hand side (as a type annotation) or the right-hand side (as part of the constructor call): | ||
|
||
```ts | ||
// Left-hand side | ||
const map: Map<string, number> = new Map(); | ||
|
||
// Right-hand side | ||
const map = new Map<string, number>(); | ||
``` | ||
|
||
This rule ensures that type arguments appear consistently on one side of the declaration. | ||
|
||
## Options | ||
|
||
```jsonc | ||
{ | ||
"rules": { | ||
"@typescript-eslint/consistent-generic-constructors": [ | ||
"error", | ||
"constructor" | ||
] | ||
} | ||
} | ||
``` | ||
|
||
This rule takes a string option: | ||
|
||
- If it's set to `constructor` (default), type arguments that **only** appear on the type annotation are disallowed. | ||
- If it's set to `type-annotation`, type arguments that **only** appear on the constructor are disallowed. | ||
|
||
## Rule Details | ||
|
||
The rule never reports when there are type parameters on both sides, or neither sides of the declaration. It also doesn't report if the names of the type annotation and the constructor don't match. | ||
|
||
### `constructor` | ||
|
||
<!--tabs--> | ||
|
||
#### ❌ Incorrect | ||
|
||
```ts | ||
const map: Map<string, number> = new Map(); | ||
const set: Set<string> = new Set(); | ||
``` | ||
|
||
#### ✅ Correct | ||
|
||
```ts | ||
const map = new Map<string, number>(); | ||
const map: Map<string, number> = new MyMap(); | ||
const set = new Set<string>(); | ||
const set = new Set(); | ||
const set: Set<string> = new Set<string>(); | ||
``` | ||
|
||
### `type-annotation` | ||
|
||
<!--tabs--> | ||
|
||
#### ❌ Incorrect | ||
|
||
```ts | ||
const map = new Map<string, number>(); | ||
const set = new Set<string>(); | ||
``` | ||
|
||
#### ✅ Correct | ||
|
||
```ts | ||
const map: Map<string, number> = new Map(); | ||
const set: Set<string> = new Set(); | ||
const set = new Set(); | ||
const set: Set<string> = new Set<string>(); | ||
``` | ||
|
||
## When Not To Use It | ||
|
||
You can turn this rule off if you don't want to enforce one kind of generic constructor style over the other. |
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
104 changes: 104 additions & 0 deletions
104
packages/eslint-plugin/src/rules/consistent-generic-constructors.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,104 @@ | ||
import { AST_NODE_TYPES } from '@typescript-eslint/utils'; | ||
import { createRule } from '../util'; | ||
|
||
type MessageIds = 'preferTypeAnnotation' | 'preferConstructor'; | ||
type Options = ['type-annotation' | 'constructor']; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'consistent-generic-constructors', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Enforce specifying generic type arguments on type annotation or constructor name of a constructor call', | ||
recommended: 'strict', | ||
}, | ||
messages: { | ||
preferTypeAnnotation: | ||
'The generic type arguments should be specified as part of the type annotation.', | ||
preferConstructor: | ||
'The generic type arguments should be specified as part of the constructor type arguments.', | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: ['type-annotation', 'constructor'], | ||
}, | ||
], | ||
}, | ||
defaultOptions: ['constructor'], | ||
create(context, [mode]) { | ||
const sourceCode = context.getSourceCode(); | ||
return { | ||
VariableDeclarator(node): void { | ||
const lhs = node.id.typeAnnotation?.typeAnnotation; | ||
const rhs = node.init; | ||
if ( | ||
!rhs || | ||
rhs.type !== AST_NODE_TYPES.NewExpression || | ||
rhs.callee.type !== AST_NODE_TYPES.Identifier | ||
) { | ||
return; | ||
} | ||
if ( | ||
lhs && | ||
(lhs.type !== AST_NODE_TYPES.TSTypeReference || | ||
lhs.typeName.type !== AST_NODE_TYPES.Identifier || | ||
lhs.typeName.name !== rhs.callee.name) | ||
) { | ||
return; | ||
} | ||
if (mode === 'type-annotation') { | ||
if (!lhs && rhs.typeParameters) { | ||
const { typeParameters, callee } = rhs; | ||
const typeAnnotation = | ||
sourceCode.getText(callee) + sourceCode.getText(typeParameters); | ||
context.report({ | ||
node, | ||
messageId: 'preferTypeAnnotation', | ||
fix(fixer) { | ||
return [ | ||
fixer.remove(typeParameters), | ||
fixer.insertTextAfter(node.id, ': ' + typeAnnotation), | ||
]; | ||
}, | ||
}); | ||
} | ||
return; | ||
} | ||
if (mode === 'constructor') { | ||
if (lhs?.typeParameters && !rhs.typeParameters) { | ||
const hasParens = | ||
sourceCode.getTokenAfter(rhs.callee)?.value === '('; | ||
const extraComments = new Set( | ||
sourceCode.getCommentsInside(lhs.parent!), | ||
); | ||
sourceCode | ||
.getCommentsInside(lhs.typeParameters) | ||
.forEach(c => extraComments.delete(c)); | ||
context.report({ | ||
node, | ||
messageId: 'preferConstructor', | ||
*fix(fixer) { | ||
yield fixer.remove(lhs.parent!); | ||
for (const comment of extraComments) { | ||
yield fixer.insertTextAfter( | ||
rhs.callee, | ||
sourceCode.getText(comment), | ||
); | ||
} | ||
yield fixer.insertTextAfter( | ||
rhs.callee, | ||
sourceCode.getText(lhs.typeParameters), | ||
); | ||
if (!hasParens) { | ||
yield fixer.insertTextAfter(rhs.callee, '()'); | ||
} | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
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
Oops, something went wrong.