Fixes T2299: Prevent REPL from printing implicit 'use strict' (#4562)

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

* Test for T2299

* make fixes
This commit is contained in:
Henry Zhu 2016-09-25 23:29:37 -04:00 committed by GitHub
parent 702259d483
commit fc2b3cb465
2 changed files with 13 additions and 1 deletions

View File

@ -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")));
}
}
});

View File

@ -0,0 +1,4 @@
{
"args": ["--eval","--print", "var a = 1;"],
"stdout": "undefined"
}