Skip to content

Commit

Permalink
Add hasLeadingNewline attribute to InlineHTML
Browse files Browse the repository at this point in the history
Use this attribute to not print an extra newline if the original
code did not have it.
  • Loading branch information
nikic committed Jul 25, 2016
1 parent 21b18eb commit ec614c9
Show file tree
Hide file tree
Showing 7 changed files with 39 additions and 8 deletions.
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
Version 3.0.0-dev
-----------------

Nothing yet.
### Added

* The `InlineHTML` node now has an `hasLeadingNewline` attribute, that specifies whether the
preceding closing tag contained a newline. The pretty printer honors this attribute.

Version 3.0.0-alpha1 (2016-07-25)
---------------------------------
Expand Down
10 changes: 10 additions & 0 deletions lib/PhpParser/Lexer.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ class Lexer
protected $pos;
protected $line;
protected $filePos;
protected $prevCloseTagHasNewline;

protected $tokenMap;
protected $dropTokens;
Expand Down Expand Up @@ -67,6 +68,10 @@ public function startLexing($code) {
$this->pos = -1;
$this->line = 1;
$this->filePos = 0;

// If inline HTML occurs without preceding code, treat it as if it had a leading newline.
// This ensures proper composability, because having a newline is the "safe" assumption.
$this->prevCloseTagHasNewline = true;
}

protected function resetErrors() {
Expand Down Expand Up @@ -166,6 +171,11 @@ public function getNextToken(&$value = null, &$startAttributes = null, &$endAttr
} elseif (!isset($this->dropTokens[$token[0]])) {
$value = $token[1];
$id = $this->tokenMap[$token[0]];
if (T_CLOSE_TAG === $token[0]) {
$this->prevCloseTagHasNewline = false !== strpos($token[1], "\n");
} else if (T_INLINE_HTML === $token[0]) {
$startAttributes['hasLeadingNewline'] = $this->prevCloseTagHasNewline;
}

$this->line += substr_count($value, "\n");
$this->filePos += \strlen($value);
Expand Down
3 changes: 2 additions & 1 deletion lib/PhpParser/PrettyPrinter/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -789,7 +789,8 @@ protected function pStmt_Unset(Stmt\Unset_ $node) {
}

protected function pStmt_InlineHTML(Stmt\InlineHTML $node) {
return '?>' . $this->pNoIndent("\n" . $node->value) . '<?php ';
$newline = $node->getAttribute('hasLeadingNewline', true) ? "\n" : '';
return '?>' . $this->pNoIndent($newline . $node->value) . '<?php ';
}

protected function pStmt_HaltCompiler(Stmt\HaltCompiler $node) {
Expand Down
3 changes: 2 additions & 1 deletion test/PhpParser/LexerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public function provideTestLex() {
),
array(
Tokens::T_INLINE_HTML, 'plaintext',
array('startLine' => 1), array('endLine' => 1)
array('startLine' => 1, 'hasLeadingNewline' => false),
array('endLine' => 1)
),
)
),
Expand Down
8 changes: 5 additions & 3 deletions test/PhpParser/ParserTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -110,9 +110,9 @@ public function testInvalidToken() {
}

/**
* @dataProvider provideTestKindAttributes
* @dataProvider provideTestExtraAttributes
*/
public function testKindAttributes($code, $expectedAttributes) {
public function testExtraAttributes($code, $expectedAttributes) {
$parser = $this->getParser(new Lexer);
$stmts = $parser->parse("<?php $code;");
$attributes = $stmts[0]->getAttributes();
Expand All @@ -121,7 +121,7 @@ public function testKindAttributes($code, $expectedAttributes) {
}
}

public function provideTestKindAttributes() {
public function provideTestExtraAttributes() {
return array(
array('0', ['kind' => Scalar\LNumber::KIND_DEC]),
array('9', ['kind' => Scalar\LNumber::KIND_DEC]),
Expand Down Expand Up @@ -158,6 +158,8 @@ public function provideTestKindAttributes() {
array("die('done')", ['kind' => Expr\Exit_::KIND_DIE]),
array("exit", ['kind' => Expr\Exit_::KIND_EXIT]),
array("exit(1)", ['kind' => Expr\Exit_::KIND_EXIT]),
array("?>Foo", ['hasLeadingNewline' => false]),
array("?>\nFoo", ['hasLeadingNewline' => true]),
);
}
}
Expand Down
8 changes: 7 additions & 1 deletion test/code/prettyPrinter/inlineHTMLandPHPtest.file-test
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,10 @@ HTML
<?php
echo 'PHP';
?>
HTML
HTML
-----
HTML<?php echo 'PHP'; ?>HTML
-----
HTML<?php
echo 'PHP';
?>HTML
10 changes: 9 additions & 1 deletion test/code/prettyPrinter/onlyInlineHTML.file-test
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ World Hallo
Hallo World
Foo Bar
Bar Foo
World Hallo
World Hallo
-----


Test
-----


Test

0 comments on commit ec614c9

Please sign in to comment.