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

fix: prevent implicit 'use strict' output #1388

Merged
merged 1 commit into from Jul 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/repl.ts
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
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