mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-22 23:02:41 +01:00
Improve linter rules for array formatting
Summary: Modify `ArcanistParenthesesSpacingXHPASTLinterRule` and `ArcanistCallParenthesesXHPASTLinterRule` to apply to `array()` and `list()` as well. Depends on D13858. Test Plan: Added unit tests. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin Differential Revision: https://secure.phabricator.com/D13859
This commit is contained in:
parent
b82e4cd40e
commit
31a4919680
4 changed files with 60 additions and 19 deletions
16
src/lint/linter/__tests__/phlxhp/array-formatting.lint-test
Normal file
16
src/lint/linter/__tests__/phlxhp/array-formatting.lint-test
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
array ( 1, 2, 3 );
|
||||||
|
list ( $x, $y ) = array();
|
||||||
|
~~~~~~~~~~
|
||||||
|
warning:3:6
|
||||||
|
warning:3:8
|
||||||
|
warning:3:16
|
||||||
|
warning:4:5
|
||||||
|
warning:4:7
|
||||||
|
warning:4:14
|
||||||
|
~~~~~~~~~~
|
||||||
|
<?php
|
||||||
|
|
||||||
|
array(1, 2, 3);
|
||||||
|
list($x, $y) = array();
|
|
@ -14,13 +14,33 @@ final class ArcanistCallParenthesesXHPASTLinterRule
|
||||||
}
|
}
|
||||||
|
|
||||||
public function process(XHPASTNode $root) {
|
public function process(XHPASTNode $root) {
|
||||||
$calls = $root->selectDescendantsOfTypes(array(
|
$nodes = $root->selectDescendantsOfTypes(array(
|
||||||
|
'n_ARRAY_LITERAL',
|
||||||
'n_FUNCTION_CALL',
|
'n_FUNCTION_CALL',
|
||||||
'n_METHOD_CALL',
|
'n_METHOD_CALL',
|
||||||
|
'n_LIST',
|
||||||
));
|
));
|
||||||
|
|
||||||
foreach ($calls as $call) {
|
foreach ($nodes as $node) {
|
||||||
$params = $call->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
switch ($node->getTypeName()) {
|
||||||
|
case 'n_ARRAY_LITERAL':
|
||||||
|
$params = $node->getChildOfType(0, 'n_ARRAY_VALUE_LIST');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'n_FUNCTION_CALL':
|
||||||
|
case 'n_METHOD_CALL':
|
||||||
|
$params = $node->getChildOfType(1, 'n_CALL_PARAMETER_LIST');
|
||||||
|
break;
|
||||||
|
|
||||||
|
case 'n_LIST':
|
||||||
|
$params = $node->getChildOfType(0, 'n_ASSIGNMENT_LIST');
|
||||||
|
break;
|
||||||
|
|
||||||
|
default:
|
||||||
|
throw new Exception(
|
||||||
|
pht("Unexpected node of type '%s'!", $node->getTypeName()));
|
||||||
|
}
|
||||||
|
|
||||||
$tokens = $params->getTokens();
|
$tokens = $params->getTokens();
|
||||||
$first = head($tokens);
|
$first = head($tokens);
|
||||||
|
|
||||||
|
@ -29,17 +49,13 @@ final class ArcanistCallParenthesesXHPASTLinterRule
|
||||||
if (preg_match('/^\s+$/', $leading_text)) {
|
if (preg_match('/^\s+$/', $leading_text)) {
|
||||||
$this->raiseLintAtOffset(
|
$this->raiseLintAtOffset(
|
||||||
$first->getOffset() - strlen($leading_text),
|
$first->getOffset() - strlen($leading_text),
|
||||||
pht('Convention: no spaces before opening parenthesis in calls.'),
|
pht('Convention: no spaces before opening parentheses.'),
|
||||||
$leading_text,
|
$leading_text,
|
||||||
'');
|
'');
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
foreach ($calls as $call) {
|
|
||||||
// If the last parameter of a call is a HEREDOC, don't apply this rule.
|
// If the last parameter of a call is a HEREDOC, don't apply this rule.
|
||||||
$params = $call
|
$params = $params->getChildren();
|
||||||
->getChildOfType(1, 'n_CALL_PARAMETER_LIST')
|
|
||||||
->getChildren();
|
|
||||||
|
|
||||||
if ($params) {
|
if ($params) {
|
||||||
$last_param = last($params);
|
$last_param = last($params);
|
||||||
|
@ -48,15 +64,19 @@ final class ArcanistCallParenthesesXHPASTLinterRule
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$tokens = $call->getTokens();
|
$tokens = $node->getTokens();
|
||||||
$last = array_pop($tokens);
|
$last = array_pop($tokens);
|
||||||
|
|
||||||
|
if ($node->getTypeName() == 'n_ARRAY_LITERAL') {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
|
||||||
$trailing = $last->getNonsemanticTokensBefore();
|
$trailing = $last->getNonsemanticTokensBefore();
|
||||||
$trailing_text = implode('', mpull($trailing, 'getValue'));
|
$trailing_text = implode('', mpull($trailing, 'getValue'));
|
||||||
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),
|
||||||
pht('Convention: no spaces before closing parenthesis in calls.'),
|
pht('Convention: no spaces before closing parentheses.'),
|
||||||
$trailing_text,
|
$trailing_text,
|
||||||
'');
|
'');
|
||||||
}
|
}
|
||||||
|
|
|
@ -15,11 +15,13 @@ final class ArcanistParenthesesSpacingXHPASTLinterRule
|
||||||
|
|
||||||
public function process(XHPASTNode $root) {
|
public function process(XHPASTNode $root) {
|
||||||
$all_paren_groups = $root->selectDescendantsOfTypes(array(
|
$all_paren_groups = $root->selectDescendantsOfTypes(array(
|
||||||
|
'n_ARRAY_VALUE_LIST',
|
||||||
|
'n_ASSIGNMENT_LIST',
|
||||||
'n_CALL_PARAMETER_LIST',
|
'n_CALL_PARAMETER_LIST',
|
||||||
|
'n_DECLARATION_PARAMETER_LIST',
|
||||||
'n_CONTROL_CONDITION',
|
'n_CONTROL_CONDITION',
|
||||||
'n_FOR_EXPRESSION',
|
'n_FOR_EXPRESSION',
|
||||||
'n_FOREACH_EXPRESSION',
|
'n_FOREACH_EXPRESSION',
|
||||||
'n_DECLARATION_PARAMETER_LIST',
|
|
||||||
));
|
));
|
||||||
|
|
||||||
foreach ($all_paren_groups as $group) {
|
foreach ($all_paren_groups as $group) {
|
||||||
|
|
|
@ -97,6 +97,7 @@ final class ArcanistConfigurationDrivenUnitTestEngine
|
||||||
$ex);
|
$ex);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ($all_paths) {
|
||||||
$include = (array)idx($spec, 'include', array());
|
$include = (array)idx($spec, 'include', array());
|
||||||
$exclude = (array)idx($spec, 'exclude', array());
|
$exclude = (array)idx($spec, 'exclude', array());
|
||||||
$paths = $this->matchPaths(
|
$paths = $this->matchPaths(
|
||||||
|
@ -105,6 +106,8 @@ final class ArcanistConfigurationDrivenUnitTestEngine
|
||||||
$exclude);
|
$exclude);
|
||||||
|
|
||||||
$test_engine->setPaths($paths);
|
$test_engine->setPaths($paths);
|
||||||
|
}
|
||||||
|
|
||||||
$built_test_engines[] = $test_engine;
|
$built_test_engines[] = $test_engine;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue