mirror of
https://we.phorge.it/source/arcanist.git
synced 2024-11-25 16:22:42 +01:00
Improve declaration formatting linter rule for anonymous functions
Summary: Ref T8742. Anonymous functions should have a space after the `function` keyword. Additionally, ensure that there is a space before and after the `use` token. Test Plan: Modified existing test cases. Reviewers: epriestley, #blessed_reviewers Reviewed By: epriestley, #blessed_reviewers Subscribers: Korvin Maniphest Tasks: T8742 Differential Revision: https://secure.phabricator.com/D13804
This commit is contained in:
parent
c304c4e045
commit
9e65fc6516
9 changed files with 79 additions and 37 deletions
|
@ -23,6 +23,7 @@ final class X {
|
|||
f(function ($x) {});
|
||||
f(function ($x ) {});
|
||||
f(function ($x ) use ($z) {});
|
||||
f(function ($x)use($z) {});
|
||||
~~~~~~~~~~
|
||||
warning:4:14
|
||||
warning:5:11
|
||||
|
@ -31,8 +32,10 @@ error:10:13
|
|||
warning:13:23
|
||||
warning:16:31
|
||||
warning:19:33
|
||||
warning:24:14
|
||||
warning:25:14
|
||||
warning:24:15
|
||||
warning:25:15
|
||||
warning:26:16
|
||||
warning:26:19
|
||||
~~~~~~~~~~
|
||||
<?php
|
||||
|
||||
|
@ -59,3 +62,4 @@ final class X {
|
|||
f(function ($x) {});
|
||||
f(function ($x) {});
|
||||
f(function ($x) use ($z) {});
|
||||
f(function ($x) use ($z) {});
|
||||
|
|
|
@ -4,7 +4,6 @@ namespace a;
|
|||
use b, c;
|
||||
f(function () {});
|
||||
g(function () use ($c) {});
|
||||
h(function /* ! */ () use ($c) {});
|
||||
static::m();
|
||||
1 ? 1 : 2;
|
||||
1 ?: 2;
|
||||
|
@ -16,10 +15,9 @@ error:3:1
|
|||
error:4:5
|
||||
error:5:3
|
||||
error:6:3
|
||||
error:7:3
|
||||
error:8:1
|
||||
error:10:1
|
||||
error:13:6
|
||||
error:7:1
|
||||
error:9:1
|
||||
error:12:6
|
||||
~~~~~~~~~~
|
||||
~~~~~~~~~~
|
||||
{
|
||||
|
|
|
@ -32,6 +32,18 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
|
|||
$trailing = $last->getNonsemanticTokensBefore();
|
||||
$trailing_text = implode('', mpull($trailing, 'getValue'));
|
||||
|
||||
if ($dec->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
|
||||
// Anonymous functions.
|
||||
if ($leading_text != ' ') {
|
||||
$this->raiseLintAtOffset(
|
||||
$first->getOffset() - strlen($leading_text),
|
||||
pht(
|
||||
'Convention: space before opening parenthesis in '.
|
||||
'anonymous function declarations.'),
|
||||
$leading_text,
|
||||
' ');
|
||||
}
|
||||
} else {
|
||||
if (preg_match('/^\s+$/', $leading_text)) {
|
||||
$this->raiseLintAtOffset(
|
||||
$first->getOffset() - strlen($leading_text),
|
||||
|
@ -41,6 +53,7 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
|
|||
$leading_text,
|
||||
'');
|
||||
}
|
||||
}
|
||||
|
||||
if (preg_match('/^\s+$/', $trailing_text)) {
|
||||
$this->raiseLintAtOffset(
|
||||
|
@ -51,6 +64,33 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
|
|||
$trailing_text,
|
||||
'');
|
||||
}
|
||||
|
||||
$use_list = $dec->getChildByIndex(4);
|
||||
if ($use_list->getTypeName() == 'n_EMPTY') {
|
||||
continue;
|
||||
}
|
||||
$use_token = $use_list->selectTokensOfType('T_USE');
|
||||
|
||||
foreach ($use_token as $use) {
|
||||
$before = $use->getNonsemanticTokensBefore();
|
||||
$after = $use->getNonsemanticTokensAfter();
|
||||
|
||||
if (!$before) {
|
||||
$this->raiseLintAtOffset(
|
||||
$use->getOffset(),
|
||||
pht('Convention: space before `%s` token.', 'use'),
|
||||
'',
|
||||
' ');
|
||||
}
|
||||
|
||||
if (!$after) {
|
||||
$this->raiseLintAtOffset(
|
||||
$use->getOffset() + strlen($use->getValue()),
|
||||
pht('Convention: space after `%s` token.', 'use'),
|
||||
'',
|
||||
' ');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue