Skip to content
This repository was archived by the owner on Dec 1, 2024. It is now read-only.

Commit df9a825

Browse files
committed
Introduce SuppressibleTrait, including common functions to suppress an error code
1 parent edc68c9 commit df9a825

File tree

6 files changed

+76
-36
lines changed

6 files changed

+76
-36
lines changed

src/Linters/HHClientLintError.hack

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ namespace Facebook\HHAST;
1111

1212
final class HHClientLintError implements LintError {
1313

14+
protected function getErrorCode(): string {
15+
return $this->getLintRule()->getErrorCode();
16+
}
17+
1418
const type TJSONError = shape(
1519
'descr' => string,
1620
'severity' => HHClientLintSeverity,

src/Linters/HHClientLintRule.hack

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ namespace Facebook\HHAST;
1313
* The lint rule of an error code reported by the hh_client
1414
*/
1515
final class HHClientLintRule implements LintRule {
16+
use SuppressibleTrait;
17+
1618
public function getName(): string {
1719
return 'Linter: '.$this->code;
1820
}

src/Linters/HHClientLinter.hack

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ use namespace HH\Lib\{C, Str, Vec};
1717
*/
1818
final class HHClientLinter implements Linter {
1919
use LinterTrait;
20+
use SuppressibleTrait;
2021

2122
const type TConfig = shape();
2223

src/Linters/LinterTrait.hack

Lines changed: 5 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -60,41 +60,14 @@ trait LinterTrait {
6060
|> Str\strip_suffix($$, 'Linter');
6161
}
6262

63-
/**
64-
* A user can choose to ignore all errors reported by this linter for a
65-
* whole file using this string as a marker
66-
*/
67-
public function getIgnoreAllMarker(): string {
68-
return LintMarkerName::HHAST_IGNORE_ALL.'['.$this->getLinterName().']';
63+
public final function getErrorCode(): string {
64+
return $this->getLinterName();
6965
}
7066

71-
/**
72-
* A user can choose to ignore a specific error reported by this linter
73-
* using this string as a marker
74-
*/
75-
public function getIgnoreSingleErrorMarker(): string {
76-
return LintMarkerName::HHAST_IGNORE_ERROR.'['.$this->getLinterName().']';
77-
}
78-
79-
/**
80-
* A user can choose to ignore a specific error reported by this linter
81-
* using this string as a marker.
82-
*
83-
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
84-
* fixed.
85-
*/
86-
public function getFixmeMarker(): string {
87-
return LintMarkerName::HHAST_FIXME.'['.$this->getLinterName().']';
88-
}
67+
abstract protected function isSuppressedForFile(File $file): bool;
8968

90-
/**
91-
* Is this linter error disabled for the entire file?
92-
* Memoized since this should not change per run.
93-
*/
9469
public function isLinterSuppressedForFile(): bool {
95-
return C\contains_key(
96-
$this->getFile()->lintMarkersForLineBasedSuppression(),
97-
$this->getIgnoreAllMarker(),
98-
);
70+
return $this->isSuppressedForFile($this->getFile());
9971
}
72+
10073
}

src/Linters/SingleRuleLinter.hack

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,15 +14,12 @@ namespace Facebook\HHAST;
1414
*/
1515
abstract class SingleRuleLinter implements LintRule, Linter {
1616
use LinterTrait;
17+
use SuppressibleTrait;
1718

1819
final public function getName(): string {
1920
return $this->getLinterName();
2021
}
2122

22-
final public function getErrorCode(): string {
23-
return $this->getLinterName();
24-
}
25-
2623
abstract public function getLintErrorsAsync(): Awaitable<vec<SingleRuleLintError>>;
2724

2825
}

src/Linters/SuppressibleTrait.hack

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* Copyright (c) 2017-present, Facebook, Inc.
3+
* All rights reserved.
4+
*
5+
* This source code is licensed under the MIT license found in the
6+
* LICENSE file in the root directory of this source tree.
7+
*
8+
*/
9+
10+
namespace Facebook\HHAST;
11+
12+
use type Facebook\HHAST\File;
13+
use namespace HH\Lib\C;
14+
15+
trait SuppressibleTrait {
16+
17+
abstract protected function getErrorCode(): string;
18+
19+
/**
20+
* A user can choose to ignore all errors reported by this linter for a
21+
* whole file using this string as a marker
22+
*/
23+
public final function getIgnoreAllMarker(): string {
24+
return LintMarkerName::HHAST_IGNORE_ALL.
25+
'['.
26+
$this->getErrorCode().
27+
']';
28+
}
29+
30+
/**
31+
* A user can choose to ignore a specific error reported by this linter
32+
* using this string as a marker
33+
*/
34+
public final function getIgnoreSingleErrorMarker(): string {
35+
return LintMarkerName::HHAST_IGNORE_ERROR.
36+
'['.
37+
$this->getErrorCode().
38+
']';
39+
}
40+
41+
/**
42+
* A user can choose to ignore a specific error reported by this linter
43+
* using this string as a marker.
44+
*
45+
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
46+
* fixed.
47+
*/
48+
public final function getFixmeMarker(): string {
49+
return
50+
LintMarkerName::HHAST_FIXME.'['.$this->getErrorCode().']';
51+
}
52+
53+
/**
54+
* Is this error disabled for the entire file?
55+
*/
56+
public final function isSuppressedForFile(File $file): bool {
57+
return C\contains_key(
58+
$file->lintMarkersForLineBasedSuppression(),
59+
$this->getIgnoreAllMarker(),
60+
);
61+
}
62+
63+
}

0 commit comments

Comments
 (0)