mirror of
https://github.com/danog/PHP-Parser.git
synced 2024-11-26 20:04:48 +01:00
Store start token position in comments
This commit is contained in:
parent
56bc8ebb9b
commit
47c973b3aa
@ -7,18 +7,23 @@ class Comment implements \JsonSerializable
|
||||
protected $text;
|
||||
protected $line;
|
||||
protected $filePos;
|
||||
protected $tokenPos;
|
||||
|
||||
/**
|
||||
* Constructs a comment node.
|
||||
*
|
||||
* @param string $text Comment text (including comment delimiters like /*)
|
||||
* @param int $startLine Line number the comment started on
|
||||
* @param int $startFilePos File offset the comment started on
|
||||
* @param string $text Comment text (including comment delimiters like /*)
|
||||
* @param int $startLine Line number the comment started on
|
||||
* @param int $startFilePos File offset the comment started on
|
||||
* @param int $startTokenPos Token offset the comment started on
|
||||
*/
|
||||
public function __construct(string $text, int $startLine = -1, int $startFilePos = -1) {
|
||||
public function __construct(
|
||||
string $text, int $startLine = -1, int $startFilePos = -1, int $startTokenPos = -1
|
||||
) {
|
||||
$this->text = $text;
|
||||
$this->line = $startLine;
|
||||
$this->filePos = $startFilePos;
|
||||
$this->tokenPos = $startTokenPos;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -48,6 +53,15 @@ class Comment implements \JsonSerializable
|
||||
return $this->filePos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the token offset the comment started on.
|
||||
*
|
||||
* @return int Token offset
|
||||
*/
|
||||
public function getTokenPos() : int {
|
||||
return $this->tokenPos;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the comment text.
|
||||
*
|
||||
@ -147,6 +161,7 @@ class Comment implements \JsonSerializable
|
||||
'text' => $this->text,
|
||||
'line' => $this->line,
|
||||
'filePos' => $this->filePos,
|
||||
'tokenPos' => $this->tokenPos,
|
||||
];
|
||||
}
|
||||
}
|
@ -71,7 +71,9 @@ class JsonDecoder {
|
||||
throw new \RuntimeException('Comment must have text');
|
||||
}
|
||||
|
||||
return new $className($value['text'], $value['line'] ?? -1, $value['filePos'] ?? -1);
|
||||
return new $className(
|
||||
$value['text'], $value['line'] ?? -1, $value['filePos'] ?? -1, $value['tokenPos'] ?? -1
|
||||
);
|
||||
}
|
||||
|
||||
private function reflectionClassFromNodeType(string $nodeType) : \ReflectionClass {
|
||||
|
@ -265,8 +265,8 @@ class Lexer
|
||||
if (T_COMMENT === $token[0] || T_DOC_COMMENT === $token[0]) {
|
||||
if (isset($this->usedAttributes['comments'])) {
|
||||
$comment = T_DOC_COMMENT === $token[0]
|
||||
? new Comment\Doc($token[1], $this->line, $this->filePos)
|
||||
: new Comment($token[1], $this->line, $this->filePos);
|
||||
? new Comment\Doc($token[1], $this->line, $this->filePos, $this->pos)
|
||||
: new Comment($token[1], $this->line, $this->filePos, $this->pos);
|
||||
$startAttributes['comments'][] = $comment;
|
||||
}
|
||||
}
|
||||
|
@ -7,12 +7,13 @@ use PHPUnit\Framework\TestCase;
|
||||
class CommentTest extends TestCase
|
||||
{
|
||||
public function testGetSet() {
|
||||
$comment = new Comment('/* Some comment */', 1, 10);
|
||||
$comment = new Comment('/* Some comment */', 1, 10, 2);
|
||||
|
||||
$this->assertSame('/* Some comment */', $comment->getText());
|
||||
$this->assertSame('/* Some comment */', (string) $comment);
|
||||
$this->assertSame(1, $comment->getLine());
|
||||
$this->assertSame(10, $comment->getFilePos());
|
||||
$this->assertSame(2, $comment->getTokenPos());
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -104,7 +104,7 @@ class LexerTest extends TestCase
|
||||
[
|
||||
'startLine' => 3,
|
||||
'comments' => [
|
||||
new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14),
|
||||
new Comment\Doc('/** doc' . "\n" . 'comment */', 2, 14, 5),
|
||||
]
|
||||
],
|
||||
['endLine' => 3]
|
||||
@ -121,10 +121,10 @@ class LexerTest extends TestCase
|
||||
[
|
||||
'startLine' => 2,
|
||||
'comments' => [
|
||||
new Comment('/* comment */', 1, 6),
|
||||
new Comment('// comment' . "\n", 1, 20),
|
||||
new Comment\Doc('/** docComment 1 */', 2, 31),
|
||||
new Comment\Doc('/** docComment 2 */', 2, 50),
|
||||
new Comment('/* comment */', 1, 6, 1),
|
||||
new Comment('// comment' . "\n", 1, 20, 3),
|
||||
new Comment\Doc('/** docComment 1 */', 2, 31, 4),
|
||||
new Comment\Doc('/** docComment 2 */', 2, 50, 5),
|
||||
],
|
||||
],
|
||||
['endLine' => 2]
|
||||
|
@ -301,13 +301,15 @@ PHP;
|
||||
"nodeType": "Comment",
|
||||
"text": "\/\/ comment\n",
|
||||
"line": 2,
|
||||
"filePos": 6
|
||||
"filePos": 6,
|
||||
"tokenPos": 1
|
||||
},
|
||||
{
|
||||
"nodeType": "Comment_Doc",
|
||||
"text": "\/** doc comment *\/",
|
||||
"line": 3,
|
||||
"filePos": 17
|
||||
"filePos": 17,
|
||||
"tokenPos": 2
|
||||
}
|
||||
],
|
||||
"endLine": 6
|
||||
|
@ -66,7 +66,7 @@ EOC;
|
||||
$this->assertInstanceOf('PhpParser\Node\Stmt\Function_', $fn);
|
||||
$this->assertEquals([
|
||||
'comments' => [
|
||||
new Comment\Doc('/** Doc comment */', 2, 6),
|
||||
new Comment\Doc('/** Doc comment */', 2, 6, 1),
|
||||
],
|
||||
'startLine' => 3,
|
||||
'endLine' => 7,
|
||||
@ -88,8 +88,8 @@ EOC;
|
||||
$this->assertInstanceOf('PhpParser\Node\Stmt\Echo_', $echo);
|
||||
$this->assertEquals([
|
||||
'comments' => [
|
||||
new Comment("// Line\n", 4, 49),
|
||||
new Comment("// Comments\n", 5, 61),
|
||||
new Comment("// Line\n", 4, 49, 12),
|
||||
new Comment("// Comments\n", 5, 61, 14),
|
||||
],
|
||||
'startLine' => 6,
|
||||
'endLine' => 6,
|
||||
|
Loading…
Reference in New Issue
Block a user