From: Joshua Spence Date: Mon, 23 Nov 2015 19:42:39 +0000 (+1100) Subject: Add a linter rule for `use` statement namespace prefixes X-Git-Url: http://git.khamidou.com/?a=commitdiff_plain;h=ae210fda9f4bb054ca606ac14e99e9a201130dd2;p=arcanist.git Add a linter rule for `use` statement namespace prefixes Summary: When importing or aliases a symbol with a `use` statement, the leading namespace separator is optional and does not modify the behavior. That is, `use \X` is equivalent to `use X`. As such, the latter syntax should be preferred because it is more concise. According to the [[http://php.net/manual/en/language.namespaces.importing.php | PHP documentation]]: > Note that for namespaced names (fully qualified namespace names containing namespace separator, such as `Foo\Bar` as opposed to global names that do not, such as `FooBar`), the leading backslash is unnecessary and not recommended, as import names must be fully qualified, and are not processed relative to the current namespace. Test Plan: Added test cases. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: epriestley Differential Revision: https://secure.phabricator.com/D14517 --- diff --git a/src/__phutil_library_map__.php b/src/__phutil_library_map__.php index f0f9a8fd..0dee95d0 100644 --- a/src/__phutil_library_map__.php +++ b/src/__phutil_library_map__.php @@ -352,6 +352,8 @@ phutil_register_library_map(array( 'ArcanistUpgradeWorkflow' => 'workflow/ArcanistUpgradeWorkflow.php', 'ArcanistUploadWorkflow' => 'workflow/ArcanistUploadWorkflow.php', 'ArcanistUsageException' => 'exception/ArcanistUsageException.php', + 'ArcanistUseStatementNamespacePrefixXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUseStatementNamespacePrefixXHPASTLinterRule.php', + 'ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase.php', 'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistUselessOverridingMethodXHPASTLinterRule.php', 'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'lint/linter/xhpast/rules/__tests__/ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase.php', 'ArcanistUserAbortException' => 'exception/usage/ArcanistUserAbortException.php', @@ -730,6 +732,8 @@ phutil_register_library_map(array( 'ArcanistUpgradeWorkflow' => 'ArcanistWorkflow', 'ArcanistUploadWorkflow' => 'ArcanistWorkflow', 'ArcanistUsageException' => 'Exception', + 'ArcanistUseStatementNamespacePrefixXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', + 'ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUselessOverridingMethodXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistUselessOverridingMethodXHPASTLinterRuleTestCase' => 'ArcanistXHPASTLinterRuleTestCase', 'ArcanistUserAbortException' => 'ArcanistUsageException', diff --git a/src/lint/linter/xhpast/rules/ArcanistUseStatementNamespacePrefixXHPASTLinterRule.php b/src/lint/linter/xhpast/rules/ArcanistUseStatementNamespacePrefixXHPASTLinterRule.php new file mode 100644 index 00000000..d9c10892 --- /dev/null +++ b/src/lint/linter/xhpast/rules/ArcanistUseStatementNamespacePrefixXHPASTLinterRule.php @@ -0,0 +1,38 @@ +selectDescendantsOfType('n_USE_LIST'); + + foreach ($use_lists as $use_list) { + $uses = $use_list->selectDescendantsOfType('n_USE'); + + foreach ($uses as $use) { + $symbol = $use->getChildOfType(0, 'n_SYMBOL_NAME'); + $symbol_name = $symbol->getConcreteString(); + + if ($symbol_name[0] == '\\') { + $this->raiseLintAtNode( + $symbol, + pht( + 'Imported symbols should not be prefixed with `%s`.', + '\\'), + substr($symbol_name, 1)); + } + } + } + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase.php b/src/lint/linter/xhpast/rules/__tests__/ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase.php new file mode 100644 index 00000000..f8341687 --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/ArcanistUseStatementNamespacePrefixXHPASTLinterRuleTestCase.php @@ -0,0 +1,11 @@ +executeTestsInDirectory( + dirname(__FILE__).'/use-statement-namespace-prefix/'); + } + +} diff --git a/src/lint/linter/xhpast/rules/__tests__/use-statement-namespace-prefix/use.lint-test b/src/lint/linter/xhpast/rules/__tests__/use-statement-namespace-prefix/use.lint-test new file mode 100644 index 00000000..d4d7e63b --- /dev/null +++ b/src/lint/linter/xhpast/rules/__tests__/use-statement-namespace-prefix/use.lint-test @@ -0,0 +1,11 @@ +