Summary: Extend the `ArcanistXHPASTLinter::LINT_BRACE_FORMATTING` rule to raise a warning when `n_STATEMENT` is used without having `n_STATEMENT_LIST` as a parent. Essentially, this means that `if ($x) { do_y(); }` is preferred over `if ($x) do_y();`.
Test Plan: Added some test cases.
Reviewers: epriestley, #blessed_reviewers
Reviewed By: epriestley, #blessed_reviewers
Subscribers: epriestley, Korvin
Differential Revision: https://secure.phabricator.com/D10571
}
}
}
+
+ $nodes = $root->selectDescendantsOfType('n_STATEMENT');
+ foreach ($nodes as $node) {
+ $parent = $node->getParentNode();
+
+ if ($parent && $parent->getTypeName() != 'n_STATEMENT_LIST') {
+ $this->raiseLintAtNode(
+ $node,
+ self::LINT_BRACE_FORMATTING,
+ 'Use braces to surround a statement block.');
+ }
+ }
}
private function lintTautologicalExpressions(XHPASTNode $root) {
error:5:3
error:7:3
error:12:7
+warning:16:3
~~~~~~~~~~
~~~~~~~~~~
{"config": {"xhpast.php-version": "5.3.0"}}
{}
function h(){}
+if ($x) foo();
+else bar();
+do baz(); while ($x);
~~~~~~~~~~
advice:3:14
warning:7:13
warning:24:4
warning:26:21
warning:29:13
+warning:30:9
+warning:31:6
+warning:32:4
~~~~~~~~~~
<?php
catch (Exception $x) {}
function h() {}
+if ($x) foo();
+else bar();
+do baz(); while ($x);
if
($x) {}
-if ($x)
+if ($x) {
echo 100;
-else
+} else {
echo 10;
-do
+}
+do {
echo 1;
-while(true);
+} while(true);
~~~~~~~~~~
warning:2:1
warning:3:1
warning:8:1
warning:13:3
warning:14:3
-warning:23:1
+warning:24:3
~~~~~~~~~~
<?php
if ($x) {}
if ($x) {}
if ($x) {}
-if ($x)
+if ($x) {
echo 100;
-else
+} else {
echo 10;
-do
+}
+do {
echo 1;
-while (true);
+} while (true);