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

Commit

Permalink
Code style fixes in promiseFunctionAsyncRule (palantir#1793)
Browse files Browse the repository at this point in the history
To promote readability
  • Loading branch information
adidahiya authored and nchen63 committed Nov 28, 2016
1 parent 2b09bb2 commit 94d62ff
Showing 1 changed file with 10 additions and 14 deletions.
24 changes: 10 additions & 14 deletions src/rules/promiseFunctionAsyncRule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -47,43 +47,39 @@ export class Rule extends Lint.Rules.TypedRule {

class PromiseAsyncWalker extends Lint.ProgramAwareRuleWalker {
public visitArrowFunction(node: ts.ArrowFunction) {
this.test(node);
this.handleDeclaration(node);
super.visitArrowFunction(node);
}

public visitFunctionDeclaration(node: ts.FunctionDeclaration) {
this.test(node);
this.handleDeclaration(node);
super.visitFunctionDeclaration(node);
}

public visitFunctionExpression(node: ts.FunctionExpression) {
this.test(node);
this.handleDeclaration(node);
super.visitFunctionExpression(node);
}

public visitMethodDeclaration(node: ts.MethodDeclaration) {
this.test(node);
this.handleDeclaration(node);
super.visitMethodDeclaration(node);
}

private test(node: ts.SignatureDeclaration & { body?: ts.Node}) {
private handleDeclaration(node: ts.SignatureDeclaration & { body?: ts.Node }) {
const tc = this.getTypeChecker();

const signature = tc.getTypeAtLocation(node).getCallSignatures()[0];
const returnType = tc.typeToString(tc.getReturnTypeOfSignature(signature));

const isAsync = Lint.hasModifier(node.modifiers, ts.SyntaxKind.AsyncKeyword);
const isPromise = returnType.indexOf("Promise<") === 0;

const signatureEnd = node.body ?
node.body.getStart() - node.getStart() - 1 :
node.getWidth()
;
const signatureEnd = node.body != null
? node.body.getStart() - node.getStart() - 1
: node.getWidth();

if (isAsync || !isPromise) {
return;
if (isPromise && !isAsync) {
this.addFailure(this.createFailure(node.getStart(), signatureEnd, Rule.FAILURE_STRING));
}

this.addFailure(this.createFailure(node.getStart(), signatureEnd, Rule.FAILURE_STRING));
}
}

0 comments on commit 94d62ff

Please sign in to comment.