Skip to content
This repository has been archived by the owner on Jul 15, 2023. It is now read-only.

proposed type-checking for noStringBased rules #417

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions src/utils/NoStringParameterToFunctionCallWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,21 @@ export class NoStringParameterToFunctionCallWalker extends ScopedSymbolTrackingW

private validateExpression(node : ts.CallExpression) : void {
const functionName : string = AstUtils.getFunctionName(node);
const functionTarget : string = AstUtils.getFunctionTarget(node);
const functionTargetType : string = this.getFunctionTargetType(node);
const firstArg : ts.Expression = node.arguments[0];
if (functionName === this.targetFunctionName && firstArg != null) {
if (functionTarget) {
if (functionTargetType) {
if (!functionTargetType.match(/^(any|Window|Worker)$/)) {
return;
}
} else {
if (!functionTarget.match(/^(this|window)$/)) {
return;
}
}
}
if (!this.isExpressionEvaluatingToFunction(firstArg)) {
const msg : string = this.failureString + firstArg.getFullText().trim().substring(0, 40);
this.addFailureAt(node.getStart(), node.getWidth(), msg);
Expand Down
9 changes: 9 additions & 0 deletions src/utils/ScopedSymbolTrackingWalker.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,15 @@ export class ScopedSymbolTrackingWalker extends ErrorTolerantWalker {
}
}

protected getFunctionTargetType(expression: ts.CallExpression) : string {
if (expression.expression.kind === ts.SyntaxKind.PropertyAccessExpression && this.typeChecker) {
const propExp : ts.PropertyAccessExpression = <ts.PropertyAccessExpression>expression.expression;
const targetType: ts.Type = this.typeChecker.getTypeAtLocation(propExp.expression);
return this.typeChecker.typeToString(targetType);
}
return null;
}

protected isExpressionEvaluatingToFunction(expression : ts.Expression) : boolean {
if (expression.kind === ts.SyntaxKind.ArrowFunction
|| expression.kind === ts.SyntaxKind.FunctionExpression) {
Expand Down