1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 00:21:05 -05:00

fix(cli/repl): interpret object literals as expressions (#7591)

This commit is contained in:
Casper Beyer 2020-09-22 04:09:53 +08:00 committed by GitHub
parent 5c2e499c3a
commit 9caeff3208
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 40 additions and 1 deletions

View file

@ -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.

View file

@ -1058,6 +1058,32 @@ fn repl_test_console_log() {
assert!(err.is_empty());
}
#[test]
fn repl_test_object_literal() {
let (out, err) = util::run_and_collect_output(
true,
"repl",
Some(vec!["{}", "{ foo: 'bar' }"]),
Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]),
false,
);
assert!(out.ends_with("{}\n{ foo: \"bar\" }\n"));
assert!(err.is_empty());
}
#[test]
fn repl_test_block_expression() {
let (out, err) = util::run_and_collect_output(
true,
"repl",
Some(vec!["{};", "{\"\"}"]),
Some(vec![("NO_COLOR".to_owned(), "1".to_owned())]),
false,
);
assert!(out.ends_with("undefined\n\"\"\n"));
assert!(err.is_empty());
}
#[test]
fn repl_cwd() {
let (_out, err) = util::run_and_collect_output(