Skip to content

Commit 8e1958d

Browse files
authoredMar 20, 2023
fix(repl): suppress autocomplete transformation errors (#205)
1 parent afa3c78 commit 8e1958d

File tree

2 files changed

+47
-19
lines changed

2 files changed

+47
-19
lines changed
 

‎src/patch-repl.ts

+18-19
Original file line numberDiff line numberDiff line change
@@ -4,28 +4,27 @@ import { transform } from '@esbuild-kit/core-utils';
44
function patchEval(nodeRepl: REPLServer) {
55
const { eval: defaultEval } = nodeRepl;
66
const preEval: REPLEval = async function (code, context, filename, callback) {
7-
const transformed = await transform(
8-
code,
9-
filename,
10-
{
11-
loader: 'ts',
12-
tsconfigRaw: {
13-
compilerOptions: {
14-
preserveValueImports: true,
7+
try {
8+
const transformed = await transform(
9+
code,
10+
filename,
11+
{
12+
loader: 'ts',
13+
tsconfigRaw: {
14+
compilerOptions: {
15+
preserveValueImports: true,
16+
},
17+
},
18+
define: {
19+
require: 'global.require',
1520
},
1621
},
17-
define: {
18-
require: 'global.require',
19-
},
20-
},
21-
).catch(
22-
(error) => {
23-
console.log(error.message);
24-
return { code: '\n' };
25-
},
26-
);
22+
);
23+
24+
code = transformed.code;
25+
} catch {}
2726

28-
return defaultEval.call(this, transformed.code, context, filename, callback);
27+
return defaultEval.call(this, code, context, filename, callback);
2928
};
3029

3130
// @ts-expect-error overwriting read-only property

‎tests/specs/repl.ts

+29
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,35 @@ export default testSuite(async ({ describe }, node: NodeApis) => {
5757
tsxProcess.kill();
5858
}, 40_000);
5959

60+
test('supports incomplete expression in segments', async () => {
61+
const tsxProcess = node.tsx({
62+
args: ['--interactive'],
63+
});
64+
65+
const commands = [
66+
['> ', '('],
67+
['... ', '1'],
68+
['... ', ')'],
69+
['1'],
70+
];
71+
72+
let [expected, nextCommand] = commands.shift()!;
73+
await new Promise<void>((resolve) => {
74+
tsxProcess.stdout!.on('data', (data: Buffer) => {
75+
const chunkString = data.toString();
76+
if (chunkString.includes(expected)) {
77+
if (nextCommand) {
78+
tsxProcess.stdin!.write(`${nextCommand}\r`);
79+
[expected, nextCommand] = commands.shift()!;
80+
} else {
81+
resolve();
82+
}
83+
}
84+
});
85+
});
86+
tsxProcess.kill();
87+
}, 40_000);
88+
6089
test('errors on import statement', async () => {
6190
const tsxProcess = node.tsx({
6291
args: ['--interactive'],

0 commit comments

Comments
 (0)
Please sign in to comment.