UNPKG

1.79 kBPlain TextView Raw
1import * as ts from 'typescript';
2
3/**
4 * Utility methods for the chai.related rules.
5 */
6export namespace ChaiUtils {
7 export function isExpectInvocation(node: ts.PropertyAccessExpression | ts.CallExpression): boolean {
8 const callExpression: ts.CallExpression = getLeftMostCallExpression(node);
9 if (callExpression == null) {
10 return false;
11 }
12 return /.*\.?expect/.test(callExpression.expression.getText());
13 }
14
15 export function getLeftMostCallExpression(node: ts.PropertyAccessExpression | ts.CallExpression): ts.CallExpression {
16 let leftSide: ts.Node = node.expression;
17 while (leftSide != null) {
18 if (leftSide.kind === ts.SyntaxKind.CallExpression) {
19 return <ts.CallExpression>leftSide;
20 } else if (leftSide.kind === ts.SyntaxKind.PropertyAccessExpression) {
21 leftSide = (<ts.PropertyAccessExpression>leftSide).expression;
22 } else {
23 return null; // cannot walk any further left in the property expression
24 }
25 }
26 return null;
27 }
28
29 export function getFirstExpectCallParameter(node: ts.CallExpression): ts.Node {
30 const expectCall: ts.CallExpression = ChaiUtils.getLeftMostCallExpression(node);
31 if (expectCall.arguments.length > 0) {
32 return expectCall.arguments[0];
33 }
34 return null;
35 }
36
37 export function getFirstExpectationParameter(node: ts.CallExpression): ts.Node {
38 if (node.arguments.length > 0) {
39 return node.arguments[0];
40 }
41 return null;
42 }
43
44 export function isEqualsInvocation(propExpression: ts.PropertyAccessExpression): boolean {
45 return /equal|equals|eq|eql|eqs/.test(propExpression.name.getText());
46 }
47}