diff --git a/packages/babel-cli/src/_babel-node.js b/packages/babel-cli/src/_babel-node.js index 40e08852fdea..3c7c40ed4d80 100644 --- a/packages/babel-cli/src/_babel-node.js +++ b/packages/babel-cli/src/_babel-node.js @@ -38,7 +38,7 @@ register({ // -let replPlugin = () => ({ +let replPlugin = ({ types: t }) => ({ visitor: { ModuleDeclaration(path) { throw path.buildCodeFrameError("Modules aren't supported in the REPL"); @@ -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"))); } } }); diff --git a/packages/babel-cli/test/fixtures/babel-node/no-strict/options.json b/packages/babel-cli/test/fixtures/babel-node/no-strict/options.json new file mode 100644 index 000000000000..63fc4b34f430 --- /dev/null +++ b/packages/babel-cli/test/fixtures/babel-node/no-strict/options.json @@ -0,0 +1,4 @@ +{ + "args": ["--eval","--print", "var a = 1;"], + "stdout": "undefined" +}