]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Expose PEP8, Flake8 and CSSLint engines to .arclint
authorepriestley <git@epriestley.com>
Fri, 23 Aug 2013 18:58:07 +0000 (11:58 -0700)
committerepriestley <git@epriestley.com>
Fri, 23 Aug 2013 18:58:07 +0000 (11:58 -0700)
Summary:
Ref T3186. Ref T2039. Allow these linters to be selected with `.arclint`.

Also allow severities to be set.

Also fix some other minor bugs.

Test Plan:
https://github.com/epriestley/arclint-examples
https://github.com/epriestley/arclint-examples/blob/master/.arclint

Reviewers: btrahan

Reviewed By: btrahan

CC: chad, aran
Maniphest Tasks: T2039, T3186

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

src/lint/engine/ArcanistConfigurationDrivenLintEngine.php
src/lint/linter/ArcanistCSSLintLinter.php
src/lint/linter/ArcanistExternalLinter.php
src/lint/linter/ArcanistFlake8Linter.php
src/lint/linter/ArcanistLinter.php
src/lint/linter/ArcanistPEP8Linter.php
src/lint/linter/__tests__/flake8/undefined.lint-test

index c2f8b5efea76b435386afd8781c8fd3e616a19b8..cb121a8062ffc8f737b8cfb3f687ff1dccd7b0ca 100644 (file)
@@ -65,13 +65,14 @@ final class ArcanistConfigurationDrivenLintEngine extends ArcanistLintEngine {
       foreach ($more as $key => $value) {
         if (array_key_exists($key, $spec)) {
           try {
-            $linter->setLinterConfigurationValue($key, $spec);
+            $linter->setLinterConfigurationValue($key, $spec[$key]);
           } catch (Exception $ex) {
             $message = pht(
               'Error in parsing ".arclint" file, in key "%s" for '.
-              'linter "%s".',
+              'linter "%s": %s',
               $key,
-              $name);
+              $name,
+              $ex->getMessage());
             throw new PhutilProxyException($message, $ex);
           }
         }
index a40c3cdc2194fc33eea41b54a5dece09d980d281..3272c47d5d77d2fd4bdbcdb2528d2cecfab0d8dd 100644 (file)
@@ -19,6 +19,10 @@ final class ArcanistCSSLintLinter extends ArcanistExternalLinter {
     return 'CSSLint';
   }
 
+  public function getLinterConfigurationName() {
+    return 'csslint';
+  }
+
   public function getMandatoryFlags() {
     return '--format=lint-xml';
   }
@@ -65,7 +69,7 @@ final class ArcanistCSSLintLinter extends ArcanistExternalLinter {
 
         $data = $this->getData($path);
         $lines = explode("\n", $data);
-        $name = $this->getLinterName() . ' - ' . $child->getAttribute('reason');
+        $name = $child->getAttribute('reason');
         $severity = ($child->getAttribute('severity') == 'warning')
           ? ArcanistLintSeverity::SEVERITY_WARNING
           : ArcanistLintSeverity::SEVERITY_ERROR;
@@ -74,11 +78,8 @@ final class ArcanistCSSLintLinter extends ArcanistExternalLinter {
         $message->setPath($path);
         $message->setLine($child->getAttribute('line'));
         $message->setChar($child->getAttribute('char'));
-        $message->setCode($child->getAttribute('severity'));
-        $message->setName($name);
-        $message->setDescription(
-          $child->getAttribute('reason').
-          "\nEvidence:".$child->getAttribute('evidence'));
+        $message->setCode('CSSLint');
+        $message->setDescription($child->getAttribute('reason'));
         $message->setSeverity($severity);
 
         if ($child->hasAttribute('line')) {
@@ -93,4 +94,20 @@ final class ArcanistCSSLintLinter extends ArcanistExternalLinter {
 
     return $messages;
   }
+
+  protected function getLintCodeFromLinterConfigurationKey($code) {
+
+    // NOTE: We can't figure out which rule generated each message, so we
+    // can not customize severities. I opened a pull request to add this
+    // ability; see:
+    //
+    // https://github.com/stubbornella/csslint/pull/409
+
+    throw new Exception(
+      pht(
+        "CSSLint does not currently support custom severity levels, because ".
+        "rules can't be identified from messages in output. ".
+        "See Pull Request #409."));
+  }
+
 }
index 4cb64f1ca74305d0f84bfe7434baca7c51ca30c8..88cda3edc459a12c360e8e782e612983a9d18c26 100644 (file)
@@ -393,8 +393,12 @@ abstract class ArcanistExternalLinter extends ArcanistFutureLinter {
     $messages = $this->parseLinterOutput($path, $err, $stdout, $stderr);
 
     if ($messages === false) {
-      $future->resolvex();
-      return;
+      if ($err) {
+        $future->resolvex();
+      } else {
+        throw new Exception(
+          "Linter failed to parse output!\n\n{$stdout}\n\n{$stderr}");
+      }
     }
 
     foreach ($messages as $message) {
@@ -407,6 +411,7 @@ abstract class ArcanistExternalLinter extends ArcanistFutureLinter {
     $options = array(
       'bin' => 'optional string | list<string>',
       'flags' => 'optional string',
+      'severity' => 'optional map<string, string>',
     );
 
     if ($this->shouldUseInterpreter()) {
@@ -464,10 +469,50 @@ abstract class ArcanistExternalLinter extends ArcanistFutureLinter {
         if (strlen($value)) {
           $this->setFlags($value);
         }
-        break;
+        return;
+      case 'severity':
+        $sev_map = array(
+          'error' => ArcanistLintSeverity::SEVERITY_ERROR,
+          'warning' => ArcanistLintSeverity::SEVERITY_WARNING,
+          'autofix' => ArcanistLintSeverity::SEVERITY_AUTOFIX,
+          'advice' => ArcanistLintSeverity::SEVERITY_ADVICE,
+          'disabled' => ArcanistLintSeverity::SEVERITY_DISABLED,
+        );
+
+        $custom = array();
+        foreach ($value as $code => $severity) {
+          if (empty($sev_map[$severity])) {
+            $valid = implode(', ', array_keys($sev_map));
+            throw new Exception(
+              pht(
+                'Unknown lint severity "%s". Valid severities are: %s.',
+                $severity,
+                $valid));
+          }
+          $code = $this->getLintCodeFromLinterConfigurationKey($code);
+          $custom[$code] = $severity;
+        }
+
+        $this->setCustomSeverityMap($custom);
+        return;
     }
 
     return parent::setLinterConfigurationValue($key, $value);
   }
 
+
+  /**
+   * Map a configuration lint code to an `arc` lint code. Primarily, this is
+   * intended for validation, but can also be used to normalize case or
+   * otherwise be more permissive in accepted inputs.
+   *
+   * If the code is not recognized, you should throw an exception.
+   *
+   * @param string  Code specified in configuration.
+   * @return string  Normalized code to use in severity map.
+   */
+  protected function getLintCodeFromLinterConfigurationKey($code) {
+    return $code;
+  }
+
 }
index 61aa644cac3f657aabddf19d0a6a52b7a2ede156..bdcebd754d336074801ac755109f4a6d9e10495c 100644 (file)
@@ -12,6 +12,10 @@ final class ArcanistFlake8Linter extends ArcanistExternalLinter {
     return 'flake8';
   }
 
+  public function getLinterConfigurationName() {
+    return 'flake8';
+  }
+
   public function getDefaultFlags() {
     // TODO: Deprecated.
 
@@ -66,12 +70,6 @@ final class ArcanistFlake8Linter extends ArcanistExternalLinter {
         $matches[$key] = trim($match);
       }
 
-      if (substr($matches[4], 0, 1) == 'E') {
-        $severity = ArcanistLintSeverity::SEVERITY_ERROR;
-      } else {
-        $severity = ArcanistLintSeverity::SEVERITY_WARNING;
-      }
-
       $message = new ArcanistLintMessage();
       $message->setPath($path);
       $message->setLine($matches[2]);
@@ -81,7 +79,7 @@ final class ArcanistFlake8Linter extends ArcanistExternalLinter {
       $message->setCode($matches[4]);
       $message->setName($this->getLinterName().' '.$matches[3]);
       $message->setDescription($matches[5]);
-      $message->setSeverity($severity);
+      $message->setSeverity($this->getLintMessageSeverity($matches[4]));
 
       $messages[] = $message;
     }
@@ -93,4 +91,34 @@ final class ArcanistFlake8Linter extends ArcanistExternalLinter {
     return $messages;
   }
 
+  protected function getDefaultMessageSeverity($code) {
+    if (preg_match('/^C/', $code)) {
+      // "C": Cyclomatic complexity
+      return ArcanistLintSeverity::SEVERITY_ADVICE;
+    } else if (preg_match('/^W/', $code)) {
+      // "W": PEP8 Warning
+      return ArcanistLintSeverity::SEVERITY_WARNING;
+    } else {
+      // "E": PEP8 Error
+      // "F": PyFlakes Error
+      return ArcanistLintSeverity::SEVERITY_ERROR;
+    }
+  }
+
+  protected function getLintCodeFromLinterConfigurationKey($code) {
+    if (!preg_match('/^(E|W|C|F)\d+$/', $code)) {
+      throw new Exception(
+        pht(
+          'Unrecognized lint message code "%s". Expected a valid flake8 '.
+          'lint code like "%s", or "%s", or "%s", or "%s".',
+          $code,
+          "E225",
+          "W291",
+          "F811",
+          "C901"));
+    }
+
+    return $code;
+  }
+
 }
index 316538b27f19284d3272a96036ccaeb3889e5ffc..69636d9b467107f1adf22cb9fe2d263152879954 100644 (file)
@@ -121,6 +121,10 @@ abstract class ArcanistLinter {
       return $map[$code];
     }
 
+    return $this->getDefaultMessageSeverity($code);
+  }
+
+  protected function getDefaultMessageSeverity($code) {
     return ArcanistLintSeverity::SEVERITY_ERROR;
   }
 
index c9d26990315b63b72eefa28d77da174824ecdf86..c314145f91a04caad006cbc4ef60dedcdf419696 100644 (file)
@@ -11,6 +11,10 @@ final class ArcanistPEP8Linter extends ArcanistExternalLinter {
     return 'PEP8';
   }
 
+  public function getLinterConfigurationName() {
+    return 'pep8';
+  }
+
   public function getCacheVersion() {
     list($stdout) = execx('%C --version', $this->getExecutableCommand());
     return $stdout.$this->getCommandFlags();
@@ -76,9 +80,6 @@ final class ArcanistPEP8Linter extends ArcanistExternalLinter {
       foreach ($matches as $key => $match) {
         $matches[$key] = trim($match);
       }
-      if (!$this->isMessageEnabled($matches[4])) {
-        continue;
-      }
       $message = new ArcanistLintMessage();
       $message->setPath($path);
       $message->setLine($matches[2]);
@@ -86,7 +87,7 @@ final class ArcanistPEP8Linter extends ArcanistExternalLinter {
       $message->setCode($matches[4]);
       $message->setName('PEP8 '.$matches[4]);
       $message->setDescription($matches[5]);
-      $message->setSeverity(ArcanistLintSeverity::SEVERITY_WARNING);
+      $message->setSeverity($this->getLintMessageSeverity($matches[4]));
 
       $messages[] = $message;
     }
@@ -98,4 +99,31 @@ final class ArcanistPEP8Linter extends ArcanistExternalLinter {
     return $messages;
   }
 
+  protected function getDefaultMessageSeverity($code) {
+    if (preg_match('/^W/', $code)) {
+      return ArcanistLintSeverity::SEVERITY_WARNING;
+    } else {
+
+      // TODO: Once severities/.arclint are more usable, restore this to
+      // "ERROR".
+      // return ArcanistLintSeverity::SEVERITY_ERROR;
+
+      return ArcanistLintSeverity::SEVERITY_WARNING;
+    }
+  }
+
+  protected function getLintCodeFromLinterConfigurationKey($code) {
+    if (!preg_match('/^(E|W)\d+$/', $code)) {
+      throw new Exception(
+        pht(
+          'Unrecognized lint message code "%s". Expected a valid PEP8 '.
+          'lint code like "%s" or "%s".',
+          $code,
+          "E101",
+          "W291"));
+    }
+
+    return $code;
+  }
+
 }
index c1fe50c9fa2da70ecaf61624633d2a69ce34ed69..146940434936fa00afb034efd9f7156a48251d2f 100644 (file)
@@ -4,4 +4,4 @@ def hello():
     return foo
 ~~~~~~~~~~
 error:3:1
-warning:4:1
+error:4:1