Title: [164487] trunk/Tools
Revision
164487
Author
commit-qu...@webkit.org
Date
2014-02-21 11:17:28 -0800 (Fri, 21 Feb 2014)

Log Message

Web Inspector: update check-webkit-style to flag single quotes in WebInspectorUI projects
https://bugs.webkit.org/show_bug.cgi?id=128422

Patch by Diego Pino García <dp...@igalia.com> on 2014-02-21
Reviewed by Joseph Pecoraro.

* Scripts/webkitpy/style/checkers/js.py:
(JSChecker.__init__):
(JSChecker.check):
(SingleQuoteChecker):
(SingleQuoteChecker.__init__):
(SingleQuoteChecker.check):
* Scripts/webkitpy/style/checkers/js_unittest.py:
(JSTestCase.assertError.error_for_test):
(JSTestCase.assertError):
(JSTestCase.test_no_error):
(JSTestCase.test_error):

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (164486 => 164487)


--- trunk/Tools/ChangeLog	2014-02-21 19:14:17 UTC (rev 164486)
+++ trunk/Tools/ChangeLog	2014-02-21 19:17:28 UTC (rev 164487)
@@ -1 +1,20 @@
+2014-02-21  Diego Pino García  <dp...@igalia.com>
+
+        Web Inspector: update check-webkit-style to flag single quotes in WebInspectorUI projects
+        https://bugs.webkit.org/show_bug.cgi?id=128422
+
+        Reviewed by Joseph Pecoraro.
+
+        * Scripts/webkitpy/style/checkers/js.py:
+        (JSChecker.__init__):
+        (JSChecker.check):
+        (SingleQuoteChecker):
+        (SingleQuoteChecker.__init__):
+        (SingleQuoteChecker.check):
+        * Scripts/webkitpy/style/checkers/js_unittest.py:
+        (JSTestCase.assertError.error_for_test):
+        (JSTestCase.assertError):
+        (JSTestCase.test_no_error):
+        (JSTestCase.test_error):
+
 == Rolled over to ChangeLog-2014-02-20 ==

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/js.py (164486 => 164487)


--- trunk/Tools/Scripts/webkitpy/style/checkers/js.py	2014-02-21 19:14:17 UTC (rev 164486)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/js.py	2014-02-21 19:17:28 UTC (rev 164487)
@@ -1,4 +1,5 @@
 # Copyright (C) 2013 University of Washington. All rights reserved.
+# Copyright (C) 2014 Igalia S.L.
 #
 # Redistribution and use in source and binary forms, with or without
 # modification, are permitted provided that the following conditions are
@@ -23,11 +24,15 @@
 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
 # OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 
-"""Supports checking WebKit style in _javascript_ files"""
+"""
+Supports checking WebKit style in _javascript_ files.
 
+This checker is only used to check WebInspector _javascript_ files.
+"""
+
 from common import TabChecker
+import re
 
-
 class JSChecker(object):
     """Processes _javascript_ lines for checking style."""
 
@@ -37,6 +42,50 @@
     def __init__(self, file_path, handle_style_error):
         self._handle_style_error = handle_style_error
         self._tab_checker = TabChecker(file_path, handle_style_error)
+        self._single_quote_checker = SingleQuoteChecker(file_path, handle_style_error)
 
     def check(self, lines):
         self._tab_checker.check(lines)
+        self._single_quote_checker.check(lines)
+
+
+class SingleQuoteChecker(object):
+    """Checks there are not single quotes in source."""
+
+    def __init__(self, file_path, handle_style_error):
+        self._file_path = file_path
+        self._handle_style_error = handle_style_error
+
+    def check(self, lines):
+        in_multiline_comment = False
+        line_number = 0
+        for line in lines:
+            line = line.strip()
+            line_number = line_number + 1
+
+            if (line.endswith("*/")):
+                in_multiline_comment = False
+                continue
+
+            if (line.startswith("/*") or line.startswith("*")):
+                in_multiline_comment = True
+                continue
+
+            #  Remove "double quoted" strings.
+            line = re.sub(r'"(?:[^"\\]|\\.)*"', '""', line)
+
+            # Remove single line comment if any.
+            single_line_comment_pos = line.find('//')
+            if (single_line_comment_pos != -1):
+                    line = line[:single_line_comment_pos]
+
+            # The whole line was a single line comment, so continue.
+            if (len(line) == 0):
+                continue
+
+            # Remove regexes.
+            line = re.sub('/.+?/', '//', line)
+
+            single_quote_pos = line.find("'")
+            if (single_quote_pos != -1):
+                self._handle_style_error(line_number, "js/syntax", 5, "Line contains single-quote character.")

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/js_unittest.py (164486 => 164487)


--- trunk/Tools/Scripts/webkitpy/style/checkers/js_unittest.py	2014-02-21 19:14:17 UTC (rev 164486)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/js_unittest.py	2014-02-21 19:17:28 UTC (rev 164487)
@@ -56,18 +56,27 @@
         def error_for_test(line_number, category, confidence, message):
             """Checks if the expected error occurs."""
             self.assertEqual(expected_line_number, line_number)
-            self.assertEqual('whitespace/tab', category)
+            self.assertTrue(category in ['whitespace/tab', 'js/syntax'])
             self.had_error = True
 
         checker = JSChecker('', error_for_test)
         checker.check(lines)
-        self.assertTrue(self.had_error, '%s should have an error [whitespace/tab].' % lines)
+        self.assertTrue(self.had_error, '%s should have an error [whitespace/tab] or [js/syntax].' % lines)
 
     def test_no_error(self):
         """Tests for no error cases."""
         self.assertNoError([''])
         self.assertNoError(['abc def', 'ggg'])
+        # Single-quotes within string are OK.
+        self.assertNoError(['var foo = "''"'])
+        # Single-quotes within regular _expression_ are OK.
+        self.assertNoError(["var regex = /[a-z']/"])
 
     def test_error(self):
         """Tests for error cases."""
         self.assertError(['\tvar foo = window;\n'], 1)
+        # Single-quotes are not OK.
+        self.assertError(["var foo = 'Hello world!;'\n"], 1)
+        self.assertError(["foo(\"a'b\", 'c');"], 1)
+        self.assertError(["foo(\"a/'b\", '/c');"], 1)
+        self.assertError(["foo(/'/, 'c');"], 1)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to