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

Add a test to detect duplicated lint rules #427

Merged
merged 5 commits into from
Dec 10, 2021
Merged
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
1 change: 1 addition & 0 deletions src/Linters/HHClientLinter.hack
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ final class HHClientLinter implements Linter {
* The error code that are always ignored
*/
const keyset<int> ALWAYS_IGNORE_ERRORS = keyset[
0 /* InternalError, indicating a bug in the OCaml linter, not a real lint error */,
5583 /* DontAwaitInALoop, which should have been covered by DontAwaitInALoopLinter */,
];

Expand Down
58 changes: 58 additions & 0 deletions tests/HHClientDuplicatedLintErrorTest.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* 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\HackTest\DataProvider;
use namespace HH\Lib\{C, Str, Vec};

/**
* The test suite ensures that no lint error is found by `HHClientLinter` in
* the examples for `SingleRuleLinter`s.
*
* If an example triggers hh_client lint errors that indicate problems other
* than the `SingleRuleLinter` supposes to detect, add HHAST_IGNORE_ALL markers
* to suppress them. If an example triggers hh_client lint errors that
* indicate problems that are essentially the same as the `SingleRuleLinter`
* supposes to detect, it means the `SingleRuleLinter` is a duplicate of the
* hh_client lint error, and we should either disable the hh_client lint error
* code or remove the `SingleRuleLinter`.
*/
final class HHClientDuplicatedLintErrorTest extends TestCase {
use HHClientLinterTestTrait;
const HHClientLinter::TConfig CONFIG = shape();

public function getDirtyFixturesFromOtherLinters(): vec<(string)> {
return \glob(__DIR__.'/examples/*/*.in')
|> Vec\filter($$, $path ==> !Str\contains($path, 'HHClientLinter'))
|> Vec\map($$, $path ==> tuple($path));
}

<<DataProvider('getDirtyFixturesFromOtherLinters')>>
public async function testDuplicatedLintError(string $path): Awaitable<void> {
$linter = $this->getLinter($path);
$errors = await $linter->getLintErrorsAsync();
if (C\is_empty($errors)) {
return;
}
Vec\map(
$errors,
$error ==> Str\format(
"- %s: %s at line %s:%d:\n%s",
$error->getLintRule()->getName(),
$error->getDescription(),
$path,
$error->getRange()[0][1] ?? -1,
$error->getPrettyBlame() ?? '',
),
)
|> Str\join($$, "\n")
|> self::fail("Expected no errors, got:\n".$$);
}
}
1 change: 1 addition & 0 deletions tests/HHClientLinterIgnoreExceptTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Facebook\HHAST;

final class HHClientLinterIgnoreExceptTest extends TestCase {
use LinterTestTrait;
use HHClientLinterTestTrait;

<<__Override>>
Expand Down
1 change: 1 addition & 0 deletions tests/HHClientLinterIgnoreTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Facebook\HHAST;

final class HHClientLinterIgnoreTest extends TestCase {
use LinterTestTrait;
use HHClientLinterTestTrait;

<<__Override>>
Expand Down
1 change: 1 addition & 0 deletions tests/HHClientLinterTest.hack
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace Facebook\HHAST;

final class HHClientLinterTest extends TestCase {
use LinterTestTrait;
use HHClientLinterTestTrait;

<<__Override>>
Expand Down
2 changes: 0 additions & 2 deletions tests/HHClientLinterTestTrait.hack
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ namespace Facebook\HHAST;
use namespace HH\Lib\{C, Str};

trait HHClientLinterTestTrait {
use LinterTestTrait;

abstract const HHClientLinter::TConfig CONFIG;

Expand All @@ -28,7 +27,6 @@ trait HHClientLinterTestTrait {
__DIR__.'/../.var/tmp/hhast/'.C\lastx(Str\split(static::class, '\\'));
}

<<__Override>>
protected function getLinter(string $file): HHClientLinter {
$ext = Str\strip_suffix($file, '.in')
|> Str\ends_with($$, '.php')
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Herp\Derp;

/*HHAST_IGNORE_ALL[CamelCasedMethodsUnderscoredFunctions]*/
// HHAST_IGNORE_ALL[5623] HHAST_IGNORE_ALL[5624] because the error code is not the interested in this example

use type Facebook\HackTest\{DataProvider, HackTest};
use function Facebook\FBExpect\expect;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
namespace Herp\Derp;

/*HHAST_IGNORE_ALL[CamelCasedMethodsUnderscoredFunctions]*/
// HHAST_IGNORE_ALL[5623] HHAST_IGNORE_ALL[5624] because the error code is not the interested in this example

use type Facebook\HackTest\{DataProvider, HackTest};
use function Facebook\FBExpect\expect;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
*/
namespace BlahNew;

// HHAST_IGNORE_ALL[5624] because the error code is not the interested in this example

final class Test {
// using private type as type annotations
function anotherTest<T as \Blah\__Private\some_private_type_t>(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?hh
// HHAST_IGNORE_ALL[5581] HHAST_IGNORE_ALL[5622] HHAST_IGNORE_ALL[5623] HHAST_IGNORE_ERROR[5624] because the error code is not the interested in this example

function no_side_effects(): void {
if ("NO SIDE EFFECTS") {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
<?hh
// HHAST_IGNORE_ALL[5581] HHAST_IGNORE_ALL[5622] HHAST_IGNORE_ALL[5623] HHAST_IGNORE_ERROR[5624] because the error code is not the interested in this example

function no_side_effects(): void {
if ("NO SIDE EFFECTS") ;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?hh

// HHAST_IGNORE_ALL[5568] because the error code is not the interested in this example
function foreach_value($items) {
foreach ($items as $_item) {
}
Expand Down
1 change: 1 addition & 0 deletions tests/examples/UnusedVariableLinter/unused_foreach.php.in
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?hh

// HHAST_IGNORE_ALL[5568] because the error code is not the interested in this example
function foreach_value($items) {
foreach ($items as $item) {
}
Expand Down