fix: preserve parentheses for same-precedence different operators#67
Merged
fix: preserve parentheses for same-precedence different operators#67
Conversation
The needBinaryOpArgParens function only compared operator precedence, so explicit parentheses on same-precedence but different operators were dropped during serialization. For example, `a + (b - c)` was serialized as `a + b - c`, which changes evaluation order when re-parsed. Add an isRight parameter to distinguish left vs right child position, and require parentheses when child and parent operators differ at the same precedence level and dropping parens would change associativity. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## master #67 +/- ##
==========================================
+ Coverage 89.67% 89.70% +0.02%
==========================================
Files 11 11
Lines 2984 3010 +26
==========================================
+ Hits 2676 2700 +24
- Misses 210 211 +1
- Partials 98 99 +1 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
needBinaryOpArgParensto preserve parentheses when child and parent binary operators have the same precedence but are different operators (e.g.,+vs-,*vs/)a + (b - c)was serialized asa + b - c, changing evaluation order froma + (b - c)to(a + b) - cAppendString,Prettify,ExpandWithExprs, andOptimizecode pathsRoot Cause
needBinaryOpArgParensonly compared operator precedence levels. When two operators shared the same precedence (e.g.,+/-at priority 4,*///%at priority 5), parentheses were dropped if the sub-expression was a "simple leaf chain." This is only safe when the operators are identical (since they're left-associative), but not when they differ.Fix
Added an
isRightparameter toneedBinaryOpArgParensto distinguish left vs right child position:a + (b - c))^precedence group)Test plan
TestParseSuccesscovering all same-precedence operator combinationsgo vetcleangofmtclean🤖 Generated with Claude Code
Summary by cubic
Fixes binary expression serialization to keep parentheses when child and parent operators share precedence but differ, so re-parsing doesn’t change evaluation order.
needBinaryOpArgParens(arg, parentOp, isRight)to factor child side and require parens when differing operators at the same precedence would change associativity.a + (b - c),a * (b / c),a and (b unless c), anda ^ (b ^ c)intact acrossAppendString,Prettify,ExpandWithExprs, andOptimize.100 * (disk_free_bytes{...} / disk_total_bytes{...}).Written for commit 2d9fa2c. Summary will update on new commits.