mirror of
https://github.com/nikic/PHP-Parser.git
synced 2025-04-20 22:12:25 +02:00
Bail out on PHP tags in removed code
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 b0edd4c41111042d43bb45c6c657b2e0db367d9e)
This commit is contained in:
parent
a4fe65bf60
commit
2e11deec46
@ -206,6 +206,11 @@ class TokenStream
|
||||
|| $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.
|
||||
*
|
||||
|
@ -774,7 +774,8 @@ abstract class PrettyPrinterAbstract
|
||||
}
|
||||
|
||||
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);
|
||||
@ -877,7 +878,8 @@ abstract class PrettyPrinterAbstract
|
||||
$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;
|
||||
|
@ -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();
|
||||
}
|
||||
@ -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
|
||||
@ -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();
|
||||
}
|
||||
-----
|
||||
@ -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();
|
||||
}
|
||||
|
Loading…
x
Reference in New Issue
Block a user