forked from hhvm/hhast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
BaseLinter.hack
115 lines (98 loc) · 2.88 KB
/
BaseLinter.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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
/*
* 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 namespace Facebook\TypeAssert;
use type Facebook\HHAST\File;
use namespace HH\Lib\{C, Str};
<<__ConsistentConstruct>>
abstract class BaseLinter {
<<__Reifiable>>
const type TConfig as shape(...) = shape(...);
abstract public function getLintErrorsAsync(): Awaitable<vec<LintError>>;
public static function shouldLintFile(File $_): bool {
return true;
}
public function __construct(
private File $file,
private ?this::TConfig $config,
) {
}
protected function getConfig(): ?this::TConfig {
return $this->config;
}
final static public function typeAssertConfig(mixed $config): this::TConfig {
try {
return TypeAssert\matches<this::TConfig>($config);
} catch (TypeAssert\UnsupportedTypeException $e) {
throw new \InvalidOperationException(
Str\format(
'%s specified an unsupported config type. See previous exception:',
static::class,
),
$e->getCode(),
$e,
);
}
}
final public static function fromPath(string $path): this {
return static::fromPathWithConfig($path, null);
}
final public static function fromPathWithConfig(
string $path,
?this::TConfig $config,
): this {
return new static(File::fromPath($path), $config);
}
final public function getFile(): File {
return $this->file;
}
// A simple name for the linter, based on the class name
<<__Memoize>>
public function getLinterName(): string {
return static::class
|> Str\split($$, '\\')
|> C\lastx($$)
|> Str\strip_suffix($$, 'Linter');
}
/**
* A user can choose to ignore all errors reported by this linter for a
* whole file using this string as a marker
*/
public function getIgnoreAllMarker(): string {
return 'HHAST_IGNORE_ALL['.$this->getLinterName().']';
}
/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker
*/
public function getIgnoreSingleErrorMarker(): string {
return 'HHAST_IGNORE_ERROR['.$this->getLinterName().']';
}
/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker.
*
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
* fixed.
*/
public function getFixmeMarker(): string {
return 'HHAST_FIXME['.$this->getLinterName().']';
}
/**
* Is this linter error disabled for the entire file?
* Memoized since this should not change per run.
*/
<<__Memoize>>
public function isLinterSuppressedForFile(): bool {
return Str\contains(
$this->getFile()->getContents(),
$this->getIgnoreAllMarker(),
);
}
}