From 8e1958db41addf52bbe10505099b9e8ed223e62c Mon Sep 17 00:00:00 2001 From: hiroki osame Date: Mon, 20 Mar 2023 13:26:16 -0400 Subject: [PATCH] fix(repl): suppress autocomplete transformation errors (#205) --- src/patch-repl.ts | 37 ++++++++++++++++++------------------- tests/specs/repl.ts | 29 +++++++++++++++++++++++++++++ 2 files changed, 47 insertions(+), 19 deletions(-) diff --git a/src/patch-repl.ts b/src/patch-repl.ts index c7ece509..c81dc026 100644 --- a/src/patch-repl.ts +++ b/src/patch-repl.ts @@ -4,28 +4,27 @@ import { transform } from '@esbuild-kit/core-utils'; function patchEval(nodeRepl: REPLServer) { const { eval: defaultEval } = nodeRepl; const preEval: REPLEval = async function (code, context, filename, callback) { - const transformed = await transform( - code, - filename, - { - loader: 'ts', - tsconfigRaw: { - compilerOptions: { - preserveValueImports: true, + try { + const transformed = await transform( + code, + filename, + { + loader: 'ts', + tsconfigRaw: { + compilerOptions: { + preserveValueImports: true, + }, + }, + define: { + require: 'global.require', }, }, - define: { - require: 'global.require', - }, - }, - ).catch( - (error) => { - console.log(error.message); - return { code: '\n' }; - }, - ); + ); + + code = transformed.code; + } catch {} - return defaultEval.call(this, transformed.code, context, filename, callback); + return defaultEval.call(this, code, context, filename, callback); }; // @ts-expect-error overwriting read-only property diff --git a/tests/specs/repl.ts b/tests/specs/repl.ts index ca482ed2..838e661e 100644 --- a/tests/specs/repl.ts +++ b/tests/specs/repl.ts @@ -57,6 +57,35 @@ export default testSuite(async ({ describe }, node: NodeApis) => { tsxProcess.kill(); }, 40_000); + test('supports incomplete expression in segments', async () => { + const tsxProcess = node.tsx({ + args: ['--interactive'], + }); + + const commands = [ + ['> ', '('], + ['... ', '1'], + ['... ', ')'], + ['1'], + ]; + + let [expected, nextCommand] = commands.shift()!; + await new Promise((resolve) => { + tsxProcess.stdout!.on('data', (data: Buffer) => { + const chunkString = data.toString(); + if (chunkString.includes(expected)) { + if (nextCommand) { + tsxProcess.stdin!.write(`${nextCommand}\r`); + [expected, nextCommand] = commands.shift()!; + } else { + resolve(); + } + } + }); + }); + tsxProcess.kill(); + }, 40_000); + test('errors on import statement', async () => { const tsxProcess = node.tsx({ args: ['--interactive'],