From 2534cdecb76f431190565426884d00e85c4e884b Mon Sep 17 00:00:00 2001 From: overlookmotel <557937+overlookmotel@users.noreply.github.com> Date: Tue, 19 Nov 2024 01:12:11 +0000 Subject: [PATCH] feat(syntax): add `AssignmentOperator::to_logical_operator` and `to_binary_operator` methods (#7350) Add methods to convert `AssignmentOperator` to `LogicalOperator` or `BinaryOperator`. e.g. `+=` -> `+`, `&&=` -> `&&`. --- crates/oxc_syntax/src/operator.rs | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/crates/oxc_syntax/src/operator.rs b/crates/oxc_syntax/src/operator.rs index 1a0a5e0eecda8..77519a34bd677 100644 --- a/crates/oxc_syntax/src/operator.rs +++ b/crates/oxc_syntax/src/operator.rs @@ -95,6 +95,35 @@ impl AssignmentOperator { ) } + /// Get [`LogicalOperator`] corresponding to this [`AssignmentOperator`]. + pub fn to_logical_operator(self) -> Option { + match self { + Self::LogicalAnd => Some(LogicalOperator::And), + Self::LogicalOr => Some(LogicalOperator::Or), + Self::LogicalNullish => Some(LogicalOperator::Coalesce), + _ => None, + } + } + + /// Get [`BinaryOperator`] corresponding to this [`AssignmentOperator`]. + pub fn to_binary_operator(self) -> Option { + match self { + Self::Addition => Some(BinaryOperator::Addition), + Self::Subtraction => Some(BinaryOperator::Subtraction), + Self::Multiplication => Some(BinaryOperator::Multiplication), + Self::Division => Some(BinaryOperator::Division), + Self::Remainder => Some(BinaryOperator::Remainder), + Self::ShiftLeft => Some(BinaryOperator::ShiftLeft), + Self::ShiftRight => Some(BinaryOperator::ShiftRight), + Self::ShiftRightZeroFill => Some(BinaryOperator::ShiftRightZeroFill), + Self::BitwiseOR => Some(BinaryOperator::BitwiseOR), + Self::BitwiseXOR => Some(BinaryOperator::BitwiseXOR), + Self::BitwiseAnd => Some(BinaryOperator::BitwiseAnd), + Self::Exponential => Some(BinaryOperator::Exponential), + _ => None, + } + } + /// Get the string representation of this operator. /// /// This is the same as how the operator appears in source code.