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

Commit

Permalink
Suppress lint error according to code level markers
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry authored Nov 5, 2021
1 parent 031eece commit ba00fa8
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 5 deletions.
41 changes: 36 additions & 5 deletions src/Linters/HHClientLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ namespace Facebook\HHAST;

use type Facebook\HHAST\{BaseLinter};
use namespace Facebook\TypeAssert;
use namespace HH\Lib\{C, Vec};
use namespace HH\Lib\{C, Str, Vec};

/**
* A linter as a proxy invoking `hh_client --lint`.
Expand All @@ -26,6 +26,38 @@ final class HHClientLinter extends BaseLinter {
'version' => string,
);

private function isCodeSuppressed(string $previous_line, int $code): bool {
return
Str\contains(
$previous_line,
self::ignoreSingleErrorMarkerOf((string)$code),
) ||
Str\contains($previous_line, self::fixmeMarkerOf((string)$code));
}

/**
* Is the linter code disabled for the entire file?
* Memoized since this should not change per run.
*/
<<__Memoize>>
public function isCodeSuppressedForFile(int $code): bool {
return Str\contains(
$this->getFile()->getContents(),
self::ignoreAllMarkerOf((string)$code),
);
}

private function isSuppressed(HHClientLintError::TJSONError $error): bool {
$previous_line = $this->getLine($error['line'] - 2);
if ($previous_line is null) {
return false;
} else {
return $this->isLinterSuppressed($previous_line) ||
$this->isCodeSuppressed($previous_line, $error['code']) ||
$this->isCodeSuppressedForFile($error['code']);
}
}

<<__Override>>
public async function getLintErrorsAsync(
): Awaitable<vec<HHClientLintError>> {
Expand All @@ -45,9 +77,8 @@ final class HHClientLinter extends BaseLinter {
\JSON_FB_HACK_ARRAYS,
),
);
return Vec\map(
$hh_client_lint_result['errors'],
$error ==> new HHClientLintError($this, $error),
);
return $hh_client_lint_result['errors']
|> Vec\filter($$, $error ==> !$this->isSuppressed($error))
|> Vec\map($$, $error ==> new HHClientLintError($this, $error));
}
}
10 changes: 10 additions & 0 deletions tests/HHClientLinterTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,16 @@ final class HHClientLinterTest extends TestCase {
public function getCleanExamples(): vec<(string)> {
return vec[
tuple("<?hh\nclass Foo {}"),
tuple(
'<?hh
function invalid_null_check(): void {
$cannot_be_null = 42;
// HHAST_FIXME[5611]
if ($cannot_be_null is null) {
throw new Exception();
}
}',
),
];
}

Expand Down

0 comments on commit ba00fa8

Please sign in to comment.