]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for detecting empty files
authorJoshua Spence <josh@joshuaspence.com>
Tue, 1 Sep 2015 09:20:11 +0000 (19:20 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Tue, 1 Sep 2015 09:30:55 +0000 (19:30 +1000)
Summary: Adds two different linter rules (one general purpose and another PHP specific) to prevent empty files from being added to a repository. For some unknown reason, people seem to like to do this.

Test Plan: Added test cases.

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: avivey, cburroughs, Korvin

Differential Revision: https://secure.phabricator.com/D13881

src/__phutil_library_map__.php
src/lint/linter/ArcanistTextLinter.php
src/lint/linter/__tests__/text/empty.lint-test [moved from src/lint/linter/__tests__/text/empty-file.lint-test with 52% similarity]
src/lint/linter/__tests__/xhpast/empty.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/xhpast/not-empty.lint-test [new file with mode: 0644]
src/lint/linter/__tests__/xhpast/php-tags-short.lint-test
src/lint/linter/xhpast/ArcanistXHPASTLinterRule.php
src/lint/linter/xhpast/rules/ArcanistEmptyFileXHPASTLinterRule.php [new file with mode: 0644]

index f8b6a6853202187cd5f78e896b689b939b997e40..96d7da918183ecaadcf8c806b600ad65d6cfa77e 100644 (file)
@@ -90,6 +90,7 @@ phutil_register_library_map(array(
     'ArcanistDuplicateSwitchCaseXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDuplicateSwitchCaseXHPASTLinterRule.php',
     'ArcanistDynamicDefineXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDynamicDefineXHPASTLinterRule.php',
     'ArcanistElseIfUsageXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistElseIfUsageXHPASTLinterRule.php',
+    'ArcanistEmptyFileXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistEmptyFileXHPASTLinterRule.php',
     'ArcanistEmptyStatementXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistEmptyStatementXHPASTLinterRule.php',
     'ArcanistEventType' => 'events/constant/ArcanistEventType.php',
     'ArcanistExitExpressionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistExitExpressionXHPASTLinterRule.php',
@@ -375,6 +376,7 @@ phutil_register_library_map(array(
     'ArcanistDuplicateSwitchCaseXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistDynamicDefineXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistElseIfUsageXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistEmptyFileXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistEmptyStatementXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistEventType' => 'PhutilEventType',
     'ArcanistExitExpressionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
index 78d86a401450964b2c93802db7d31beffd5be621..2f81b03991c7d4114f70c2470d952737b8fcf269 100644 (file)
@@ -13,6 +13,7 @@ final class ArcanistTextLinter extends ArcanistLinter {
   const LINT_TRAILING_WHITESPACE  = 6;
   const LINT_BOF_WHITESPACE       = 8;
   const LINT_EOF_WHITESPACE       = 9;
+  const LINT_EMPTY_FILE           = 10;
 
   private $maxLineLength = 80;
 
@@ -85,16 +86,23 @@ final class ArcanistTextLinter extends ArcanistLinter {
       self::LINT_TRAILING_WHITESPACE => pht('Trailing Whitespace'),
       self::LINT_BOF_WHITESPACE      => pht('Leading Whitespace at BOF'),
       self::LINT_EOF_WHITESPACE      => pht('Trailing Whitespace at EOF'),
+      self::LINT_EMPTY_FILE          => pht('Empty File'),
     );
   }
 
   public function lintPath($path) {
+    $this->lintEmptyFile($path);
+
     if (!strlen($this->getData($path))) {
       // If the file is empty, don't bother; particularly, don't require
       // the user to add a newline.
       return;
     }
 
+    if ($this->didStopAllLinters()) {
+      return;
+    }
+
     $this->lintNewlines($path);
     $this->lintTabs($path);
 
@@ -116,6 +124,29 @@ final class ArcanistTextLinter extends ArcanistLinter {
     $this->lintEOFWhitespace($path);
   }
 
+  protected function lintEmptyFile($path) {
+    $data = $this->getData($path);
+
+    // It is reasonable for certain file types to be completely empty,
+    // so they are excluded here.
+    switch ($filename = basename($this->getActivePath())) {
+      case '__init__.py':
+        return;
+
+      default:
+        if (strlen($filename) && $filename[0] == '.') {
+          return;
+        }
+    }
+
+    if (preg_match('/^\s*$/', $data)) {
+      $this->raiseLintAtPath(
+        self::LINT_EMPTY_FILE,
+        pht("Empty files usually don't serve any useful purpose."));
+      $this->stopAllLinters();
+    }
+  }
+
   protected function lintNewlines($path) {
     $data = $this->getData($path);
     $pos  = strpos($this->getData($path), "\r");
similarity index 52%
rename from src/lint/linter/__tests__/text/empty-file.lint-test
rename to src/lint/linter/__tests__/text/empty.lint-test
index 383e5ae1aecffd4c8a6de0163c49c71c57a15334..546b0f1e36aa21b88100cfcc104d6926a81455b0 100644 (file)
@@ -1 +1,4 @@
+
+
 ~~~~~~~~~~
+error::
diff --git a/src/lint/linter/__tests__/xhpast/empty.lint-test b/src/lint/linter/__tests__/xhpast/empty.lint-test
new file mode 100644 (file)
index 0000000..5759570
--- /dev/null
@@ -0,0 +1,3 @@
+<?php ?>
+~~~~~~~~~~
+warning::
diff --git a/src/lint/linter/__tests__/xhpast/not-empty.lint-test b/src/lint/linter/__tests__/xhpast/not-empty.lint-test
new file mode 100644 (file)
index 0000000..2dccc3c
--- /dev/null
@@ -0,0 +1,4 @@
+<?php
+
+// This file is not empty.
+~~~~~~~~~~
index 61f018b9e604d2e9793f1e5f871202069bef8dd3..11d82b5c7d33ab252effe8df998f23f1556837d4 100644 (file)
@@ -1,6 +1,7 @@
 <?
 
 ~~~~~~~~~~
+warning::
 error:1:1
 ~~~~~~~~~~
 <?php
index da5a37b5e55ccbda39d4e8de8a2731707eec824e..aa0cad388f4298f736137b3cee8791b37bd277fb 100644 (file)
@@ -146,6 +146,10 @@ abstract class ArcanistXHPASTLinterRule extends Phobject {
       $replace);
   }
 
+  final protected function raiseLintAtPath($desc) {
+    return $this->linter->raiseLintAtPath($this->getLintID(), $desc);
+  }
+
   final protected function raiseLintAtToken(
     XHPASTToken $token,
     $desc,
diff --git a/src/lint/linter/xhpast/rules/ArcanistEmptyFileXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistEmptyFileXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..28d6b4e
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+final class ArcanistEmptyFileXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 82;
+
+  public function getLintName() {
+    return pht('Empty File');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $tokens = $root->getTokens();
+
+    foreach ($tokens as $token) {
+      switch ($token->getTypeName()) {
+        case 'T_OPEN_TAG':
+        case 'T_CLOSE_TAG':
+        case 'T_WHITESPACE':
+          break;
+
+        default:
+          return;
+      }
+    }
+
+    $this->raiseLintAtPath(
+      pht("Empty files usually don't serve any useful purpose."));
+  }
+
+}