Skip to content

Commit

Permalink
Fix palantir#2986 await-promise accept intersection (palantir#2987)
Browse files Browse the repository at this point in the history
  • Loading branch information
yokomotod authored and HyphnKnight committed Apr 9, 2018
1 parent 3f87ac7 commit 0c3ce52
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 6 deletions.
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.

0 comments on commit 0c3ce52

Please sign in to comment.