Skip to content
This repository has been archived by the owner on Mar 25, 2021. It is now read-only.

Commit

Permalink
no-inferred-empty-object-type: Refactor to AbstractWalker (#2750)
Browse files Browse the repository at this point in the history
  • Loading branch information
ajafff authored and nchen63 committed May 13, 2017
1 parent 294242e commit 240d9a0
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions src/rules/noInferredEmptyObjectTypeRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<void> {
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[];
Expand All @@ -60,18 +68,16 @@ 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;
if (this.isEmptyObjectInterface(retType)) {
this.addFailureAtNode(node, Rule.EMPTY_INTERFACE_FUNCTION);
}
}
super.visitCallExpression(node);
}

private isEmptyObjectInterface(objType: ts.ObjectType): boolean {
Expand Down

0 comments on commit 240d9a0

Please sign in to comment.