Skip to content

fix(eslint-plugin): [no-unused-expressions] ignore directives #1285

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

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions packages/eslint-plugin/src/rules/no-unused-expressions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,20 +12,21 @@ export default util.createRule({
recommended: false,
},
schema: baseRule.meta.schema,
messages: {
expected:
'Expected an assignment or function call and instead saw an expression.',
},
messages: {},
},
defaultOptions: [],
create(context) {
const rules = baseRule.create(context);

return {
ExpressionStatement(node): void {
if (node.expression.type === AST_NODE_TYPES.OptionalCallExpression) {
if (
node.directive ||
node.expression.type === AST_NODE_TYPES.OptionalCallExpression
) {
return;
}

rules.ExpressionStatement(node);
},
};
Expand Down
76 changes: 75 additions & 1 deletion packages/eslint-plugin/tests/rules/no-unused-expressions.test.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TSESLint } from '@typescript-eslint/experimental-utils';
import rule from '../../src/rules/no-unused-expressions';
import { RuleTester } from '../RuleTester';

Expand All @@ -10,9 +11,11 @@ const ruleTester = new RuleTester({
parser: '@typescript-eslint/parser',
});

type TestCaseError = Omit<TSESLint.TestCaseError<string>, 'messageId'>;

// the base rule doesn't have messageIds
function error(
messages: { line: number; column: number }[],
messages: TestCaseError[],
// eslint-disable-next-line @typescript-eslint/no-explicit-any
): any[] {
return messages.map(message => ({
Expand Down Expand Up @@ -42,6 +45,26 @@ ruleTester.run('no-unused-expressions', rule, {
`
a?.['b']?.c();
`,
`
module Foo {
'use strict';
}
`,
`
namespace Foo {
'use strict';

export class Foo {}
export class Bar {}
}
`,
`
function foo() {
'use strict';

return null;
}
`,
],
invalid: [
{
Expand Down Expand Up @@ -176,5 +199,56 @@ one.two?.three.four;
},
]),
},
{
code: `
module Foo {
const foo = true;
'use strict';
}
`,
errors: error([
{
line: 4,
endLine: 4,
column: 3,
endColumn: 16,
},
]),
},
{
code: `
namespace Foo {
export class Foo {}
export class Bar {}

'use strict';
}
`,
errors: error([
{
line: 6,
endLine: 6,
column: 3,
endColumn: 16,
},
]),
},
{
code: `
function foo() {
const foo = true;

'use strict';
}
`,
errors: error([
{
line: 5,
endLine: 5,
column: 3,
endColumn: 16,
},
]),
},
],
});
1 change: 1 addition & 0 deletions packages/typescript-estree/src/ts-estree/ts-estree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -691,6 +691,7 @@ export interface ExportSpecifier extends BaseNode {
export interface ExpressionStatement extends BaseNode {
type: AST_NODE_TYPES.ExpressionStatement;
expression: Expression;
directive?: string;
}

export interface ForInStatement extends BaseNode {
Expand Down