Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(406)

Unified Diff: third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js

Issue 1875903002: [DevTools] Better heuristic for detecting object literals (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal-expected.txt ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
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 aff5da307645c6f3864ce0b9e8603c14857f9d7e..7b93cf02e4a200ab41a1c16b4750f11a2761971b 100644
--- a/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
+++ b/third_party/WebKit/Source/devtools/front_end/sdk/ConsoleModel.js
@@ -212,8 +212,33 @@ WebInspector.ConsoleModel.evaluateCommandInConsole = function(executionContext,
target.consoleModel.dispatchEventToListeners(WebInspector.ConsoleModel.Events.CommandEvaluated, {result: result, wasThrown: wasThrown, text: text, commandMessage: commandMessage, exceptionDetails: exceptionDetails});
}
}
- if (/^\s*\{/.test(text) && /\}\s*$/.test(text))
- text = '(' + text + ')';
+
+ /**
+ * @param {string} code
+ * @suppress {uselessCode}
dgozman 2016/04/26 23:50:27 What is this?
+ * @return {boolean}
+ */
+ function looksLikeAnObjectLiteral(code) {
dgozman 2016/04/26 23:50:27 style: { on next line
+ // Only parenthesize what appears to be an object literal.
+ if (!(/^\s*\{/.test(code) && /\}\s*$/.test(code)))
+ return false;
+
+ try {
+ // Check if the code can be interpreted as an expression.
+ Function("return " + code + ";");
+
+ // No syntax error! Does it work parenthesized?
+ Function("(" + code + ")");
+
+ return true;
+ } catch (e) {
+ return false;
+ }
+ }
+ if (looksLikeAnObjectLiteral(text)) {
+ text = "(" + text + ")";
+ WebInspector.console.warn("Evaluating input as an expression.");
dgozman 2016/04/26 23:50:27 I still don't agree with this. Let's extract this
+ }
executionContext.evaluate(text, "console", !!useCommandLineAPI, false, false, true, printResult);
WebInspector.userMetrics.actionTaken(WebInspector.UserMetrics.Action.ConsoleEvaluated);
« no previous file with comments | « third_party/WebKit/LayoutTests/inspector/console/console-eval-object-literal-expected.txt ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698