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

Add LinterTrait, which provides the common functions for both SingleRuleLinter and HHClientLinter #387

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions src/Linters/LinterTrait.hack → src/Linters/BaseLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,8 @@ namespace Facebook\HHAST;
use type Facebook\HHAST\File;
use namespace HH\Lib\{C, Str};

<<__ConsistentConstruct>>
trait LinterTrait {
require implements Linter;
<<__ConsistentConstruct, __Sealed(SingleRuleLinter::class, HHClientLinter::class)>>
abstract class BaseLinter implements Linter {

public static function shouldLintFile(File $_): bool {
return true;
Expand Down
12 changes: 6 additions & 6 deletions src/Linters/HHClientLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@ use namespace HH\Lib\{C, Str, Vec};
/**
* A linter as a proxy invoking `hh_client --lint`.
*/
final class HHClientLinter implements Linter {
use LinterTrait;
final class HHClientLinter extends BaseLinter {
use SuppressibleTrait;

const type TConfig = shape();
Expand All @@ -40,6 +39,7 @@ final class HHClientLinter implements Linter {
);
}

<<__Override>>
public async function getLintErrorsAsync(
): Awaitable<vec<HHClientLintError>> {
$lines = await __Private\execute_async(
Expand All @@ -63,13 +63,13 @@ final class HHClientLinter implements Linter {
|> Vec\map(
$$,
$error ==> new HHClientLintError(
$this->file,
$this->getFile(),
$error,
$this::blameCode($file_lines, $error),
),
)
|> Vec\filter($$, $error ==> {
if ($error->getLintRule()->isSuppressedForFile($this->file)) {
if ($error->getLintRule()->isSuppressedForFile($this->getFile())) {
return false;
}
$range = $error->getRange();
Expand All @@ -78,12 +78,12 @@ final class HHClientLinter implements Linter {
}
list(list($line_number, $_), $_) = $range;
$previous_line_number = $line_number - 1;
if ($this->isSuppressedForLine($this->file, $previous_line_number)) {
if ($this->isSuppressedForLine($this->getFile(), $previous_line_number)) {
return false;
}
if (
$error->getLintRule()
->isSuppressedForLine($this->file, $previous_line_number)
->isSuppressedForLine($this->getFile(), $previous_line_number)
) {
return false;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Linters/Linter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace Facebook\HHAST;
* Problems found by a Linter could associated with different LintRules,
* especially when the Linter is a proxy calling other linting tools.
*/
<<__Sealed(SingleRuleLinter::class, HHClientLinter::class)>>
<<__Sealed(BaseLinter::class)>>
interface Linter {
<<__Reifiable>>
abstract const type TConfig;
Expand Down
4 changes: 2 additions & 2 deletions src/Linters/SingleRuleLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -12,14 +12,14 @@ namespace Facebook\HHAST;
/**
* A linter that applies a single lint rule.
*/
abstract class SingleRuleLinter implements LintRule, Linter {
use LinterTrait;
abstract class SingleRuleLinter extends BaseLinter implements LintRule {
use SuppressibleTrait;

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

<<__Override>>
abstract public function getLintErrorsAsync(): Awaitable<vec<SingleRuleLintError>>;

}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " \/**\n * Set up stuff\n *\/\n public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " \/**\n * Set up stuff\n *\/\n public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " \/**\n * Set up stuff\n *\/\n <<HerpDerp>>\n public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " \/**\n * Set up stuff\n *\/\n <<HerpDerp>>\n public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": "\n public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": "\n public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " <<Foo>>\n public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " <<Foo>>\n public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@
{
"blame": " <<Foo('bar')>>\n public static function shouldLintFile(File $_): bool {\n return false;\n }\n",
"blame_pretty": " <<Foo('bar')>>\n public static function shouldLintFile(File $_): bool {",
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\SingleRuleLinter::shouldLintFile() without <<__Override>>"
"description": "Foo::shouldLintFile() overrides Facebook\\HHAST\\BaseLinter::shouldLintFile() without <<__Override>>"
}
]