Web Inspector: JS PrettyPrinting unary - and + issues
authorcommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 4 Feb 2016 00:56:22 +0000 (00:56 +0000)
committercommit-queue@webkit.org <commit-queue@webkit.org@268f45cc-cd09-0410-ab3c-d52691b4dbfc>
Thu, 4 Feb 2016 00:56:22 +0000 (00:56 +0000)
https://bugs.webkit.org/show_bug.cgi?id=134007
<rdar://problem/17351953>

Patch by Joseph Pecoraro <pecoraro@apple.com> on 2016-02-03
Reviewed by Timothy Hatcher.

Source/WebInspectorUI:

* UserInterface/Views/CodeMirrorFormatters.js:
(shouldHaveSpaceBeforeToken):
(shouldHaveSpaceAfterLastToken):
(removeLastNewline):
(modifyStateForTokenPre):
(modifyStateForTokenPost):

LayoutTests:

* inspector/codemirror/prettyprinting-javascript-expected.txt:
* inspector/codemirror/prettyprinting-javascript.html:
* inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators-expected.js: Added.
* inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators.js: Added.
New tests for unary and some binary operators in JavaScript pretty printing.

git-svn-id: https://svn.webkit.org/repository/webkit/trunk@196098 268f45cc-cd09-0410-ab3c-d52691b4dbfc

LayoutTests/ChangeLog
LayoutTests/inspector/codemirror/prettyprinting-javascript-expected.txt
LayoutTests/inspector/codemirror/prettyprinting-javascript.html
LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators-expected.js [new file with mode: 0644]
LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators.js [new file with mode: 0644]
Source/WebInspectorUI/ChangeLog
Source/WebInspectorUI/UserInterface/Views/CodeMirrorFormatters.js

index 396b83e..23aa151 100644 (file)
@@ -1,3 +1,17 @@
+2016-02-03  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: JS PrettyPrinting unary - and + issues
+        https://bugs.webkit.org/show_bug.cgi?id=134007
+        <rdar://problem/17351953>
+
+        Reviewed by Timothy Hatcher.
+
+        * inspector/codemirror/prettyprinting-javascript-expected.txt:
+        * inspector/codemirror/prettyprinting-javascript.html:
+        * inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators-expected.js: Added.
+        * inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators.js: Added.
+        New tests for unary and some binary operators in JavaScript pretty printing.
+
 2016-02-03  Brent Fulgham  <bfulgham@apple.com>
 
         REGRESSION: LayoutTest http/tests/plugins/visible_plugins.html failing on Yosemite WK1
index 0dc3c2e..03bcf37 100644 (file)
@@ -11,3 +11,6 @@ PASS
 -- Running test case: CodeMirror.PrettyPrinting.JavaScript.switch-case-default.js
 PASS
 
+-- Running test case: CodeMirror.PrettyPrinting.JavaScript.unary-binary-operators.js
+PASS
+
index e7f70fd..bbf1a4d 100644 (file)
@@ -12,6 +12,7 @@ function test()
         "resources/prettyprinting/javascript-tests/block-comment.js",
         "resources/prettyprinting/javascript-tests/single-statement-blocks.js",
         "resources/prettyprinting/javascript-tests/switch-case-default.js",
+        "resources/prettyprinting/javascript-tests/unary-binary-operators.js",
     ]);
 
     suite.runTestCasesAndFinish();
diff --git a/LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators-expected.js b/LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators-expected.js
new file mode 100644 (file)
index 0000000..769fa11
--- /dev/null
@@ -0,0 +1,45 @@
++1;
+-1;
+~1;
+!1;
+!!1;
+1 + 1 - 1 * 1 / 1 % 1;
+1.2 + 1.2 - 1.2 * 1.2 / 1.2 % 1.2;
+(1 + 1 - 1 * 1 / 1 % 1);
+(1) + (1) - (1) * (1) / (1) % (1);
++(1);
++s;
+-s;
+~s;
+x = 1;
+x = 1e2;
+x = 1 + 1;
+x = 1e2 + 1e2;
+x = x + 1;
+x = x = x;
+x = x[1] + 1;
+x = [1];
+x = (1);
+x = (s);
+x = [1, 1, s];
+foo(1, 1, s);
+x = [1 + 1 - 1 * 1 / 1 % 1, +1, 1 - 1, -1, ~1, -s, s + 1, s - 1, a[1] + 1, a[1] - 1];
+foo(1 + 1 - 1 * 1 / 1 % 1, +1, 1 - 1, -1, ~1, -s, s + 1, s - 1, a[1] + 1, a[1] - 1);
+x = [-1 + 1 * 1 / 1 % 1];
+x = (-1 + 1 * 1 / 1 % 1);
+x = 1 ? +1 : -s;
+x = (1 ? +1 : -s);
+x = [1 ? -1 : +s];
+x = {
+    a: -1,
+    b: +1,
+    c: 1 - 1,
+    d: 1,
+    s: s
+};
+// FIXME: Still broken cases. Codemirror treats adjacent operator characters as one operator. For example:
+// x=+1 // Codemirror treats "=+" as a single operator.
+// x=1?+1:1 // Codemirror treats "?+" as a single operator.
+// x=1+-2+1 // Codemirror treats "+-" as a single operator.
+// x=1-+2+1 // Codemirror treats "-+" as a single operator.
+
diff --git a/LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators.js b/LayoutTests/inspector/codemirror/resources/prettyprinting/javascript-tests/unary-binary-operators.js
new file mode 100644 (file)
index 0000000..f27c673
--- /dev/null
@@ -0,0 +1,38 @@
++1;
+-1;
+~1;
+!1;
+!!1;
+1+1-1*1/1%1;
+1.2+1.2-1.2*1.2/1.2%1.2;
+(1+1-1*1/1%1);
+(1)+(1)-(1)*(1)/(1)%(1);
++(1);
++s;
+-s;
+~s;
+x=1;
+x=1e2;
+x=1+1;
+x=1e2+1e2;
+x=x+1;
+x=x=x;
+x=x[1]+1;
+x=[1];
+x=(1);
+x=(s);
+x=[1,1,s];
+foo(1,1,s);
+x=[1+1-1*1/1%1,+1,1-1,-1,~1,-s,s+1,s-1,a[1]+1,a[1]-1];
+foo(1+1-1*1/1%1,+1,1-1,-1,~1,-s,s+1,s-1,a[1]+1,a[1]-1);
+x=[-1+1*1/1%1];
+x=(-1+1*1/1%1);
+x=1? +1:-s;
+x=(1? +1:-s);
+x=[1? -1:+s];
+x={a:-1,b:+1,c:1-1,d:1,s:s};
+// FIXME: Still broken cases. Codemirror treats adjacent operator characters as one operator. For example:
+// x=+1 // Codemirror treats "=+" as a single operator.
+// x=1?+1:1 // Codemirror treats "?+" as a single operator.
+// x=1+-2+1 // Codemirror treats "+-" as a single operator.
+// x=1-+2+1 // Codemirror treats "-+" as a single operator.
index 42973fc..70ba72f 100644 (file)
@@ -1,3 +1,18 @@
+2016-02-03  Joseph Pecoraro  <pecoraro@apple.com>
+
+        Web Inspector: JS PrettyPrinting unary - and + issues
+        https://bugs.webkit.org/show_bug.cgi?id=134007
+        <rdar://problem/17351953>
+
+        Reviewed by Timothy Hatcher.
+
+        * UserInterface/Views/CodeMirrorFormatters.js:
+        (shouldHaveSpaceBeforeToken):
+        (shouldHaveSpaceAfterLastToken):
+        (removeLastNewline):
+        (modifyStateForTokenPre):
+        (modifyStateForTokenPost):
+
 2016-02-03  Dave Hyatt  <hyatt@apple.com>
 
         Add hanging-punctuation property to Web Inspector
index fb5bdba..4373225 100644 (file)
@@ -35,7 +35,7 @@ CodeMirror.extendMode("javascript", {
             if (content === "(") // Most keywords like "if (" but not "function(" or "typeof(".
                 return lastToken && /\bkeyword\b/.test(lastToken) && (lastContent !== "function" && lastContent !== "typeof" && lastContent !== "instanceof");
             if (content === ":") // Ternary.
-                return (state.lexical.type === "stat" || state.lexical.type === ")");
+                return (state.lexical.type === "stat" || state.lexical.type === ")" || state.lexical.type === "]");
             return false;
         }
 
@@ -43,9 +43,11 @@ CodeMirror.extendMode("javascript", {
             return true;
 
         if (/\boperator\b/.test(token)) {
+            if (!lastToken && (content === "+" || content === "-" || content === "~") && (lastContent !== ")" && lastContent !== "]")) // Possible Unary +/-.
+                return false;
             if (content === "!") // Unary ! should not be confused with "!=".
                 return false;
-            return "+-/*&&||!===+=-=>=<=?".indexOf(content) >= 0; // Operators.
+            return "+-/*%&&||!===+=-=>=<=?".indexOf(content) >= 0; // Operators.
         }
 
         if (/\bkeyword\b/.test(token)) { // Most keywords require spaces before them, unless a '}' can come before it.
@@ -86,7 +88,11 @@ CodeMirror.extendMode("javascript", {
         if (lastContent === "!") // Unary ! should not be confused with "!=".
             return false;
 
-        return ",+-/*&&||:!===+=-=>=<=?".indexOf(lastContent) >= 0; // Operators.
+        // If this unary operator did not have a leading expression it is probably unary.
+        if ((lastContent === "+" || lastContent === "-" || lastContent === "~") && !state._jsPrettyPrint.unaryOperatorHadLeadingExpr)
+            return false;
+
+        return ",+-/*%&&||:!===+=-=>=<=?".indexOf(lastContent) >= 0; // Operators.
     },
 
     newlinesAfterToken: function(lastToken, lastContent, token, state, content, isComment)
@@ -120,7 +126,7 @@ CodeMirror.extendMode("javascript", {
             if (content === ";") // "x = {};" or ";;".
                 return "};".indexOf(lastContent) >= 0;
             if (content === ":") // Ternary.
-                return lastContent === "}" && (state.lexical.type === "stat" || state.lexical.type === ")");
+                return lastContent === "}" && (state.lexical.type === "stat" || state.lexical.type === ")" || state.lexical.type === "]");
             if (",().".indexOf(content) >= 0) // "})", "}.bind", "function() { ... }()", or "}, false)".
                 return lastContent === "}";
             return false;
@@ -194,6 +200,7 @@ CodeMirror.extendMode("javascript", {
                 lastIfIndentCount: 0, // Keep track of the indent the last time we saw an if without braces.
                 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.
             };
         }
 
@@ -303,6 +310,11 @@ CodeMirror.extendMode("javascript", {
             state._jsPrettyPrint.dedentSize = 0;
             state._jsPrettyPrint.shouldDedent = false;
         }
+
+        if ((content === "+" || content === "-" || content === "~") && (lastContent === ")" || lastContent === "]" || /\b(?:variable|number)\b/.test(lastToken)))
+            state._jsPrettyPrint.unaryOperatorHadLeadingExpr = true;
+        else
+            state._jsPrettyPrint.unaryOperatorHadLeadingExpr = false;
     }
 });