Skip to content

Commit

Permalink
fix(repl): suppress autocomplete transformation errors (#205)
Browse files Browse the repository at this point in the history
  • Loading branch information
privatenumber committed Mar 20, 2023
1 parent afa3c78 commit 8e1958d
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 19 deletions.
37 changes: 18 additions & 19 deletions src/patch-repl.ts
Expand Up @@ -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) {

Check warning on line 6 in src/patch-repl.ts

View workflow job for this annotation

GitHub Actions / Test (ubuntu-latest)

Unexpected unnamed async function

Check warning on line 6 in src/patch-repl.ts

View workflow job for this annotation

GitHub Actions / Release

Unexpected unnamed async function
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
Expand Down
29 changes: 29 additions & 0 deletions tests/specs/repl.ts
Expand Up @@ -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<void>((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'],
Expand Down

0 comments on commit 8e1958d

Please sign in to comment.