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

Fix #2986 await-promise accept intersection #2987

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
8 changes: 2 additions & 6 deletions src/rules/awaitPromiseRule.ts
Expand Up @@ -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";

Expand Down Expand Up @@ -65,7 +65,7 @@ function walk(ctx: Lint.WalkContext<void>, tc: ts.TypeChecker, promiseTypes: Set
return true;
}

if (isUnionType(type)) {
if (isUnionOrIntersectionType(type)) {
return type.types.some(couldBePromise);
}

Expand All @@ -78,7 +78,3 @@ function walk(ctx: Lint.WalkContext<void>, 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);
}
8 changes: 8 additions & 0 deletions test/rules/await-promise/custom-promise/test.ts.lint
Expand Up @@ -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> & number;
await intersectionPromise;
}

async function fCustomPromise() {
Expand All @@ -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> & number;
await intersectionPromise;
}

[0]: 'await' of non-Promise.
4 changes: 4 additions & 0 deletions test/rules/await-promise/es6-promise/test.ts.lint
Expand Up @@ -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> & number;
await intersectionPromise;
}

[0]: 'await' of non-Promise.