From 2e3937047fd030bf46239086caca806dc67c8628 Mon Sep 17 00:00:00 2001 From: Andy Stanberry Date: Tue, 28 Sep 2021 19:11:52 -0400 Subject: [PATCH 1/4] failing swc transpiler repl test --- src/test/repl/repl.spec.ts | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/test/repl/repl.spec.ts b/src/test/repl/repl.spec.ts index 6ec33f434..d3b03a83d 100644 --- a/src/test/repl/repl.spec.ts +++ b/src/test/repl/repl.spec.ts @@ -30,6 +30,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'); From 561b6ddd4a5ca873edf3062068623583a32be128 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 27 Dec 2021 23:05:21 +0000 Subject: [PATCH 2/4] Strip sourcemap comment from REPL JS before diffing and executing --- src/repl.ts | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/repl.ts b/src/repl.ts index ad399b850..34332b8f5 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -480,6 +480,8 @@ export function createEvalAwarePartialHost( return { readFile, fileExists }; } +const sourcemapCommentRe = /\/\/# ?sourceMappingURL=\S+[\s\r\n]*$/; + type AppendCompileAndEvalInputResult = | { containsTopLevelAwait: true; valuePromise: Promise } | { containsTopLevelAwait: false; value: any }; @@ -525,8 +527,14 @@ function appendCompileAndEvalInput(options: { output = adjustUseStrict(output); + 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(); From 339df1d669b4f538f59db82bc68d9a86a4f84363 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 27 Dec 2021 23:12:09 +0000 Subject: [PATCH 3/4] add comment about how repl does not do sourcemaps --- src/repl.ts | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/repl.ts b/src/repl.ts index 34332b8f5..a0f5e78b5 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -527,6 +527,12 @@ 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, From 82152ebfce2314111054f727688fdfd899ddf4b7 Mon Sep 17 00:00:00 2001 From: Andrew Bradley Date: Mon, 27 Dec 2021 23:12:44 +0000 Subject: [PATCH 4/4] lint-fix --- src/repl.ts | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/repl.ts b/src/repl.ts index a0f5e78b5..e402f3df7 100644 --- a/src/repl.ts +++ b/src/repl.ts @@ -540,7 +540,10 @@ function appendCompileAndEvalInput(options: { ); // Use `diff` to check for new JavaScript to execute. - const changes = diffLines(oldOutputWithoutSourcemapComment, outputWithoutSourcemapComment); + const changes = diffLines( + oldOutputWithoutSourcemapComment, + outputWithoutSourcemapComment + ); if (isCompletion) { undo();