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

Commit

Permalink
convert no-function-constructor-with-string-args to use a walk functi…
Browse files Browse the repository at this point in the history
…on (#742)
  • Loading branch information
drexler authored and Josh Goldberg committed Dec 31, 2018
1 parent b7866bf commit 506d04e
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions src/noFunctionConstructorWithStringArgsRule.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import * as tsutils from 'tsutils';

import { AstUtils } from './utils/AstUtils';
import { ExtendedMetadata } from './utils/ExtendedMetadata';
Expand Down Expand Up @@ -33,20 +34,21 @@ export class Rule extends Lint.Rules.AbstractRule {
Rule.isWarningShown = true;
}

return this.applyWithWalker(new NoFunctionConstructorWithStringArgsWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk);
}
}

class NoFunctionConstructorWithStringArgsWalker extends Lint.RuleWalker {
public constructor(sourceFile: ts.SourceFile, options: Lint.IOptions) {
super(sourceFile, options);
}

protected visitNewExpression(node: ts.NewExpression): void {
const functionName = AstUtils.getFunctionName(node);
if (functionName === 'Function' && node.arguments !== undefined && node.arguments.length > 0) {
this.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (tsutils.isNewExpression(node)) {
const functionName = AstUtils.getFunctionName(node);
if (functionName === 'Function' && node.arguments && node.arguments.length > 0) {
ctx.addFailureAt(node.getStart(), node.getWidth(), Rule.FAILURE_STRING);
}
}
super.visitNewExpression(node);

return ts.forEachChild(node, cb);
}

return ts.forEachChild(ctx.sourceFile, cb);
}

0 comments on commit 506d04e

Please sign in to comment.