From: vrana Date: Thu, 10 Jan 2013 23:03:33 +0000 (-0800) Subject: Allow specifying multiple locations of the same lint error X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=a82493a84d327f0651077180d6e448ba7ede9023;p=arcanist.git Allow specifying multiple locations of the same lint error Summary: Some errors (duplicate declaration, invalid number of arguments) have more related places. We need to notify user if he changes any related place. This could be currently achieved by triggering errors instead of warnings or by including both files in the range (impossible if the locations are in different files) or by issuing multiple errors. All options are too aggressive. Test Plan: Issued error on unmodified line with other location on modified line. Reviewers: epriestley CC: aran, Korvin Differential Revision: https://secure.phabricator.com/D4392 --- diff --git a/src/lint/ArcanistLintMessage.php b/src/lint/ArcanistLintMessage.php index 25f193d7..a0c71c09 100644 --- a/src/lint/ArcanistLintMessage.php +++ b/src/lint/ArcanistLintMessage.php @@ -18,6 +18,7 @@ final class ArcanistLintMessage { protected $replacementText; protected $appliedToDisk; protected $dependentMessages = array(); + protected $otherLocations = array(); protected $obsolete; protected $uncacheable; @@ -37,6 +38,7 @@ final class ArcanistLintMessage { if (isset($dict['replacement'])) { $message->setReplacementText($dict['replacement']); } + $message->setOtherLocations(idx($dict, 'locations', array())); return $message; } @@ -51,6 +53,7 @@ final class ArcanistLintMessage { 'description' => $this->getDescription(), 'original' => $this->getOriginalText(), 'replacement' => $this->getReplacementText(), + 'locations' => $this->getOtherLocations(), ); } @@ -135,6 +138,18 @@ final class ArcanistLintMessage { return $this->replacementText; } + /** + * @param dict Keys 'path', 'line', 'char', 'original'. + */ + public function setOtherLocations(array $locations) { + $this->otherLocations = $locations; + return $this; + } + + public function getOtherLocations() { + return $this->otherLocations; + } + public function isError() { return $this->getSeverity() == ArcanistLintSeverity::SEVERITY_ERROR; } diff --git a/src/lint/engine/ArcanistLintEngine.php b/src/lint/engine/ArcanistLintEngine.php index c46a8922..9025b839 100644 --- a/src/lint/engine/ArcanistLintEngine.php +++ b/src/lint/engine/ArcanistLintEngine.php @@ -305,28 +305,43 @@ abstract class ArcanistLintEngine { abstract protected function buildLinters(); - private function isRelevantMessage($message) { + private function isRelevantMessage(ArcanistLintMessage $message) { // When a user runs "arc lint", we default to raising only warnings on // lines they have changed (errors are still raised anywhere in the // file). The list of $changed lines may be null, to indicate that the // path is a directory or a binary file so we should not exclude // warnings. - $changed = $this->getPathChangedLines($message->getPath()); - - if ($changed === null || $message->isError() || !$message->getLine()) { + if (!$this->changedLines || $message->isError()) { return true; } - $last_line = $message->getLine(); - if ($message->getOriginalText()) { - $last_line += substr_count($message->getOriginalText(), "\n"); - } + $locations = $message->getOtherLocations(); + $locations[] = $message->toDictionary(); + + foreach ($locations as $location) { + $path = idx($location, 'path', $message->getPath()); + + if (!array_key_exists($path, $this->changedLines)) { + continue; + } - for ($l = $message->getLine(); $l <= $last_line; $l++) { - if (!empty($changed[$l])) { + $changed = $this->getPathChangedLines($path); + + if ($changed === null || !$location['line']) { return true; } + + $last_line = $location['line']; + if (isset($location['original'])) { + $last_line += substr_count($location['original'], "\n"); + } + + for ($l = $location['line']; $l <= $last_line; $l++) { + if (!empty($changed[$l])) { + return true; + } + } } return false; diff --git a/src/lint/linter/ArcanistConduitLinter.php b/src/lint/linter/ArcanistConduitLinter.php index c9e50cf9..741d04fb 100644 --- a/src/lint/linter/ArcanistConduitLinter.php +++ b/src/lint/linter/ArcanistConduitLinter.php @@ -29,6 +29,7 @@ * 'name' * 'description' * 'original' & 'replacement' => optional patch information + * 'locations' => other locations of the same error (in the same format) * * This class is intended for customization via instantiation, not via * subclassing. diff --git a/src/lint/linter/ArcanistLinter.php b/src/lint/linter/ArcanistLinter.php index 909aa71b..eb2b9d9d 100644 --- a/src/lint/linter/ArcanistLinter.php +++ b/src/lint/linter/ArcanistLinter.php @@ -135,6 +135,17 @@ abstract class ArcanistLinter { return $this->messages; } + protected function newLintAtLine($line, $char, $code, $desc) { + return id(new ArcanistLintMessage()) + ->setPath($this->getActivePath()) + ->setLine($line) + ->setChar($char) + ->setCode($this->getLintMessageFullCode($code)) + ->setSeverity($this->getLintMessageSeverity($code)) + ->setName($this->getLintMessageName($code)) + ->setDescription($desc); + } + protected function raiseLintAtLine( $line, $char, @@ -143,24 +154,11 @@ abstract class ArcanistLinter { $original = null, $replacement = null) { - $dict = array( - 'path' => $this->getActivePath(), - 'line' => $line, - 'char' => $char, - 'code' => $this->getLintMessageFullCode($code), - 'severity' => $this->getLintMessageSeverity($code), - 'name' => $this->getLintMessageName($code), - 'description' => $desc, - ); - - if ($original !== null) { - $dict['original'] = $original; - } - if ($replacement !== null) { - $dict['replacement'] = $replacement; - } + $message = $this->newLintAtLine($line, $char, $code, $desc) + ->setOriginalText($original) + ->setReplacementText($replacement); - return $this->addLintMessage(ArcanistLintMessage::newFromDictionary($dict)); + return $this->addLintMessage($message); } protected function raiseLintAtPath(