Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Comments in multi-line string concatenation statement gets removed #1066

Closed
naveen17797 opened this issue Jan 24, 2025 · 3 comments
Closed

Comments

@naveen17797
Copy link

naveen17797 commented Jan 24, 2025

For example

$test = 'apple' .
       // this comment wont be retained
       'orange';

After processing, this comment gets removed when AST is converted to code with new PrettyPrinter\Standard

$test = 'apple' . 'orange';
@nikic
Copy link
Owner

nikic commented Jan 25, 2025

The comment is part of the AST:

array(
    0: Stmt_Expression(
        expr: Expr_Assign(
            var: Expr_Variable(
                name: test
            )
            expr: Expr_BinaryOp_Concat(
                left: Scalar_String(
                    value: apple
                )
                right: Scalar_String(
                    value: orange
                    comments: array(
                        0: // this comment wont be retained
                    )
                )
            )
        )
    )
)

But the pretty printer will only print comments that are at the statement (or some other kind of list) level, but not those nested inside expressions in odd ways.

Using the formatting-preserving printer (https://github.com/nikic/PHP-Parser/blob/master/doc/component/Pretty_printing.markdown#formatting-preserving-pretty-printing) should retain this comment though.

@naveen17797
Copy link
Author

naveen17797 commented Jan 30, 2025

@nikic i tried this, but it doesn't seem to work.

Example snippet

<?php
		$code = file_get_contents(__DIR__ . '/test.php');
		$parser = (new ParserFactory())->createForVersion(PhpVersion::fromString('7.4'));
		$old_ast = $parser->parse($code);
		$old_tokens = $parser->getTokens();
		$traverser = new NodeTraverser();
		$new_ast = $traverser->traverse($old_ast);
		$pretty_printer =  new PhpParser\PrettyPrinter\Standard();
		var_dump($pretty_printer->printFormatPreserving($new_ast, $old_ast, $old_tokens));

file.php contents

<?php
$test = 'apple' .
       // this comment wont be retained
       'orange';

Output:

<?php
$example = 'apple' . 'orange';"

Version: 5.4.0

@nikic
Copy link
Owner

nikic commented Jan 30, 2025

I think the problem with that code snippet is that CloningVisitor is not added to the NodeTraverser.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants