Skip to content

Commit

Permalink
fix: prevent implicit 'use strict' output (#1388)
Browse files Browse the repository at this point in the history
  • Loading branch information
ejose19 authored Jul 9, 2021
1 parent 5643ad6 commit 4e7fcb7
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
9 changes: 9 additions & 0 deletions src/repl.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,13 +214,22 @@ function _eval(service: Service, state: EvalState, input: string) {
const undo = appendEval(state, input);
let output: string;

// Based on https://github.com/nodejs/node/blob/92573721c7cff104ccb82b6ed3e8aa69c4b27510/lib/repl.js#L457-L461
function adjustUseStrict(code: string) {
// "void 0" keeps the repl from returning "use strict" as the result
// value for statements and declarations that don't return a value.
return code.replace(/^"use strict";/, '"use strict"; void 0;');
}

try {
output = service.compile(state.input, state.path, -lines);
} catch (err) {
undo();
throw err;
}

output = adjustUseStrict(output);

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

Expand Down
4 changes: 2 additions & 2 deletions src/test/index.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -407,7 +407,7 @@ test.suite('ts-node', (test) => {
const { err, stdout } = await execPromise;
expect(err).to.equal(null);
expect(stdout).to.equal(
"> 'use strict'\n" + '> undefined\n' + '> const a: 123\n' + '> '
'> undefined\n' + '> undefined\n' + '> const a: 123\n' + '> '
);
});

Expand Down Expand Up @@ -439,7 +439,7 @@ test.suite('ts-node', (test) => {
stderr.end();
expect(await getStream(stderr)).to.equal('');
expect(await getStream(stdout)).to.equal(
"> 'use strict'\n" + '> undefined\n' + '> const a: 123\n' + '> '
'> undefined\n' + '> undefined\n' + '> const a: 123\n' + '> '
);
});

Expand Down

0 comments on commit 4e7fcb7

Please sign in to comment.