All files noMixedInterfaceRule.ts

100% Statements 21/21
100% Branches 13/13
100% Functions 1/1
100% Lines 20/20
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 40 41 42 43 44 45 46 47 48 49 50 51 52 531x   1x                 1x                 41x 41x 5x 5x 5x 9x 9x 9x   9x 5x   5x         1x     9x       2x   9x 9x     41x    
import * as ts from "typescript";
import * as Lint from "tslint";
import {
  createInvalidNode,
  CheckNodeResult,
  createCheckNodeRule
} from "./shared/check-node";
 
type Options = {};
 
// tslint:disable-next-line:variable-name
export const Rule = createCheckNodeRule(
  checkNode,
  "Only the same kind of members allowed in interfaces."
);
 
function checkNode(
  node: ts.Node,
  _ctx: Lint.WalkContext<Options>
): CheckNodeResult {
  const invalidNodes = [];
  if (node.kind === ts.SyntaxKind.InterfaceDeclaration) {
    const interfaceDeclaration = node as ts.InterfaceDeclaration;
    let prevMemberKind: number | undefined = undefined;
    let prevMemberType: number | undefined = undefined;
    for (const member of interfaceDeclaration.members) {
      const memberKind = member.kind;
      let memberType = 0;
      // If it is a property declaration we need to check the type too
      if (member.kind === ts.SyntaxKind.PropertySignature) {
        const propertySignature = member as ts.PropertySignature;
        // Special care for function type
        if (
          propertySignature.type &&
          propertySignature.type.kind === ts.SyntaxKind.FunctionType
        ) {
          // We only set memberType for Functions
          memberType = propertySignature.type.kind;
        }
      }
      if (
        prevMemberKind !== undefined &&
        (prevMemberKind !== memberKind || prevMemberType !== memberType)
      ) {
        invalidNodes.push(createInvalidNode(member, []));
      }
      prevMemberKind = memberKind;
      prevMemberType = memberType;
    }
  }
  return { invalidNodes };
}