Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(eslint-plugin): [no-extra-parens] false positive with arrow generic #1689

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
21 changes: 21 additions & 0 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Expand Up @@ -62,11 +62,32 @@ export default util.createRule<Options, MessageIds>({

return rule(node);
}

function callExp(
node: TSESTree.CallExpression | TSESTree.NewExpression,
): void {
const rule = rules.CallExpression as (n: typeof node) => void;

// ESLint core cannot check parens well when there is function type in type parameters with only one argument.
// e.g. foo<() => void>(a);
if (
node.typeParameters?.params.some(util.isTSFunctionType) &&
node.arguments.length === 1
) {
const sourceCode = context.getSourceCode();
const tokenAfterTypeParam = sourceCode.getTokenAfter(
node.typeParameters,
);
const tokenBeforeArg = sourceCode.getTokenBefore(node.arguments[0]);

if (tokenAfterTypeParam === tokenBeforeArg) {
return rule({
...node,
arguments: [],
});
}
}

if (util.isTypeAssertion(node.callee)) {
// reduces the precedence of the node so the rule thinks it needs to be wrapped
return rule({
Expand Down
70 changes: 70 additions & 0 deletions packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Expand Up @@ -14,6 +14,17 @@ ruleTester.run('no-extra-parens', rule, {
valid: [
...batchedSingleLineTests({
code: `
foo<() => void>(a);
foo<() => void>(a = b);
foo<() => void>(a, b);
foo<() => void>((a, b));
foo<() => () => void>(a);
foo<() => void>(()=> undefined);
foo<T, () => void>(a);
`,
}),
...batchedSingleLineTests({
code: `
(0).toString();
(function(){}) ? a() : b();
(/^a$/).test(x);
Expand Down Expand Up @@ -52,6 +63,10 @@ for (;(a = b););
code: `b => (b = 1);`,
options: ['all', { returnAssign: false }],
},
{
code: `foo<() => void>(b => (b = 1));`,
options: ['all', { returnAssign: false }],
},
{
code: `b => b ? (c = d) : (c = e);`,
options: ['all', { returnAssign: false }],
Expand All @@ -61,6 +76,7 @@ for (;(a = b););
x = a || (b && c);
x = a + (b * c);
x = (a * b) / c;
foo<() => void>(x = (a * b) / c);
`,
options: ['all', { nestedBinaryExpressions: false }],
}),
Expand Down Expand Up @@ -222,6 +238,60 @@ switch (foo) { case 1: case (<2>2): break; default: break; }
invalid: [
...batchedSingleLineTests({
code: `
foo<() => void>((a));
foo<() => void>((a = b));
foo<() => void>((a), b);
foo<() => () => void>((a));
foo<() => void>((()=> undefined));
foo<T, () => void>((a));
foo<T, () => void>((a),);
foo<T, () => void>(((a)));
`,
errors: [
{
messageId: 'unexpected',
line: 2,
column: 17,
},
{
messageId: 'unexpected',
line: 3,
column: 17,
},
{
messageId: 'unexpected',
line: 4,
column: 17,
},
{
messageId: 'unexpected',
line: 5,
column: 23,
},
{
messageId: 'unexpected',
line: 6,
column: 17,
},
{
messageId: 'unexpected',
line: 7,
column: 20,
},
{
messageId: 'unexpected',
line: 8,
column: 20,
},
{
messageId: 'unexpected',
line: 9,
column: 21,
},
],
}),
...batchedSingleLineTests({
code: `
a = (b * c);
(a * b) + c;
for (a in (b, c));
Expand Down