Skip to content

Conversation

@cdonovick
Copy link
Contributor

Summary

resolves #341

Adds _PrecedenceNode as a helper class to automatically parenthesize child nodes when necessary

Test Plan

Adds basic unit tests for implicit parens and extends fuzz testing.

Notes

I had originally tried to allow MaybeSentinel in {l,r}par attrs but this proved difficulty to get right. It would require numerous changes to the parser to ensure proper construction of _BaseParenthesizableNode as they are often explicitly constructed with empty lpar and rpar attributes.

@facebook-github-bot facebook-github-bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Feb 17, 2021
@notEvil
Copy link

notEvil commented Nov 12, 2022

Isn't this pretty important? Afaik any transformer might produce logically different code. Is there a simple "workaround" I don't see?

thank you for libcst!

@cdonovick
Copy link
Contributor Author

@notEvil I have a quick a dirty work around in the linked issue. #341 (comment)

You could probably do something more clever by incorporating the logic I use here in the transformer.

@notEvil
Copy link

notEvil commented Nov 12, 2022

@notEvil I have a quick a dirty work around in the linked issue. #341 (comment)

Thanks, I've seen it but don't want to introduce parentheses.

You could probably do something more clever by incorporating the logic I use here in the transformer.

I just did, except for Power which probably takes me another hour to figure out (because transformer act at child level)*. Still think this PR should get more attention.

* eventually, after I recognized the relevance of wrap_ties

@kiri11
Copy link
Contributor

kiri11 commented Jul 30, 2024

Looks amazing! Prevents tricky issues.

@zsol
Copy link
Contributor

zsol commented Jul 30, 2024

I'm happy to review this if someone's willing to resurrect and rebase

@notEvil
Copy link

notEvil commented Mar 9, 2025

I've just published my recent work on parentheses and precedence at https://gitlab.com/notEvil/normcst. The tests generate ~7.5k combinations of expressions/statements/match patterns and ensure that normcst.ParenthesisTransformer produces valid and minimal results. I will use it in my own transformations for a while before pushing it to PyPI and would be happy about anyone who helps by giving it a try.

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

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

5 participants