/
prefer-top-level-await.mjs
189 lines (183 loc) · 3.19 KB
/
prefer-top-level-await.mjs
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
import outdent from 'outdent';
import {getTester} from './utils/test.mjs';
const {test} = getTester(import.meta);
// Async IIFE
test.snapshot({
valid: [
'a()',
'a = async () => {}',
'(async function *() {})()',
outdent`
function foo() {
if (foo) {
(async () => {})()
}
}
`,
'await (async () => {})()',
],
invalid: [
'(async () => {})()',
'(async () => {})?.()',
'(async function() {})()',
'(async function() {}())',
'(async function run() {})()',
'(async function(c, d) {})(a, b)',
'if (foo) (async () => {})()',
outdent`
{
(async () => {})();
}
`,
'a = (async () => {})()',
'!async function() {}()',
'void async function() {}()',
'(async () => {})().catch(foo)',
],
});
// Promise
test.snapshot({
valid: [
'foo.then',
'await foo.then(bar)',
'await foo.then(bar).catch(bar)',
'await foo.then?.(bar)',
'await foo.then(bar)?.catch(bar)',
'await foo.then(bar)?.catch?.(bar)',
],
invalid: [
'foo.then(bar)',
'foo.then?.(bar)',
'foo?.then(bar)',
'foo.catch(() => process.exit(1))',
'foo.finally(bar)',
'foo.then(bar, baz)',
'foo.then(bar, baz).finally(qux)',
'(foo.then(bar, baz)).finally(qux)',
'(async () => {})().catch(() => process.exit(1))',
'(async function() {}()).finally(() => {})',
'for (const foo of bar) foo.then(bar)',
'foo?.then(bar).finally(qux)',
'foo.then().toString()',
'!foo.then()',
'foo.then(bar).then(baz)?.then(qux)',
'foo.then(bar).then(baz).then?.(qux)',
'foo.then(bar).catch(bar).finally(bar)',
],
});
// Identifier
test.snapshot({
valid: [
'foo()',
'foo.bar()',
outdent`
function foo() {
return async () => {};
}
foo()();
`,
outdent`
const [foo] = [async () => {}];
foo();
`,
outdent`
function foo() {}
foo();
`,
outdent`
async function * foo() {}
foo();
`,
outdent`
var foo = async () => {};
foo();
`,
outdent`
let foo = async () => {};
foo();
`,
outdent`
const foo = 1, bar = async () => {};
foo();
`,
outdent`
async function foo() {}
const bar = foo;
bar();
`,
{
code: outdent`
async function foo() {}
async function foo() {}
foo();
`,
parserOptions: {sourceType: 'script'},
},
{
code: outdent`
foo();
async function foo() {}
async function foo() {}
`,
parserOptions: {sourceType: 'script'},
},
outdent`
const program = {async run () {}};
program.run()
`,
outdent`
const program = {async run () {}};
const {run} = program;
run()
`,
outdent`
const foo = async () => {};
await foo();
`,
'for (const statement of statements) { statement() };',
],
invalid: [
outdent`
const foo = async () => {};
foo();
`,
outdent`
const foo = async () => {};
foo?.();
`,
outdent`
const foo = async () => {};
foo().then(foo);
`,
outdent`
const foo = async function () {}, bar = 1;
foo(bar);
`,
outdent`
foo();
async function foo() {}
`,
outdent`
const foo = async () => {};
if (true) {
alert();
} else {
foo();
}
`,
],
});
test.babel({
valid: [
'await foo',
'await foo()',
outdent`
try {
await run()
} catch {
process.exit(1)
}
`,
],
invalid: [],
});