From 240d9a0c0a51b8f627688e2637df052fd1b8bbc6 Mon Sep 17 00:00:00 2001 From: Klaus Meinhardt Date: Sat, 13 May 2017 05:20:39 +0200 Subject: [PATCH] no-inferred-empty-object-type: Refactor to AbstractWalker (#2750) --- src/rules/noInferredEmptyObjectTypeRule.ts | 30 +++++++++++++--------- 1 file changed, 18 insertions(+), 12 deletions(-) diff --git a/src/rules/noInferredEmptyObjectTypeRule.ts b/src/rules/noInferredEmptyObjectTypeRule.ts index 84ec9565299..7ee5e91a782 100644 --- a/src/rules/noInferredEmptyObjectTypeRule.ts +++ b/src/rules/noInferredEmptyObjectTypeRule.ts @@ -37,21 +37,29 @@ export class Rule extends Lint.Rules.TypedRule { public static EMPTY_INTERFACE_FUNCTION = "Explicit type parameter needs to be provided to the function call"; public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] { - return this.applyWithWalker(new NoInferredEmptyObjectTypeRule(sourceFile, this.getOptions(), program)); + return this.applyWithWalker(new NoInferredEmptyObjectTypeRule(sourceFile, this.ruleName, program.getTypeChecker())); } } -class NoInferredEmptyObjectTypeRule extends Lint.ProgramAwareRuleWalker { - private checker: ts.TypeChecker; +class NoInferredEmptyObjectTypeRule extends Lint.AbstractWalker { + constructor(sourceFile: ts.SourceFile, ruleName: string, private checker: ts.TypeChecker) { + super(sourceFile, ruleName, undefined); + } - constructor(srcFile: ts.SourceFile, lintOptions: Lint.IOptions, program: ts.Program) { - super(srcFile, lintOptions, program); - this.checker = this.getTypeChecker(); + public walk(sourceFile: ts.SourceFile) { + const cb = (node: ts.Node): void => { + if (node.kind === ts.SyntaxKind.CallExpression) { + this.checkCallExpression(node as ts.CallExpression); + } else if (node.kind === ts.SyntaxKind.NewExpression) { + this.checkNewExpression(node as ts.NewExpression); + } + return ts.forEachChild(node, cb); + }; + return ts.forEachChild(sourceFile, cb); } - public visitNewExpression(node: ts.NewExpression): void { - const nodeTypeArgs = node.typeArguments; - if (nodeTypeArgs === undefined) { + private checkNewExpression(node: ts.NewExpression): void { + if (node.typeArguments === undefined) { const objType = this.checker.getTypeAtLocation(node) as ts.TypeReference; if (isTypeFlagSet(objType, ts.TypeFlags.Object) && objType.typeArguments !== undefined) { const typeArgs = objType.typeArguments as ts.ObjectType[]; @@ -60,10 +68,9 @@ class NoInferredEmptyObjectTypeRule extends Lint.ProgramAwareRuleWalker { } } } - super.visitNewExpression(node); } - public visitCallExpression(node: ts.CallExpression): void { + private checkCallExpression(node: ts.CallExpression): void { if (node.typeArguments === undefined) { const callSig = this.checker.getResolvedSignature(node); const retType = this.checker.getReturnTypeOfSignature(callSig) as ts.TypeReference; @@ -71,7 +78,6 @@ class NoInferredEmptyObjectTypeRule extends Lint.ProgramAwareRuleWalker { this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION); } } - super.visitCallExpression(node); } private isEmptyObjectInterface(objType: ts.ObjectType): boolean {