Summary: Adds a linter rule which ensures that binary integers are written in lowercase (i.e. `0b1` instead of `0B1`).
Test Plan: Added test cases.
Reviewers: epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: Korvin
Differential Revision: https://secure.phabricator.com/D14644
'ArcanistBaseXHPASTLinter' => 'lint/linter/ArcanistBaseXHPASTLinter.php',
'ArcanistBinaryExpressionSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistBinaryExpressionSpacingXHPASTLinterRule.php',
'ArcanistBinaryExpressionSpacingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistBinaryExpressionSpacingXHPASTLinterRuleTestCase.php',
+ 'ArcanistBinaryNumericScalarCasingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistBinaryNumericScalarCasingXHPASTLinterRule.php',
+ 'ArcanistBinaryNumericScalarCasingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistBinaryNumericScalarCasingXHPASTLinterRuleTestCase.php',
'ArcanistBlacklistedFunctionXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistBlacklistedFunctionXHPASTLinterRule.php',
'ArcanistBlacklistedFunctionXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistBlacklistedFunctionXHPASTLinterRuleTestCase.php',
'ArcanistBookmarkWorkflow' => 'workflow/ArcanistBookmarkWorkflow.php',
'ArcanistBaseXHPASTLinter' => 'ArcanistFutureLinter',
'ArcanistBinaryExpressionSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistBinaryExpressionSpacingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
+ 'ArcanistBinaryNumericScalarCasingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+ 'ArcanistBinaryNumericScalarCasingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistBlacklistedFunctionXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistBlacklistedFunctionXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistBookmarkWorkflow' => 'ArcanistFeatureWorkflow',
--- /dev/null
+<?php
+
+final class ArcanistBinaryNumericScalarCasingXHPASTLinterRule
+ extends ArcanistXHPASTLinterRule {
+
+ const ID = 131;
+
+ public function getLintName() {
+ return pht('Binary Integer Casing');
+ }
+
+ public function getLintSeverity() {
+ return ArcanistLintSeverity::SEVERITY_WARNING;
+ }
+
+ public function process(XHPASTNode $root) {
+ $binaries = $this->getBinaryNumericScalars($root);
+
+ foreach ($binaries as $binary) {
+ $value = substr($binary->getConcreteString(), 2);
+
+ if (!preg_match('/^0b[01]+$/', $binary->getConcreteString())) {
+ $this->raiseLintAtNode(
+ $binary,
+ pht(
+ 'For consistency, write binary integers with a leading `%s`.',
+ '0b'),
+ '0b'.$value);
+ }
+ }
+ }
+
+ private function getBinaryNumericScalars(XHPASTNode $root) {
+ $numeric_scalars = $root->selectDescendantsOfType('n_NUMERIC_SCALAR');
+ $binary_numeric_scalars = array();
+
+ foreach ($numeric_scalars as $numeric_scalar) {
+ $number = $numeric_scalar->getConcreteString();
+
+ if (preg_match('/^0b[01]+$/i', $number)) {
+ $binary_numeric_scalars[] = $numeric_scalar;
+ }
+ }
+
+ return $binary_numeric_scalars;
+
+ }
+
+}
const ID = 127;
public function getLintName() {
- return pht('Hexadecimal Casing');
+ return pht('Hexadecimal Integer Casing');
}
public function getLintSeverity() {
$this->raiseLintAtNode(
$hexadecimal,
pht(
- 'For consistency, write hexadecimals in uppercase '.
- 'with a leading `%s`.',
+ 'For consistency, write hexadecimals integers '.
+ 'in uppercase with a leading `%s`.',
'0x'),
'0x'.strtoupper($value));
}
--- /dev/null
+<?php
+
+final class ArcanistBinaryNumericScalarCasingXHPASTLinterRuleTestCase
+ extends ArcanistXHPASTLinterRuleTestCase {
+
+ public function testLinter() {
+ $this->executeTestsInDirectory(
+ dirname(__FILE__).'/binary-numeric-scalar-casing/');
+ }
+
+}
--- /dev/null
+<?php
+0b1;
+0B1;
+~~~~~~~~~~
+warning:3:1