This repository has been archived by the owner on Mar 25, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 887
/
awaitPromiseRule.ts
117 lines (106 loc) · 4.34 KB
/
awaitPromiseRule.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
/**
* @license
* Copyright 2017 Palantir Technologies, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
import {
isAwaitExpression,
isForOfStatement,
isTypeFlagSet,
isTypeReference,
isUnionOrIntersectionType,
} from "tsutils";
import * as ts from "typescript";
import * as Lint from "../index";
export class Rule extends Lint.Rules.TypedRule {
/* tslint:disable:object-literal-sort-keys */
public static metadata: Lint.IRuleMetadata = {
ruleName: "await-promise",
description: "Warns for an awaited value that is not a Promise.",
optionsDescription: Lint.Utils.dedent`
A list of 'string' names of any additional classes that should also be treated as Promises.
For example, if you are using a class called 'Future' that implements the Thenable interface,
you might tell the rule to consider type references with the name 'Future' as valid Promise-like
types. Note that this rule doesn't check for type assignability or compatibility; it just checks
type reference names.
`,
options: {
type: "list",
listType: {
type: "array",
items: { type: "string" },
},
},
optionExamples: [true, [true, "Thenable"]],
rationale: Lint.Utils.dedent`
While it is valid JavaScript to await a non-Promise-like value (it will resolve immediately),
this pattern is often a programmer error and the resulting semantics can be unintuitive.
Awaiting non-Promise-like values often is an indication of programmer error, such as
forgetting to add parenthesis to call a function that returns a Promise.
`,
type: "functionality",
typescriptOnly: true,
requiresTypeInfo: true,
};
/* tslint:enable:object-literal-sort-keys */
public static FAILURE_STRING = "Invalid 'await' of a non-Promise value.";
public static FAILURE_FOR_AWAIT_OF = "Invalid 'for-await-of' of a non-AsyncIterable value.";
public applyWithProgram(sourceFile: ts.SourceFile, program: ts.Program): Lint.RuleFailure[] {
const promiseTypes = new Set(["Promise", ...(this.ruleArguments as string[])]);
return this.applyWithFunction(sourceFile, walk, promiseTypes, program.getTypeChecker());
}
}
function walk(ctx: Lint.WalkContext<Set<string>>, tc: ts.TypeChecker) {
const promiseTypes = ctx.options;
return ts.forEachChild(ctx.sourceFile, cb);
function cb(node: ts.Node): void {
if (
isAwaitExpression(node) &&
!containsType(tc.getTypeAtLocation(node.expression), isPromiseType)
) {
ctx.addFailureAtNode(node, Rule.FAILURE_STRING);
} else if (
isForOfStatement(node) &&
node.awaitModifier !== undefined &&
!containsType(tc.getTypeAtLocation(node.expression), isAsyncIterable)
) {
ctx.addFailureAtNode(node.expression, Rule.FAILURE_FOR_AWAIT_OF);
}
return ts.forEachChild(node, cb);
}
function isPromiseType(name: string) {
return promiseTypes.has(name);
}
}
function containsType(type: ts.Type, predicate: (name: string) => boolean): boolean {
if (isTypeFlagSet(type, ts.TypeFlags.Any)) {
return true;
}
if (isTypeReference(type)) {
type = type.target;
}
if (type.symbol !== undefined && predicate(type.symbol.name)) {
return true;
}
if (isUnionOrIntersectionType(type)) {
return type.types.some(t => containsType(t, predicate));
}
const bases = type.getBaseTypes();
return bases !== undefined && bases.some(t => containsType(t, predicate));
}
function isAsyncIterable(name: string) {
return (
name === "AsyncIterable" || name === "AsyncIterableIterator" || name === "AsyncGenerator"
);
}