UNPKG

1.6 kBPlain TextView Raw
1import * as ts from 'typescript';
2import * as Lint from 'tslint';
3import { ScopedSymbolTrackingWalker } from './ScopedSymbolTrackingWalker';
4
5import { AstUtils } from './AstUtils';
6
7/**
8 * A walker that creates failures whenever it detects a string parameter is being passed to a certain constructor. .
9 */
10export class NoStringParameterToFunctionCallWalker extends ScopedSymbolTrackingWalker {
11 private failureString: string;
12 private targetFunctionName: string;
13
14 public constructor(sourceFile: ts.SourceFile, targetFunctionName: string, options: Lint.IOptions, program?: ts.Program) {
15 super(sourceFile, options, program);
16 this.targetFunctionName = targetFunctionName;
17 this.failureString = 'Forbidden ' + targetFunctionName + ' string parameter: ';
18 }
19
20 protected visitCallExpression(node: ts.CallExpression) {
21 this.validateExpression(node);
22 super.visitCallExpression(node);
23 }
24
25 private validateExpression(node: ts.CallExpression): void {
26 const functionName: string = AstUtils.getFunctionName(node);
27 const firstArg: ts.Expression = node.arguments[0];
28 if (functionName === this.targetFunctionName && firstArg != null) {
29 if (!this.isExpressionEvaluatingToFunction(firstArg)) {
30 const msg: string =
31 this.failureString +
32 firstArg
33 .getFullText()
34 .trim()
35 .substring(0, 40);
36 this.addFailureAt(node.getStart(), node.getWidth(), msg);
37 }
38 }
39 }
40}