Page MenuHomePhabricator

D10535.id.diff
No OneTemporary

D10535.id.diff

diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php
--- a/src/lint/linter/ArcanistXHPASTLinter.php
+++ b/src/lint/linter/ArcanistXHPASTLinter.php
@@ -47,6 +47,7 @@
const LINT_PHP_COMPATIBILITY = 45;
const LINT_LANGUAGE_CONSTRUCT_PAREN = 46;
const LINT_EMPTY_STATEMENT = 47;
+ const LINT_ARRAY_SEPARATOR = 48;
private $naminghook;
private $switchhook;
@@ -105,6 +106,7 @@
self::LINT_PHP_COMPATIBILITY => 'PHP Compatibility',
self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses',
self::LINT_EMPTY_STATEMENT => 'Empty Block Statement',
+ self::LINT_ARRAY_SEPARATOR => 'Array Separator',
);
}
@@ -144,6 +146,7 @@
self::LINT_CONCATENATION_OPERATOR => $warning,
self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning,
self::LINT_EMPTY_STATEMENT => $advice,
+ self::LINT_ARRAY_SEPARATOR => $advice,
);
}
@@ -193,7 +196,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '8';
+ return '9';
}
protected function resolveFuture($path, Future $future) {
@@ -263,6 +266,7 @@
'lintPHPCompatibility' => self::LINT_PHP_COMPATIBILITY,
'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN,
'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT,
+ 'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR,
);
foreach ($method_codes as $method => $codes) {
@@ -2714,6 +2718,48 @@
}
}
+ protected function lintArraySeparator(XHPASTNode $root) {
+ $arrays = $root->selectDescendantsOfType('n_ARRAY_LITERAL');
+
+ foreach ($arrays as $array) {
+ $commas = $array->selectTokensOfType(',');
+ $values = $array->selectDescendantsOfType('n_ARRAY_VALUE');
+
+ if ($values->count() == 0) {
+ // There is no need to check an empty array.
+ continue;
+ }
+
+ // This is a little messy... we want to do `last($values)` but
+ // `$values` is an `AASTNodeList`.
+ $last = null;
+ foreach ($values as $value) {
+ $last = $value;
+ }
+
+ $multiline = $array->getLineNumber() != $array->getEndLineNumber();
+
+ if ($multiline && count($commas) != count($values)) {
+ $this->raiseLintAtNode(
+ $last,
+ self::LINT_ARRAY_SEPARATOR,
+ pht('Multi-lined arrays should have trailing commas.'),
+ $last->getConcreteString().',');
+ } else if (!$multiline && count($commas) == count($values)) {
+ $last_comma = null;
+ foreach ($commas as $comma) {
+ $last_comma = $comma;
+ }
+
+ $this->raiseLintAtToken(
+ $last_comma,
+ self::LINT_ARRAY_SEPARATOR,
+ pht('Single lined arrays should not have a trailing comma.'),
+ '');
+ }
+ }
+ }
+
public function getSuperGlobalNames() {
return array(
'$GLOBALS',
diff --git a/src/lint/linter/__tests__/xhpast/array-comma.lint-test b/src/lint/linter/__tests__/xhpast/array-comma.lint-test
new file mode 100644
--- /dev/null
+++ b/src/lint/linter/__tests__/xhpast/array-comma.lint-test
@@ -0,0 +1,30 @@
+<?php
+array(1, 2, 3);
+array(1, 2, 3,);
+array(
+ 1,
+ 2,
+ 3,
+);
+array(
+ 1,
+ 2,
+ 3
+);
+~~~~~~~~~~
+advice:3:14
+advice:12:3
+~~~~~~~~~~
+<?php
+array(1, 2, 3);
+array(1, 2, 3);
+array(
+ 1,
+ 2,
+ 3,
+);
+array(
+ 1,
+ 2,
+ 3,
+);

File Metadata

Mime Type
text/plain
Expires
Thu, Apr 10, 10:29 PM (1 w, 5 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
7579660
Default Alt Text
D10535.id.diff (3 KB)

Event Timeline