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

Preserve line breaks in scoped code #1083

Open
hirasso opened this issue Nov 4, 2024 · 4 comments
Open

Preserve line breaks in scoped code #1083

hirasso opened this issue Nov 4, 2024 · 4 comments

Comments

@hirasso
Copy link

hirasso commented Nov 4, 2024

Feature Request

Currently, quite some line breaks are being stripped from scoped code. This makes that code hard to read (I'm also scoping my own package's main code).

If it's technically possible, it would be amazing to preserve all line breaks / leave them untouched.

@theofidry
Copy link
Member

I agree it would be best to preserve formatting if possible. I tried it at some point but had to give up as it requires more work, as PHP-Scoper modifies/inserts some statements.

There was an attempt done at #1051. I think nikic/PHP-Parser#1007 (comment) opened as part of that PR nikic/PHP-Parser#1007 (comment)

@hirasso
Copy link
Author

hirasso commented Nov 6, 2024

If you ever solve this it would be amazing! I'm sure though you have enough on your plate 🥤

@hirasso
Copy link
Author

hirasso commented Nov 7, 2024

Seems like others are struggling with the same problems.

Rector uses nikic/php-parser, built on technology called an abstract syntax tree (AST). An AST doesn't know about spaces and when written to a file it produces poorly formatted code in both PHP and docblock annotations.

@theofidry
Copy link
Member

The problem is not so much the nature of AST: AST works fine because the formatting information is preserved as part of the nodes data. What is less clear and harder is knowing how to modify that formatting when inserting or removing nodes or alternatively how to handle incoherent token positions within the printer.

There is a native printer that somewhat handles this, but it's not clear to me if I'm missing something within PHP-Scoper or if there is more to address in the PHP-Parser printer

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

No branches or pull requests

2 participants