1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2025-01-10 14:51:05 +01:00
phorge-arcanist/src/lint/linter/ArcanistPyFlakesLinter.php
Joshua Spence 8244b3582a Improve behavior for detecting the failure to parse external linter output
Summary:
Currently, a lot of `ArcanistExternalLinter` subclasses have something like this:

```lang=php
if ($err && !$messages) {
  return false;
}
```

We can avoid this code duplication by moving this check to the `ArcanistExternalLinter` class.

Test Plan: `arc unit`

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin, epriestley

Differential Revision: https://secure.phabricator.com/D11321
2015-05-22 07:30:17 +10:00

87 lines
2.1 KiB
PHP

<?php
/**
* Uses "PyFlakes" to detect various errors in Python code.
*/
final class ArcanistPyFlakesLinter extends ArcanistExternalLinter {
public function getInfoURI() {
return 'https://pypi.python.org/pypi/pyflakes';
}
public function getInfoName() {
return pht('PyFlakes');
}
public function getInfoDescription() {
return pht(
'PyFlakes is a simple program which checks Python source files for '.
'errors.');
}
public function getLinterName() {
return 'PYFLAKES';
}
public function getLinterConfigurationName() {
return 'pyflakes';
}
public function getDefaultBinary() {
return 'pyflakes';
}
public function getVersion() {
list($stdout) = execx('%C --version', $this->getExecutableCommand());
$matches = array();
if (preg_match('/^(?P<version>\d+\.\d+\.\d+)$/', $stdout, $matches)) {
return $matches['version'];
} else {
return false;
}
}
public function getInstallInstructions() {
return pht('Install pyflakes with `%s`.', 'pip install pyflakes');
}
protected function parseLinterOutput($path, $err, $stdout, $stderr) {
$lines = phutil_split_lines($stdout, false);
$messages = array();
foreach ($lines as $line) {
$matches = null;
if (!preg_match('/^(.*?):(\d+): (.*)$/', $line, $matches)) {
continue;
}
foreach ($matches as $key => $match) {
$matches[$key] = trim($match);
}
$severity = ArcanistLintSeverity::SEVERITY_WARNING;
$description = $matches[3];
$error_regexp = '/(^undefined|^duplicate|before assignment$)/';
if (preg_match($error_regexp, $description)) {
$severity = ArcanistLintSeverity::SEVERITY_ERROR;
}
$message = new ArcanistLintMessage();
$message->setPath($path);
$message->setLine($matches[2]);
$message->setCode($this->getLinterName());
$message->setDescription($description);
$message->setSeverity($severity);
$messages[] = $message;
}
return $messages;
}
protected function canCustomizeLintSeverities() {
return false;
}
}