]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Add an XHPAST lint rule for brace formatting
authorepriestley <git@epriestley.com>
Mon, 1 Aug 2011 20:18:53 +0000 (13:18 -0700)
committerepriestley <git@epriestley.com>
Tue, 2 Aug 2011 17:14:58 +0000 (10:14 -0700)
Summary: See D753. Let's just have lint fix this. Depends on D754 (it fails to
detect all the blocks without that patch, but doesn't do anything bad).
Test Plan: Ran unit tests.
Reviewed By: jungejason
Reviewers: jungejason, tuomaspelkonen, aran
CC: hunterbridges, aran, epriestley, jungejason
Differential Revision: 755

src/lint/linter/xhpast/ArcanistXHPASTLinter.php
src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test [new file with mode: 0644]

index 404e13e7ae1f1e5970960ad89f1da5d91b417801..cf616f4cd5b2ae6fc6c69e5021f188ba0c9e0ff9 100644 (file)
@@ -155,6 +155,30 @@ class ArcanistXHPASTLinter extends ArcanistLinter {
     $this->lintPlusOperatorOnStrings($root);
     $this->lintDuplicateKeysInArray($root);
     $this->lintReusedIterators($root);
+    $this->lintBraceFormatting($root);
+  }
+
+  private function lintBraceFormatting($root) {
+
+    foreach ($root->selectDescendantsOfType('n_STATEMENT_LIST') as $list) {
+      $tokens = $list->getTokens();
+      if (!$tokens || head($tokens)->getValue() != '{') {
+        continue;
+      }
+      list($before, $after) = $list->getSurroundingNonsemanticTokens();
+      if (count($before) == 1) {
+        $before = reset($before);
+        if ($before->getValue() != ' ') {
+          $this->raiseLintAtToken(
+            $before,
+            self::LINT_FORMATTING_CONVENTIONS,
+            'Put opening braces on the same line as control statements and '.
+            'declarations, with a single space before them.',
+            ' ');
+        }
+      }
+    }
+
   }
 
   private function lintTautologicalExpressions($root) {
diff --git a/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test b/src/lint/linter/xhpast/__tests__/data/creative-brace-use.lint-test
new file mode 100644 (file)
index 0000000..dc4f8bc
--- /dev/null
@@ -0,0 +1,56 @@
+<?php
+
+function f() {
+
+}
+
+function g()
+{
+
+}
+
+if (1)
+{}
+
+foreach (x() as $y)
+{}
+
+while (1)
+{}
+
+switch (1)
+{}
+
+try
+{}
+catch (Exception $x)
+{}
+~~~~~~~~~~
+warning:7:13
+warning:12:7
+warning:15:20
+warning:18:10
+warning:21:11
+warning:24:4
+warning:26:21
+~~~~~~~~~~
+<?php
+
+function f() {
+
+}
+
+function g() {
+
+}
+
+if (1) {}
+
+foreach (x() as $y) {}
+
+while (1) {}
+
+switch (1) {}
+
+try {}
+catch (Exception $x) {}