This repository has been archived by the owner on Dec 1, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add linter banning PHP equality checks (#54)
* Add linter banning PHP equality checks * Run linter, autofix, support <> Add linter to default linter list, run it on hhast
- Loading branch information
1 parent
4a65e4b
commit 2837bd2
Showing
10 changed files
with
156 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
<?hh // strict | ||
/* | ||
* 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\Linters; | ||
|
||
use type Facebook\HHAST\{ | ||
BinaryExpression, | ||
EditableNode, | ||
EqualEqualToken, | ||
EqualEqualEqualToken, | ||
ExclamationEqualToken, | ||
ExclamationEqualEqualToken, | ||
LessThanGreaterThanToken, | ||
}; | ||
use function Facebook\HHAST\Missing; | ||
use namespace Facebook\TypeAssert; | ||
use namespace HH\Lib\{C, Vec}; | ||
|
||
final class NoPHPEqualityLinter | ||
extends AutoFixingASTLinter<BinaryExpression> { | ||
<<__Override>> | ||
protected static function getTargetType(): classname<BinaryExpression> { | ||
return BinaryExpression::class; | ||
} | ||
|
||
<<__Override>> | ||
public function getLintErrorForNode( | ||
BinaryExpression $expr, | ||
vec<EditableNode> $_parents, | ||
): ?FixableASTLintError<BinaryExpression> { | ||
$token = $expr->getOperator(); | ||
$replacement = null; | ||
if ($token instanceof EqualEqualToken) { | ||
$replacement = '==='; | ||
} else if ( | ||
$token instanceof ExclamationEqualToken | ||
|| $token instanceof LessThanGreaterThanToken) { | ||
$replacement = '!=='; | ||
} else { | ||
return null; | ||
} | ||
|
||
return new FixableASTLintError( | ||
$this, | ||
'Do not use PHP equality - use "'.$replacement.'" instead.', | ||
$expr, | ||
); | ||
return null; | ||
} | ||
|
||
<<__Override>> | ||
public function getFixedNode(BinaryExpression $expr): ?EditableNode { | ||
$op = $expr->getOperator(); | ||
if ($op instanceof EqualEqualToken) { | ||
$op = new EqualEqualEqualToken($op->getLeading(), $op->getTrailing()); | ||
} else if ( | ||
$op instanceof ExclamationEqualToken | ||
|| $op instanceof LessThanGreaterThanToken | ||
) { | ||
$op = | ||
new ExclamationEqualEqualToken($op->getLeading(), $op->getTrailing()); | ||
} else { | ||
return null; | ||
} | ||
return $expr->withOperator($op); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
<?hh // strict | ||
/* | ||
* 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 NoPHPEqualityLinterTest extends TestCase { | ||
use AutoFixingLinterTestTrait<Linters\FixableASTLintError<BinaryExpression>>; | ||
|
||
protected function getLinter( | ||
string $file, | ||
): Linters\NoPHPEqualityLinter{ | ||
return new Linters\NoPHPEqualityLinter($file); | ||
} | ||
|
||
public function getCleanExamples(): array<array<string>> { | ||
return [ | ||
['<?hh "foo" === "bar";'], | ||
['<?hh "foo" !== "bar";'], | ||
['<?hh "foo" > "bar";'], | ||
]; | ||
} | ||
} |
15 changes: 15 additions & 0 deletions
15
tests/fixtures/NoPHPEqualityLinter/double_equals.php.autofix.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?hh // strict | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
function foo(): void { | ||
var_dump('0e123' === '0e456'); | ||
var_dump('0e123' !== '0e456'); | ||
var_dump('0e123' !== '0e456'); | ||
} |
17 changes: 17 additions & 0 deletions
17
tests/fixtures/NoPHPEqualityLinter/double_equals.php.expect
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
[ | ||
{ | ||
"blame": "'0e123' == '0e456'", | ||
"blame_pretty": "'0e123' == '0e456'", | ||
"description": "Do not use PHP equality - use \"===\" instead." | ||
}, | ||
{ | ||
"blame": "'0e123' != '0e456'", | ||
"blame_pretty": "'0e123' != '0e456'", | ||
"description": "Do not use PHP equality - use \"!==\" instead." | ||
}, | ||
{ | ||
"blame": "'0e123' <> '0e456'", | ||
"blame_pretty": "'0e123' <> '0e456'", | ||
"description": "Do not use PHP equality - use \"!==\" instead." | ||
} | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
<?hh // strict | ||
/* | ||
* 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. | ||
* | ||
*/ | ||
|
||
function foo(): void { | ||
var_dump('0e123' == '0e456'); | ||
var_dump('0e123' != '0e456'); | ||
var_dump('0e123' <> '0e456'); | ||
} |