From: Joshua Spence Date: Tue, 7 Oct 2014 12:59:03 +0000 (+1100) Subject: Add a `LINT_CONSTRUCTOR_PARENTHESES` rule to `ArcanistXHPASTLinter` X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=e54725ec891894510865f0a0671c3e62503a4dd4;p=arcanist.git Add a `LINT_CONSTRUCTOR_PARENTHESES` rule to `ArcanistXHPASTLinter` Summary: Adds an `ArcanistXHPASTLinter::LINT_CONSTRUCTOR_PARENTHESES` rule which ensures that (possibly empty) parentheses are used when calling a constructor. Test Plan: Added test cases. Reviewers: #blessed_reviewers, epriestley Reviewed By: #blessed_reviewers, epriestley Subscribers: epriestley, Korvin Differential Revision: https://secure.phabricator.com/D10597 --- diff --git a/src/lint/linter/ArcanistXHPASTLinter.php b/src/lint/linter/ArcanistXHPASTLinter.php index 2a84ed30..37dd5adf 100644 --- a/src/lint/linter/ArcanistXHPASTLinter.php +++ b/src/lint/linter/ArcanistXHPASTLinter.php @@ -48,6 +48,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { const LINT_LANGUAGE_CONSTRUCT_PAREN = 46; const LINT_EMPTY_STATEMENT = 47; const LINT_ARRAY_SEPARATOR = 48; + const LINT_CONSTRUCTOR_PARENTHESES = 49; private $naminghook; private $switchhook; @@ -107,6 +108,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_LANGUAGE_CONSTRUCT_PAREN => 'Language Construct Parentheses', self::LINT_EMPTY_STATEMENT => 'Empty Block Statement', self::LINT_ARRAY_SEPARATOR => 'Array Separator', + self::LINT_CONSTRUCTOR_PARENTHESES => 'Constructor Parentheses', ); } @@ -147,6 +149,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { self::LINT_LANGUAGE_CONSTRUCT_PAREN => $warning, self::LINT_EMPTY_STATEMENT => $advice, self::LINT_ARRAY_SEPARATOR => $advice, + self::LINT_CONSTRUCTOR_PARENTHESES => $advice, ); } @@ -196,7 +199,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { public function getVersion() { // The version number should be incremented whenever a new rule is added. - return '9'; + return '10'; } protected function resolveFuture($path, Future $future) { @@ -267,6 +270,7 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { 'lintLanguageConstructParentheses' => self::LINT_LANGUAGE_CONSTRUCT_PAREN, 'lintEmptyBlockStatements' => self::LINT_EMPTY_STATEMENT, 'lintArraySeparator' => self::LINT_ARRAY_SEPARATOR, + 'lintConstructorParentheses' => self::LINT_CONSTRUCTOR_PARENTHESES, ); foreach ($method_codes as $method => $codes) { @@ -2792,6 +2796,23 @@ final class ArcanistXHPASTLinter extends ArcanistBaseXHPASTLinter { } } + private function lintConstructorParentheses(XHPASTNode $root) { + $nodes = $root->selectDescendantsOfType('n_NEW'); + + foreach ($nodes as $node) { + $class = $node->getChildByIndex(0); + $params = $node->getChildByIndex(1); + + if ($params->getTypeName() == 'n_EMPTY') { + $this->raiseLintAtNode( + $class, + self::LINT_CONSTRUCTOR_PARENTHESES, + pht('Use parentheses when invoking a constructor.'), + $class->getConcreteString().'()'); + } + } + } + public function getSuperGlobalNames() { return array( '$GLOBALS', diff --git a/src/lint/linter/__tests__/xhpast/constructor-parentheses.lint-test b/src/lint/linter/__tests__/xhpast/constructor-parentheses.lint-test new file mode 100644 index 00000000..ad76cf4c --- /dev/null +++ b/src/lint/linter/__tests__/xhpast/constructor-parentheses.lint-test @@ -0,0 +1,12 @@ +