Skip to content

Commit

Permalink
convert prefer-type-cast rule to use a walk function (microsoft#776)
Browse files Browse the repository at this point in the history
  • Loading branch information
drexler authored and apawast committed Feb 26, 2019
1 parent e0917a9 commit 62513ac
Showing 1 changed file with 9 additions and 10 deletions.
19 changes: 9 additions & 10 deletions src/preferTypeCastRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,20 @@ export class Rule extends Lint.Rules.AbstractRule {
};

public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] {
return this.applyWithWalker(new PreferTypeCastRuleWalker(sourceFile, this.getOptions()));
return this.applyWithFunction(sourceFile, walk);
}
}

class PreferTypeCastRuleWalker extends Lint.RuleWalker {
protected visitSourceFile(node: ts.SourceFile): void {
if (AstUtils.getLanguageVariant(node) === ts.LanguageVariant.Standard) {
super.visitSourceFile(node);
function walk(ctx: Lint.WalkContext<void>) {
function cb(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.AsExpression) {
ctx.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + node.getText());
}

return ts.forEachChild(node, cb);
}

protected visitNode(node: ts.Node): void {
if (node.kind === ts.SyntaxKind.AsExpression) {
this.addFailureAt(node.getStart(), node.getWidth(), FAILURE_STRING + node.getText());
}
super.visitNode(node);
if (AstUtils.getLanguageVariant(ctx.sourceFile) === ts.LanguageVariant.Standard) {
return ts.forEachChild(ctx.sourceFile, cb);
}
}

0 comments on commit 62513ac

Please sign in to comment.