]> khamidou — GitHub mirrors - arcanist.git/commitdiff
Improve declaration formatting linter rule for anonymous functions
authorJoshua Spence <josh@joshuaspence.com>
Sat, 8 Aug 2015 13:30:24 +0000 (23:30 +1000)
committerJoshua Spence <josh@joshuaspence.com>
Sat, 8 Aug 2015 23:13:57 +0000 (09:13 +1000)
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

src/lint/linter/__tests__/xhpast/call-time-pass-by-reference.lint-test
src/lint/linter/__tests__/xhpast/decl-parens-hug-closing.lint-test
src/lint/linter/__tests__/xhpast/inner-function.lint-test
src/lint/linter/__tests__/xhpast/naming-conventions.lint-test
src/lint/linter/__tests__/xhpast/php53-features.lint-test
src/lint/linter/__tests__/xhpast/reused-iterator-reference.lint-test
src/lint/linter/__tests__/xhpast/switches.lint-test
src/lint/linter/__tests__/xhpast/undeclared-variables.lint-test
src/lint/linter/xhpast/rules/ArcanistDeclarationParenthesesXHPASTLinterRule.php

index 6aacffdb08d3b8365b0960be1680b7fce17fcdfa..0f03a7742bd6630c310f6a4261109f2e6479a057 100644 (file)
@@ -1,8 +1,8 @@
 <?php
 class MyClass {
-    public function myfunc($var) {
-        echo $var;
-    }
+  public function myfunc($var) {
+    echo $var;
+  }
 }
 
 $myvar = true;
@@ -21,7 +21,7 @@ sprintf('0%o', 0777 & $p);
 
 $foo(&$myvar);
 
-array_walk(array(), function() use (&$x) {});
+array_walk(array(), function () use (&$x) {});
 MyClass::myfunc(array(&$x, &$y));
 ~~~~~~~~~~
 error:2:7 XHP19
index c6accb1521622130bae37ee4dc23391db27dce30..be9438e325b6b592b4d918c4c003dfc3e89744bb 100644 (file)
@@ -20,9 +20,10 @@ final class X {
 
 }
 
-f(function($x) {});
-f(function($x ) {});
-f(function($x ) use ($z) {});
+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
 
@@ -56,6 +59,7 @@ final class X {
 
 }
 
-f(function($x) {});
-f(function($x) {});
-f(function($x) use ($z) {});
+f(function ($x) {});
+f(function ($x) {});
+f(function ($x) use ($z) {});
+f(function ($x) use ($z) {});
index 38d0ca15eae3a963a1d5b2934beab8d629a677e2..6294eb0aed7263fcde244e8d74dc70edfba04fde 100644 (file)
@@ -8,7 +8,7 @@ function outer() {
 
 // Closures are allowed.
 function my_func($foo) {
-  function() {};
+  function () {};
 }
 ~~~~~~~~~~
 warning:5:5
index 3bf1a71ad64421b61f38d629496264e129959955..cc78e403ebc82ce7ad8ed8947a77ac678d801075 100644 (file)
@@ -21,7 +21,7 @@ final class Quack {
 
 
 
-function() use ($this_is_a_closure) {};
+function () use ($this_is_a_closure) {};
 
 function f(&$YY) {}
 
index 6417a56d92ab147f0112d2482f6a7cb0ec05c330..a22372938f6a3c9e0f6dbef431389f9ac286929e 100644 (file)
@@ -2,9 +2,8 @@
 
 namespace a;
 use b, c;
-f(function() {});
-g(function() use ($c) {});
-h(function /* ! */ () use ($c) {});
+f(function () {});
+g(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
 ~~~~~~~~~~
 ~~~~~~~~~~
 {
index cbfa13b159e4766685c38c9defe901199ff612b0..ac65ff57fa8a13b8174ebe2515acfa9a5789b124 100644 (file)
@@ -90,13 +90,13 @@ function variable_variable() {
 function closure1() {
   $ar = array();
   foreach ($ar as &$a) {}
-  function($a) {
+  function ($a) {
     $a++;
   };
 }
 
 function closure2() {
-  function() {
+  function () {
     $ar = array();
     foreach ($ar as &$a) {}
   };
@@ -104,7 +104,7 @@ function closure2() {
 }
 
 function closure3() {
-  function() {
+  function () {
     $ar = array();
     foreach ($ar as &$a) {}
     $a++; // Reuse of $a
@@ -114,7 +114,7 @@ function closure3() {
 function closure4() {
   $ar = array();
   foreach ($ar as &$a) {}
-  function($a) {
+  function ($a) {
     unset($a);
   };
   $a++; // Reuse of $a
index 89e97069c73d2b0615541d9cd62eb2481c7f8ab4..6c6fa9446c0011b5dc04ed751c213a232e3ec553 100644 (file)
@@ -65,7 +65,7 @@ switch ($x) {
     }
   case 4:
     function f() { throw new Exception(); }
-    g(function() { return; });
+    g(function () { return; });
     final class C { public function m() { return; } }
     interface I {}
   case 5:
index 903e620a4aa459d8923664c82114ba6357ddd6c8..c6cd1b4081fc8f7e3ddab08cf21d59cb995a5b56 100644 (file)
@@ -1,6 +1,6 @@
 <?php
 
-function() use ($c) {
+function () use ($c) {
   $c++;
 };
 
@@ -173,7 +173,7 @@ function catchy() {
 }
 
 function some_func($x, $y) {
-  $func = function($z) use ($x) {
+  $func = function ($z) use ($x) {
     echo $x;
     echo $y;
     echo $z;
index 357ac77f9ddf223cdf63dea55d3119cdaab2143d..6606b0b30469fd7244f46ad0931b3053bb58a53f 100644 (file)
@@ -24,7 +24,7 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
       $tokens = $params->getTokens();
 
       $first = head($tokens);
-      $last = last($tokens);
+      $last  = last($tokens);
 
       $leading = $first->getNonsemanticTokensBefore();
       $leading_text = implode('', mpull($leading, 'getValue'));
@@ -32,14 +32,27 @@ final class ArcanistDeclarationParenthesesXHPASTLinterRule
       $trailing = $last->getNonsemanticTokensBefore();
       $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 ($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),
+            pht(
+              'Convention: no spaces before opening parenthesis in '.
+              'function and method declarations.'),
+            $leading_text,
+            '');
+        }
       }
 
       if (preg_match('/^\s+$/', $trailing_text)) {
@@ -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'),
+            '',
+            ' ');
+        }
+      }
     }
   }