Bug 141737

Summary: Web Inspector: Typing object literal in the console causes a parse error
Product: WebKit Reporter: Nikita Vasilyev <nvasilyev>
Component: Web InspectorAssignee: Nobody <webkit-unassigned>
Status: RESOLVED FIXED    
Severity: Normal CC: bburg, buildbot, commit-queue, graouts, joepeck, jonowells, mattbaker, nvasilyev, rniwa, timothy, webkit-bug-importer
Priority: P2 Keywords: InRadar
Version: 528+ (Nightly build)   
Hardware: All   
OS: All   
Attachments:
Description Flags
A screenshot
none
[PATCH] Proposed Fix - For Object Literals
buildbot: commit-queue-
Archive of layout-test-results from ews103 for mac-yosemite
none
Archive of layout-test-results from ews113 for mac-yosemite
none
Archive of layout-test-results from ews105 for mac-yosemite-wk2
none
[PATCH] Proposed Fix - For Object Literals none

Description Nikita Vasilyev 2015-02-17 19:11:13 PST
Created attachment 246785 [details]
A screenshot

> {a: 1, b: 2}
< SyntaxError: Unexpected token ':'. Parse error.

Note that `obj = {a: 1, b: 2}` doesn't throw any errors.
Comment 1 Radar WebKit Bug Importer 2015-02-17 19:11:34 PST
<rdar://problem/19870653>
Comment 2 Joseph Pecoraro 2015-02-18 01:17:37 PST
(In reply to comment #0)
> Created attachment 246785 [details]
> A screenshot
> 
> > {a: 1, b: 2}
> < SyntaxError: Unexpected token ':'. Parse error.

This is because typing in the console is treated as a complete program. If this is treated as a program it is parsed as an anonymous block with a label.

That is why "{a: 1}" is treated as "1" instead of Object. Because it was a program, with a statement of just "1".

We've debated in the past whether or not we should or even can detect. A simple workaround for developers is to wrap in parenthesis and force it to evaluate as an expression. E.g. "({a:1, b:2})". Note this is used to be a common practice when parsing XHR JSON responses before JSON.parse, people would "obj = eval( '(' + str + ')' )" because of the same issue.

That said, I think we've had enough people be confused by this that we should probably just fix it.
Comment 3 Timothy Hatcher 2015-03-24 09:58:35 PDT
My suggestion to fix this would be to try evaluation as-is, if there is a syntax error, try evaluation wrapped in ().
Comment 4 Joseph Pecoraro 2015-03-24 12:10:36 PDT
I believe this also affects class expressions.

>   js> class foo {};
>   <-  SyntaxError: Unexpected keyword 'class'. Class declaration is not allowed in a lexically nested statement.

Which is pretty annoying.

I end up having to type `foo = class foo {}`.
Comment 5 Timothy Hatcher 2015-03-24 15:38:49 PDT
Even more annoying now that we have $n.
Comment 6 Timothy Hatcher 2015-04-22 07:43:32 PDT
*** Bug 144034 has been marked as a duplicate of this bug. ***
Comment 7 Joseph Pecoraro 2015-12-15 18:35:29 PST
Created attachment 267417 [details]
[PATCH] Proposed Fix - For Object Literals

Basing this off of Blink's recent change that does the same thing.

I think it is a good idea, and easy to opt-out for those that really understand what they are doing.
Comment 8 Joseph Pecoraro 2015-12-15 18:39:15 PST
(In reply to comment #7)
> Created attachment 267417 [details]
> [PATCH] Proposed Fix - For Object Literals
> 
> Basing this off of Blink's recent change that does the same thing.

Which was:
<https://twitter.com/ChromeDevTools/status/676531222609182720>
<https://chromium.googlesource.com/chromium/src.git/+/4fd348fdb9c0b3842829acdfb2b82c86dacd8e0a%5E%21/#F2>
Comment 9 Joseph Pecoraro 2015-12-15 18:41:29 PST
Apparently node.js's interpreter does the same thing.
Comment 10 Build Bot 2015-12-15 19:14:50 PST
Comment on attachment 267417 [details]
[PATCH] Proposed Fix - For Object Literals

Attachment 267417 [details] did not pass mac-ews (mac):
Output: http://webkit-queues.webkit.org/results/564892

Number of test failures exceeded the failure limit.
Comment 11 Build Bot 2015-12-15 19:14:53 PST
Created attachment 267421 [details]
Archive of layout-test-results from ews103 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-ews.
Bot: ews103  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 12 Build Bot 2015-12-15 19:18:53 PST
Comment on attachment 267417 [details]
[PATCH] Proposed Fix - For Object Literals

Attachment 267417 [details] did not pass mac-debug-ews (mac):
Output: http://webkit-queues.webkit.org/results/564884

Number of test failures exceeded the failure limit.
Comment 13 Build Bot 2015-12-15 19:18:56 PST
Created attachment 267422 [details]
Archive of layout-test-results from ews113 for mac-yosemite

The attached test failures were seen while running run-webkit-tests on the mac-debug-ews.
Bot: ews113  Port: mac-yosemite  Platform: Mac OS X 10.10.5
Comment 14 Build Bot 2015-12-15 19:20:18 PST
Comment on attachment 267417 [details]
[PATCH] Proposed Fix - For Object Literals

Attachment 267417 [details] did not pass mac-wk2-ews (mac-wk2):
Output: http://webkit-queues.webkit.org/results/564899

Number of test failures exceeded the failure limit.
Comment 15 Build Bot 2015-12-15 19:20:20 PST
Created attachment 267423 [details]
Archive of layout-test-results from ews105 for mac-yosemite-wk2

The attached test failures were seen while running run-webkit-tests on the mac-wk2-ews.
Bot: ews105  Port: mac-yosemite-wk2  Platform: Mac OS X 10.10.5
Comment 16 Joseph Pecoraro 2015-12-15 23:19:05 PST
Created attachment 267435 [details]
[PATCH] Proposed Fix - For Object Literals
Comment 17 WebKit Commit Bot 2015-12-16 09:40:42 PST
Comment on attachment 267435 [details]
[PATCH] Proposed Fix - For Object Literals

Clearing flags on attachment: 267435

Committed r194149: <http://trac.webkit.org/changeset/194149>
Comment 18 WebKit Commit Bot 2015-12-16 09:40:47 PST
All reviewed patches have been landed.  Closing bug.