]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for hexadecimal number casing
authorJoshua Spence <josh@freelancer.com>
Tue, 1 Dec 2015 20:41:37 +0000 (07:41 +1100)
committerJoshua Spence <josh@freelancer.com>
Tue, 1 Dec 2015 20:47:16 +0000 (07:47 +1100)
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

src/__phutil_library_map__.php
src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php [new file with mode: 0644]
src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test [new file with mode: 0644]

index 218a1fa97690815e4cf7746f7e0436504bde0d59..3c859155b51f89376e92b6ea4c82848fcb8b7b9a 100644 (file)
@@ -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',
diff --git a/src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..790526a
--- /dev/null
@@ -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;
+
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase.php
new file mode 100644 (file)
index 0000000..f73d2b0
--- /dev/null
@@ -0,0 +1,11 @@
+<?php
+
+final class ArcanistHexadecimalNumericScalarCasingXHPASTLinterRuleTestCase
+  extends ArcanistXHPASTLinterRuleTestCase {
+
+  public function testLinter() {
+    $this->executeTestsInDirectory(
+      dirname(__FILE__).'/hexadecimal-numeric-scalar-casing/');
+  }
+
+}
diff --git a/src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test b/src/lint/linter/xhpast/rules/__tests__/hexadecimal-numeric-scalar-casing/hexadecimal.lint-test
new file mode 100644 (file)
index 0000000..aae5eec
--- /dev/null
@@ -0,0 +1,12 @@
+<?php
+0xFF;
+0xff;
+0XFF;
+~~~~~~~~~~
+warning:3:1
+warning:4:1
+~~~~~~~~~~
+<?php
+0xFF;
+0xFF;
+0xFF;