Repository navigation
fix: preserve arithmetic unary operator boundaries - #21
Conversation
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
left a comment
There was a problem hiding this comment.
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.
Printing
(( - -x ))currently produces(( --x )). Withx=7, that changes the result to 6 and decrements x. The same issue turns+ +xinto 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.