1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-26 00:32:41 +01:00

Add a linter rule for nested namespaces

Summary: Namespace declarations cannot be nested, see https://3v4l.org/kRUNj.

Test Plan: Added unit tests

Reviewers: #blessed_reviewers, epriestley

Reviewed By: #blessed_reviewers, epriestley

Subscribers: Korvin

Differential Revision: https://secure.phabricator.com/D14513
This commit is contained in:
Joshua Spence 2015-11-19 19:31:45 +11:00
parent 7386afc953
commit 149e7895fb
4 changed files with 52 additions and 0 deletions

View file

@ -226,6 +226,8 @@ phutil_register_library_map(array(
'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistModifierOrderingXHPASTLinterRuleTestCase.php',
'ArcanistNamingConventionsXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNamingConventionsXHPASTLinterRule.php',
'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNamingConventionsXHPASTLinterRuleTestCase.php',
'ArcanistNestedNamespacesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNestedNamespacesXHPASTLinterRule.php',
'ArcanistNestedNamespacesXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNestedNamespacesXHPASTLinterRuleTestCase.php',
'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistNewlineAfterOpenTagXHPASTLinterRule.php',
'ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase.php',
'ArcanistNoEffectException' => 'exception/usage/ArcanistNoEffectException.php',
@ -598,6 +600,8 @@ phutil_register_library_map(array(
'ArcanistModifierOrderingXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistNamingConventionsXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistNamingConventionsXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistNestedNamespacesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistNestedNamespacesXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistNewlineAfterOpenTagXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistNewlineAfterOpenTagXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase',
'ArcanistNoEffectException' => 'ArcanistUsageException',

View file

@ -0,0 +1,29 @@
<?php
final class ArcanistNestedNamespacesXHPASTLinterRule
extends ArcanistXHPASTLinterRule {
const ID = 90;
public function getLintName() {
return pht('Nested `%s` Statements', 'namespace');
}
public function process(XHPASTNode $root) {
$namespaces = $root->selectDescendantsOfType('n_NAMESPACE');
foreach ($namespaces as $namespace) {
$nested_namespaces = $namespace->selectDescendantsOfType('n_NAMESPACE');
foreach ($nested_namespaces as $nested_namespace) {
$this->raiseLintAtNode(
$nested_namespace,
pht(
'`%s` declarations cannot be nested. '.
'This construct will cause a PHP fatal error.',
'namespace'));
}
}
}
}

View file

@ -0,0 +1,10 @@
<?php
final class ArcanistNestedNamespacesXHPASTLinterRuleTestCase
extends ArcanistXHPASTLinterRuleTestCase {
public function testLinter() {
$this->executeTestsInDirectory(dirname(__FILE__).'/nested-namespaces/');
}
}

View file

@ -0,0 +1,9 @@
<?php
namespace X {
namespace Y {
namespace Z {}
}
}
~~~~~~~~~~
error:3:3
error:4:5