From 2a81660c459fee65b3f2b39af4af4b790c9fbf52 Mon Sep 17 00:00:00 2001 From: yokomotod Date: Mon, 3 Jul 2017 16:55:28 +0900 Subject: [PATCH] Fix #2986 await-promise accept intersection --- src/rules/awaitPromiseRule.ts | 8 ++------ test/rules/await-promise/custom-promise/test.ts.lint | 8 ++++++++ test/rules/await-promise/es6-promise/test.ts.lint | 4 ++++ 3 files changed, 14 insertions(+), 6 deletions(-) diff --git a/src/rules/awaitPromiseRule.ts b/src/rules/awaitPromiseRule.ts index 1cc245c62b6..4bf3f0c1fec 100644 --- a/src/rules/awaitPromiseRule.ts +++ b/src/rules/awaitPromiseRule.ts @@ -15,7 +15,7 @@ * limitations under the License. */ -import { isAwaitExpression } from "tsutils"; +import { isAwaitExpression, isUnionOrIntersectionType } from "tsutils"; import * as ts from "typescript"; import * as Lint from "../index"; @@ -65,7 +65,7 @@ function walk(ctx: Lint.WalkContext, tc: ts.TypeChecker, promiseTypes: Set return true; } - if (isUnionType(type)) { + if (isUnionOrIntersectionType(type)) { return type.types.some(couldBePromise); } @@ -78,7 +78,3 @@ function walk(ctx: Lint.WalkContext, tc: ts.TypeChecker, promiseTypes: Set return target !== undefined && target.symbol !== undefined && promiseTypes.has(target.symbol.name); } } - -function isUnionType(type: ts.Type): type is ts.UnionType { - return Lint.isTypeFlagSet(type, ts.TypeFlags.Union); -} diff --git a/test/rules/await-promise/custom-promise/test.ts.lint b/test/rules/await-promise/custom-promise/test.ts.lint index 1b89af2d1b0..5d8221fdc7f 100644 --- a/test/rules/await-promise/custom-promise/test.ts.lint +++ b/test/rules/await-promise/custom-promise/test.ts.lint @@ -44,6 +44,10 @@ async function fStandardPromise() { await (Math.random() > 0.5 ? numberPromise : 0); await (Math.random() > 0.5 ? foo : 0); await (Math.random() > 0.5 ? bar : 0); + + // intersection type + const intersectionPromise: Promise & number; + await intersectionPromise; } async function fCustomPromise() { @@ -65,6 +69,10 @@ async function fCustomPromise() { await (Math.random() > 0.5 ? numberPromise : 0); await (Math.random() > 0.5 ? foo : 0); await (Math.random() > 0.5 ? bar : 0); + + // intersection type + const intersectionPromise: CustomPromise & number; + await intersectionPromise; } [0]: 'await' of non-Promise. diff --git a/test/rules/await-promise/es6-promise/test.ts.lint b/test/rules/await-promise/es6-promise/test.ts.lint index b049ec8c3c3..2d8ed107a56 100644 --- a/test/rules/await-promise/es6-promise/test.ts.lint +++ b/test/rules/await-promise/es6-promise/test.ts.lint @@ -42,6 +42,10 @@ async function fPromise() { await (Math.random() > 0.5 ? numberPromise : 0); await (Math.random() > 0.5 ? foo : 0); await (Math.random() > 0.5 ? bar : 0); + + // intersection type + const intersectionPromise: Promise & number; + await intersectionPromise; } [0]: 'await' of non-Promise.