1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 14:52:40 +01:00

Improve the declaration parentheses linting

Summary: Improve `ArcanistClosingDeclarationParenthesesXHPASTLinterRule` (and rename it to `ArcanistDeclarationParenthesesXHPASTLinterRule`) to ensure that there is no whitespace before the opening parenthesis of a function/method declaration.

Test Plan: Added test cases.

Reviewers: epriestley, #blessed_reviewers

Reviewed By: epriestley, #blessed_reviewers

Subscribers: epriestley, Korvin

Differential Revision: https://secure.phabricator.com/D13675
This commit is contained in:
Joshua Spence 2015-07-23 06:43:35 +10:00
parent b7e87e959c
commit e286ef66c8
3 changed files with 31 additions and 13 deletions

View file

@ -43,7 +43,6 @@ phutil_register_library_map(array(
'ArcanistCloseRevisionWorkflow' => 'workflow/ArcanistCloseRevisionWorkflow.php', 'ArcanistCloseRevisionWorkflow' => 'workflow/ArcanistCloseRevisionWorkflow.php',
'ArcanistCloseWorkflow' => 'workflow/ArcanistCloseWorkflow.php', 'ArcanistCloseWorkflow' => 'workflow/ArcanistCloseWorkflow.php',
'ArcanistClosingCallParenthesesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistClosingCallParenthesesXHPASTLinterRule.php', 'ArcanistClosingCallParenthesesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistClosingCallParenthesesXHPASTLinterRule.php',
'ArcanistClosingDeclarationParenthesesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistClosingDeclarationParenthesesXHPASTLinterRule.php',
'ArcanistClosureLinter' => 'lint/linter/ArcanistClosureLinter.php', 'ArcanistClosureLinter' => 'lint/linter/ArcanistClosureLinter.php',
'ArcanistClosureLinterTestCase' => 'lint/linter/__tests__/ArcanistClosureLinterTestCase.php', 'ArcanistClosureLinterTestCase' => 'lint/linter/__tests__/ArcanistClosureLinterTestCase.php',
'ArcanistCoffeeLintLinter' => 'lint/linter/ArcanistCoffeeLintLinter.php', 'ArcanistCoffeeLintLinter' => 'lint/linter/ArcanistCoffeeLintLinter.php',
@ -67,6 +66,7 @@ phutil_register_library_map(array(
'ArcanistCppcheckLinterTestCase' => 'lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php', 'ArcanistCppcheckLinterTestCase' => 'lint/linter/__tests__/ArcanistCppcheckLinterTestCase.php',
'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php', 'ArcanistCpplintLinter' => 'lint/linter/ArcanistCpplintLinter.php',
'ArcanistCpplintLinterTestCase' => 'lint/linter/__tests__/ArcanistCpplintLinterTestCase.php', 'ArcanistCpplintLinterTestCase' => 'lint/linter/__tests__/ArcanistCpplintLinterTestCase.php',
'ArcanistDeclarationParenthesesXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDeclarationParenthesesXHPASTLinterRule.php',
'ArcanistDefaultParametersXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDefaultParametersXHPASTLinterRule.php', 'ArcanistDefaultParametersXHPASTLinterRule' => 'lint/linter/xhpast/rules/ArcanistDefaultParametersXHPASTLinterRule.php',
'ArcanistDiffChange' => 'parser/diff/ArcanistDiffChange.php', 'ArcanistDiffChange' => 'parser/diff/ArcanistDiffChange.php',
'ArcanistDiffChangeType' => 'parser/diff/ArcanistDiffChangeType.php', 'ArcanistDiffChangeType' => 'parser/diff/ArcanistDiffChangeType.php',
@ -316,7 +316,6 @@ phutil_register_library_map(array(
'ArcanistCloseRevisionWorkflow' => 'ArcanistWorkflow', 'ArcanistCloseRevisionWorkflow' => 'ArcanistWorkflow',
'ArcanistCloseWorkflow' => 'ArcanistWorkflow', 'ArcanistCloseWorkflow' => 'ArcanistWorkflow',
'ArcanistClosingCallParenthesesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistClosingCallParenthesesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistClosingDeclarationParenthesesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistClosureLinter' => 'ArcanistExternalLinter', 'ArcanistClosureLinter' => 'ArcanistExternalLinter',
'ArcanistClosureLinterTestCase' => 'ArcanistExternalLinterTestCase', 'ArcanistClosureLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistCoffeeLintLinter' => 'ArcanistExternalLinter', 'ArcanistCoffeeLintLinter' => 'ArcanistExternalLinter',
@ -340,6 +339,7 @@ phutil_register_library_map(array(
'ArcanistCppcheckLinterTestCase' => 'ArcanistExternalLinterTestCase', 'ArcanistCppcheckLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistCpplintLinter' => 'ArcanistExternalLinter', 'ArcanistCpplintLinter' => 'ArcanistExternalLinter',
'ArcanistCpplintLinterTestCase' => 'ArcanistExternalLinterTestCase', 'ArcanistCpplintLinterTestCase' => 'ArcanistExternalLinterTestCase',
'ArcanistDeclarationParenthesesXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDefaultParametersXHPASTLinterRule' => 'ArcanistXHPASTLinterRule', 'ArcanistDefaultParametersXHPASTLinterRule' => 'ArcanistXHPASTLinterRule',
'ArcanistDiffChange' => 'Phobject', 'ArcanistDiffChange' => 'Phobject',
'ArcanistDiffChangeType' => 'Phobject', 'ArcanistDiffChangeType' => 'Phobject',

View file

@ -2,9 +2,10 @@
function f($x) {} function f($x) {}
function g($x ) {} function g($x ) {}
function h ($x) {}
function &h($x) {} function &i($x) {}
function &i($x ) {} function &j($x ) {}
final class X { final class X {
@ -24,21 +25,23 @@ f(function($x ) {});
f(function($x ) use ($z) {}); f(function($x ) use ($z) {});
~~~~~~~~~~ ~~~~~~~~~~
warning:4:14 warning:4:14
warning:7:15 warning:5:11
error:9:13 warning:8:15
warning:12:23 error:10:13
warning:15:31 warning:11:23
warning:18:33 warning:16:31
warning:23:14 warning:19:33
warning:24:14 warning:24:14
warning:25:14
~~~~~~~~~~ ~~~~~~~~~~
<?php <?php
function f($x) {} function f($x) {}
function g($x) {} function g($x) {}
function h($x) {}
function &h($x) {}
function &i($x) {} function &i($x) {}
function &j($x) {}
final class X { final class X {

View file

@ -1,6 +1,6 @@
<?php <?php
final class ArcanistClosingDeclarationParenthesesXHPASTLinterRule final class ArcanistDeclarationParenthesesXHPASTLinterRule
extends ArcanistXHPASTLinterRule { extends ArcanistXHPASTLinterRule {
const ID = 38; const ID = 38;
@ -22,11 +22,26 @@ final class ArcanistClosingDeclarationParenthesesXHPASTLinterRule
foreach ($decs as $dec) { foreach ($decs as $dec) {
$params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST'); $params = $dec->getChildOfType(3, 'n_DECLARATION_PARAMETER_LIST');
$tokens = $params->getTokens(); $tokens = $params->getTokens();
$last = array_pop($tokens);
$first = head($tokens);
$last = last($tokens);
$leading = $first->getNonsemanticTokensBefore();
$leading_text = implode('', mpull($leading, 'getValue'));
$trailing = $last->getNonsemanticTokensBefore(); $trailing = $last->getNonsemanticTokensBefore();
$trailing_text = implode('', mpull($trailing, 'getValue')); $trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^\s+$/', $leading_text)) {
$this->raiseLintAtOffset(
$first->getOffset() - strlen($leading_text),
pht(
'Convention: no spaces before opening parenthesis in '.
'function and method declarations.'),
$leading_text,
'');
}
if (preg_match('/^\s+$/', $trailing_text)) { if (preg_match('/^\s+$/', $trailing_text)) {
$this->raiseLintAtOffset( $this->raiseLintAtOffset(
$last->getOffset() - strlen($trailing_text), $last->getOffset() - strlen($trailing_text),