Skip to content

Commit

Permalink
fix: allow no parens on RHS
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 14, 2022
1 parent 33cb2d3 commit ba54b9e
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 12 deletions.
26 changes: 14 additions & 12 deletions packages/eslint-plugin/src/rules/consistent-generic-constructors.ts
Expand Up @@ -49,32 +49,34 @@ export default createRule<Options, MessageIds>({
return;
}
if (mode === 'lhs' && !lhs && rhs.typeParameters) {
const { typeParameters, callee } = rhs;
const typeAnnotation =
sourceCode.getText(callee) + sourceCode.getText(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.remove(typeParameters),
fixer.insertTextAfter(node.id, ': ' + typeAnnotation),
];
},
});
}
if (mode === 'rhs' && lhs?.typeParameters && !rhs.typeParameters) {
const hasParens = sourceCode.getTokenAfter(rhs.callee)?.value === '(';
context.report({
node,
messageId: 'preferRHS',
fix(fixer) {
return [
fixer.remove(lhs.parent!),
fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeParameters),
),
];
*fix(fixer) {
yield fixer.remove(lhs.parent!);
yield fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeParameters),
);
if (!hasParens) {
yield fixer.insertTextAfter(rhs.callee, '()');
}
},
});
}
Expand Down
Expand Up @@ -107,6 +107,44 @@ ruleTester.run('consistent-generic-constructors', rule, {
],
output: noFormat`const a = new Map<string, number> ();`,
},
{
code: noFormat`const a: Foo<number> = new Foo;`,
errors: [
{
messageId: 'preferRHS',
},
],
output: noFormat`const a = new Foo<number>();`,
},
{
code: 'const a: Foo/* comment */ <string> = new Foo();',
errors: [
{
messageId: 'preferRHS',
},
],
// FIXME
output: 'const a = new Foo<string>();',
},
{
code: 'const a: Foo/* comment */ <string> = new Foo /* another */();',
errors: [
{
messageId: 'preferRHS',
},
],
// FIXME
output: 'const a = new Foo<string> /* another */();',
},
{
code: noFormat`const a: Foo<string> = new \n Foo \n ();`,
errors: [
{
messageId: 'preferRHS',
},
],
output: noFormat`const a = new \n Foo<string> \n ();`,
},
{
code: 'const a = new Foo<string>();',
options: ['lhs'],
Expand Down Expand Up @@ -147,5 +185,25 @@ ruleTester.run('consistent-generic-constructors', rule, {
],
output: noFormat`const a: Map< string, number > = new Map();`,
},
{
code: noFormat`const a = new \n Foo<string> \n ();`,
options: ['lhs'],
errors: [
{
messageId: 'preferLHS',
},
],
output: noFormat`const a: Foo<string> = new \n Foo \n ();`,
},
{
code: 'const a = new Foo/* comment */ <string> /* another */();',
options: ['lhs'],
errors: [
{
messageId: 'preferLHS',
},
],
output: noFormat`const a: Foo<string> = new Foo/* comment */ /* another */();`,
},
],
});

0 comments on commit ba54b9e

Please sign in to comment.