Skip to content

Commit

Permalink
fix(eslint-plugin): [no-extra-parens] keep parens around call express…
Browse files Browse the repository at this point in the history
…ion arguments when type arguments contain parens (#7375)

* fix(eslint-plugin): [no-extra-parens] keep parens around call expression arguments when type arguments contain parens

* Update packages/eslint-plugin/src/rules/no-extra-parens.ts

---------

Co-authored-by: Josh Goldberg ✨ <git@joshuakgoldberg.com>
  • Loading branch information
auvred and JoshuaKGoldberg committed Aug 5, 2023
1 parent 56919e4 commit 38e5e4e
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 5 deletions.
9 changes: 4 additions & 5 deletions packages/eslint-plugin/src/rules/no-extra-parens.ts
Expand Up @@ -27,6 +27,7 @@ export default util.createRule<Options, MessageIds>({
},
defaultOptions: ['all'],
create(context) {
const sourceCode = context.getSourceCode();
const rules = baseRule.create(context);

function binaryExp(
Expand Down Expand Up @@ -79,11 +80,9 @@ export default util.createRule<Options, MessageIds>({

if (
node.arguments.length === 1 &&
node.typeArguments?.params.some(
param =>
param.type === AST_NODE_TYPES.TSImportType ||
param.type === AST_NODE_TYPES.TSArrayType,
)
// is there any opening parenthesis in type arguments
sourceCode.getTokenAfter(node.callee, util.isOpeningParenToken) !==
sourceCode.getTokenBefore(node.arguments[0], util.isOpeningParenToken)
) {
return rule({
...node,
Expand Down
30 changes: 30 additions & 0 deletions packages/eslint-plugin/tests/rules/no-extra-parens.test.ts
Expand Up @@ -515,6 +515,26 @@ f<(number | string)[]>(['a', 1])
},
},
},
{
code: `
f<(number)>(1)
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
{
code: `
f<(number) | string>(1)
`,
parserOptions: {
ecmaFeatures: {
jsx: true,
},
},
},
],

invalid: [
Expand Down Expand Up @@ -608,6 +628,16 @@ f<(number | string)[]>(['a', 1])
},
],
},
{
code: 'a<(A) | number>((1));',
output: 'a<(A) | number>(1);',
errors: [
{
messageId: 'unexpected',
column: 17,
},
],
},
{
code: 'async function f(arg: Promise<any>) { await (arg); }',
output: 'async function f(arg: Promise<any>) { await arg; }',
Expand Down

0 comments on commit 38e5e4e

Please sign in to comment.