]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Allow specifying multiple locations of the same lint error
authorvrana <jakubv@fb.com>
Thu, 10 Jan 2013 23:03:33 +0000 (15:03 -0800)
committervrana <jakubv@fb.com>
Thu, 10 Jan 2013 23:35:11 +0000 (15:35 -0800)
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

src/lint/ArcanistLintMessage.php
src/lint/engine/ArcanistLintEngine.php
src/lint/linter/ArcanistConduitLinter.php
src/lint/linter/ArcanistLinter.php

index 25f193d74a2842128f9969291e19ee7d50d9b0df..a0c71c09164f0112134ba298efd92845453da113 100644 (file)
@@ -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;
   }
index c46a892224b00512d9d2ee6b4a6a7a86253f8a49..9025b839eb45dd2eb901d16704ac59f0353d4093 100644 (file)
@@ -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;
index c9e50cf91f2b1827126fa2866b4ca735e88a93a6..741d04fb6226f5305cccfad7ff192291b29a1847 100644 (file)
@@ -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.
index 909aa71be6bd0380aa2438890db97c8d325f5329..eb2b9d9dae3970c7a09a79d3c05e8951af55db5f 100644 (file)
@@ -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(