Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fixed closure indenting (fixes #11)

  • Loading branch information...
commit 95fece83a4d1b69383238535b87879825923e1a7 1 parent 66aed8a
@jmalloc jmalloc authored
View
5 lib/Icecave/Rasta/Renderer.php
@@ -554,10 +554,15 @@ public function visitClosure(Closure $node)
$use = ' use ' . '(' . implode(', ', $closureParameters) . ')';
}
+ $indentLevel = $this->indentLevel;
+ $this->indentLevel = 1;
+
$code = 'function (' . implode(', ', $parameters) . ')' . $use . ' {' . $this->newlineString;
$code .= $this->visitChildren($node->statementBlock());
$code .= '}';
+ $this->indentLevel = $indentLevel;
+
return $code;
}
View
43 test/suite/Icecave/Rasta/RendererTest.php
@@ -781,7 +781,7 @@ public function testVisitClosure()
$code = $node->accept($this->_renderer);
$expected = 'function ($param) {' . $this->_newlineString;
- $expected .= 'foo();' . $this->_newlineString;
+ $expected .= ' foo();' . $this->_newlineString;
$expected .= '}';
$this->assertSame($expected, $code);
}
@@ -829,6 +829,47 @@ public function testVisitClosureIndenting()
$this->assertSame($expected, $code);
}
+ /**
+ * @group regression
+ * @link https://github.com/IcecaveStudios/rasta/issues/8
+ */
+ public function testVisitClosureDeepIndenting()
+ {
+ $closure = new Closure;
+ $closure->statementBlock()->add(
+ new ExpressionStatement(new Call(QualifiedIdentifier::fromString('foo')))
+ );
+
+ $innerNode1 = new StatementBlock;
+ $innerNode1->add(
+ new ExpressionStatement(
+ new Assign(
+ new Variable(new Identifier('target')),
+ $closure
+ )
+ )
+ );
+
+ $innerNode2 = new StatementBlock;
+ $innerNode2->add($innerNode1);
+
+ $node = new StatementBlock;
+ $node->add($innerNode2);
+
+ $code = $node->accept($this->_renderer);
+
+ $expected = '{' . $this->_newlineString;
+ $expected .= ' {' . $this->_newlineString;
+ $expected .= ' {' . $this->_newlineString;
+ $expected .= ' $target = function () {' . $this->_newlineString;
+ $expected .= ' foo();' . $this->_newlineString;
+ $expected .= ' };' . $this->_newlineString;
+ $expected .= ' }' . $this->_newlineString;
+ $expected .= ' }' . $this->_newlineString;
+ $expected .= '}' . $this->_newlineString;
+ $this->assertSame($expected, $code);
+ }
+
public function testVisitQualifiedIdentifier()
{
$node = QualifiedIdentifier::fromString('\Foo\Bar');
Please sign in to comment.
Something went wrong with that request. Please try again.