All files noExpressionStatementRule.ts

100% Statements 17/17
100% Branches 10/10
100% Functions 2/2
100% Lines 17/17
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 401x   1x         1x         1x                 49x 7x 7x 7x 7x   7x 7x 1x 1x   7x 7x 3x     46x    
import * as ts from "typescript";
import * as Lint from "tslint";
import {
  createInvalidNode,
  CheckNodeResult,
  createCheckNodeRule
} from "./shared/check-node";
import * as Ignore from "./shared/ignore";
 
type Options = Ignore.IgnorePrefixOption;
 
// tslint:disable-next-line:variable-name
export const Rule = createCheckNodeRule(
  checkNode,
  "Using expressions to cause side-effects not allowed."
);
 
function checkNode(
  node: ts.Node,
  ctx: Lint.WalkContext<Options>
): CheckNodeResult {
  if (node && node.kind === ts.SyntaxKind.ExpressionStatement) {
    const esNode = node as ts.ExpressionStatement;
    const children = esNode.getChildren();
    const isYield = children.every(
      (n: ts.Node) => n.kind === ts.SyntaxKind.YieldExpression
    );
    let text = esNode.getText(esNode.getSourceFile());
    if (esNode.expression.kind === ts.SyntaxKind.AwaitExpression) {
      const awaitNode = esNode.expression as ts.AwaitExpression;
      text = awaitNode.expression.getText(awaitNode.getSourceFile());
    }
    const isIgnored2 = Ignore.isIgnoredPrefix(text, ctx.options.ignorePrefix);
    if (!isYield && !isIgnored2) {
      return { invalidNodes: [createInvalidNode(esNode, [])] };
    }
  }
  return { invalidNodes: [] };
}