Skip to content
Browse files

Fixed closure indentation (fixes #3)

  • Loading branch information...
1 parent 087203c commit fc0201caa340bd16f1055276bf2d85f4d8d8b5e5 @jmalloc jmalloc committed Jan 14, 2013
Showing with 43 additions and 8 deletions.
  1. +3 −2 lib/Icecave/Rasta/Renderer.php
  2. +40 −6 test/suite/Icecave/Rasta/RendererTest.php
View
5 lib/Icecave/Rasta/Renderer.php
@@ -541,8 +541,9 @@ public function visitClosure(Closure $node)
$use = ' use ' . '(' . implode(', ', $closureParameters) . ')';
}
- $code = $this->indent('function (' . implode(', ', $parameters) . ')' . $use . PHP_EOL);
- $code .= $node->statementBlock()->accept($this);
+ $code = 'function (' . implode(', ', $parameters) . ')' . $use . ' {' . PHP_EOL;
+ $code .= $this->visitChildren($node->statementBlock());
+ $code .= '}';
return $code;
}
View
46 test/suite/Icecave/Rasta/RendererTest.php
@@ -3,6 +3,7 @@
use Icecave\Pasta\AST\ContentBlock;
use Icecave\Pasta\AST\Expr\ArrayLiteral;
+use Icecave\Pasta\AST\Expr\Assign;
use Icecave\Pasta\AST\Expr\Association;
use Icecave\Pasta\AST\Expr\Call;
use Icecave\Pasta\AST\Expr\Constant;
@@ -40,8 +41,8 @@
use Icecave\Pasta\AST\Type\ClassDefinition;
use Icecave\Pasta\AST\Type\ClassModifier;
use Icecave\Pasta\AST\Type\ConcreteMethod;
-use Icecave\Pasta\AST\Type\Property;
use Icecave\Pasta\AST\Type\InterfaceDefinition;
+use Icecave\Pasta\AST\Type\Property;
use PHPUnit_Framework_TestCase;
use ReflectionClass;
@@ -766,11 +767,14 @@ public function testVisitClosure()
{
$node = new Closure;
$node->addParameter(new Parameter(new Identifier('param')));
+ $node->statementBlock()->add(
+ new ExpressionStatement(new Call(QualifiedIdentifier::fromString('foo')))
+ );
$code = $node->accept($this->_renderer);
- $expected = 'function ($param)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
- $expected .= '}' . PHP_EOL;
+ $expected = 'function ($param) {' . PHP_EOL;
+ $expected .= 'foo();' . PHP_EOL;
+ $expected .= '}';
$this->assertSame($expected, $code);
}
@@ -781,8 +785,38 @@ public function testVisitClosureWithUse()
$node->addClosureParameter(new Parameter(new Identifier('closureParam')));
$code = $node->accept($this->_renderer);
- $expected = 'function ($param) use ($closureParam)' . PHP_EOL;
- $expected .= '{' . PHP_EOL;
+ $expected = 'function ($param) use ($closureParam) {' . PHP_EOL;
+ $expected .= '}';
+ $this->assertSame($expected, $code);
+ }
+
+ /**
+ * @group regression
+ * @link https://github.com/IcecaveStudios/rasta/issues/3
+ */
+ public function testVisitClosureIndenting()
+ {
+ $closure = new Closure;
+ $closure->statementBlock()->add(
+ new ExpressionStatement(new Call(QualifiedIdentifier::fromString('foo')))
+ );
+
+ $node = new StatementBlock;
+ $node->add(
+ new ExpressionStatement(
+ new Assign(
+ new Variable(new Identifier('target')),
+ $closure
+ )
+ )
+ );
+
+ $code = $node->accept($this->_renderer);
+
+ $expected = '{' . PHP_EOL;
+ $expected .= ' ($target = function () {' . PHP_EOL;
+ $expected .= ' foo();' . PHP_EOL;
+ $expected .= ' });' . PHP_EOL;
$expected .= '}' . PHP_EOL;
$this->assertSame($expected, $code);
}

0 comments on commit fc0201c

Please sign in to comment.
Something went wrong with that request. Please try again.