Bug 200800 - Web Inspector: JavaScript formatting of single statement arrow function can be poor
Summary: Web Inspector: JavaScript formatting of single statement arrow function can b...
Status: RESOLVED FIXED
Alias: None
Product: WebKit
Classification: Unclassified
Component: Web Inspector (show other bugs)
Version: WebKit Nightly Build
Hardware: All All
: P2 Normal
Assignee: Joseph Pecoraro
URL:
Keywords: InRadar
Depends on:
Blocks:
 
Reported: 2019-08-15 17:12 PDT by Joseph Pecoraro
Modified: 2019-08-16 19:07 PDT (History)
5 users (show)

See Also:


Attachments
[PATCH] Proposed Fix (5.49 KB, patch)
2019-08-15 17:28 PDT, Joseph Pecoraro
no flags Details | Formatted Diff | Diff
[PATCH] Proposed Fix (5.49 KB, patch)
2019-08-15 17:29 PDT, Joseph Pecoraro
ross.kirsling: review+
Details | Formatted Diff | Diff
[PATCH] For Landing (5.50 KB, patch)
2019-08-16 12:08 PDT, Joseph Pecoraro
no flags Details | Formatted Diff | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Joseph Pecoraro 2019-08-15 17:12:12 PDT
JavaScript formatting of single statement arrow function can be poor

Test:
```
() => { 1 }
() => { for(x of []); }
() => { try{1}catch{2} }
```

Expected:
```
() => {1}
() => {
    for (x of [])
        ;
}
() => {
    try {
        1
    } catch {
        2
    }
}
```

Actual:
```
() => {1}
() => {for (x of [])
        ;
}
() => {try {
        1
    } catch {
        2
    }
}
```
Comment 1 Joseph Pecoraro 2019-08-15 17:28:01 PDT
Created attachment 376447 [details]
[PATCH] Proposed Fix
Comment 2 Joseph Pecoraro 2019-08-15 17:28:42 PDT
Comment on attachment 376447 [details]
[PATCH] Proposed Fix

View in context: https://bugs.webkit.org/attachment.cgi?id=376447&action=review

> Source/WebInspectorUI/UserInterface/Workers/Formatter/EsprimaFormatter.js:309
> +            let isSingleStatementArrowFunctionWithUnlikelyNewlineContent = node.parent.type === "ArrowFunctionExpression" && node.body.length === 1 && !this._isLikelyToHaveNewline(node.body[0]);

Perhaps a better name is "WithUnlikelyMultilineContent"
Comment 3 Joseph Pecoraro 2019-08-15 17:29:31 PDT
Created attachment 376448 [details]
[PATCH] Proposed Fix
Comment 4 Ross Kirsling 2019-08-16 11:11:48 PDT
Comment on attachment 376448 [details]
[PATCH] Proposed Fix

View in context: https://bugs.webkit.org/attachment.cgi?id=376448&action=review

Seems like a reasonable improvement. I agree with your renaming suggestion too.

> Source/WebInspectorUI/UserInterface/Workers/Formatter/EsprimaFormatter.js:196
> +        if (nodeType === "ExpressionStatement")
> +            return false;

This shouldn't be necessary. (Unless it's meant as an optimization? But in that case it'd be incomplete, and you'd probably want to opt for a switch...)

> Source/WebInspectorUI/UserInterface/Workers/Formatter/EsprimaFormatter.js:206
> +            || nodeType === "FunctionDeclaration"

"ClassDeclaration" too, right?
Comment 5 Joseph Pecoraro 2019-08-16 11:43:35 PDT
(In reply to Ross Kirsling from comment #4)
> Comment on attachment 376448 [details]
> [PATCH] Proposed Fix
> 
> View in context:
> https://bugs.webkit.org/attachment.cgi?id=376448&action=review
> 
> Seems like a reasonable improvement. I agree with your renaming suggestion
> too.
> 
> > Source/WebInspectorUI/UserInterface/Workers/Formatter/EsprimaFormatter.js:196
> > +        if (nodeType === "ExpressionStatement")
> > +            return false;
> 
> This shouldn't be necessary. (Unless it's meant as an optimization? But in
> that case it'd be incomplete, and you'd probably want to opt for a switch...)

Yeah, I'll just remove this. I assumed the common case would be Expression and no need to check 10 things if its almost _always_ this. But I didn't measure and it seems unlikely to be important.

> 
> > Source/WebInspectorUI/UserInterface/Workers/Formatter/EsprimaFormatter.js:206
> > +            || nodeType === "FunctionDeclaration"
> 
> "ClassDeclaration" too, right?

Yep! Thanks
Comment 6 Joseph Pecoraro 2019-08-16 12:08:28 PDT
Created attachment 376515 [details]
[PATCH] For Landing
Comment 7 WebKit Commit Bot 2019-08-16 12:51:05 PDT
Comment on attachment 376515 [details]
[PATCH] For Landing

Clearing flags on attachment: 376515

Committed r248785: <https://trac.webkit.org/changeset/248785>
Comment 8 Radar WebKit Bug Importer 2019-08-16 19:07:17 PDT
<rdar://problem/54417189>