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

Commit b19848d

Browse files
authored
Add LinterTrait, which provide the common functions for SingleRuleLinter and HHClientLinter
1 parent abf4458 commit b19848d

File tree

2 files changed

+101
-84
lines changed

2 files changed

+101
-84
lines changed

src/Linters/LinterTrait.hack

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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, Str};
14+
15+
<<__ConsistentConstruct>>
16+
trait LinterTrait {
17+
require implements Linter;
18+
19+
public static function shouldLintFile(File $_): bool {
20+
return true;
21+
}
22+
23+
private File $file;
24+
private ?this::TConfig $config;
25+
26+
public function __construct(File $file, ?this::TConfig $config) {
27+
$this->file = $file;
28+
$this->config = $config;
29+
}
30+
31+
public static function newInstance(File $file, ?this::TConfig $config): this {
32+
return new static($file, $config);
33+
}
34+
35+
protected function getConfig(): ?this::TConfig {
36+
return $this->config;
37+
}
38+
39+
final public static function fromPath(string $path): this {
40+
return static::fromPathWithConfig($path, null);
41+
}
42+
43+
final public static function fromPathWithConfig(
44+
string $path,
45+
?this::TConfig $config,
46+
): this {
47+
return new static(File::fromPath($path), $config);
48+
}
49+
50+
final public function getFile(): File {
51+
return $this->file;
52+
}
53+
54+
// A simple name for the linter, based on the class name
55+
<<__Memoize>>
56+
public function getLinterName(): string {
57+
return static::class
58+
|> Str\split($$, '\\')
59+
|> C\lastx($$)
60+
|> Str\strip_suffix($$, 'Linter');
61+
}
62+
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().']';
69+
}
70+
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+
}
89+
90+
/**
91+
* Is this linter error disabled for the entire file?
92+
* Memoized since this should not change per run.
93+
*/
94+
public function isLinterSuppressedForFile(): bool {
95+
return C\contains_key(
96+
$this->getFile()->lintMarkers(),
97+
$this->getIgnoreAllMarker(),
98+
);
99+
}
100+
}

src/Linters/SingleRuleLinter.hack

Lines changed: 1 addition & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -9,14 +9,11 @@
99

1010
namespace Facebook\HHAST;
1111

12-
use type Facebook\HHAST\File;
13-
use namespace HH\Lib\{C, Str};
14-
1512
/**
1613
* A linter that applies a single lint rule.
1714
*/
18-
<<__ConsistentConstruct>>
1915
abstract class SingleRuleLinter implements LintRule, Linter {
16+
use LinterTrait;
2017

2118
final public function getName(): string {
2219
return $this->getLinterName();
@@ -28,84 +25,4 @@ abstract class SingleRuleLinter implements LintRule, Linter {
2825

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

31-
public static function shouldLintFile(File $_): bool {
32-
return true;
33-
}
34-
35-
public function __construct(
36-
private File $file,
37-
private ?this::TConfig $config,
38-
) {
39-
}
40-
41-
public static function newInstance(File $file, ?this::TConfig $config): this {
42-
return new static($file, $config);
43-
}
44-
45-
protected function getConfig(): ?this::TConfig {
46-
return $this->config;
47-
}
48-
49-
final public static function fromPath(string $path): this {
50-
return static::fromPathWithConfig($path, null);
51-
}
52-
53-
final public static function fromPathWithConfig(
54-
string $path,
55-
?this::TConfig $config,
56-
): this {
57-
return new static(File::fromPath($path), $config);
58-
}
59-
60-
final public function getFile(): File {
61-
return $this->file;
62-
}
63-
64-
// A simple name for the linter, based on the class name
65-
<<__Memoize>>
66-
public function getLinterName(): string {
67-
return static::class
68-
|> Str\split($$, '\\')
69-
|> C\lastx($$)
70-
|> Str\strip_suffix($$, 'Linter');
71-
}
72-
73-
/**
74-
* A user can choose to ignore all errors reported by this linter for a
75-
* whole file using this string as a marker
76-
*/
77-
public function getIgnoreAllMarker(): string {
78-
return LintMarkerName::HHAST_IGNORE_ALL.'['.$this->getLinterName().']';
79-
}
80-
81-
/**
82-
* A user can choose to ignore a specific error reported by this linter
83-
* using this string as a marker
84-
*/
85-
public function getIgnoreSingleErrorMarker(): string {
86-
return LintMarkerName::HHAST_IGNORE_ERROR.'['.$this->getLinterName().']';
87-
}
88-
89-
/**
90-
* A user can choose to ignore a specific error reported by this linter
91-
* using this string as a marker.
92-
*
93-
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
94-
* fixed.
95-
*/
96-
public function getFixmeMarker(): string {
97-
return LintMarkerName::HHAST_FIXME.'['.$this->getLinterName().']';
98-
}
99-
100-
/**
101-
* Is this linter error disabled for the entire file?
102-
* Memoized since this should not change per run.
103-
*/
104-
public function isLinterSuppressedForFile(): bool {
105-
return C\contains_key(
106-
$this->getFile()->lintMarkers(),
107-
$this->getIgnoreAllMarker(),
108-
);
109-
}
110-
11128
}

0 commit comments

Comments
 (0)