Skip to content

Commit 4e7fcb7

Browse files
authored
fix: prevent implicit 'use strict' output (#1388)
1 parent 5643ad6 commit 4e7fcb7

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

src/repl.ts

+9
Original file line numberDiff line numberDiff line change
@@ -214,13 +214,22 @@ function _eval(service: Service, state: EvalState, input: string) {
214214
const undo = appendEval(state, input);
215215
let output: string;
216216

217+
// Based on https://github.com/nodejs/node/blob/92573721c7cff104ccb82b6ed3e8aa69c4b27510/lib/repl.js#L457-L461
218+
function adjustUseStrict(code: string) {
219+
// "void 0" keeps the repl from returning "use strict" as the result
220+
// value for statements and declarations that don't return a value.
221+
return code.replace(/^"use strict";/, '"use strict"; void 0;');
222+
}
223+
217224
try {
218225
output = service.compile(state.input, state.path, -lines);
219226
} catch (err) {
220227
undo();
221228
throw err;
222229
}
223230

231+
output = adjustUseStrict(output);
232+
224233
// Use `diff` to check for new JavaScript to execute.
225234
const changes = diffLines(state.output, output);
226235

src/test/index.spec.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -407,7 +407,7 @@ test.suite('ts-node', (test) => {
407407
const { err, stdout } = await execPromise;
408408
expect(err).to.equal(null);
409409
expect(stdout).to.equal(
410-
"> 'use strict'\n" + '> undefined\n' + '> const a: 123\n' + '> '
410+
'> undefined\n' + '> undefined\n' + '> const a: 123\n' + '> '
411411
);
412412
});
413413

@@ -439,7 +439,7 @@ test.suite('ts-node', (test) => {
439439
stderr.end();
440440
expect(await getStream(stderr)).to.equal('');
441441
expect(await getStream(stdout)).to.equal(
442-
"> 'use strict'\n" + '> undefined\n' + '> const a: 123\n' + '> '
442+
'> undefined\n' + '> undefined\n' + '> const a: 123\n' + '> '
443443
);
444444
});
445445

0 commit comments

Comments
 (0)