Skip to content

Commit

Permalink
fix: print extra comments
Browse files Browse the repository at this point in the history
  • Loading branch information
Josh-Cena committed May 14, 2022
1 parent ba54b9e commit 46de101
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 5 deletions.
Expand Up @@ -65,11 +65,24 @@ export default createRule<Options, MessageIds>({
}
if (mode === 'rhs' && 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: 'preferRHS',
*fix(fixer) {
yield fixer.remove(lhs.parent!);
for (const comment of extraComments) {
yield fixer.insertTextAfter(
rhs.callee,
// @ts-expect-error: `sourceCode.getText` should accept `TSESTree.Comment`
sourceCode.getText(comment),
);
}
yield fixer.insertTextAfter(
rhs.callee,
sourceCode.getText(lhs.typeParameters),
Expand Down
Expand Up @@ -117,14 +117,13 @@ ruleTester.run('consistent-generic-constructors', rule, {
output: noFormat`const a = new Foo<number>();`,
},
{
code: 'const a: Foo/* comment */ <string> = new Foo();',
code: 'const a: /* comment */ Foo/* another */ <string> = new Foo();',
errors: [
{
messageId: 'preferRHS',
},
],
// FIXME
output: 'const a = new Foo<string>();',
output: noFormat`const a = new Foo/* comment *//* another */<string>();`,
},
{
code: 'const a: Foo/* comment */ <string> = new Foo /* another */();',
Expand All @@ -133,8 +132,7 @@ ruleTester.run('consistent-generic-constructors', rule, {
messageId: 'preferRHS',
},
],
// FIXME
output: 'const a = new Foo<string> /* another */();',
output: noFormat`const a = new Foo/* comment */<string> /* another */();`,
},
{
code: noFormat`const a: Foo<string> = new \n Foo \n ();`,
Expand Down Expand Up @@ -205,5 +203,15 @@ ruleTester.run('consistent-generic-constructors', rule, {
],
output: noFormat`const a: Foo<string> = new Foo/* comment */ /* another */();`,
},
{
code: 'const a = new Foo</* comment */ string, /* another */ number>();',
options: ['lhs'],
errors: [
{
messageId: 'preferLHS',
},
],
output: noFormat`const a: Foo</* comment */ string, /* another */ number> = new Foo();`,
},
],
});

0 comments on commit 46de101

Please sign in to comment.