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-unused-expressions] false negatives when using assertions #8668

9 changes: 7 additions & 2 deletions packages/eslint-plugin/src/rules/no-unused-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,9 +57,14 @@ export default createRule<Options, MessageIds>({
return;
}

const expressionType = node.expression.type;

if (
node.expression.type ===
TSESTree.AST_NODE_TYPES.TSInstantiationExpression
expressionType ===
TSESTree.AST_NODE_TYPES.TSInstantiationExpression ||
expressionType === TSESTree.AST_NODE_TYPES.TSAsExpression ||
expressionType === TSESTree.AST_NODE_TYPES.TSNonNullExpression ||
expressionType === TSESTree.AST_NODE_TYPES.TSTypeAssertion
) {
rules.ExpressionStatement({
...node,
Expand Down
56 changes: 56 additions & 0 deletions packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -325,5 +325,61 @@ Foo<string>;
},
]),
},
{
code: `
declare const foo: number | undefined;
foo;
`,
errors: error([
{
line: 3,
endLine: 3,
column: 1,
endColumn: 5,
},
]),
},
{
code: `
declare const foo: number | undefined;
foo as any;
`,
errors: error([
{
line: 3,
endLine: 3,
column: 1,
endColumn: 12,
},
]),
},
{
code: `
declare const foo: number | undefined;
<any>foo;
`,
errors: error([
{
line: 3,
endLine: 3,
column: 1,
endColumn: 10,
},
]),
},
{
code: `
declare const foo: number | undefined;
foo!;
`,
errors: error([
{
line: 3,
endLine: 3,
column: 1,
endColumn: 6,
},
]),
},
],
});