]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Added `ArcanistTextLinter::LINT_BOF_WHITESPACE` and `ArcanistTextLinter::LINT_EOF_WHI...
authorJoshua Spence <josh@joshuaspence.com>
Tue, 14 Jan 2014 02:05:42 +0000 (18:05 -0800)
committerepriestley <git@epriestley.com>
Tue, 14 Jan 2014 02:05:42 +0000 (18:05 -0800)
Summary: This might not be universally desireable, but I found myself writing an additional linter (which I had called `WhitespaceTextLinter`) for the sake of these two linter tests. I figured it may be of use upstream, and so I decided to submit it as a diff. I won't be offended if it is rejected however.

Test Plan: `arc lint` and `arc unit` are both okay with it.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley

CC: Korvin, epriestley, aran
Differential Revision: https://secure.phabricator.com/D7957

.divinerconfig
bin/arc.bat
scripts/arcanist.php
src/lint/linter/ArcanistLicenseLinter.php
src/lint/linter/ArcanistTextLinter.php
src/lint/linter/__tests__/text/bof-whitespace-1.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/text/bof-whitespace-2.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/text/eof-whitespace.lint-test [new file with mode: 0644]
src/unit/engine/CSharpToolsTestEngine.php
src/workflow/ArcanistBackoutWorkflow.php
src/workflow/ArcanistRevertWorkflow.php

index 6b02d18bc002accee73fdb5c8da9d668408cc266..17f75d8853ae677123e1c0d3888c5f50c23203e8 100644 (file)
@@ -20,4 +20,3 @@
     ["DivinerXHPEngine", {}]
   ]
 }
-
index a1fd8f6c8928a5f17dc3696d535da2c3cc74dc80..5ce474bfc8c9666904a387efbec5016109c4bfc6 100644 (file)
@@ -1,3 +1,2 @@
 @echo off
 php -f "%~dp0..\scripts\arcanist.php" -- %*
-
index 3c4936b8928b1db56f8d7170a6a715b3eccc5213..62a67e50ae7bd3380d866c0523d688514c57ffa3 100755 (executable)
@@ -624,4 +624,3 @@ function reenter_if_this_is_arcanist_or_libphutil(
 
   exit($err);
 }
-
index 1f071ddf6542e912e062a68646b0ed99c3541b6d..815e178ab4186dce77a5c86b76389bb9e64dedcb 100644 (file)
@@ -57,5 +57,3 @@ abstract class ArcanistLicenseLinter extends ArcanistLinter {
     }
   }
 }
-
-
index 4fdbef482b39387e8c81c6cb52f231318e590583..9042207ebf1144ab33c9e5849eb1e40121f29000 100644 (file)
@@ -14,6 +14,8 @@ final class ArcanistTextLinter extends ArcanistLinter {
   const LINT_BAD_CHARSET            = 5;
   const LINT_TRAILING_WHITESPACE    = 6;
   const LINT_NO_COMMIT              = 7;
+  const LINT_BOF_WHITESPACE         = 8;
+  const LINT_EOF_WHITESPACE         = 9;
 
   private $maxLineLength = 80;
 
@@ -38,6 +40,8 @@ final class ArcanistTextLinter extends ArcanistLinter {
     return array(
       self::LINT_LINE_WRAP => ArcanistLintSeverity::SEVERITY_WARNING,
       self::LINT_TRAILING_WHITESPACE => ArcanistLintSeverity::SEVERITY_AUTOFIX,
+      self::LINT_BOF_WHITESPACE => ArcanistLintSeverity::SEVERITY_AUTOFIX,
+      self::LINT_EOF_WHITESPACE => ArcanistLintSeverity::SEVERITY_AUTOFIX,
     );
   }
 
@@ -50,6 +54,8 @@ final class ArcanistTextLinter extends ArcanistLinter {
       self::LINT_BAD_CHARSET          => pht('Bad Charset'),
       self::LINT_TRAILING_WHITESPACE  => pht('Trailing Whitespace'),
       self::LINT_NO_COMMIT            => pht('Explicit %s', '@no'.'commit'),
+      self::LINT_BOF_WHITESPACE       => pht('Leading Whitespace at BOF'),
+      self::LINT_EOF_WHITESPACE       => pht('Trailing Whitespace at EOF'),
     );
   }
 
@@ -77,6 +83,9 @@ final class ArcanistTextLinter extends ArcanistLinter {
     $this->lintEOFNewline($path);
     $this->lintTrailingWhitespace($path);
 
+    $this->lintBOFWhitespace($path);
+    $this->lintEOFWhitespace($path);
+
     if ($this->getEngine()->getCommitHookMode()) {
       $this->lintNoCommit($path);
     }
@@ -194,6 +203,54 @@ final class ArcanistTextLinter extends ArcanistLinter {
     }
   }
 
+  protected function lintBOFWhitespace($path) {
+    $data = $this->getData($path);
+
+    $matches = null;
+    $preg = preg_match(
+      '/^\s*\n/',
+      $data,
+      $matches,
+      PREG_OFFSET_CAPTURE);
+
+    if (!$preg) {
+      return;
+    }
+
+    list($string, $offset) = $matches[0];
+    $this->raiseLintAtOffset(
+      $offset,
+      self::LINT_BOF_WHITESPACE,
+      'This file contains leading whitespace at the beginning of the file. ' .
+      'This is unnecessary and should be avoided when possible.',
+      $string,
+      '');
+  }
+
+  protected function lintEOFWhitespace($path) {
+    $data = $this->getData($path);
+
+    $matches = null;
+    $preg = preg_match(
+      '/(?<=\n)\s+$/',
+      $data,
+      $matches,
+      PREG_OFFSET_CAPTURE);
+
+    if (!$preg) {
+      return;
+    }
+
+    list($string, $offset) = $matches[0];
+    $this->raiseLintAtOffset(
+      $offset,
+      self::LINT_EOF_WHITESPACE,
+      'This file contains trailing whitespace at the end of the file. This ' .
+      'is unnecessary and should be avoided when possible.',
+      $string,
+      '');
+  }
+
   private function lintNoCommit($path) {
     $data = $this->getData($path);
 
@@ -210,5 +267,4 @@ final class ArcanistTextLinter extends ArcanistLinter {
     }
   }
 
-
 }
diff --git a/src/lint/linter/__tests__/text/bof-whitespace-1.lint-test b/src/lint/linter/__tests__/text/bof-whitespace-1.lint-test
new file mode 100644 (file)
index 0000000..995aad6
--- /dev/null
@@ -0,0 +1,9 @@
+
+
+
+
+The quick brown fox jumps over the lazy dog.
+~~~~~~~~~~
+autofix:1:1
+~~~~~~~~~~
+The quick brown fox jumps over the lazy dog.
diff --git a/src/lint/linter/__tests__/text/bof-whitespace-2.lint-test b/src/lint/linter/__tests__/text/bof-whitespace-2.lint-test
new file mode 100644 (file)
index 0000000..87ae7e6
--- /dev/null
@@ -0,0 +1,2 @@
+   The quick brown fox jumps over the lazy dog.
+~~~~~~~~~~
diff --git a/src/lint/linter/__tests__/text/eof-whitespace.lint-test b/src/lint/linter/__tests__/text/eof-whitespace.lint-test
new file mode 100644 (file)
index 0000000..b3b778c
--- /dev/null
@@ -0,0 +1,9 @@
+The quick brown fox jumps over the lazy dog.
+
+
+
+
+~~~~~~~~~~
+autofix:2:1
+~~~~~~~~~~
+The quick brown fox jumps over the lazy dog.
index 9b85257a9d461a68723662a788244a4a8c20eb47..d2557894b969bc501577b7f95c7f5ed9d13de897 100644 (file)
@@ -284,5 +284,3 @@ final class CSharpToolsTestEngine extends XUnitTestEngine {
     return $reports;
   }
 }
-
-
index ac7c73d219459f90ab94cfe7eebe80683a886d6d..b7e0486d538c1d62eecd26cd86d59ae357232488 100644 (file)
@@ -181,4 +181,3 @@ EOTEXT
 
   }
 }
-
index ee7bc743a7118f3c5c4a439c013b22313698ee67..4b1b949160f5c10e028766481b15fbdc0cc8349a 100644 (file)
@@ -35,4 +35,3 @@ EOTEXT
     $console->writeOut("Please use arc backout instead.\n");
   }
 }
-