From 6f67e9062b28b60fe7ca092e2ac02a7bace4e701 Mon Sep 17 00:00:00 2001 From: Ryan Greenberg Date: Tue, 3 May 2022 10:37:07 -0400 Subject: [PATCH 1/5] Add UnusedPipeVariableLinter --- src/Linters/UnusedPipeVariableLinter.hack | 40 +++++++++++++++++++ tests/UnusedPipeVariableLinterTest.hack | 26 ++++++++++++ .../unused_pipe.php.expect | 7 ++++ .../unused_pipe.php.in | 5 +++ 4 files changed, 78 insertions(+) create mode 100644 src/Linters/UnusedPipeVariableLinter.hack create mode 100644 tests/UnusedPipeVariableLinterTest.hack create mode 100644 tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect create mode 100644 tests/examples/UnusedPipeVariableLinter/unused_pipe.php.in diff --git a/src/Linters/UnusedPipeVariableLinter.hack b/src/Linters/UnusedPipeVariableLinter.hack new file mode 100644 index 000000000..cb7e6f170 --- /dev/null +++ b/src/Linters/UnusedPipeVariableLinter.hack @@ -0,0 +1,40 @@ +/* + * 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", + $rhs, + ); + } + + return null; + } +} diff --git a/tests/UnusedPipeVariableLinterTest.hack b/tests/UnusedPipeVariableLinterTest.hack new file mode 100644 index 000000000..36e855c33 --- /dev/null +++ b/tests/UnusedPipeVariableLinterTest.hack @@ -0,0 +1,26 @@ +/* + * 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; + + protected function getLinter(string $file): UnusedPipeVariableLinter { + return UnusedPipeVariableLinter::fromPath($file); + } + + public function getCleanExamples(): vec<(string)> { + return vec[ + tuple( + ' Str\uppercase($$); }', + ), + ]; + } +} diff --git a/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect new file mode 100644 index 000000000..046a6d794 --- /dev/null +++ b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect @@ -0,0 +1,7 @@ +[ + { + "blame": "serialize($html)", + "blame_pretty": "serialize($html)", + "description": "Missing pipe variable in right-hand side of pipe operator" + } +] diff --git a/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.in b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.in new file mode 100644 index 000000000..f6cf71fd8 --- /dev/null +++ b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.in @@ -0,0 +1,5 @@ + serialize($html); +} From ca606ffb6a2270bf4c102d1ad32c29a8d9537830 Mon Sep 17 00:00:00 2001 From: Ryan Greenberg Date: Tue, 3 May 2022 10:39:00 -0400 Subject: [PATCH 2/5] Show error for entire expression --- src/Linters/UnusedPipeVariableLinter.hack | 2 +- .../examples/UnusedPipeVariableLinter/unused_pipe.php.expect | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Linters/UnusedPipeVariableLinter.hack b/src/Linters/UnusedPipeVariableLinter.hack index cb7e6f170..63dc58c84 100644 --- a/src/Linters/UnusedPipeVariableLinter.hack +++ b/src/Linters/UnusedPipeVariableLinter.hack @@ -31,7 +31,7 @@ final class UnusedPipeVariableLinter extends ASTLinter { return new ASTLintError( $this, "Missing pipe variable in right-hand side of pipe operator", - $rhs, + $expr, ); } diff --git a/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect index 046a6d794..ebc5b85fa 100644 --- a/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect +++ b/tests/examples/UnusedPipeVariableLinter/unused_pipe.php.expect @@ -1,7 +1,7 @@ [ { - "blame": "serialize($html)", - "blame_pretty": "serialize($html)", + "blame": " htmlspecialchars($html) |> serialize($html)", + "blame_pretty": " htmlspecialchars($html) |> serialize($html)", "description": "Missing pipe variable in right-hand side of pipe operator" } ] From 640a3195bfd27203a435d1b46181f8638d2b3c22 Mon Sep 17 00:00:00 2001 From: Ryan Greenberg Date: Tue, 3 May 2022 10:42:31 -0400 Subject: [PATCH 3/5] Apply hackfmt --- src/Linters/UnusedPipeVariableLinter.hack | 47 +++++++++++------------ tests/UnusedPipeVariableLinterTest.hack | 22 +++++------ 2 files changed, 34 insertions(+), 35 deletions(-) diff --git a/src/Linters/UnusedPipeVariableLinter.hack b/src/Linters/UnusedPipeVariableLinter.hack index 63dc58c84..ead1d6eb4 100644 --- a/src/Linters/UnusedPipeVariableLinter.hack +++ b/src/Linters/UnusedPipeVariableLinter.hack @@ -10,31 +10,30 @@ namespace Facebook\HHAST; final class UnusedPipeVariableLinter extends ASTLinter { - const type TConfig = shape(); - const type TNode = BinaryExpression; - const type TContext = Script; + 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, - ); - } + <<__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); - return null; + if ($pipe_var is null) { + return new ASTLintError( + $this, + "Missing pipe variable in right-hand side of pipe operator", + $expr, + ); } + + return null; + } } diff --git a/tests/UnusedPipeVariableLinterTest.hack b/tests/UnusedPipeVariableLinterTest.hack index 36e855c33..b3832ff29 100644 --- a/tests/UnusedPipeVariableLinterTest.hack +++ b/tests/UnusedPipeVariableLinterTest.hack @@ -10,17 +10,17 @@ namespace Facebook\HHAST; final class UnusedPipeVariableLinterTest extends TestCase { - use LinterTestTrait; + use LinterTestTrait; - protected function getLinter(string $file): UnusedPipeVariableLinter { - return UnusedPipeVariableLinter::fromPath($file); - } + protected function getLinter(string $file): UnusedPipeVariableLinter { + return UnusedPipeVariableLinter::fromPath($file); + } - public function getCleanExamples(): vec<(string)> { - return vec[ - tuple( - ' Str\uppercase($$); }', - ), - ]; - } + public function getCleanExamples(): vec<(string)> { + return vec[ + tuple( + ' Str\uppercase($$); }', + ), + ]; + } } From dd7b7516fa39968217142ea087e5ea2c516daf53 Mon Sep 17 00:00:00 2001 From: Ryan Greenberg Date: Tue, 3 May 2022 10:55:35 -0400 Subject: [PATCH 4/5] Fix lint errors --- src/Linters/UnusedPipeVariableLinter.hack | 2 +- tests/UnusedPipeVariableLinterTest.hack | 2 ++ 2 files changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Linters/UnusedPipeVariableLinter.hack b/src/Linters/UnusedPipeVariableLinter.hack index ead1d6eb4..d8ff614f1 100644 --- a/src/Linters/UnusedPipeVariableLinter.hack +++ b/src/Linters/UnusedPipeVariableLinter.hack @@ -29,7 +29,7 @@ final class UnusedPipeVariableLinter extends ASTLinter { if ($pipe_var is null) { return new ASTLintError( $this, - "Missing pipe variable in right-hand side of pipe operator", + 'Missing pipe variable in right-hand side of pipe operator', $expr, ); } diff --git a/tests/UnusedPipeVariableLinterTest.hack b/tests/UnusedPipeVariableLinterTest.hack index b3832ff29..cc9dd3872 100644 --- a/tests/UnusedPipeVariableLinterTest.hack +++ b/tests/UnusedPipeVariableLinterTest.hack @@ -12,10 +12,12 @@ 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( From 2778b871a8235ccb24fca007632106b2454c7ee2 Mon Sep 17 00:00:00 2001 From: Ryan Greenberg Date: Wed, 4 May 2022 00:37:57 -0400 Subject: [PATCH 5/5] Update UnusedPipeVariableLinter.hack --- src/Linters/UnusedPipeVariableLinter.hack | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Linters/UnusedPipeVariableLinter.hack b/src/Linters/UnusedPipeVariableLinter.hack index d8ff614f1..6f18b6f5b 100644 --- a/src/Linters/UnusedPipeVariableLinter.hack +++ b/src/Linters/UnusedPipeVariableLinter.hack @@ -16,7 +16,7 @@ final class UnusedPipeVariableLinter extends ASTLinter { <<__Override>> public function getLintErrorForNode( - Script $script, + Script $_script, BinaryExpression $expr, ): ?ASTLintError { $op = $expr->getOperator();