Skip to content

Commit

Permalink
fix(cli/repl): interpret object literals as expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
caspervonb committed Sep 20, 2020
1 parent 79e5b57 commit 75f0e74
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions cli/rt/40_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`);
Expand Down
13 changes: 13 additions & 0 deletions cli/tests/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down

0 comments on commit 75f0e74

Please sign in to comment.