Skip to content

fix: preserve arithmetic unary operator boundaries - #21

Merged
webpro merged 1 commit into
webpro-nl:mainfrom
everton-dgn:fix/preserve-arithmetic-unary-operators
Nov 7, 2026
Merged

webpro merged 1 commit into
webpro-nl:mainfrom
everton-dgn:fix/preserve-arithmetic-unary-operators

Conversation

@everton-dgn

Copy link
Copy Markdown
Contributor

Printing (( - -x )) currently produces (( --x )). With x=7, that changes the result to 6 and decrements x. The same issue turns + +x into an increment.

Keep a separator when a unary + or - would otherwise join the same leading sign on its operand. The regression tests cover the operator tree after reparsing, nested signs, increment/decrement operands and arithmetic for loops.

Validation: 2,063 tests, build/types and lint passed on Node 24. The package smoke test passed on Node 14.21.3, and execution checks against Bash 3.2 and 5.1 preserve both values and mutations for the covered expressions.

Printing `(( - -x ))` currently produces `(( --x ))`. With `x=7`, that changes the result to 6 and decrements x. The same issue turns `+ +x` into an increment.

Keep a separator when a unary + or - would otherwise join the same leading sign on its operand. The regression tests cover the operator tree after reparsing, nested signs, increment/decrement operands and arithmetic for loops.

Validation: 2,063 tests, build/types and lint passed on Node 24. The package smoke test passed on Node 14.21.3, and execution checks against Bash 3.2 and 5.1 preserve both values and mutations for the covered expressions.

@webpro webpro left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, Éverton! The premise holds: in Bash 5.3, x=7; (( - -x )) leaves x at 7, while the (( --x )) that main prints decrements it to 6, so the printer was quietly changing what a script does. The same collapse hits - --x (printed as ---x), ~ - -x, ! + +x and the arithmetic for headers, all of which your tests cover.

The fix sits in the right place and the rule is as narrow as it should be: a separator only where a prefix + or - would otherwise touch an operand that starts with the same sign. - -x, + +x, - --x and - -2 now round-trip, mixed signs still join as -+x, ! keeps its ! spelling and -(-x) is unchanged. Every fixture in the repository (the bench inputs, installers, the large scripts and the mvdan-sh, sh-syntax and tree-sitter corpora) prints byte-identical output on main and on this branch, so the change is confined to the intended inputs. The branch merges cleanly with current main and the full suite passes on the merged tree. Printer only, so the parse path is untouched.

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.

2 participants