Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-expressions] handle TSInstantiationExp…
Browse files Browse the repository at this point in the history
…ression expression (#7831)
  • Loading branch information
yeonjuan committed Oct 25, 2023
1 parent 6455278 commit 31988e0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 2 deletions.
14 changes: 12 additions & 2 deletions packages/eslint-plugin/src/rules/no-unused-expressions.ts
@@ -1,5 +1,4 @@
import type { TSESTree } from '@typescript-eslint/utils';
import { AST_NODE_TYPES } from '@typescript-eslint/utils';
import { AST_NODE_TYPES, TSESTree } from '@typescript-eslint/utils';

import type {
InferMessageIdsTypeFromRule,
Expand Down Expand Up @@ -58,6 +57,17 @@ export default createRule<Options, MessageIds>({
return;
}

if (
node.expression.type ===
TSESTree.AST_NODE_TYPES.TSInstantiationExpression
) {
rules.ExpressionStatement({
...node,
expression: node.expression.expression,
});
return;
}

rules.ExpressionStatement(node);
},
};
Expand Down
29 changes: 29 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts
Expand Up @@ -72,6 +72,10 @@ ruleTester.run('no-unused-expressions', rule, {
`
import('./foo').then(() => {});
`,
`
class Foo<T> {}
new Foo<string>();
`,
{
code: 'foo && foo?.();',
options: [{ allowShortCircuit: true }],
Expand Down Expand Up @@ -296,5 +300,30 @@ function foo() {
},
]),
},
{
code: noFormat`
class Foo<T> {}
Foo<string>;
`,
errors: error([
{
line: 3,
endLine: 3,
column: 1,
endColumn: 13,
},
]),
},
{
code: 'Map<string, string>;',
errors: error([
{
line: 1,
endLine: 1,
column: 1,
endColumn: 21,
},
]),
},
],
});

0 comments on commit 31988e0

Please sign in to comment.