Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions cli/rt/40_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,13 @@
// 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
// to match the behavior found in a typical prompt including browser developer tools.
if (code.startsWith("{") && code.endsWith("}")) {
code = `(${code.trim()})`;
}
caspervonb marked this conversation as resolved.
Show resolved Hide resolved

// 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