Properly respect associativity when reformatting (#7486)

Signed-off-by: Nick Cameron <nrc@ncameron.org>
This commit is contained in:
Nick Cameron
2025-06-17 11:10:37 +12:00
committed by GitHub
parent 3049d939e1
commit 1a4a030671
2 changed files with 58 additions and 7 deletions

View File

@ -3005,6 +3005,8 @@ impl BinaryOperator {
}
}
/// The operator associativity of the operator (as in the parsing sense, not the mathematical sense of associativity).
///
/// Follow JS definitions of each operator.
/// Taken from <https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Operators/Operator_precedence#table>
pub fn associativity(&self) -> Associativity {
@ -3015,6 +3017,12 @@ impl BinaryOperator {
Self::And | Self::Or => Associativity::Left,
}
}
/// Whether an operator is mathematically associative. If it is, then the operator associativity (given by the
/// `associativity` method) is mostly irrelevant.
pub fn associative(&self) -> bool {
matches!(self, Self::Add | Self::Mul | Self::And | Self::Or)
}
}
#[derive(Debug, Clone, Deserialize, Serialize, PartialEq, ts_rs::TS, JsonSchema)]
#[ts(export)]