Skip to content

Commit

Permalink
feat(eslint-plugin): new rule consistent-generic-constructors
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 7, 2022
1 parent 7735475 commit 03dca0c
Show file tree
Hide file tree
Showing 2 changed files with 219 additions and 0 deletions.
@@ -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),
),
];
},
});
}
},
};
},
});
@@ -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();`,
},
],
});

0 comments on commit 03dca0c

Please sign in to comment.