diff options
author | Casper Beyer <caspervonb@pm.me> | 2020-09-22 04:09:53 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-09-21 16:09:53 -0400 |
commit | 9caeff3208a33098418ac2c64414010a3060a362 (patch) | |
tree | beef3d92183fd4110cc3b028c3557904c72280f1 /cli/rt/40_repl.js | |
parent | 5c2e499c3a70ec58b7f5adab3a7801d601f9b2e1 (diff) |
fix(cli/repl): interpret object literals as expressions (#7591)
Diffstat (limited to 'cli/rt/40_repl.js')
-rw-r--r-- | cli/rt/40_repl.js | 15 |
1 files changed, 14 insertions, 1 deletions
diff --git a/cli/rt/40_repl.js b/cli/rt/40_repl.js index 650fb7fc8..a249b578d 100644 --- a/cli/rt/40_repl.js +++ b/cli/rt/40_repl.js @@ -52,10 +52,21 @@ // Evaluate code. // Returns true if code is consumed (no error/irrecoverable error). // Returns false if error is recoverable - function evaluate(code) { + function evaluate(code, preprocess = true) { + const rawCode = code; + if (preprocess) { + // It is a bit unexpected that { "foo": "bar" } is interpreted as a block + // statement rather than an object literal so we interpret it as an expression statement + // to match the behavior found in a typical prompt including browser developer tools. + if (code.trimLeft().startsWith("{") && !code.trimRight().endsWith(";")) { + code = `(${code})`; + } + } + // each evalContext is a separate function body, and we want strict mode to // work, so we should ensure that the code starts with "use strict" const [result, errInfo] = core.evalContext(`"use strict";\n\n${code}`); + if (!errInfo) { // when a function is eval'ed with just "use strict" sometimes the result // is "use strict" which should be discarded @@ -65,6 +76,8 @@ if (!isCloseCalled()) { replLog("%o", lastEvalResult); } + } else if (errInfo.isCompileError && code.length != rawCode.length) { + return evaluate(rawCode, false); } else if (errInfo.isCompileError && isRecoverableError(errInfo.thrown)) { // Recoverable compiler error return false; // don't consume code. |