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

Commit

Permalink
Separate the roles of SingleRuleLinter into Linter and LintRule
Browse files Browse the repository at this point in the history
  • Loading branch information
Atry authored Nov 9, 2021
1 parent 0b12b53 commit 717b695
Show file tree
Hide file tree
Showing 5 changed files with 121 additions and 2 deletions.
36 changes: 36 additions & 0 deletions src/Linters/LintError.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
/*
* 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;

/**
* A problem detected by a lint rule
*
* LintError is more generic than SingleRuleLintError because LintError is
* associated with any LintRule, not necessarily a SingleRuleLinter.
*/
<<__Sealed(
SingleRuleLintError::class,
// HHClientProblem::class
)>>
interface LintError {
public function getFile(): File;

public function getPosition(): ?(int, int);

public function getRange(): ?((int, int), ?(int, int));

public function getDescription(): string;

public function getBlameCode(): ?string;

public function getPrettyBlame(): ?string;

public function getLintRule(): LintRule;
}
26 changes: 26 additions & 0 deletions src/Linters/LintRule.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
/*
* 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;

/**
* An abstract lint rule that provides a single error reason, which could be
* either a HHAST linter or a lint rule written in OCaml.
*/
<<__Sealed(
SingleRuleLinter::class,
// HHClientLintRule::class,
)>>
interface LintRule {
/**
* The name of the lint rule, which can be used to determine markers to
* suppress lint errors.
*/
public function getName(): string;
}
38 changes: 38 additions & 0 deletions src/Linters/Linter.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/*
* 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;

/**
* The process to find problems against a single source file.
*
* 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
)
>>
interface Linter {
abstract const type TConfig;

public function getLintErrorsAsync(): Awaitable<vec<LintError>>;

public static function shouldLintFile(File $_): bool;

public static function newInstance(File $file, ?this::TConfig $config): this;

public function isLinterSuppressedForFile(): bool;

public function getFile(): File;

public static function typeAssertConfig(mixed $config): this::TConfig;
}
10 changes: 9 additions & 1 deletion src/Linters/SingleRuleLintError.hack
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,10 @@ namespace Facebook\HHAST;

use type Facebook\HHAST\File;

class SingleRuleLintError {
/**
* A problem detected by a SingleRuleLinter
*/
class SingleRuleLintError implements LintError {
public function __construct(
private SingleRuleLinter $linter,
private string $description,
Expand Down Expand Up @@ -57,4 +60,9 @@ class SingleRuleLintError {
final public function getLinter(): SingleRuleLinter {
return $this->linter;
}

final public function getLintRule(): LintRule {
return $this->getLinter();
}

}
13 changes: 12 additions & 1 deletion src/Linters/SingleRuleLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,18 @@ use namespace Facebook\TypeAssert;
use type Facebook\HHAST\File;
use namespace HH\Lib\{C, Str};

/**
* A linter that applies a single lint rule.
*/
<<__ConsistentConstruct>>
abstract class SingleRuleLinter {
abstract class SingleRuleLinter implements LintRule, Linter {
<<__Reifiable>>
abstract const type TConfig;

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

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

public static function shouldLintFile(File $_): bool {
Expand All @@ -30,6 +37,10 @@ abstract class SingleRuleLinter {
) {
}

public static function newInstance(File $file, ?this::TConfig $config): this {
return new static($file, $config);
}

protected function getConfig(): ?this::TConfig {
return $this->config;
}
Expand Down

0 comments on commit 717b695

Please sign in to comment.