Skip to content

Commit

Permalink
Fixes T2299: Prevent REPL from printing implicit 'use strict' (babel#…
Browse files Browse the repository at this point in the history
…4562)

* Fixes T2299: Prevent REPL from printing implicit 'use strict'

* Test for T2299

* make fixes
  • Loading branch information
hzoo authored and panagosg7 committed Jan 17, 2017
1 parent 9677916 commit 54e7acf
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 1 deletion.
10 changes: 9 additions & 1 deletion packages/babel-cli/src/_babel-node.js
Expand Up @@ -38,7 +38,7 @@ register({

//

let replPlugin = () => ({
let replPlugin = ({ types: t }) => ({
visitor: {
ModuleDeclaration(path) {
throw path.buildCodeFrameError("Modules aren't supported in the REPL");
Expand All @@ -48,6 +48,14 @@ let replPlugin = () => ({
if (path.node.kind !== "var") {
throw path.buildCodeFrameError("Only `var` variables are supported in the REPL");
}
},

Program(path) {
if (path.get("body").some((child) => child.isExpressionStatement())) return;

// If the executed code doesn't evaluate to a value,
// prevent implicit strict mode from printing 'use strict'.
path.pushContainer("body", t.expressionStatement(t.identifier("undefined")));
}
}
});
Expand Down
@@ -0,0 +1,4 @@
{
"args": ["--eval","--print", "var a = 1;"],
"stdout": "undefined"
}

0 comments on commit 54e7acf

Please sign in to comment.