]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add a linter rule for array elements
authorJoshua Spence <josh@joshuaspence.com>
Mon, 10 Aug 2015 20:58:28 +0000 (06:58 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Mon, 10 Aug 2015 21:16:35 +0000 (07:16 +1000)
Summary: Add a linter rule to ensure that array elements occupy a single line each.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D13842

src/__phutil_library_map__.php
src/lint/linter/__tests__/xhpast/array-value.lint-test [new file with mode: 0644]
src/lint/linter/xhpast/rules/ArcanistArrayValueXHPASTLinterRule.php [new file with mode: 0644]

index 841b4e84195b18dd44fa0e1d0fb9f2207dc0a8de..422c792f04a7606d4b75095fd0234c9b73b2073f 100644 (file)
@@ -15,6 +15,7 @@ phutil_register_library_map(array(
     'ArcanistAnoidWorkflow' => 'workflow/ArcanistAnoidWorkflow.php',
     'ArcanistArrayIndexSpacingXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistArrayIndexSpacingXHPASTLinterRule.php',
     'ArcanistArraySeparatorXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistArraySeparatorXHPASTLinterRule.php',
+    'ArcanistArrayValueXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistArrayValueXHPASTLinterRule.php',
     'ArcanistBackoutWorkflow' => 'workflow/ArcanistBackoutWorkflow.php',
     'ArcanistBaseCommitParser' => 'parser/ArcanistBaseCommitParser.php',
     'ArcanistBaseCommitParserTestCase' => 'parser/__tests__/ArcanistBaseCommitParserTestCase.php',
@@ -292,6 +293,7 @@ phutil_register_library_map(array(
     'ArcanistAnoidWorkflow' => 'ArcanistWorkflow',
     'ArcanistArrayIndexSpacingXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistArraySeparatorXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
+    'ArcanistArrayValueXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
     'ArcanistBackoutWorkflow' => 'ArcanistWorkflow',
     'ArcanistBaseCommitParser' => 'Phobject',
     'ArcanistBaseCommitParserTestCase' => 'PhutilTestCase',
diff --git a/src/lint/linter/__tests__/xhpast/array-value.lint-test b/src/lint/linter/__tests__/xhpast/array-value.lint-test
new file mode 100644 (file)
index 0000000..a133979
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+array(
+  1, 2, 3,
+);
+
+array(
+  'foo' => 'bar', 'bar' => 'baz',
+);
+
+array(
+  1, /* quack */ 2, /* quack */3,
+);
+
+array(
+  /* OPEN   */ 1,
+  /* CLOSED */ 2,
+);
+~~~~~~~~~~
+warning:4:5
+warning:4:8
+warning:8:18
+warning:12:17
+warning:12:32
+~~~~~~~~~~
+<?php
+
+array(
+  1,
+  2,
+  3,
+);
+
+array(
+  'foo' => 'bar',
+  'bar' => 'baz',
+);
+
+array(
+  1, /* quack */
+  2, /* quack */
+  3,
+);
+
+array(
+  /* OPEN   */ 1,
+  /* CLOSED */ 2,
+);
diff --git a/src/lint/linter/xhpast/rules/ArcanistArrayValueXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistArrayValueXHPASTLinterRule.php
new file mode 100644 (file)
index 0000000..21f5092
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+final class ArcanistArrayValueXHPASTLinterRule
+  extends ArcanistXHPASTLinterRule {
+
+  const ID = 76;
+
+  public function getLintName() {
+    return pht('Array Element');
+  }
+
+  public function getLintSeverity() {
+    return ArcanistLintSeverity::SEVERITY_WARNING;
+  }
+
+  public function process(XHPASTNode $root) {
+    $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
+
+    foreach ($arrays as $array) {
+      $array_values = $array
+        ->getChildOfType(0, 'n_ARRAY_VALUE_LIST')
+        ->getChildrenOfType('n_ARRAY_VALUE');
+
+      if (!$array_values) {
+        // There is no need to check an empty array.
+        continue;
+      }
+
+      $multiline = $array->getLineNumber() != $array->getEndLineNumber();
+
+      if (!$multiline) {
+        continue;
+      }
+
+      foreach ($array_values as $value) {
+        list($before, $after) = $value->getSurroundingNonsemanticTokens();
+
+        if (strpos(implode('', mpull($before, 'getValue')), "\n") === false) {
+          if (last($before)->isAnyWhitespace()) {
+            $token = last($before);
+            $replacement = "\n".$value->getIndentation();
+          } else {
+            $token = head($value->getTokens());
+            $replacement = "\n".$value->getIndentation().$token->getValue();
+          }
+
+          $this->raiseLintAtToken(
+            $token,
+            pht('Array elements should each occupy a single line.'),
+            $replacement);
+        }
+      }
+    }
+  }
+
+}