diff --git a/cli/rt/40_repl.js b/cli/rt/40_repl.js index 650fb7fc8dee94..bfd75860e93e1d 100644 --- a/cli/rt/40_repl.js +++ b/cli/rt/40_repl.js @@ -53,6 +53,12 @@ // Returns true if code is consumed (no error/irrecoverable error). // Returns false if error is recoverable function evaluate(code) { + // 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. + if (code.startsWith("{") && code.endsWith("}")) { + code = `(${code.trim()})`; + } + // 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}`); diff --git a/cli/tests/integration_tests.rs b/cli/tests/integration_tests.rs index 943e567998e46e..ddafe4aee95d6e 100644 --- a/cli/tests/integration_tests.rs +++ b/cli/tests/integration_tests.rs @@ -1057,6 +1057,19 @@ 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("{ foo: \"bar\" }\n")); + assert!(err.is_empty()); +} + #[test] fn repl_cwd() { let (_out, err) = util::run_and_collect_output(