Title: [250544] trunk
Revision
250544
Author
drou...@apple.com
Date
2019-09-30 21:57:38 -0700 (Mon, 30 Sep 2019)

Log Message

Web Inspector: JS Formatter: unexpected newlines added inside template string _expression_
https://bugs.webkit.org/show_bug.cgi?id=202374

Reviewed by Joseph Pecoraro.

Source/WebInspectorUI:

Wrap every "add newline" with a check to see if the current node is inside a template
literal node. If so (and we aren't forcibly adding a newline), prevent the newline from
being added.

* UserInterface/Workers/Formatter/JSFormatter.js:
(JSFormatter.prototype._appendNewline): Added.
(JSFormatter.prototype._insertNewlinesBeforeToken):
(JSFormatter.prototype._insertComment):
(JSFormatter.prototype._insertSameLineTrailingComments):
(JSFormatter.prototype._insertCommentsAndNewlines):
(JSFormatter.prototype._before):
(JSFormatter.prototype._after):
(JSFormatter.prototype._handleTokenAtNode):
(JSFormatter.prototype._exitNode):
(JSFormatter.prototype._afterProgram):

LayoutTests:

* inspector/formatting/resources/_javascript_-tests/template-strings.js:
* inspector/formatting/resources/_javascript_-tests/template-strings-expected.js:

Modified Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (250543 => 250544)


--- trunk/LayoutTests/ChangeLog	2019-10-01 04:06:25 UTC (rev 250543)
+++ trunk/LayoutTests/ChangeLog	2019-10-01 04:57:38 UTC (rev 250544)
@@ -1,3 +1,13 @@
+2019-09-30  Devin Rousso  <drou...@apple.com>
+
+        Web Inspector: JS Formatter: unexpected newlines added inside template string _expression_
+        https://bugs.webkit.org/show_bug.cgi?id=202374
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/formatting/resources/_javascript_-tests/template-strings.js:
+        * inspector/formatting/resources/_javascript_-tests/template-strings-expected.js:
+
 2019-09-30  Saam Barati  <sbar...@apple.com>
 
         Inline caching is wrong for custom accessors and custom values

Modified: trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings-expected.js (250543 => 250544)


--- trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings-expected.js	2019-10-01 04:06:25 UTC (rev 250543)
+++ trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings-expected.js	2019-10-01 04:57:38 UTC (rev 250544)
@@ -8,3 +8,14 @@
 tag`tagged template string`
 tag`tagged template string`
 tag`before ${1 + 1} after`
+
+`
+    before ${1 + 1} after
+    before ${x} after
+    before ${func()} after
+    before ${{a: 1}} after
+    before ${[1]} after
+    before ${
+y
+} after
+`

Modified: trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings.js (250543 => 250544)


--- trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings.js	2019-10-01 04:06:25 UTC (rev 250543)
+++ trunk/LayoutTests/inspector/formatting/resources/_javascript_-tests/template-strings.js	2019-10-01 04:57:38 UTC (rev 250544)
@@ -8,3 +8,14 @@
 tag`tagged template string`
 tag `tagged template string` 
 tag`before ${1+1} after`
+
+`
+    before ${ 1 + 1 } after
+    before ${ x } after
+    before ${ func() } after
+    before ${ { a: 1 } } after
+    before ${ [ 1 ] } after
+    before ${
+        y
+    } after
+`
\ No newline at end of file

Modified: trunk/Source/WebInspectorUI/ChangeLog (250543 => 250544)


--- trunk/Source/WebInspectorUI/ChangeLog	2019-10-01 04:06:25 UTC (rev 250543)
+++ trunk/Source/WebInspectorUI/ChangeLog	2019-10-01 04:57:38 UTC (rev 250544)
@@ -1,5 +1,28 @@
 2019-09-30  Devin Rousso  <drou...@apple.com>
 
+        Web Inspector: JS Formatter: unexpected newlines added inside template string _expression_
+        https://bugs.webkit.org/show_bug.cgi?id=202374
+
+        Reviewed by Joseph Pecoraro.
+
+        Wrap every "add newline" with a check to see if the current node is inside a template
+        literal node. If so (and we aren't forcibly adding a newline), prevent the newline from
+        being added.
+
+        * UserInterface/Workers/Formatter/JSFormatter.js:
+        (JSFormatter.prototype._appendNewline): Added.
+        (JSFormatter.prototype._insertNewlinesBeforeToken):
+        (JSFormatter.prototype._insertComment):
+        (JSFormatter.prototype._insertSameLineTrailingComments):
+        (JSFormatter.prototype._insertCommentsAndNewlines):
+        (JSFormatter.prototype._before):
+        (JSFormatter.prototype._after):
+        (JSFormatter.prototype._handleTokenAtNode):
+        (JSFormatter.prototype._exitNode):
+        (JSFormatter.prototype._afterProgram):
+
+2019-09-30  Devin Rousso  <drou...@apple.com>
+
         Web Inspector: Canvas: shader type header is white in dark mode
         https://bugs.webkit.org/show_bug.cgi?id=202253
 

Modified: trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/JSFormatter.js (250543 => 250544)


--- trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/JSFormatter.js	2019-10-01 04:06:25 UTC (rev 250543)
+++ trunk/Source/WebInspectorUI/UserInterface/Workers/Formatter/JSFormatter.js	2019-10-01 04:57:38 UTC (rev 250544)
@@ -94,23 +94,35 @@
 
     // Private
 
-    _insertNewlinesBeforeToken(token)
+    _appendNewline(node, force)
     {
+        if (!force && node.type !== "TemplateElement") {
+            while (node = node.parent) {
+                if (node.type === "TemplateLiteral")
+                    return;
+            }
+        }
+
+        this._builder.appendNewline(force);
+    }
+
+    _insertNewlinesBeforeToken(node, token)
+    {
         let force = false;
         while (token.range[0] > this._lineEndings[this._lineEndingsIndex]) {
-            this._builder.appendNewline(force);
+            this._appendNewline(node, force);
             this._lineEndingsIndex++;
             force = true;
         }
     }
 
-    _insertComment(comment)
+    _insertComment(node, comment)
     {
         if (comment.type === "Line")
             this._builder.appendToken("//" + comment.value, comment.range[0]);
         else if (comment.type === "Block")
             this._builder.appendToken("/*" + comment.value + "*/", comment.range[0]);
-        this._builder.appendNewline();
+        this._appendNewline(node);
         comment.__handled = true;
     }
 
@@ -122,11 +134,11 @@
                 break;
             this._builder.removeLastNewline();
             this._builder.appendSpace();
-            this._insertComment(comment);
+            this._insertComment(node, comment);
         }
     }
 
-    _insertCommentsAndNewlines(comments)
+    _insertCommentsAndNewlines(node, comments)
     {
         for (let comment of comments) {
             // A previous node may have handled this as a trailing comment.
@@ -139,7 +151,7 @@
             if (comment.range[0] > this._lineEndings[this._lineEndingsIndex + 1])
                 this._builder.appendNewline(true);
 
-            this._insertComment(comment);
+            this._insertComment(node, comment);
 
             // Remove line endings for this comment.
             while (comment.range[1] > this._lineEndings[this._lineEndingsIndex])
@@ -155,12 +167,12 @@
         // Handle the tokens before this node, so in the context of our parent node.
         while (this._tokenIndex < this._tokensLength && this._tokens[this._tokenIndex].range[0] < node.range[0]) {
             let token = this._tokens[this._tokenIndex++];
-            this._insertNewlinesBeforeToken(token);
+            this._insertNewlinesBeforeToken(node, token);
             this._handleTokenAtNode(token, node.parent);
         }
 
         if (node.leadingComments)
-            this._insertCommentsAndNewlines(node.leadingComments);
+            this._insertCommentsAndNewlines(node, node.leadingComments);
     }
 
     _after(node)
@@ -168,7 +180,7 @@
         // Handle any other tokens inside of this node before exiting.
         while (this._tokenIndex < this._tokensLength && this._tokens[this._tokenIndex].range[0] < node.range[1]) {
             let token = this._tokens[this._tokenIndex++];
-            this._insertNewlinesBeforeToken(token);
+            this._insertNewlinesBeforeToken(node, token);
             this._handleTokenAtNode(token, node);
         }
 
@@ -269,7 +281,7 @@
             }
 
             builder.appendToken(tokenValue, tokenOffset);
-            builder.appendNewline();
+            this._appendNewline(node);
             return;
         }
 
@@ -306,21 +318,21 @@
             if (tokenValue === "{") {
                 // Class methods we put the opening brace on its own line.
                 if (node.parent && node.parent.parent && node.parent.parent.type === "MethodDefinition" && node.body.length) {
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     builder.appendToken(tokenValue, tokenOffset);
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     builder.indent();
                     return;
                 }
                 builder.appendToken(tokenValue, tokenOffset);
                 if (node.body.length && !isSingleStatementArrowFunctionWithUnlikelyMultilineContent)
-                    builder.appendNewline();
+                    this._appendNewline(node);
                 builder.indent();
                 return;
             }
             if (tokenValue === "}") {
                 if (node.body.length && !isSingleStatementArrowFunctionWithUnlikelyMultilineContent)
-                    builder.appendNewline();
+                    this._appendNewline(node);
                 builder.dedent();
                 builder.appendToken(tokenValue, tokenOffset);
                 return;
@@ -338,7 +350,7 @@
                     return;
                 }
                 builder.appendToken(tokenValue, tokenOffset);
-                builder.appendNewline();
+                this._appendNewline(node);
                 return;
             }
             builder.appendToken(tokenValue, tokenOffset);
@@ -377,7 +389,7 @@
                     builder.appendToken(tokenValue, tokenOffset);
 
                     if (node.alternate && (node.alternate.type !== "BlockStatement" && node.alternate.type !== "IfStatement")) {
-                        builder.appendNewline();
+                        this._appendNewline(node);
                         builder.indent();
                         node.__autoDedent = true;
                     } else
@@ -401,7 +413,7 @@
                 }
 
                 builder.appendToken(tokenValue, tokenOffset);
-                builder.appendNewline();
+                this._appendNewline(node);
                 builder.indent();
                 node.__autoDedent = true;
                 return;
@@ -475,7 +487,7 @@
                 }
 
                 builder.appendToken(tokenValue, tokenOffset);
-                builder.appendNewline();
+                this._appendNewline(node);
                 builder.indent();
                 node.__autoDedent = true;
                 return;
@@ -508,7 +520,7 @@
                 }
 
                 builder.appendToken(tokenValue, tokenOffset);
-                builder.appendNewline();
+                this._appendNewline(node);
                 builder.indent();
                 node.__autoDedent = true;
                 return;
@@ -533,13 +545,13 @@
                 }
                 if (tokenValue === "{") {
                     builder.appendToken(tokenValue, tokenOffset);
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 if (tokenValue === "}") {
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     builder.appendToken(tokenValue, tokenOffset);
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
             }
@@ -565,7 +577,7 @@
 
             if (tokenValue === ":") {
                 builder.appendToken(tokenValue, tokenOffset);
-                builder.appendNewline();
+                this._appendNewline(node);
                 if (node.consequent.length) {
                     builder.indent();
                     node.__autoDedent = true;
@@ -649,7 +661,7 @@
             if (tokenValue === "{") {
                 builder.appendToken(tokenValue, tokenOffset);
                 if (node.properties.length) {
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     builder.indent();
                 }
                 return;
@@ -656,7 +668,7 @@
             }
             if (tokenValue === "}") {
                 if (node.properties.length) {
-                    builder.appendNewline();
+                    this._appendNewline(node);
                     builder.dedent();
                 }
                 builder.appendToken(tokenValue, tokenOffset);
@@ -665,7 +677,7 @@
             if (tokenValue === ",") {
                 builder.appendToken(tokenValue, tokenOffset);
                 if (node.properties.length)
-                    builder.appendNewline();
+                    this._appendNewline(node);
                 else
                     builder.appendSpace();
                 return;
@@ -874,42 +886,42 @@
             if (node.parent) {
                 // Newline after if(){}
                 if (node.parent.type === "IfStatement" && (!node.parent.alternate || node.parent.consequent !== node)) {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after for(){}
                 if (node.parent.type === "ForStatement" || node.parent.type === "ForOfStatement" || node.parent.type === "ForInStatement") {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after while(){}
                 if (node.parent.type === "WhileStatement") {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after function(){}
                 if (node.parent.type === "FunctionDeclaration") {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after catch block in try{}catch(e){}
                 if (node.parent.type === "CatchClause" && !node.parent.parent.finalizer) {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after finally block in try{}catch(e){}finally{}
                 if (node.parent.type === "TryStatement" && node.parent.finalizer && node.parent.finalizer === node) {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after class body methods in class {method(){}}
                 if (node.parent.parent && node.parent.parent.type === "MethodDefinition") {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
                 // Newline after anonymous block inside a block or program.
                 if (node.parent.type === "BlockStatement" || node.parent.type === "Program") {
-                    this._builder.appendNewline();
+                    this._appendNewline(node);
                     return;
                 }
             }
@@ -932,10 +944,10 @@
         if (programNode.body.length) {
             let lastNode = programNode.body[programNode.body.length - 1];
             if (lastNode.trailingComments)
-                this._insertCommentsAndNewlines(lastNode.trailingComments);
+                this._insertCommentsAndNewlines(lastNode, lastNode.trailingComments);
         } else {
             if (programNode.leadingComments)
-                this._insertCommentsAndNewlines(programNode.leadingComments);
+                this._insertCommentsAndNewlines(programNode, programNode.leadingComments);
             console.assert(!programNode.trailingComments);
         }
     }
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to