Title: [160319] trunk/Tools
Revision
160319
Author
commit-qu...@webkit.org
Date
2013-12-09 11:01:09 -0800 (Mon, 09 Dec 2013)

Log Message

check-webkit-style should check for extra newlines at EOF
https://bugs.webkit.org/show_bug.cgi?id=125424

Patch by Brian J. Burg <b...@cs.washington.edu> on 2013-12-09
Reviewed by Darin Adler.

Report a style violation if extraneous newlines are added
to the end of a C++ file. There should only be one newline at EOF.

* Scripts/webkitpy/style/checkers/cpp.py:
(check_for_missing_new_line_at_eof): Renamed from check_for_new_line_at_eof.
(check_for_extra_new_line_at_eof): Added.
(_process_lines): Added new check and renamed existing EOF check.
* Scripts/webkitpy/style/checkers/cpp_unittest.py:
(CppStyleTest.test_extra_newlines_at_eof): Added.
(CppStyleTest.test_extra_newlines_at_eof.do_test): Added.

Modified Paths

Diff

Modified: trunk/Tools/ChangeLog (160318 => 160319)


--- trunk/Tools/ChangeLog	2013-12-09 18:57:53 UTC (rev 160318)
+++ trunk/Tools/ChangeLog	2013-12-09 19:01:09 UTC (rev 160319)
@@ -1,3 +1,21 @@
+2013-12-09  Brian J. Burg  <b...@cs.washington.edu>
+
+        check-webkit-style should check for extra newlines at EOF
+        https://bugs.webkit.org/show_bug.cgi?id=125424
+
+        Reviewed by Darin Adler.
+
+        Report a style violation if extraneous newlines are added
+        to the end of a C++ file. There should only be one newline at EOF.
+
+        * Scripts/webkitpy/style/checkers/cpp.py:
+        (check_for_missing_new_line_at_eof): Renamed from check_for_new_line_at_eof.
+        (check_for_extra_new_line_at_eof): Added.
+        (_process_lines): Added new check and renamed existing EOF check.
+        * Scripts/webkitpy/style/checkers/cpp_unittest.py:
+        (CppStyleTest.test_extra_newlines_at_eof): Added.
+        (CppStyleTest.test_extra_newlines_at_eof.do_test): Added.
+
 2013-12-09  Laszlo Vidacs  <l...@inf.u-szeged.hu>
 
         check-webkit-style: ternary operator in macro call identified as initialization list

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py (160318 => 160319)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2013-12-09 18:57:53 UTC (rev 160318)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp.py	2013-12-09 19:01:09 UTC (rev 160319)
@@ -957,8 +957,8 @@
                   'Line contains invalid UTF-8 (or Unicode replacement character).')
 
 
-def check_for_new_line_at_eof(lines, error):
-    """Logs an error if there is no newline char at the end of the file.
+def check_for_missing_new_line_at_eof(lines, error):
+    """Logs an error if there is not a newline character at the end of the file.
 
     Args:
       lines: An array of strings, each representing a line of the file.
@@ -974,6 +974,20 @@
               'Could not find a newline character at the end of the file.')
 
 
+def check_for_extra_new_line_at_eof(lines, error):
+    """Logs an error if there is not a single newline at the end of the file.
+
+    Args:
+      lines: An array of strings, each representing a line of the file.
+      error: The function to call with any errors found.
+    """
+    # The array lines() was created by adding two newlines to the
+    # original file (go figure), then splitting on \n.
+    if len(lines) > 3 and not lines[-3]:
+        error(len(lines) - 2, 'whitespace/ending_newline', 5,
+              'There was more than one newline at the end of the file.')
+
+
 def check_for_multiline_comments_and_strings(clean_lines, line_number, error):
     """Logs an error if we see /* ... */ or "..." that extend past one line.
 
@@ -3635,7 +3649,8 @@
     # lines rather than "cleaned" lines.
     check_for_unicode_replacement_characters(lines, error)
 
-    check_for_new_line_at_eof(lines, error)
+    check_for_missing_new_line_at_eof(lines, error)
+    check_for_extra_new_line_at_eof(lines, error)
 
 
 class CppChecker(object):

Modified: trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py (160318 => 160319)


--- trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2013-12-09 18:57:53 UTC (rev 160318)
+++ trunk/Tools/Scripts/webkitpy/style/checkers/cpp_unittest.py	2013-12-09 19:01:09 UTC (rev 160319)
@@ -1937,6 +1937,23 @@
         do_test(self, '// Newline\n// at EOF\n', False)
         do_test(self, '// No newline\n// at EOF', True)
 
+    def test_extra_newlines_at_eof(self):
+        def do_test(self, data, too_many_newlines):
+            error_collector = ErrorCollector(self.assertTrue)
+            self.process_file_data('foo.cpp', 'cpp', data.split('\n'),
+                                   error_collector)
+            # The warning appears only once.
+            self.assertEqual(
+                int(too_many_newlines),
+                error_collector.results().count(
+                    'There was more than one newline at the end of the file.'
+                    '  [whitespace/ending_newline] [5]'))
+
+        do_test(self, '// No Newline\n// at EOF', False)
+        do_test(self, '// One Newline\n// at EOF\n', False)
+        do_test(self, '// Two Newlines\n// at EOF\n\n', True)
+        do_test(self, '// Three Newlines\n// at EOF\n\n\n', True)
+
     def test_invalid_utf8(self):
         def do_test(self, raw_bytes, has_invalid_utf8):
             error_collector = ErrorCollector(self.assertTrue)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to