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

Option to keep redundant parentheses #932

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

theriault
Copy link

@theriault theriault commented Jul 22, 2023

I know this goes against a true AST, but could there be an option to keep redundant parentheses?

Something similar to https://clang.llvm.org/doxygen/classclang_1_1ParenExpr.html#details

@nikic
Copy link
Owner

nikic commented Sep 16, 2023

I'm generally open to the idea -- what do you want to use this for?

@theriault
Copy link
Author

@nikic I am working on an automated refactor of a legacy codebase where files contain a mix of functions and side-effects. Each file will become a class of static functions, where the side-effects will become the body of a "catchall" static function and the other functions becoming a part of the class; rescoping all the code.

Preserving the existing spacing/comments while simultaneously indenting/relocating large chunks of code is not working well with the existing preservation feature of PrettyPrinter, so I am working on a before/after comment/spacing consumer NodeVisitor and a special PrettyPrinter that then utilizes the before/after attributes, so that I can move chunks of code around more easily without worrying too much about the source comments/spacing getting mangled/lost as each Node can define surrounding spacing/comments (and if those attributes aren't defined, the default formatting kicks in). Each comment/spacing token will be flagged as being owned by only one node for existing code.

To do this, I need to try to maintain location information of redundant parentheses and such so that surrounding comments/spacing can be demarcated/consumed by what may ultimately be redundant parentheses, e.g.

if (
     (/*some condition*/) // some comment around this condition 
     || (/* some other condition */) // some comment around this other condition
) {
   //...
}

Ideally, all parenthesized and braced chunks of code could be captured, so even if a dev did some outlandish style like the following, it could be captured.

$something = 123;

// commenting/grouping a chunk of code
{
       // code within its own redundant block
}

// commenting/grouping another chunk of code
{
       // code within its own redundant block
}

However, this PR in its current state only handles redundant parentheses as I haven't seen the above style too much outside of intermediary refactors of code or code that required an anonymous scope (which PHP doesn't support).

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

Successfully merging this pull request may close these issues.

None yet

2 participants