Skip to content

Commit

Permalink
Add LinterTrait, which provide the common functions for SingleRuleLin…
Browse files Browse the repository at this point in the history
…ter and HHClientLinter
  • Loading branch information
Atry authored Nov 10, 2021
1 parent abf4458 commit b19848d
Show file tree
Hide file tree
Showing 2 changed files with 101 additions and 84 deletions.
100 changes: 100 additions & 0 deletions src/Linters/LinterTrait.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
/*
* 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;

use type Facebook\HHAST\File;
use namespace HH\Lib\{C, Str};

<<__ConsistentConstruct>>
trait LinterTrait {
require implements Linter;

public static function shouldLintFile(File $_): bool {
return true;
}

private File $file;
private ?this::TConfig $config;

public function __construct(File $file, ?this::TConfig $config) {
$this->file = $file;
$this->config = $config;
}

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

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

final public static function fromPath(string $path): this {
return static::fromPathWithConfig($path, null);
}

final public static function fromPathWithConfig(
string $path,
?this::TConfig $config,
): this {
return new static(File::fromPath($path), $config);
}

final public function getFile(): File {
return $this->file;
}

// A simple name for the linter, based on the class name
<<__Memoize>>
public function getLinterName(): string {
return static::class
|> Str\split($$, '\\')
|> C\lastx($$)
|> Str\strip_suffix($$, 'Linter');
}

/**
* A user can choose to ignore all errors reported by this linter for a
* whole file using this string as a marker
*/
public function getIgnoreAllMarker(): string {
return LintMarkerName::HHAST_IGNORE_ALL.'['.$this->getLinterName().']';
}

/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker
*/
public function getIgnoreSingleErrorMarker(): string {
return LintMarkerName::HHAST_IGNORE_ERROR.'['.$this->getLinterName().']';
}

/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker.
*
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
* fixed.
*/
public function getFixmeMarker(): string {
return LintMarkerName::HHAST_FIXME.'['.$this->getLinterName().']';
}

/**
* Is this linter error disabled for the entire file?
* Memoized since this should not change per run.
*/
public function isLinterSuppressedForFile(): bool {
return C\contains_key(
$this->getFile()->lintMarkers(),
$this->getIgnoreAllMarker(),
);
}
}
85 changes: 1 addition & 84 deletions src/Linters/SingleRuleLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,11 @@

namespace Facebook\HHAST;

use type Facebook\HHAST\File;
use namespace HH\Lib\{C, Str};

/**
* A linter that applies a single lint rule.
*/
<<__ConsistentConstruct>>
abstract class SingleRuleLinter implements LintRule, Linter {
use LinterTrait;

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

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

public static function shouldLintFile(File $_): bool {
return true;
}

public function __construct(
private File $file,
private ?this::TConfig $config,
) {
}

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

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

final public static function fromPath(string $path): this {
return static::fromPathWithConfig($path, null);
}

final public static function fromPathWithConfig(
string $path,
?this::TConfig $config,
): this {
return new static(File::fromPath($path), $config);
}

final public function getFile(): File {
return $this->file;
}

// A simple name for the linter, based on the class name
<<__Memoize>>
public function getLinterName(): string {
return static::class
|> Str\split($$, '\\')
|> C\lastx($$)
|> Str\strip_suffix($$, 'Linter');
}

/**
* A user can choose to ignore all errors reported by this linter for a
* whole file using this string as a marker
*/
public function getIgnoreAllMarker(): string {
return LintMarkerName::HHAST_IGNORE_ALL.'['.$this->getLinterName().']';
}

/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker
*/
public function getIgnoreSingleErrorMarker(): string {
return LintMarkerName::HHAST_IGNORE_ERROR.'['.$this->getLinterName().']';
}

/**
* A user can choose to ignore a specific error reported by this linter
* using this string as a marker.
*
* The difference to HHAST_IGNORE_ERROR is that we expect this one to be
* fixed.
*/
public function getFixmeMarker(): string {
return LintMarkerName::HHAST_FIXME.'['.$this->getLinterName().']';
}

/**
* Is this linter error disabled for the entire file?
* Memoized since this should not change per run.
*/
public function isLinterSuppressedForFile(): bool {
return C\contains_key(
$this->getFile()->lintMarkers(),
$this->getIgnoreAllMarker(),
);
}

}

0 comments on commit b19848d

Please sign in to comment.