From c1c8e3844076039aa7dacc6cfc66b0472f3b7158 Mon Sep 17 00:00:00 2001 From: Andy Hanson Date: Sun, 7 May 2017 09:55:24 -0700 Subject: [PATCH] one-variable-per-declaration: Convert to function --- src/rules/oneVariablePerDeclarationRule.ts | 40 ++++++++-------------- 1 file changed, 15 insertions(+), 25 deletions(-) diff --git a/src/rules/oneVariablePerDeclarationRule.ts b/src/rules/oneVariablePerDeclarationRule.ts index db191ef8205..43b3a3f2e4e 100644 --- a/src/rules/oneVariablePerDeclarationRule.ts +++ b/src/rules/oneVariablePerDeclarationRule.ts @@ -15,6 +15,7 @@ * limitations under the License. */ +import { isForStatement, isVariableStatement } from "tsutils"; import * as ts from "typescript"; import * as Lint from "../index"; @@ -48,33 +49,22 @@ export class Rule extends Lint.Rules.AbstractRule { public static FAILURE_STRING = "Multiple variable declarations in the same statement are forbidden"; public apply(sourceFile: ts.SourceFile): Lint.RuleFailure[] { - const oneVarWalker = new OneVariablePerDeclarationWalker(sourceFile, this.getOptions()); - return this.applyWithWalker(oneVarWalker); + return this.applyWithFunction(sourceFile, walk, { ignoreForLoop: this.ruleArguments.indexOf(OPTION_IGNORE_FOR_LOOP) !== -1 }); } } -class OneVariablePerDeclarationWalker extends Lint.RuleWalker { - public visitVariableStatement(node: ts.VariableStatement) { - const { declarationList } = node; - - if (declarationList.declarations.length > 1) { - this.addFailureAtNode(node, Rule.FAILURE_STRING); - } - - super.visitVariableStatement(node); - } - - public visitForStatement(node: ts.ForStatement) { - const initializer = node.initializer as ts.VariableDeclarationList; - const shouldIgnoreForLoop = this.hasOption(OPTION_IGNORE_FOR_LOOP); - - if (!shouldIgnoreForLoop - && initializer != null - && initializer.kind === ts.SyntaxKind.VariableDeclarationList - && initializer.declarations.length > 1) { - this.addFailureAtNode(initializer, Rule.FAILURE_STRING); +function walk(ctx: Lint.WalkContext<{ ignoreForLoop: boolean }>): void { + ts.forEachChild(ctx.sourceFile, function cb(node) { + if (isVariableStatement(node) && node.declarationList.declarations.length > 1) { + ctx.addFailureAtNode(node, Rule.FAILURE_STRING); + } else if (isForStatement(node) && !ctx.options.ignoreForLoop) { + const { initializer } = node; + if (initializer !== undefined + && initializer.kind === ts.SyntaxKind.VariableDeclarationList + && (initializer as ts.VariableDeclarationList).declarations.length > 1) { + ctx.addFailureAtNode(initializer, Rule.FAILURE_STRING); + } } - - super.visitForStatement(node); - } + ts.forEachChild(node, cb); + }); }