1
0
Fork 0
mirror of https://we.phorge.it/source/arcanist.git synced 2024-11-22 06:42:41 +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:
Joshua Spence 2015-08-08 23:30:24 +10:00
parent c304c4e045
commit 9e65fc6516
9 changed files with 79 additions and 37 deletions

View file

@ -1,8 +1,8 @@
<?php <?php
class MyClass { class MyClass {
public function myfunc($var) { public function myfunc($var) {
echo $var; echo $var;
} }
} }
$myvar = true; $myvar = true;
@ -21,7 +21,7 @@ sprintf('0%o', 0777 & $p);
$foo(&$myvar); $foo(&$myvar);
array_walk(array(), function() use (&$x) {}); array_walk(array(), function () use (&$x) {});
MyClass::myfunc(array(&$x, &$y)); MyClass::myfunc(array(&$x, &$y));
~~~~~~~~~~ ~~~~~~~~~~
error:2:7 XHP19 error:2:7 XHP19

View file

@ -20,9 +20,10 @@ final class X {
} }
f(function($x) {}); f(function ($x) {});
f(function($x ) {}); f(function ($x ) {});
f(function($x ) use ($z) {}); f(function ($x ) use ($z) {});
f(function ($x)use($z) {});
~~~~~~~~~~ ~~~~~~~~~~
warning:4:14 warning:4:14
warning:5:11 warning:5:11
@ -31,8 +32,10 @@ error:10:13
warning:13:23 warning:13:23
warning:16:31 warning:16:31
warning:19:33 warning:19:33
warning:24:14 warning:24:15
warning:25:14 warning:25:15
warning:26:16
warning:26:19
~~~~~~~~~~ ~~~~~~~~~~
<?php <?php
@ -56,6 +59,7 @@ final class X {
} }
f(function($x) {}); f(function ($x) {});
f(function($x) {}); f(function ($x) {});
f(function($x) use ($z) {}); f(function ($x) use ($z) {});
f(function ($x) use ($z) {});

View file

@ -8,7 +8,7 @@ function outer() {
// Closures are allowed. // Closures are allowed.
function my_func($foo) { function my_func($foo) {
function() {}; function () {};
} }
~~~~~~~~~~ ~~~~~~~~~~
warning:5:5 warning:5:5

View file

@ -21,7 +21,7 @@ final class Quack {
function() use ($this_is_a_closure) {}; function () use ($this_is_a_closure) {};
function f(&$YY) {} function f(&$YY) {}

View file

@ -2,9 +2,8 @@
namespace a; namespace a;
use b, c; use b, c;
f(function() {}); f(function () {});
g(function() use ($c) {}); g(function () use ($c) {});
h(function /* ! */ () use ($c) {});
static::m(); static::m();
1 ? 1 : 2; 1 ? 1 : 2;
1 ?: 2; 1 ?: 2;
@ -16,10 +15,9 @@ error:3:1
error:4:5 error:4:5
error:5:3 error:5:3
error:6:3 error:6:3
error:7:3 error:7:1
error:8:1 error:9:1
error:10:1 error:12:6
error:13:6
~~~~~~~~~~ ~~~~~~~~~~
~~~~~~~~~~ ~~~~~~~~~~
{ {

View file

@ -90,13 +90,13 @@ function variable_variable() {
function closure1() { function closure1() {
$ar = array(); $ar = array();
foreach ($ar as &$a) {} foreach ($ar as &$a) {}
function($a) { function ($a) {
$a++; $a++;
}; };
} }
function closure2() { function closure2() {
function() { function () {
$ar = array(); $ar = array();
foreach ($ar as &$a) {} foreach ($ar as &$a) {}
}; };
@ -104,7 +104,7 @@ function closure2() {
} }
function closure3() { function closure3() {
function() { function () {
$ar = array(); $ar = array();
foreach ($ar as &$a) {} foreach ($ar as &$a) {}
$a++; // Reuse of $a $a++; // Reuse of $a
@ -114,7 +114,7 @@ function closure3() {
function closure4() { function closure4() {
$ar = array(); $ar = array();
foreach ($ar as &$a) {} foreach ($ar as &$a) {}
function($a) { function ($a) {
unset($a); unset($a);
}; };
$a++; // Reuse of $a $a++; // Reuse of $a

View file

@ -65,7 +65,7 @@ switch ($x) {
} }
case 4: case 4:
function f() { throw new Exception(); } function f() { throw new Exception(); }
g(function() { return; }); g(function () { return; });
final class C { public function m() { return; } } final class C { public function m() { return; } }
interface I {} interface I {}
case 5: case 5:

View file

@ -1,6 +1,6 @@
<?php <?php
function() use ($c) { function () use ($c) {
$c++; $c++;
}; };
@ -173,7 +173,7 @@ function catchy() {
} }
function some_func($x, $y) { function some_func($x, $y) {
$func = function($z) use ($x) { $func = function ($z) use ($x) {
echo $x; echo $x;
echo $y; echo $y;
echo $z; echo $z;

View file

@ -24,7 +24,7 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
$tokens = $params->getTokens(); $tokens = $params->getTokens();
$first = head($tokens); $first = head($tokens);
$last = last($tokens); $last = last($tokens);
$leading = $first->getNonsemanticTokensBefore(); $leading = $first->getNonsemanticTokensBefore();
$leading_text = implode('', mpull($leading, 'getValue')); $leading_text = implode('', mpull($leading, 'getValue'));
@ -32,14 +32,27 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
$trailing = $last->getNonsemanticTokensBefore(); $trailing = $last->getNonsemanticTokensBefore();
$trailing_text = implode('', mpull($trailing, 'getValue')); $trailing_text = implode('', mpull($trailing, 'getValue'));
if (preg_match('/^\s+$/', $leading_text)) { if ($dec->getChildByIndex(2)->getTypeName() == 'n_EMPTY') {
$this->raiseLintAtOffset( // Anonymous functions.
$first->getOffset() - strlen($leading_text), if ($leading_text != ' ') {
pht( $this->raiseLintAtOffset(
'Convention: no spaces before opening parenthesis in '. $first->getOffset() - strlen($leading_text),
'function and method declarations.'), pht(
$leading_text, '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),
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)) {
@ -51,6 +64,33 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
$trailing_text, $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'),
'',
' ');
}
}
} }
} }