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

Add UnusedPipeVariableLinter #459

Merged
merged 5 commits into from
May 16, 2022
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
39 changes: 39 additions & 0 deletions src/Linters/UnusedPipeVariableLinter.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
/*
* 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;

final class UnusedPipeVariableLinter extends ASTLinter {
const type TConfig = shape();
const type TNode = BinaryExpression;
const type TContext = Script;

<<__Override>>
public function getLintErrorForNode(
Script $_script,
BinaryExpression $expr,
): ?ASTLintError {
$op = $expr->getOperator();
if (!$op is BarGreaterThanToken) {
return null;
}
$rhs = $expr->getRightOperand();
$pipe_var = $rhs->getFirstDescendantOfType(PipeVariableExpression::class);

if ($pipe_var is null) {
return new ASTLintError(
$this,
'Missing pipe variable in right-hand side of pipe operator',
$expr,
);
}

return null;
}
}
28 changes: 28 additions & 0 deletions tests/UnusedPipeVariableLinterTest.hack
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
/*
* 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;

final class UnusedPipeVariableLinterTest extends TestCase {
use LinterTestTrait;

<<__Override>>
protected function getLinter(string $file): UnusedPipeVariableLinter {
return UnusedPipeVariableLinter::fromPath($file);
}

<<__Override>>
public function getCleanExamples(): vec<(string)> {
return vec[
tuple(
'<?hh function foo(): string { return "abc" |> Str\uppercase($$); }',
),
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
[
{
"blame": " htmlspecialchars($html) |> serialize($html)",
"blame_pretty": " htmlspecialchars($html) |> serialize($html)",
"description": "Missing pipe variable in right-hand side of pipe operator"
}
]
5 changes: 5 additions & 0 deletions tests/examples/UnusedPipeVariableLinter/unused_pipe.php.in
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<?hh

function unused_pipe(string $html): void {
htmlspecialchars($html) |> serialize($html);
}