Skip to content

Commit

Permalink
Bail out on PHP tags in removed code
Browse files Browse the repository at this point in the history
If dropping a node would drop PHP tags, bail out of formatting
preservation. This will lose formatting, but at least produce
legal code.

Closes GH-884.

(cherry picked from commit b0edd4c)
  • Loading branch information
nikic committed Nov 12, 2022
1 parent a4fe65b commit 2e11dee
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
5 changes: 5 additions & 0 deletions lib/PhpParser/Internal/TokenStream.php
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,11 @@ public function haveBracesInRange(int $startPos, int $endPos) {
|| $this->haveTokenInRange($startPos, $endPos, '}');
}

public function haveTagInRange(int $startPos, int $endPos): bool {
return $this->haveTokenInRange($startPos, $endPos, \T_OPEN_TAG)
|| $this->haveTokenInRange($startPos, $endPos, \T_CLOSE_TAG);
}

/**
* Get indentation before token position.
*
Expand Down
6 changes: 4 additions & 2 deletions lib/PhpParser/PrettyPrinterAbstract.php
Original file line number Diff line number Diff line change
Expand Up @@ -774,7 +774,8 @@ protected function pArray(
}

if ($skipRemovedNode) {
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) ||
$this->origTokens->haveTagInRange($pos, $itemStartPos))) {
// We'd remove the brace of a code block.
// TODO: Preserve formatting.
$this->setIndentLevel($origIndentLevel);
Expand Down Expand Up @@ -877,7 +878,8 @@ protected function pArray(
$pos, $itemStartPos, $indentAdjustment);
$skipRemovedNode = true;
} else {
if ($isStmtList && $this->origTokens->haveBracesInRange($pos, $itemStartPos)) {
if ($isStmtList && ($this->origTokens->haveBracesInRange($pos, $itemStartPos) ||
$this->origTokens->haveTagInRange($pos, $itemStartPos))) {
// We'd remove the brace of a code block.
// TODO: Preserve formatting.
return null;
Expand Down
28 changes: 16 additions & 12 deletions test/code/formatPreservation/inlineHtml.test
Original file line number Diff line number Diff line change
Expand Up @@ -42,13 +42,14 @@ function test() {
baz();
}
-----
// TODO Fix broken result
// TODO Preserve formatting
$stmts[0]->stmts[1] = $stmts[0]->stmts[2];
-----
<?php

function test() {
foo();<?php
function test()
{
foo();
baz();
baz();
}
Expand All @@ -61,14 +62,15 @@ function test() {
baz();
}
-----
// TODO Fix broken result
// TODO Preserve formatting
unset($stmts[0]->stmts[2]);
-----
<?php

function test() {
function test()
{
foo();
?>Bar
?>Bar<?php
}
-----
<?php
Expand All @@ -79,13 +81,14 @@ function test() {
baz();
}
-----
// TODO Fix broken result
// TODO Preserve formatting
array_splice($stmts[0]->stmts, 0, 1, []);
-----
<?php

function test() {
Bar<?php
function test()
{
?>Bar<?php
baz();
}
-----
Expand All @@ -97,12 +100,13 @@ function test() {
baz();
}
-----
// TODO Fix broken result
// TODO Preserve formatting
array_splice($stmts[0]->stmts, 1, 1, []);
-----
<?php

function test() {
foo();<?php
function test()
{
foo();
baz();
}

0 comments on commit 2e11dee

Please sign in to comment.