diff --git a/packages/eslint-plugin/src/rules/no-unused-expressions.ts b/packages/eslint-plugin/src/rules/no-unused-expressions.ts index 3c9652e46d8..b1605c0f92d 100644 --- a/packages/eslint-plugin/src/rules/no-unused-expressions.ts +++ b/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, @@ -58,6 +57,17 @@ export default createRule({ return; } + if ( + node.expression.type === + TSESTree.AST_NODE_TYPES.TSInstantiationExpression + ) { + rules.ExpressionStatement({ + ...node, + expression: node.expression.expression, + }); + return; + } + rules.ExpressionStatement(node); }, }; diff --git a/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts b/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts index 1160917f6ed..eb5413f49e3 100644 --- a/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts +++ b/packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts @@ -72,6 +72,10 @@ ruleTester.run('no-unused-expressions', rule, { ` import('./foo').then(() => {}); `, + ` + class Foo {} + new Foo(); + `, { code: 'foo && foo?.();', options: [{ allowShortCircuit: true }], @@ -296,5 +300,30 @@ function foo() { }, ]), }, + { + code: noFormat` +class Foo {} +Foo; + `, + errors: error([ + { + line: 3, + endLine: 3, + column: 1, + endColumn: 13, + }, + ]), + }, + { + code: 'Map;', + errors: error([ + { + line: 1, + endLine: 1, + column: 1, + endColumn: 21, + }, + ]), + }, ], });