fix: preserve Less negative values - #62
Conversation
Follow-up FixAfter the previous commit, I found one more edge case: Less negative values can be followed by a higher-precedence operation, such as multiplication. For example: margin: 2 -@card-spacing * 2;
margin: 2 -(@card-spacing) * 2;The previous fix handled cases where the right-hand side of the outer That missed cases where Raffia parses the right-hand side as another So the right-hand side is not directly Side note: this feels like we are fitting the gap between Raffia's parser and Less syntax. The new fix models this more accurately. Instead of asking whether the entire right-hand side is a Less negative value, it asks whether the right-hand side starts with a value that can be negated in Less. If the right-hand side is a nested This preserves cases like: margin: 2 -@card-spacing * 2;
margin: 2 -@card-spacing / 2;
margin: 2 -(@card-spacing + 1px) * 2;while still formatting real binary subtraction normally: margin: 2 - @card-spacing * 2;
margin: 2 - (@card-spacing) * 2; |
|
This should be a parser bug. I'll fix this in parser. |
Summary
Fixes Less formatting for negative values that appear after another value.
Malva previously formatted:
as:
In Less, those are not equivalent. The first is a value list containing
0and-@card-spacing; the second is binary subtraction.This PR preserves unary negative values such as:
while still formatting actual binary subtraction normally:
Format Flow
flowchart TD A["Less input: margin: 2 -@x * 2"] --> B["Raffia AST: LessBinaryOperation"] B --> C{"Is spaced unary negative?"} C -->|"minus operator, left ends before -, and - touches right"| D["Find the start atom of the right expression"] C -->|"otherwise"| E["Print operator with normal spacing"] D --> F["If right is nested LessBinaryOperation, follow its left operand"] F --> G{"Start atom can be a Less negative value?"} G -->|"yes: @var, @@var, $prop, (...), or @var[lookup]"| H["Print operator without suffix space"] G -->|"no"| E H --> I["Output: margin: 2 -@x * 2"] E --> J["Output: binary subtraction formatting"] I --> K["Less value-list semantics preserved"]Tests
Added Less snapshot coverage for:
margin: 2 -@card-spacing;margin: 2 - @card-spacing;margin: 2-@card-spacing;margin: 2 -(@card-spacing);margin: 2 - (@card-spacing);Also updated the existing parenthesized negative-value snapshot to preserve Less semantics.
Verified with:
cargo fmt --all --check RUST_MIN_STACK=33554432 cargo test