Page MenuHomePhabricator

D11504.id27664.diff
No OneTemporary

D11504.id27664.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
@@ -53,8 +53,10 @@
const LINT_BLACKLISTED_FUNCTION = 51;
const LINT_IMPLICIT_VISIBILITY = 52;
const LINT_CALL_TIME_PASS_BY_REF = 53;
+ const LINT_INDENTATION = 54;
private $blacklistedFunctions = array();
+ private $indentation;
private $naminghook;
private $switchhook;
private $version;
@@ -118,6 +120,7 @@
self::LINT_BLACKLISTED_FUNCTION => 'Use of Blacklisted Function',
self::LINT_IMPLICIT_VISIBILITY => 'Implicit Method Visibility',
self::LINT_CALL_TIME_PASS_BY_REF => 'Call-Time Pass-By-Reference',
+ self::LINT_INDENTATION => 'Indentation',
);
}
@@ -160,6 +163,7 @@
self::LINT_ARRAY_SEPARATOR => $advice,
self::LINT_CONSTRUCTOR_PARENTHESES => $advice,
self::LINT_IMPLICIT_VISIBILITY => $advice,
+ self::LINT_INDENTATION => $advice,
);
}
@@ -169,6 +173,11 @@
'type' => 'optional map<string, string>',
'help' => pht('Blacklisted functions which should not be used.'),
),
+ 'xhpast.indentation' => array(
+ 'type' => 'optional int',
+ 'help' => pht(
+ 'Number of whitespace characters to be used for indentation.'),
+ ),
'xhpast.naminghook' => array(
'type' => 'optional string',
'help' => pht(
@@ -197,6 +206,9 @@
case 'xhpast.blacklisted.function':
$this->blacklistedFunctions = $value;
return;
+ case 'xhpast.indentation':
+ $this->indentation = $value;
+ return;
case 'xhpast.naminghook':
$this->naminghook = $value;
return;
@@ -216,7 +228,7 @@
public function getVersion() {
// The version number should be incremented whenever a new rule is added.
- return '15';
+ return '16';
}
protected function resolveFuture($path, Future $future) {
@@ -293,6 +305,7 @@
'lintMethodModifier' => self::LINT_IMPLICIT_VISIBILITY,
'lintPropertyModifier' => self::LINT_IMPLICIT_VISIBILITY,
'lintCallTimePassByReference' => self::LINT_CALL_TIME_PASS_BY_REF,
+ 'lintIndentation' => self::LINT_INDENTATION,
);
foreach ($method_codes as $method => $codes) {
@@ -3101,6 +3114,71 @@
}
}
+ private function lintIndentation(XHPASTNode $root) {
+ $statement_lists = $root->selectDescendantsOfType('n_STATEMENT_LIST');
+
+ foreach ($statement_lists as $statement_list) {
+ $statements = $statement_list->getChildrenOfType('n_STATEMENT');
+
+ foreach ($statements as $statement) {
+ list($before, $after) = $statement->getSurroundingNonsemanticTokens();
+
+ $indent = '';
+ foreach ($before as $token) {
+ $indent .= preg_replace("/^\s*\n/", '', $token->getValue());
+ }
+
+ if (strlen($indent) != 2 * $this->getLevel($statement)) {
+ $this->raiseLintAtToken(
+ head($before),
+ self::LINT_INDENTATION,
+ pht('Incorrect indentation.'),
+ preg_replace(
+ "/(?<=\n)\s*/",
+ '',
+ $token->getValue()).str_repeat(
+ ' ',
+ 2 * $this->getLevel($statement)));
+ }
+
+ $indent = '';
+ foreach ($after as $token) {
+ $indent .= preg_replace("/^\s*\n/", '', $token->getValue());
+ }
+
+ if (strlen($indent) != 2 * ($this->getLevel($statement) - 1)) {
+ $this->raiseLintAtToken(
+ head($after),
+ self::LINT_INDENTATION,
+ pht('Incorrect indentation.'),
+ preg_replace(
+ "/(?<=\n)\s*/",
+ '',
+ $token->getValue()).str_repeat(
+ ' ',
+ max(0, 2 * ($this->getLevel($statement) - 1))));
+ }
+ }
+ }
+ }
+
+ private function getLevel(XHPASTNode $node) {
+ $level = 0;
+
+ for (; $node != null; $node = $node->getParentNode()) {
+ switch ($node->getTypeName()) {
+ case 'n_STATEMENT_LIST':
+ $level++;
+ break;
+
+ default:
+ break;
+ }
+ }
+
+ return $level - 1;
+ }
+
public function getSuperGlobalNames() {
return array(
'$GLOBALS',

File Metadata

Mime Type
text/plain
Expires
Wed, Oct 23, 3:46 PM (3 w, 2 d ago)
Storage Engine
blob
Storage Format
Encrypted (AES-256-CBC)
Storage Handle
6744616
Default Alt Text
D11504.id27664.diff (4 KB)

Event Timeline