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): new rule consistent-generic-constructors
- Loading branch information
Showing
2 changed files
with
219 additions
and
0 deletions.
There are no files selected for viewing
87 changes: 87 additions & 0 deletions
87
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,87 @@ | ||
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils'; | ||
import { createRule } from '../util'; | ||
|
||
type MessageIds = 'preferLHS' | 'preferRHS'; | ||
type Options = ['lhs' | 'rhs']; | ||
|
||
export default createRule<Options, MessageIds>({ | ||
name: 'consistent-generic-constructors', | ||
meta: { | ||
type: 'suggestion', | ||
docs: { | ||
description: | ||
'Enforce specifying generic type arguments on LHS or RHS of constructor call', | ||
recommended: false, | ||
}, | ||
messages: { | ||
preferLHS: | ||
'The generic type arguments should be specified on the left-hand side of the constructor call.', | ||
preferRHS: | ||
'The generic type arguments should be specified on the right-hand side of the constructor call.', | ||
}, | ||
fixable: 'code', | ||
schema: [ | ||
{ | ||
enum: ['lhs', 'rhs'], | ||
}, | ||
], | ||
}, | ||
defaultOptions: ['rhs'], | ||
create(context, [mode]) { | ||
return { | ||
VariableDeclarator(node: TSESTree.VariableDeclarator): void { | ||
const sourceCode = context.getSourceCode(); | ||
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) | ||
) { | ||
return; | ||
} | ||
if (mode === 'lhs' && !lhs && rhs.typeParameters) { | ||
context.report({ | ||
node, | ||
messageId: 'preferLHS', | ||
fix(fixer) { | ||
const { typeParameters, callee } = rhs; | ||
const typeAnnotation = | ||
sourceCode.getText(callee) + sourceCode.getText(typeParameters); | ||
return [ | ||
fixer.remove(typeParameters!), | ||
fixer.insertTextAfter(node.id, ': ' + typeAnnotation), | ||
]; | ||
}, | ||
}); | ||
} else if ( | ||
mode === 'rhs' && | ||
lhs?.typeParameters && | ||
!rhs.typeParameters && | ||
(lhs.typeName as TSESTree.Identifier).name === rhs.callee.name | ||
) { | ||
context.report({ | ||
node, | ||
messageId: 'preferRHS', | ||
fix(fixer) { | ||
return [ | ||
fixer.remove(lhs.parent!), | ||
fixer.insertTextAfter( | ||
rhs.callee, | ||
sourceCode.getText(lhs.typeParameters), | ||
), | ||
]; | ||
}, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}); |
132 changes: 132 additions & 0 deletions
132
packages/eslint-plugin/tests/rules/consistent-generic-constructors.test.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,132 @@ | ||
import rule from '../../src/rules/consistent-generic-constructors'; | ||
import { RuleTester, noFormat } from '../RuleTester'; | ||
|
||
const ruleTester = new RuleTester({ | ||
parser: '@typescript-eslint/parser', | ||
}); | ||
|
||
ruleTester.run('consistent-generic-constructors', rule, { | ||
valid: [ | ||
// default: rhs | ||
'const a = new Foo();', | ||
'const a = new Foo<string>();', | ||
'const a: Foo<string> = new Foo<string>();', | ||
'const a: Foo = new Foo();', | ||
'const a: Bar<string> = new Foo();', | ||
'const a: Foo = new Foo<string>();', | ||
'const a: Bar = new Foo<string>();', | ||
'const a: Bar<string> = new Foo<string>();', | ||
// lhs | ||
{ | ||
code: 'const a = new Foo();', | ||
options: ['lhs'], | ||
}, | ||
{ | ||
code: 'const a: Foo<string> = new Foo();', | ||
options: ['lhs'], | ||
}, | ||
{ | ||
code: 'const a: Foo<string> = new Foo<string>();', | ||
options: ['lhs'], | ||
}, | ||
{ | ||
code: 'const a: Foo = new Foo();', | ||
options: ['lhs'], | ||
}, | ||
{ | ||
code: 'const a: Bar = new Foo<string>();', | ||
options: ['lhs'], | ||
}, | ||
{ | ||
code: 'const a: Bar<string> = new Foo<string>();', | ||
options: ['lhs'], | ||
}, | ||
], | ||
invalid: [ | ||
{ | ||
code: 'const a: Foo<string> = new Foo();', | ||
errors: [ | ||
{ | ||
messageId: 'preferRHS', | ||
}, | ||
], | ||
output: 'const a = new Foo<string>();', | ||
}, | ||
{ | ||
code: 'const a: Map<string, number> = new Map();', | ||
errors: [ | ||
{ | ||
messageId: 'preferRHS', | ||
}, | ||
], | ||
output: 'const a = new Map<string, number>();', | ||
}, | ||
{ | ||
code: noFormat`const a: Map <string, number> = new Map();`, | ||
errors: [ | ||
{ | ||
messageId: 'preferRHS', | ||
}, | ||
], | ||
output: noFormat`const a = new Map<string, number>();`, | ||
}, | ||
{ | ||
code: noFormat`const a: Map< string, number > = new Map();`, | ||
errors: [ | ||
{ | ||
messageId: 'preferRHS', | ||
}, | ||
], | ||
output: noFormat`const a = new Map< string, number >();`, | ||
}, | ||
{ | ||
code: noFormat`const a: Map<string, number> = new Map ();`, | ||
errors: [ | ||
{ | ||
messageId: 'preferRHS', | ||
}, | ||
], | ||
output: noFormat`const a = new Map<string, number> ();`, | ||
}, | ||
{ | ||
code: 'const a = new Foo<string>();', | ||
options: ['lhs'], | ||
errors: [ | ||
{ | ||
messageId: 'preferLHS', | ||
}, | ||
], | ||
output: 'const a: Foo<string> = new Foo();', | ||
}, | ||
{ | ||
code: 'const a = new Map<string, number>();', | ||
options: ['lhs'], | ||
errors: [ | ||
{ | ||
messageId: 'preferLHS', | ||
}, | ||
], | ||
output: 'const a: Map<string, number> = new Map();', | ||
}, | ||
{ | ||
code: noFormat`const a = new Map <string, number> ();`, | ||
options: ['lhs'], | ||
errors: [ | ||
{ | ||
messageId: 'preferLHS', | ||
}, | ||
], | ||
output: noFormat`const a: Map<string, number> = new Map ();`, | ||
}, | ||
{ | ||
code: noFormat`const a = new Map< string, number >();`, | ||
options: ['lhs'], | ||
errors: [ | ||
{ | ||
messageId: 'preferLHS', | ||
}, | ||
], | ||
output: noFormat`const a: Map< string, number > = new Map();`, | ||
}, | ||
], | ||
}); |