From b19848d405b216618614844c9567189f818922d2 Mon Sep 17 00:00:00 2001 From: "Yang, Bo" Date: Tue, 9 Nov 2021 22:05:53 +0000 Subject: [PATCH] Add LinterTrait, which provide the common functions for SingleRuleLinter and HHClientLinter --- src/Linters/LinterTrait.hack | 100 ++++++++++++++++++++++++++++++ src/Linters/SingleRuleLinter.hack | 85 +------------------------ 2 files changed, 101 insertions(+), 84 deletions(-) create mode 100644 src/Linters/LinterTrait.hack diff --git a/src/Linters/LinterTrait.hack b/src/Linters/LinterTrait.hack new file mode 100644 index 000000000..1a990012f --- /dev/null +++ b/src/Linters/LinterTrait.hack @@ -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(), + ); + } +} diff --git a/src/Linters/SingleRuleLinter.hack b/src/Linters/SingleRuleLinter.hack index 3251a9f0e..1d3b58304 100644 --- a/src/Linters/SingleRuleLinter.hack +++ b/src/Linters/SingleRuleLinter.hack @@ -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(); @@ -28,84 +25,4 @@ abstract class SingleRuleLinter implements LintRule, Linter { abstract public function getLintErrorsAsync(): Awaitable>; - 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(), - ); - } - }