2 // Copyright 2004-present Facebook. All Rights Reserved.
3 // This source code is licensed under the BSD-style license found in the
4 // LICENSE file in the root directory of this source tree. An additional grant
5 // of patent rights can be found in the PATENTS file in the same directory.
7 abstract class BaseDirectoryScopedFormatLinter
extends ArcanistLinter
{
9 const LINT_FORMATTING
= 1;
11 private $changedLines = array();
12 private $rawLintOutput = array();
14 abstract protected function getPathsToLint();
16 protected function shouldLintPath($path) {
17 foreach ($this->getPathsToLint() as $p) {
18 // check if $path starts with $p
19 if (strncmp($path, $p, strlen($p)) === 0) {
26 // API to tell this linter which lines were changed
27 final public function setPathChangedLines($path, $changed) {
28 $this->changedLines
[$path] = $changed;
31 final public function willLintPaths(array $paths) {
33 foreach ($paths as $path) {
34 if (!$this->shouldLintPath($path)) {
38 $changed = $this->changedLines
[$path];
39 if (!isset($changed)) {
40 // do not run linter if there are no changes
44 $futures[$path] = $this->getFormatFuture($path, $changed);
47 foreach (id(new FutureIterator($futures))->limit(8) as $p => $f) {
48 $this->rawLintOutput
[$p] = $f->resolvex();
52 abstract protected function getFormatFuture($path, array $changed);
53 abstract protected function getLintMessage($diff);
55 final public function lintPath($path) {
56 if (!isset($this->rawLintOutput
[$path])) {
60 list($new_content) = $this->rawLintOutput
[$path];
61 $old_content = $this->getData($path);
63 if ($new_content != $old_content) {
64 $diff = ArcanistDiffUtils
::renderDifferences($old_content, $new_content);
65 $this->raiseLintAtOffset(
67 self
::LINT_FORMATTING
,
68 $this->getLintMessage($diff),