forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LineLinter.hack
62 lines (49 loc) · 1.69 KB
/
LineLinter.hack
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
/*
* Copyright (c) 2017-present, Facebook, Inc.
* All rights reserved.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*
*/
namespace Facebook\HHAST;
use type Facebook\HHAST\{BaseLinter};
use namespace HH\Lib\{C, Str, Vec};
abstract class LineLinter<+Terror as LineLintError> extends BaseLinter {
public function getLinesFromFile(): vec<string> {
$code = $this->getFile()->getContents();
$lines = Str\split($code, "\n");
return vec($lines);
}
public function getLine(int $l): ?string {
return idx($this->getLinesFromFile(), $l);
}
<<__Override>>
public async function getLintErrorsAsync(): Awaitable<vec<Terror>> {
$lines = $this->getLinesFromFile();
$errs = vec[];
foreach ($lines as $ln => $line) {
$line_errors = vec($this->getLintErrorsForLine($line, $ln));
if (C\is_empty($line_errors)) {
continue;
}
// We got an error. Let's check the previous line to see if it is marked as ignorable
if ($ln - 1 >= 0 && $this->isLinterSuppressed($lines[$ln - 1])) {
continue;
}
$errs = Vec\concat($errs, $line_errors);
}
return $errs;
}
/** Check if this linter has been disabled by a comment on the previous line.
*/
protected function isLinterSuppressed(string $previous_line): bool {
return Str\contains($previous_line, $this->getFixmeMarker()) ||
Str\contains($previous_line, $this->getIgnoreSingleErrorMarker());
}
/** Parse a single line of code and attempts to find lint errors */
abstract public function getLintErrorsForLine(
string $line,
int $line_number,
): Traversable<Terror>;
}