Skip to content

Commit 81c26de

Browse files
fix(no-expression-statements): allow yield expressions (#570)
1 parent 7648500 commit 81c26de

File tree

6 files changed

+44
-3
lines changed

6 files changed

+44
-3
lines changed

src/rules/no-expression-statements.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { shouldIgnorePattern, ignorePatternOptionSchema } from "~/options";
77
import { isDirectivePrologue } from "~/utils/misc";
88
import type { RuleResult, NamedCreateRuleMetaWithCategory } from "~/utils/rule";
99
import { createRule, getTypeOfNode } from "~/utils/rule";
10-
import { isVoidType } from "~/utils/type-guards";
10+
import { isVoidType, isYieldExpression } from "~/utils/type-guards";
1111

1212
/**
1313
* The name of this rule.
@@ -86,8 +86,8 @@ function checkExpressionStatement(
8686
};
8787
}
8888

89-
// Allow specifying directive prologues.
90-
if (isDirectivePrologue(node)) {
89+
// Allow specifying directive prologues and using yield expressions.
90+
if (isDirectivePrologue(node) || isYieldExpression(node.expression)) {
9191
return {
9292
context,
9393
descriptors: [],

src/utils/type-guards.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -346,6 +346,12 @@ export function isVariableDeclarator(
346346
return node.type === AST_NODE_TYPES.VariableDeclarator;
347347
}
348348

349+
export function isYieldExpression(
350+
node: TSESTree.Node
351+
): node is TSESTree.YieldExpression {
352+
return node.type === AST_NODE_TYPES.YieldExpression;
353+
}
354+
349355
export function hasID(
350356
node: TSESTree.Node
351357
): node is Extract<TSESTree.Node, { id: unknown }> {
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import invalid from "./invalid";
2+
import valid from "./valid";
3+
4+
export default {
5+
valid,
6+
invalid,
7+
};
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import dedent from "dedent";
2+
3+
import type { InvalidTestCase } from "~/tests/helpers/util";
4+
5+
const tests: InvalidTestCase[] = [];
6+
7+
export default tests;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import dedent from "dedent";
2+
3+
import type { ValidTestCase } from "~/tests/helpers/util";
4+
5+
const tests: ValidTestCase[] = [
6+
// Allow yield.
7+
{
8+
code: dedent`
9+
export function* foo() {
10+
yield "hello";
11+
return "world";
12+
}
13+
`,
14+
optionsSet: [[]],
15+
},
16+
];
17+
18+
export default tests;

tests/rules/no-expression-statement/index.test.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,12 @@ import { name, rule } from "~/rules/no-expression-statements";
22
import { testUsing } from "~/tests/helpers/testers";
33

44
import es3Tests from "./es3";
5+
import es6Tests from "./es6";
56
import tsTests from "./ts";
67

78
testUsing.typescript(name, rule, tsTests);
9+
testUsing.typescript(name, rule, es6Tests);
810
testUsing.typescript(name, rule, es3Tests);
911

1012
testUsing.es3(name, rule, es3Tests);
13+
testUsing.es6(name, rule, es6Tests);

0 commit comments

Comments
 (0)