mirror of
https://github.com/phabelio/PHP-Parser.git
synced 2024-11-27 04:24:43 +01:00
Retain comments on blocks on first inner statement
This commit is contained in:
parent
13f7321def
commit
83f34e7fa4
@ -150,7 +150,7 @@ inner_statement:
|
||||
;
|
||||
|
||||
non_empty_statement:
|
||||
'{' inner_statement_list '}' { $$ = $2; }
|
||||
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
|
||||
| T_IF parentheses_expr statement elseif_list else_single
|
||||
{ $$ = Stmt\If_[$2, ['stmts' => toArray($3), 'elseifs' => $4, 'else' => $5]]; }
|
||||
| T_IF parentheses_expr ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
|
@ -150,7 +150,7 @@ inner_statement:
|
||||
;
|
||||
|
||||
non_empty_statement:
|
||||
'{' inner_statement_list '}' { $$ = $2; }
|
||||
'{' inner_statement_list '}' { $$ = $2; prependLeadingComments($$); }
|
||||
| T_IF '(' expr ')' statement elseif_list else_single
|
||||
{ $$ = Stmt\If_[$3, ['stmts' => toArray($5), 'elseifs' => $6, 'else' => $7]]; }
|
||||
| T_IF '(' expr ')' ':' inner_statement_list new_elseif_list new_else_single T_ENDIF ';'
|
||||
|
@ -195,6 +195,15 @@ function resolveMacros($code) {
|
||||
. $args[0] . '[\'docLabel\'] = $matches[1];';
|
||||
}
|
||||
|
||||
if ('prependLeadingComments' == $name) {
|
||||
assertArgs(1, $args, $name);
|
||||
|
||||
return '$attrs = $this->startAttributeStack[#1]; $stmts = ' . $args[0] . '; '
|
||||
. 'if (!empty($attrs[\'comments\']) && isset($stmts[0])) {'
|
||||
. '$stmts[0]->setAttribute(\'comments\', '
|
||||
. 'array_merge($attrs[\'comments\'], $stmts[0]->getAttribute(\'comments\', []))); }';
|
||||
}
|
||||
|
||||
return $matches[0];
|
||||
},
|
||||
$code
|
||||
|
@ -1420,7 +1420,7 @@ class Php5 extends \PhpParser\ParserAbstract
|
||||
}
|
||||
|
||||
protected function reduceRule127() {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)];
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
}
|
||||
|
||||
protected function reduceRule128() {
|
||||
|
@ -1300,7 +1300,7 @@ class Php7 extends \PhpParser\ParserAbstract
|
||||
}
|
||||
|
||||
protected function reduceRule127() {
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)];
|
||||
$this->semValue = $this->semStack[$this->stackPos-(3-2)]; $attrs = $this->startAttributeStack[$this->stackPos-(3-1)]; $stmts = $this->semValue; if (!empty($attrs['comments']) && isset($stmts[0])) {$stmts[0]->setAttribute('comments', array_merge($attrs['comments'], $stmts[0]->getAttribute('comments', []))); };
|
||||
}
|
||||
|
||||
protected function reduceRule128() {
|
||||
|
23
test/code/parser/blockComments.test
Normal file
23
test/code/parser/blockComments.test
Normal file
@ -0,0 +1,23 @@
|
||||
Comments on blocks
|
||||
-----
|
||||
<?php
|
||||
|
||||
// foo
|
||||
{
|
||||
// bar
|
||||
{
|
||||
// baz
|
||||
$a;
|
||||
}
|
||||
}
|
||||
-----
|
||||
array(
|
||||
0: Expr_Variable(
|
||||
name: a
|
||||
comments: array(
|
||||
0: // foo
|
||||
1: // bar
|
||||
2: // baz
|
||||
)
|
||||
)
|
||||
)
|
Loading…
Reference in New Issue
Block a user