Skip to content

Commit

Permalink
fix(eslint-plugin): [no-unused-expressions] ignore directives (#1285)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored and bradzacher committed Nov 30, 2019
1 parent f84eb96 commit ce4c803
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 6 deletions.
11 changes: 6 additions & 5 deletions packages/eslint-plugin/src/rules/no-unused-expressions.ts
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
@@ -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
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

0 comments on commit ce4c803

Please sign in to comment.