forked from typescript-eslint/typescript-eslint
/
await-thenable.test.ts
223 lines (203 loc) · 5.63 KB
/
await-thenable.test.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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
import rule from '../../src/rules/await-thenable';
import { RuleTester, getFixturesRootDir } from '../RuleTester';
const rootDir = getFixturesRootDir();
const parserOptions = {
ecmaVersion: 2018,
tsconfigRootDir: rootDir,
project: './tsconfig.json',
};
const messageId = 'await';
const ruleTester = new RuleTester({
parserOptions,
parser: '@typescript-eslint/parser',
});
ruleTester.run('await-promise', rule, {
valid: [
`
async function test() {
await Promise.resolve("value");
await Promise.reject(new Error("message"));
}
`,
`
async function test() {
await (async () => true)();
}
`,
`
async function test() {
function returnsPromise() {
return Promise.resolve("value");
}
await returnsPromise();
}
`,
`
async function test() {
async function returnsPromiseAsync() {}
await returnsPromiseAsync();
}
`,
`
async function test() {
let anyValue: any;
await anyValue;
}
`,
`
async function test() {
let unknownValue: unknown;
await unknownValue;
}
`,
`
async function test() {
const numberPromise: Promise<number>;
await numberPromise;
}
`,
`
async function test() {
class Foo extends Promise<number> {}
const foo: Foo = Foo.resolve(2);
await foo;
class Bar extends Foo {}
const bar: Bar = Bar.resolve(2);
await bar;
}
`,
`
async function test() {
await (Math.random() > 0.5 ? numberPromise : 0);
await (Math.random() > 0.5 ? foo : 0);
await (Math.random() > 0.5 ? bar : 0);
const intersectionPromise: Promise<number> & number;
await intersectionPromise;
}
`,
`
async function test() {
class Thenable { then(callback: () => {}) { } };
const thenable = new Thenable();
await thenable;
}
`,
`
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/promise-polyfill/index.d.ts
// Type definitions for promise-polyfill 6.0
// Project: https://github.com/taylorhakes/promise-polyfill
// Definitions by: Steve Jenkins <https://github.com/skysteve>
// Daniel Cassidy <https://github.com/djcsdy>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
interface PromisePolyfillConstructor extends PromiseConstructor {
_immediateFn?: (handler: (() => void) | string) => void;
}
declare const PromisePolyfill: PromisePolyfillConstructor;
async function test() {
const promise = new PromisePolyfill(() => {});
await promise;
}
`,
`
// https://github.com/DefinitelyTyped/DefinitelyTyped/blob/master/types/bluebird/index.d.ts
// Type definitions for bluebird 3.5
// Project: https://github.com/petkaantonov/bluebird
// Definitions by: Leonard Hecker <https://github.com/lhecker>
// Definitions: https://github.com/DefinitelyTyped/DefinitelyTyped
// TypeScript Version: 2.8
/*!
* The code following this comment originates from:
* https://github.com/types/npm-bluebird
*
* Note for browser users: use bluebird-global typings instead of this one
* if you want to use Bluebird via the global Promise symbol.
*
* Licensed under:
* The MIT License (MIT)
*
* Copyright (c) 2016 unional
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
type Constructor<E> = new (...args: any[]) => E;
type CatchFilter<E> = ((error: E) => boolean) | (object & E);
type IterableItem<R> = R extends Iterable<infer U> ? U : never;
type IterableOrNever<R> = Extract<R, Iterable<any>>;
type Resolvable<R> = R | PromiseLike<R>;
type IterateFunction<T, R> = (item: T, index: number, arrayLength: number) => Resolvable<R>;
declare class Bluebird<R> implements PromiseLike<R> {
then<U>(onFulfill?: (value: R) => Resolvable<U>, onReject?: (error: any) => Resolvable<U>): Bluebird<U>; // For simpler signature help.
then<TResult1 = R, TResult2 = never>(
onfulfilled?: ((value: R) => Resolvable<TResult1>) | null,
onrejected?: ((reason: any) => Resolvable<TResult2>) | null
): Bluebird<TResult1 | TResult2>;
}
declare const bluebird: Bluebird;
async function test() {
await bluebird;
}
`,
],
invalid: [
{
code: `
async function test() {
await 0;
await "value";
await (Math.random() > 0.5 ? "" : 0);
class NonPromise extends Array {}
await new NonPromise();
}
`,
errors: [
{
line: 3,
messageId,
},
{
line: 4,
messageId,
},
{
line: 6,
messageId,
},
{
line: 9,
messageId,
},
],
},
{
code: `
async function test() {
class IncorrectThenable { then() { } };
const thenable = new IncorrectThenable();
await thenable;
}
`,
errors: [
{
line: 6,
messageId,
},
],
},
],
});