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

Commit

Permalink
#417 Fixed several false positives in the noStringBased rules
Browse files Browse the repository at this point in the history
closes #417
  • Loading branch information
tosmolka authored and HamletDRC committed Apr 16, 2018
1 parent 7196a26 commit 231b944
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
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

0 comments on commit 231b944

Please sign in to comment.