/
node-repl-tla.ts
337 lines (308 loc) · 9.26 KB
/
node-repl-tla.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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
import { expect } from 'chai';
import type { Key } from 'readline';
import { Stream } from 'stream';
import semver = require('semver');
import { ts } from '../helpers';
import type { ContextWithTsNodeUnderTest } from './helpers';
import { nodeSupportsEsmHooks } from '../helpers';
interface SharedObjects extends ContextWithTsNodeUnderTest {
TEST_DIR: string;
}
// Based on https://github.com/nodejs/node/blob/88799930794045795e8abac874730f9eba7e2300/test/parallel/test-repl-top-level-await.js
export async function upstreamTopLevelAwaitTests({
TEST_DIR,
tsNodeUnderTest,
}: SharedObjects) {
const PROMPT = 'await repl > ';
const putIn = new REPLStream();
const replService = tsNodeUnderTest.createRepl({
// @ts-ignore
stdin: putIn,
// @ts-ignore
stdout: putIn,
// @ts-ignore
stderr: putIn,
});
const service = tsNodeUnderTest.create({
...replService.evalAwarePartialHost,
project: `${TEST_DIR}/tsconfig.json`,
experimentalReplAwait: true,
transpileOnly: true,
compilerOptions: {
target: semver.gte(ts.version, '3.0.1')
? 'es2018'
: // TS 2.7 is using polyfill for async interator even though they
// were added in es2018
'esnext',
},
});
replService.setService(service);
(
replService.stdout as NodeJS.WritableStream & {
isTTY: boolean;
}
).isTTY = true;
const replServer = replService.startInternal({
prompt: PROMPT,
terminal: true,
useColors: true,
useGlobal: false,
});
function runAndWait(cmds: Array<string | Key>) {
const promise = putIn.wait();
for (const cmd of cmds) {
if (typeof cmd === 'string') {
putIn.run([cmd]);
} else {
replServer.write('', cmd);
}
}
return promise;
}
await runAndWait([
'function foo(x) { return x; }',
'function koo() { return Promise.resolve(4); }',
]);
const testCases = [
['await Promise.resolve(0)', '0'],
// issue: { a: await Promise.resolve(1) } is being interpreted as a block
// remove surrounding parenthesis once issue is fixed
['({ a: await Promise.resolve(1) })', '{ a: 1 }'],
['_', '{ a: 1 }'],
['let { aa, bb } = await Promise.resolve({ aa: 1, bb: 2 }), f = 5;'],
['aa', '1'],
['bb', '2'],
['f', '5'],
['let cc = await Promise.resolve(2)'],
['cc', '2'],
['let dd;'],
['dd'],
['let [ii, { abc: { kk } }] = [0, { abc: { kk: 1 } }];'],
['ii', '0'],
['kk', '1'],
['var ll = await Promise.resolve(2);'],
['ll', '2'],
['foo(await koo())', '4'],
['_', '4'],
['const m = foo(await koo());'],
['m', '4'],
// issue: REPL doesn't recognize end of input
// compile is returning TS1005 after second line even though
// it's valid syntax
// [
// 'const n = foo(await\nkoo());',
// ['const n = foo(await\r', '... koo());\r', 'undefined'],
// ],
[
'`status: ${(await Promise.resolve({ status: 200 })).status}`',
"'status: 200'",
],
['for (let i = 0; i < 2; ++i) await i'],
['for (let i = 0; i < 2; ++i) { await i }'],
['await 0', '0'],
['await 0; function foo() {}'],
['foo', '[Function: foo]'],
['class Foo {}; await 1;', '1'],
[
'Foo',
// Adjusted since ts-node supports older versions of node
semver.gte(process.version, '12.18.0')
? '[class Foo]'
: '[Function: Foo]',
],
['if (await true) { function fooz() {}; }'],
['fooz', '[Function: fooz]'],
['if (await true) { class Bar {}; }'],
[
'Bar',
// Adjusted since ts-node supports older versions of node
nodeSupportsEsmHooks
? 'Uncaught ReferenceError: Bar is not defined'
: 'ReferenceError: Bar is not defined',
// Line increased due to TS added lines
{
line: nodeSupportsEsmHooks ? 4 : 5,
},
],
['await 0; function* gen(){}'],
['for (var i = 0; i < 10; ++i) { await i; }'],
['i', '10'],
['for (let j = 0; j < 5; ++j) { await j; }'],
[
'j',
// Adjusted since ts-node supports older versions of node
nodeSupportsEsmHooks
? 'Uncaught ReferenceError: j is not defined'
: 'ReferenceError: j is not defined',
// Line increased due to TS added lines
{
line: nodeSupportsEsmHooks ? 4 : 5,
},
],
['gen', '[GeneratorFunction: gen]'],
[
'return 42; await 5;',
// Adjusted since ts-node supports older versions of node
nodeSupportsEsmHooks
? 'Uncaught SyntaxError: Illegal return statement'
: 'SyntaxError: Illegal return statement',
// Line increased due to TS added lines
{
line: nodeSupportsEsmHooks ? 4 : 5,
},
],
['let o = await 1, p'],
['p'],
['let q = 1, s = await 2'],
['s', '2'],
[
'for await (let i of [1,2,3]) console.log(i)',
[
'for await (let i of [1,2,3]) console.log(i)\r',
'1',
'2',
'3',
'undefined',
],
],
// issue: REPL is expecting more input to finish execution
// compiler is returning TS1003 error
// [
// 'await Promise..resolve()',
// [
// 'await Promise..resolve()\r',
// 'Uncaught SyntaxError: ',
// 'await Promise..resolve()',
// ' ^',
// '',
// "Unexpected token '.'",
// ],
// ],
[
'for (const x of [1,2,3]) {\nawait x\n}',
['for (const x of [1,2,3]) {\r', '... await x\r', '... }\r', 'undefined'],
],
[
'for (const x of [1,2,3]) {\nawait x;\n}',
[
'for (const x of [1,2,3]) {\r',
'... await x;\r',
'... }\r',
'undefined',
],
],
[
'for await (const x of [1,2,3]) {\nconsole.log(x)\n}',
[
'for await (const x of [1,2,3]) {\r',
'... console.log(x)\r',
'... }\r',
'1',
'2',
'3',
'undefined',
],
],
[
'for await (const x of [1,2,3]) {\nconsole.log(x);\n}',
[
'for await (const x of [1,2,3]) {\r',
'... console.log(x);\r',
'... }\r',
'1',
'2',
'3',
'undefined',
],
],
] as const;
for (const [
input,
expected = [`${input}\r`],
options = {} as { line?: number },
] of testCases) {
const toBeRun = input.split('\n');
const lines = await runAndWait(toBeRun);
if (Array.isArray(expected)) {
if (expected.length === 1) expected.push('undefined');
if (lines[0] === input) lines.shift();
expect(lines).to.eqls([...expected, PROMPT]);
} else if ('line' in options) {
expect(lines[toBeRun.length + options.line!]).to.eqls(expected);
} else {
const echoed = toBeRun.map((a, i) => `${i > 0 ? '... ' : ''}${a}\r`);
expect(lines).to.eqls([...echoed, expected, PROMPT]);
}
}
}
// copied from https://github.com/nodejs/node/blob/88799930794045795e8abac874730f9eba7e2300/lib/internal/util/inspect.js#L220-L227
// Regex used for ansi escape code splitting
// Adopted from https://github.com/chalk/ansi-regex/blob/HEAD/index.js
// License: MIT, authors: @sindresorhus, Qix-, arjunmehta and LitoMore
// Matches all ansi escape code sequences in a string
const ansiPattern =
'[\\u001B\\u009B][[\\]()#;?]*' +
'(?:(?:(?:[a-zA-Z\\d]*(?:;[-a-zA-Z\\d\\/#&.:=?%@~_]*)*)?\\u0007)' +
'|(?:(?:\\d{1,4}(?:;\\d{0,4})*)?[\\dA-PR-TZcf-ntqry=><~]))';
const ansi = new RegExp(ansiPattern, 'g');
// copied from https://github.com/nodejs/node/blob/88799930794045795e8abac874730f9eba7e2300/lib/internal/util/inspect.js#L2112-L2117
/**
* Remove all VT control characters. Use to estimate displayed string width.
*/
function stripVTControlCharacters(str: string) {
return str.replace(ansi, '');
}
// copied from https://github.com/nodejs/node/blob/88799930794045795e8abac874730f9eba7e2300/test/parallel/test-repl-top-level-await.js
class ArrayStream extends Stream {
readable = true;
writable = true;
run(data: string[]) {
data.forEach((line) => {
this.emit('data', `${line}\n`);
});
}
pause() {}
resume() {}
write(_chunk: Buffer | string, _encoding: string, _callback: () => {}) {}
}
export class REPLStream extends ArrayStream {
waitingForResponse = false;
lines = [''];
constructor() {
super();
}
write(chunk: Buffer | string, encoding: string, callback: () => void) {
if (Buffer.isBuffer(chunk)) {
chunk = chunk.toString(encoding);
}
const chunkLines = stripVTControlCharacters(chunk).split('\n');
this.lines[this.lines.length - 1] += chunkLines[0];
if (chunkLines.length > 1) {
this.lines.push(...chunkLines.slice(1));
}
this.emit('line');
if (callback) callback();
return true;
}
wait(): Promise<string[]> {
if (this.waitingForResponse) {
throw new Error('Currently waiting for response to another command');
}
this.lines = [''];
return new Promise((resolve, reject) => {
const onError = (err: any) => {
this.removeListener('line', onLine);
reject(err);
};
const onLine = () => {
if (this.lines[this.lines.length - 1].includes('> ')) {
this.removeListener('error', onError);
this.removeListener('line', onLine);
resolve(this.lines);
}
};
this.once('error', onError);
this.on('line', onLine);
});
}
}