Title: [198815] trunk
Revision
198815
Author
dburk...@apple.com
Date
2016-03-29 17:31:01 -0700 (Tue, 29 Mar 2016)

Log Message

Web Inspector: JS PrettyPrinting in do/while loops, "while" should be on the same line as "}" if there was a closing brace
https://bugs.webkit.org/show_bug.cgi?id=117616
<rdar://problem/15796884>

Reviewed by Joseph Pecoraro.

Source/WebInspectorUI:

This patch fixes the formatting of do / while loops in the WebInspector CodeFormatter.

Before:
    do {
      "x"
    }
    while (0);

After:
    do {
      "x"
    } while (0);

* UserInterface/Views/CodeMirrorFormatters.js:
(shouldHaveSpaceBeforeToken):
If we encounter a while token and the last token was a closing brace, we *should* add a space if that closing
brace was closing a do block.

(removeLastNewline):
If we encounter a while token and the last token was a closing brace, we *should not* add a newline if that closing
brace closes a do block.

(modifyStateForTokenPre):
We should keep track of the last token that we encountered before entering into a block. We do this by setting
a lastContentBeforeBlock property on openBraceStartMarker / state objects.

In addition, this fixes a bug where we do not pop a state object off of openBraceStartMarkers if our indentCount
is 0. Without doing this, we cannot reliably determine whether or not our while token needs to be inline or not.

LayoutTests:

* inspector/codemirror/prettyprinting-_javascript_-expected.txt:
* inspector/codemirror/prettyprinting-_javascript_.html:
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while.js: Added.

Modified Paths

Added Paths

Diff

Modified: trunk/LayoutTests/ChangeLog (198814 => 198815)


--- trunk/LayoutTests/ChangeLog	2016-03-30 00:26:00 UTC (rev 198814)
+++ trunk/LayoutTests/ChangeLog	2016-03-30 00:31:01 UTC (rev 198815)
@@ -1,3 +1,24 @@
+2016-03-29  Dana Burkart and Matthew Hanson  <dburk...@apple.com>
+
+        Web Inspector: JS PrettyPrinting in do/while loops, "while" should be on the same line as "}" if there was a closing brace
+        https://bugs.webkit.org/show_bug.cgi?id=117616
+        <rdar://problem/15796884>
+
+        Reviewed by Joseph Pecoraro.
+
+        * inspector/codemirror/prettyprinting-_javascript_-expected.txt:
+        * inspector/codemirror/prettyprinting-_javascript_.html:
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while.js: Added.
+
 2016-03-29  Saam barati  <sbar...@apple.com>
 
         Fix typos in our error messages and remove some trailing periods

Modified: trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_-expected.txt (198814 => 198815)


--- trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_-expected.txt	2016-03-30 00:26:00 UTC (rev 198814)
+++ trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_-expected.txt	2016-03-30 00:31:01 UTC (rev 198815)
@@ -14,3 +14,18 @@
 -- Running test case: CodeMirror.PrettyPrinting._javascript_.unary-binary-operators.js
 PASS
 
+-- Running test case: CodeMirror.PrettyPrinting._javascript_.do-while-loop.js
+PASS
+
+-- Running test case: CodeMirror.PrettyPrinting._javascript_.if-followed-by-while.js
+PASS
+
+-- Running test case: CodeMirror.PrettyPrinting._javascript_.while-within-do-while.js
+PASS
+
+-- Running test case: CodeMirror.PrettyPrinting._javascript_.if-while-within-do-while.js
+PASS
+
+-- Running test case: CodeMirror.PrettyPrinting._javascript_.do-while-within-if.js
+PASS
+

Modified: trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_.html (198814 => 198815)


--- trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_.html	2016-03-30 00:26:00 UTC (rev 198814)
+++ trunk/LayoutTests/inspector/codemirror/prettyprinting-_javascript_.html	2016-03-30 00:31:01 UTC (rev 198815)
@@ -13,6 +13,11 @@
         "resources/prettyprinting/_javascript_-tests/single-statement-blocks.js",
         "resources/prettyprinting/_javascript_-tests/switch-case-default.js",
         "resources/prettyprinting/_javascript_-tests/unary-binary-operators.js",
+        "resources/prettyprinting/_javascript_-tests/do-while-loop.js",
+        "resources/prettyprinting/_javascript_-tests/if-followed-by-while.js",
+        "resources/prettyprinting/_javascript_-tests/while-within-do-while.js",
+        "resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js",
+        "resources/prettyprinting/_javascript_-tests/do-while-within-if.js",
     ]);
 
     suite.runTestCasesAndFinish();

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop-expected.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop-expected.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop-expected.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1,4 @@
+do {
+    "x"
+} while (0);
+

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-loop.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1 @@
+do{"x"}while(0);

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if-expected.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if-expected.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if-expected.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1,6 @@
+if (true) {
+    do {
+        var x;
+    } while (0);
+}
+

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/do-while-within-if.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1 @@
+if (true) { do {var x;} while (0);}

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while-expected.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while-expected.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while-expected.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1,7 @@
+if (x == 2) {
+    var y
+}
+while (0) {
+    y = 3
+}
+

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-followed-by-while.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1 @@
+if(x==2){var y}while(0){y=3}

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while-expected.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while-expected.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while-expected.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1,13 @@
+do {
+    if (true) {
+        var x;
+    }
+    while (0) {
+        x = 1;
+    }
+    if (true)
+        var y;
+    while (0)
+        y = 1;
+} while (0);
+

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/if-while-within-do-while.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1 @@
+do {if (true) {var x;}while (0) {x = 1;}if (true)var y;while (0)y = 1;}while (0);

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while-expected.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while-expected.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while-expected.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1,7 @@
+do {
+    var counter = 0;
+    while (counter < 3) {
+        counter += 1;
+    }
+} while (0);
+

Added: trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while.js (0 => 198815)


--- trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while.js	                        (rev 0)
+++ trunk/LayoutTests/inspector/codemirror/resources/prettyprinting/_javascript_-tests/while-within-do-while.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -0,0 +1 @@
+do{var counter=0;while(counter<3){counter+=1;}}while(0);

Modified: trunk/Source/WebInspectorUI/ChangeLog (198814 => 198815)


--- trunk/Source/WebInspectorUI/ChangeLog	2016-03-30 00:26:00 UTC (rev 198814)
+++ trunk/Source/WebInspectorUI/ChangeLog	2016-03-30 00:31:01 UTC (rev 198815)
@@ -1,3 +1,40 @@
+2016-03-29  Dana Burkart and Matthew Hanson  <dburk...@apple.com>
+
+        Web Inspector: JS PrettyPrinting in do/while loops, "while" should be on the same line as "}" if there was a closing brace
+        https://bugs.webkit.org/show_bug.cgi?id=117616
+        <rdar://problem/15796884>
+
+        Reviewed by Joseph Pecoraro.
+
+        This patch fixes the formatting of do / while loops in the WebInspector CodeFormatter.
+
+        Before:
+            do {
+              "x"
+            }
+            while (0);
+
+        After:
+            do {
+              "x"
+            } while (0);
+
+        * UserInterface/Views/CodeMirrorFormatters.js:
+        (shouldHaveSpaceBeforeToken):
+        If we encounter a while token and the last token was a closing brace, we *should* add a space if that closing
+        brace was closing a do block.
+
+        (removeLastNewline):
+        If we encounter a while token and the last token was a closing brace, we *should not* add a newline if that closing
+        brace closes a do block.
+
+        (modifyStateForTokenPre):
+        We should keep track of the last token that we encountered before entering into a block. We do this by setting
+        a lastContentBeforeBlock property on openBraceStartMarker / state objects.
+
+        In addition, this fixes a bug where we do not pop a state object off of openBraceStartMarkers if our indentCount
+        is 0. Without doing this, we cannot reliably determine whether or not our while token needs to be inline or not.
+
 2016-03-29  Joseph Pecoraro  <pecor...@apple.com>
 
         Web Inspector: REGRESSION: ⌘E and ⌘G text searching does not work

Modified: trunk/Source/WebInspectorUI/UserInterface/Views/CodeMirrorFormatters.js (198814 => 198815)


--- trunk/Source/WebInspectorUI/UserInterface/Views/CodeMirrorFormatters.js	2016-03-30 00:26:00 UTC (rev 198814)
+++ trunk/Source/WebInspectorUI/UserInterface/Views/CodeMirrorFormatters.js	2016-03-30 00:31:01 UTC (rev 198815)
@@ -53,6 +53,8 @@
         if (/\bkeyword\b/.test(token)) { // Most keywords require spaces before them, unless a '}' can come before it.
             if (content === "else" || content === "catch" || content === "finally")
                 return lastContent === "}";
+            if (content === "while" && lastContent === "}")
+                return state._jsPrettyPrint.lastContentBeforeBlock === "do";
             return false;
         }
 
@@ -141,6 +143,8 @@
         if (/\bkeyword\b/.test(token)) {
             if (content === "else" || content === "catch" || content === "finally") // "} else", "} catch", "} finally"
                 return lastContent === "}";
+            if (content === "while" && lastContent === "}")
+                return state._jsPrettyPrint.lastContentBeforeBlock === "do";
             return false;
         }
 
@@ -201,6 +205,7 @@
                 openBraceStartMarkers: [],  // Keep track of non-single statement blocks.
                 openBraceTrackingCount: -1, // Keep track of "{" and "}" in non-single statement blocks.
                 unaryOperatorHadLeadingExpr: false, // Try to detect if a unary operator had a leading _expression_ and therefore may be binary.
+                lastContentBeforeBlock: undefined, // Used to detect if this was a do/while.
             };
         }
 
@@ -224,7 +229,7 @@
         if (!isComment && state.lexical.prev && state.lexical.prev.type === "form" && !state.lexical.prev._jsPrettyPrintMarker && (lastContent === ")" || lastContent === "else" || lastContent === "do") && (state.lexical.type !== ")")) {
             if (content === "{") {
                 // Save the state at the opening brace so we can return to it when we see "}".
-                var savedState = {indentCount: state._jsPrettyPrint.indentCount, openBraceTrackingCount: state._jsPrettyPrint.openBraceTrackingCount};
+                var savedState = {indentCount: state._jsPrettyPrint.indentCount, openBraceTrackingCount: state._jsPrettyPrint.openBraceTrackingCount, lastContentBeforeBlock: lastContent};
                 state._jsPrettyPrint.openBraceStartMarkers.push(savedState);
                 state._jsPrettyPrint.openBraceTrackingCount = 1;
             } else if (state.lexical.type !== "}") {
@@ -241,7 +246,6 @@
 
         // - Leaving:
         //   - Preconditions:
-        //     - we must be indented
         //     - ignore ";", wait for the next token instead.
         //   - Cases:
         //     1. "else"
@@ -250,7 +254,7 @@
         //       - dedent to the last "{"
         //     3. Token without a marker on the stack
         //       - dedent all the way
-        else if (state._jsPrettyPrint.indentCount) {
+        else {
             console.assert(!state._jsPrettyPrint.shouldDedent);
             console.assert(!state._jsPrettyPrint.dedentSize);
 
@@ -277,6 +281,7 @@
                 state._jsPrettyPrint.shouldDedent = true;
                 state._jsPrettyPrint.dedentSize = state._jsPrettyPrint.indentCount - savedState.indentCount;
                 state._jsPrettyPrint.openBraceTrackingCount = savedState.openBraceTrackingCount;
+                state._jsPrettyPrint.lastContentBeforeBlock = savedState.lastContentBeforeBlock;
             } else {
                 // Dedent all the way.
                 var shouldDedent = true;
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to