NodeAtCursorProvider: different `__toString()` value after refactor · Issue #3009 · phpactor/phpactor · GitHub
Skip to content

NodeAtCursorProvider: different __toString() value after refactor #3009

Description

@przepompownia

Switched for a while to 89c6cbb to look what Node object was recognized previously. After adding

        yield 'at the end of full name' => [
            '<?php class F { public function foo() { while<> }}',
            [],
        ];

to KeywordCompletorTest::provideComplete() I see

	$node = Microsoft\PhpParser\Node\Statement\ExpressionStatement
		*Microsoft\PhpParser\Node*childNames = array(0)
		parent = Microsoft\PhpParser\Node\Statement\WhileStatement
			*Microsoft\PhpParser\Node*childNames = array(0)
			parent = Microsoft\PhpParser\Node\Statement\CompoundStatementNode
			whileToken = Microsoft\PhpParser\Token
			openParen = Microsoft\PhpParser\MissingToken
			expression = Microsoft\PhpParser\MissingToken
			closeParen = Microsoft\PhpParser\MissingToken
			colon = null
			statements = Microsoft\PhpParser\Node\Statement\ExpressionStatement
			endWhile = null
			semicolon = null
		expression = Microsoft\PhpParser\MissingToken
		semicolon = Microsoft\PhpParser\MissingToken
	$offset = Phpactor\TextDocument\ByteOffset
		offset = 45
	$source = Phpactor\TextDocument\StandardTextDocument
		language = Phpactor\TextDocument\TextDocumentLanguage
		text = "<?php class F { public function foo() { while }}"
		uri = Phpactor\TextDocument\TextDocumentUri
dap> $node->__toString()
""

On the master branch I get the same direct and parent type, but different string representation:

dap> $node->__toString()
" }"
diff --git a/lib/Completion/Tests/Unit/Bridge/TolerantParser/NodeAtCursorProviderTest.php b/lib/Completion/Tests/Unit/Bridge/TolerantParser/NodeAtCursorProviderTest.php
index 1bf5ab3c..51b1a417 100644
--- a/lib/Completion/Tests/Unit/Bridge/TolerantParser/NodeAtCursorProviderTest.php
+++ b/lib/Completion/Tests/Unit/Bridge/TolerantParser/NodeAtCursorProviderTest.php
@@ -10,6 +10,8 @@ use Microsoft\PhpParser\Node\Expression\MemberAccessExpression;
 use Microsoft\PhpParser\Node\Expression\Variable;
 use Microsoft\PhpParser\Node\MethodDeclaration;
 use Microsoft\PhpParser\Node\Statement\CompoundStatementNode;
+use Microsoft\PhpParser\Node\Statement\ExpressionStatement;
+use Microsoft\PhpParser\Node\Statement\WhileStatement;
 use PHPUnit\Framework\Attributes\DataProvider;
 use Phpactor\Completion\Bridge\TolerantParser\NodeAtCursorProvider;
 use Phpactor\Completion\Tests\TestCase;
@@ -113,6 +115,17 @@ class NodeAtCursorProviderTest extends TestCase
                 self::assertInstanceOf(CaseStatementNode::class, $node);
             }
         ];
+
+        yield [
+            <<<'PHP'
+                <?php class F { public function foo() { while<> }}
+                PHP,
+            function (Node $node): void {
+                self::assertInstanceOf(ExpressionStatement::class, $node);
+                self::assertInstanceOf(WhileStatement::class, $node->parent);
+                self::assertSame('', $node->__toString());
+            }
+        ];
     }

     public function testDoesNotCorruptOriginalAst(): void
There was 1 failure:

1) Phpactor\Completion\Tests\Unit\Bridge\TolerantParser\NodeAtCursorProviderTest::testProvide with data set #6 ('<?php class F { public functi...e<> }}', Closure Object (...))
Failed asserting that two strings are identical.
--- Expected
+++ Actual
@@ @@
-''
+' }'

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type
    No fields configured for issues without a type.

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions