mirror of
https://we.phorge.it/source/arcanist.git
synced 2025-01-10 23:01:04 +01:00
Add a linter rule for hexadecimal number casing
Summary: Hexadecimal numbers should be written as `0xFF` and not `0xff` or `0Xff`. Test Plan: Added test cases. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D14612
This commit is contained in:
parent
5218ec357c
commit
00efcd294f
4 changed files with 77 additions and 0 deletions
|
@ -163,6 +163,8 @@ phutil_register_library_map(array(
|
|||
'ArcanistHLintLinter' => 'lint/linter/ArcanistHLintLinter.php',
|
||||
'ArcanistHLintLinterTestCase' => 'lint/linter/__tests__/ArcanistHLintLinterTestCase.php',
|
||||
'ArcanistHelpWorkflow' => 'workflow/ArcanistHelpWorkflow.php',
|
||||
'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php',
|
||||
'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php',
|
||||
'ArcanistHgClientChannel' => 'hgdaemon/ArcanistHgClientChannel.php',
|
||||
'ArcanistHgProxyClient' => 'hgdaemon/ArcanistHgProxyClient.php',
|
||||
'ArcanistHgProxyServer' => 'hgdaemon/ArcanistHgProxyServer.php',
|
||||
|
@ -561,6 +563,8 @@ phutil_register_library_map(array(
|
|||
'ArcanistHLintLinter' => 'ArcanistExternalLinter',
|
||||
'ArcanistHLintLinterTestCase' => 'ArcanistExternalLinterTestCase',
|
||||
'ArcanistHelpWorkflow' => 'ArcanistWorkflow',
|
||||
'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
|
||||
'ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
|
||||
'ArcanistHgClientChannel' => 'PhutilProtocolChannel',
|
||||
'ArcanistHgProxyClient' => 'Phobject',
|
||||
'ArcanistHgProxyServer' => 'Phobject',
|
||||
|
|
|
@ -0,0 +1,50 @@
|
|||
<?php
|
||||
|
||||
final class ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule
|
||||
extends ArcanistXHPASTLinterRule {
|
||||
|
||||
const ID = 127;
|
||||
|
||||
public function getLintName() {
|
||||
return pht('Hexadecimal Casing');
|
||||
}
|
||||
|
||||
public function getLintSeverity() {
|
||||
return ArcanistLintSeverity::SEVERITY_WARNING;
|
||||
}
|
||||
|
||||
public function process(XHPASTNode $root) {
|
||||
$hexadecimals = $this->getHexadecimalNumericScalars($root);
|
||||
|
||||
foreach ($hexadecimals as $hexadecimal) {
|
||||
$value = substr($hexadecimal->getConcreteString(), 2);
|
||||
|
||||
if (!preg_match('/^0x[0-9A-F]+$/', $hexadecimal->getConcreteString())) {
|
||||
$this->raiseLintAtNode(
|
||||
$hexadecimal,
|
||||
pht(
|
||||
'For consistency, write hexadecimals in uppercase '.
|
||||
'with a leading `%s`.',
|
||||
'0x'),
|
||||
'0x'.strtoupper($value));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private function getHexadecimalNumericScalars(XHPASTNode $root) {
|
||||
$numeric_scalars = $root->selectDescendantsOfType('n_NUMERIC_SCALAR');
|
||||
$hexadecimal_numeric_scalars = array();
|
||||
|
||||
foreach ($numeric_scalars as $numeric_scalar) {
|
||||
$number = $numeric_scalar->getConcreteString();
|
||||
|
||||
if (preg_match('/^0x[0-9A-F]+$/i', $number)) {
|
||||
$hexadecimal_numeric_scalars[] = $numeric_scalar;
|
||||
}
|
||||
}
|
||||
|
||||
return $hexadecimal_numeric_scalars;
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,11 @@
|
|||
<?php
|
||||
|
||||
final class ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase
|
||||
extends ArcanistXHPASTLinterRuleTestCase {
|
||||
|
||||
public function testLinter() {
|
||||
$this->executeTestsInDirectory(
|
||||
dirname(__FILE__).'/hexadecimal-numeric-scalar-casing/');
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,12 @@
|
|||
<?php
|
||||
0xFF;
|
||||
0xff;
|
||||
0XFF;
|
||||
~~~~~~~~~~
|
||||
warning:3:1
|
||||
warning:4:1
|
||||
~~~~~~~~~~
|
||||
<?php
|
||||
0xFF;
|
||||
0xFF;
|
||||
0xFF;
|
Loading…
Reference in a new issue