Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Strip sourcemap comment from REPL JS before diffing and executing #1580

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -480,6 +480,8 @@ export function createEvalAwarePartialHost(
return { readFile, fileExists };
}

const sourcemapCommentRe = /\/\/# ?sourceMappingURL=\S+[\s\r\n]*$/;

type AppendCompileAndEvalInputResult =
| { containsTopLevelAwait: true; valuePromise: Promise<any> }
| { containsTopLevelAwait: false; value: any };
Expand Down Expand Up @@ -525,8 +527,23 @@ function appendCompileAndEvalInput(options: {

output = adjustUseStrict(output);

// Note: REPL does not respect sourcemaps!
// To properly do that, we'd need to prefix the code we eval -- which comes
// from `diffLines` -- with newlines so that it's at the proper line numbers.
// Then we'd need to ensure each bit of eval-ed code, if there are multiples,
// has the sourcemap appended to it.
// We might also need to integrate with our sourcemap hooks' cache; I'm not sure.
const outputWithoutSourcemapComment = output.replace(sourcemapCommentRe, '');
const oldOutputWithoutSourcemapComment = state.output.replace(
sourcemapCommentRe,
''
);

// Use `diff` to check for new JavaScript to execute.
const changes = diffLines(state.output, output);
const changes = diffLines(
oldOutputWithoutSourcemapComment,
outputWithoutSourcemapComment
);

if (isCompletion) {
undo();
Expand Down
10 changes: 10 additions & 0 deletions src/test/repl/repl.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,16 @@ test('should run REPL when --interactive passed and stdin is not a TTY', async (
expect(stdout).toBe('> 123\n' + 'undefined\n' + '> ');
});

test('should echo a value when using the swc transpiler', async () => {
const execPromise = exec(
`${CMD_TS_NODE_WITH_PROJECT_FLAG} --interactive --transpiler ts-node/transpilers/swc-experimental`
);
execPromise.child.stdin!.end('400\n401\n');
const { err, stdout } = await execPromise;
expect(err).toBe(null);
expect(stdout).toBe('> 400\n>401\n > ');
});

test('REPL has command to get type information', async () => {
const execPromise = exec(`${CMD_TS_NODE_WITH_PROJECT_FLAG} --interactive`);
execPromise.child.stdin!.end('\nconst a = 123\n.type a');
Expand Down