[DevTools] Evaluate expression like { ... } as Object Literal
If user evaluate in console expression like {a:1, b:2} then console will report SyntaxError exception because this expression is interpreted as block scope declaration with incorrect label.
With this CL console wrap all expressions that begins with { and ends with }.
BUG=499864
R=paulirish@chromium.org, pfeldman@chromium.org
Review URL: https://codereview.chromium.org/1511043002
Cr-Commit-Position: refs/heads/master@{#364270}
diff --git a/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal-expected.txt b/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal-expected.txt
new file mode 100644
index 0000000..bbf6fad
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal-expected.txt
@@ -0,0 +1,11 @@
+Tests that evaluating object literal in the console correctly reported.
+
+{a:1, b:2}
+Object {a: 1, b: 2}
+{a:1}
+Object {a: 1}
+{var a = 1; eval("{a :1, b:2 }");}
+ VM:2 Uncaught SyntaxError: Unexpected identifier(…)
+{ for (var i = 0; i < 5; ++i); }
+ VM:2 Uncaught SyntaxError: Unexpected token var(…)
+
diff --git a/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal.html b/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal.html
new file mode 100644
index 0000000..afa3fd3
--- /dev/null
+++ b/third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal.html
@@ -0,0 +1,29 @@
+<html>
+<head>
+<script src="../../http/tests/inspector/inspector-test.js"></script>
+<script src="../../http/tests/inspector/console-test.js"></script>
+<script>
+function test()
+{
+ var commands = ["{a:1, b:2}", "{a:1}", "{var a = 1; eval(\"{a :1, b:2 }\");}", "{ for (var i = 0; i < 5; ++i); }"];
+ var current = -1;
+ loopOverCommands();
+ function loopOverCommands()
+ {
+ ++current;
+ if (current < commands.length) {
+ InspectorTest.evaluateInConsole(commands[current], loopOverCommands);
+ } else {
+ InspectorTest.dumpConsoleMessagesIgnoreErrorStackFrames();
+ InspectorTest.completeTest();
+ }
+ }
+}
+</script>
+</head>
+<body onload="runTest()">
+<p>
+Tests that evaluating object literal in the console correctly reported.
+</p>
+</body>
+</html>
diff --git a/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js b/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
index acc0d6b..a814dca 100644
--- a/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
+++ b/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
@@ -189,7 +189,6 @@
*/
WebInspector.ConsoleModel.evaluateCommandInConsole = function(executionContext, text, useCommandLineAPI)
{
- useCommandLineAPI = !!useCommandLineAPI;
var target = executionContext.target();
var commandMessage = new WebInspector.ConsoleMessage(target, WebInspector.ConsoleMessage.MessageSource.JS, null, text, WebInspector.ConsoleMessage.MessageType.Command);
@@ -213,8 +212,9 @@
target.consoleModel.dispatchEventToListeners(WebInspector.ConsoleModel.Events.CommandEvaluated, {result: result, wasThrown: wasThrown, text: text, commandMessage: commandMessage, exceptionDetails: exceptionDetails});
}
}
-
- executionContext.evaluate(text, "console", useCommandLineAPI, false, false, true, printResult);
+ if (/^\s*\{/.test(text) && /\}\s*$/.test(text))
+ text = '(' + text + ')';
+ executionContext.evaluate(text, "console", !!useCommandLineAPI, false, false, true, printResult);
WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.ConsoleEvaluated);
}