Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add isPromiseType. #29

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions test/rules/promise-use/promise.ts.lint
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
function returnsPromise() {
return Promise.resolve(true);
}

type Future = Promise<number>;
function aliasedPromise(): Future {
return Promise.resolve(1);
}

class Extended extends Promise<number> {}
function extendedPromise(): Extended {
return Promise.resolve(1);
}

class DoubleExtended extends Extended {}
function doubleExtendedPromise(): Extended {
return Promise.resolve(1);
}

function maybePromise(): Promise<number>|number {
return 3;
}

async function returnLaterPromise() {
return () => Promise.resolve(1);
}

function unusedPromises() {
returnsPromise();
~~~~~~~~~~~~~~~~ [Promise]
maybePromise();
~~~~~~~~~~~~~~ [Promise]
aliasedPromise();
~~~~~~~~~~~~~~~~ [Promise]
extendedPromise();
~~~~~~~~~~~~~~~~~ [Promise]
doubleExtendedPromise();
~~~~~~~~~~~~~~~~~~~~~~~ [Promise]
const later = await returnLaterPromise();
later();
~~~~~~~ [Promise]
}
37 changes: 37 additions & 0 deletions test/rules/promise-use/testPromiseUseRule.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import * as ts from 'typescript';
import * as Lint from 'tslint';
import { isCallExpression } from "../../../typeguard/node";
import { isExpressionValueUsed } from "../../../util/util";
import { isPromiseType } from '../../../util/type';

export class Rule extends Lint.Rules.TypedRule {
applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
return this.applyWithFunction(sourceFile, (ctx) => walk(ctx, program));
}
}

function walk(ctx: Lint.WalkContext<void>, program: ts.Program) {
const cb = (node: ts.Node): void => {
if (isCallExpression(node)) {
if (!isExpressionValueUsed(node) && isPromiseExpression(node, program)) {
ctx.addFailureAtNode(node, 'Promise');
}
}
return ts.forEachChild(node, cb);
};
return ts.forEachChild(ctx.sourceFile, cb);
}

function isPromiseExpression(node: ts.CallExpression, program: ts.Program) {
const checker = program.getTypeChecker();
const signature = checker.getResolvedSignature(node);
if (signature === undefined) {
return false;
}
const returnType = checker.getReturnTypeOfSignature(signature);
if (!!(returnType.flags & ts.TypeFlags.Void)) {
return false;
}

return isPromiseType(returnType);
}
26 changes: 26 additions & 0 deletions test/rules/promise-use/tsconfig.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"compilerOptions": {
"target": "es6",
"module": "commonjs",
"removeComments": true,
"inlineSourceMap": true,
"preserveConstEnums": true,
"noImplicitAny": true,
"noImplicitThis": true,
"suppressImplicitAnyIndexErrors": true,
"strictFunctionTypes": true,
"noUnusedLocals": true,
"noUnusedParameters": true,
"strictNullChecks": true,
"stripInternal": true,
"lib": ["es2016"],
"skipLibCheck": true,
"declaration": true,
"importHelpers": true
},
"exclude": [
],
"files": [
"../../type/promise.ts"
]
}
8 changes: 8 additions & 0 deletions test/rules/promise-use/tslint.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"rulesDirectory": [
"."
],
"rules":{
"test-promise-use": true
}
}
19 changes: 19 additions & 0 deletions util/type.ts
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,25 @@ function isTypeAssignableTo(checker: ts.TypeChecker, type: ts.Type, flags: ts.Ty
})(type);
}

/**
* Returns true if the given type is a union type that includes Promise or a type that extends
* Promise.
*/
export function isPromiseType(type: ts.Type): boolean {
const isPromise = (t: ts.Type) => {
const sym = t.getSymbol();
if (sym !== undefined) return sym.name === 'Promise';
return false;
};

const baseTypes = type.getBaseTypes();
if (baseTypes && baseTypes.some(isPromise)) return true;

if (isUnionType(type) || isIntersectionType(type)) return type.types.some(isPromise);

return isPromise(type);
}

export function getCallSignaturesOfType(type: ts.Type): ts.Signature[] {
if (isUnionType(type)) {
const signatures = [];
Expand Down