diff --git a/crates/oxc_linter/src/rules/no_constant_binary_expression.rs b/crates/oxc_linter/src/rules/no_constant_binary_expression.rs index 76a7f6391..f76628712 100644 --- a/crates/oxc_linter/src/rules/no_constant_binary_expression.rs +++ b/crates/oxc_linter/src/rules/no_constant_binary_expression.rs @@ -66,7 +66,7 @@ struct NoConstantBinaryExpressionDiagnostic(#[label] pub Span); #[diagnostic(severity(warning))] struct ConstantShortCircuit( &'static str, // property - String, // operator + &'static str, // operator #[label("This expression always evaluates to the constant on the left-hand side")] Span, ); @@ -75,7 +75,7 @@ struct ConstantShortCircuit( #[diagnostic(severity(warning))] struct ConstantBinaryOperand( &'static str, // otherSide - String, // operator + &'static str, // operator #[label("This compares constantly with the {0}-hand side of the `{1}`")] Span, ); @@ -98,7 +98,7 @@ impl Rule for NoConstantBinaryExpression { LogicalOperator::Or | LogicalOperator::And if expr.left.is_constant(true, ctx) => { ctx.diagnostic(ConstantShortCircuit( "truthiness", - expr.operator.to_string(), + expr.operator.as_str(), expr.span, )); } @@ -107,7 +107,7 @@ impl Rule for NoConstantBinaryExpression { { ctx.diagnostic(ConstantShortCircuit( "nullishness", - expr.operator.to_string(), + expr.operator.as_str(), expr.span, )); } @@ -125,12 +125,12 @@ impl Rule for NoConstantBinaryExpression { Self::find_binary_expression_constant_operand(right, left, operator, ctx); if right_constant_operand.is_some() { - ctx.diagnostic(ConstantBinaryOperand("left", operator.to_string(), expr.span)); + ctx.diagnostic(ConstantBinaryOperand("left", operator.as_str(), expr.span)); return; } if left_constant_operand.is_some() { - ctx.diagnostic(ConstantBinaryOperand("right", operator.to_string(), expr.span)); + ctx.diagnostic(ConstantBinaryOperand("right", operator.as_str(), expr.span)); return; } diff --git a/crates/oxc_minifier/src/printer/gen.rs b/crates/oxc_minifier/src/printer/gen.rs index 37209f066..9c49dd8e4 100644 --- a/crates/oxc_minifier/src/printer/gen.rs +++ b/crates/oxc_minifier/src/printer/gen.rs @@ -2,10 +2,10 @@ use oxc_allocator::{Box, Vec}; #[allow(clippy::wildcard_imports)] use oxc_hir::hir::*; use oxc_syntax::operator::{ - AssignmentOperator, BinaryOperator, LogicalOperator, Operator, UnaryOperator, UpdateOperator, + AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator, }; -use super::{Printer, Separator}; +use super::{Operator, Printer, Separator}; pub trait Gen { fn gen(&self, p: &mut Printer); diff --git a/crates/oxc_minifier/src/printer/mod.rs b/crates/oxc_minifier/src/printer/mod.rs index 599c5d2f9..b54820f38 100644 --- a/crates/oxc_minifier/src/printer/mod.rs +++ b/crates/oxc_minifier/src/printer/mod.rs @@ -4,6 +4,7 @@ #![allow(unused)] mod gen; +mod operator; use std::rc::Rc; @@ -11,10 +12,11 @@ use std::rc::Rc; use oxc_hir::hir::*; use oxc_semantic::SymbolTable; use oxc_syntax::operator::{ - AssignmentOperator, BinaryOperator, LogicalOperator, Operator, UnaryOperator, UpdateOperator, + AssignmentOperator, BinaryOperator, LogicalOperator, UnaryOperator, UpdateOperator, }; use self::gen::Gen; +use self::operator::Operator; #[derive(Debug, Clone, Copy)] pub struct PrinterOptions { @@ -147,15 +149,15 @@ impl Printer { // "x + ++ y" => "x+ ++y" // "-- >" => "-- >" // "< ! --" => " for Operator { + fn from(op: BinaryOperator) -> Self { + Self::Binary(op) + } +} + +impl From for Operator { + fn from(op: UnaryOperator) -> Self { + Self::Unary(op) + } +} + +impl From for Operator { + fn from(op: UpdateOperator) -> Self { + Self::Update(op) + } +} diff --git a/crates/oxc_syntax/src/operator.rs b/crates/oxc_syntax/src/operator.rs index e9d394842..c7f677271 100644 --- a/crates/oxc_syntax/src/operator.rs +++ b/crates/oxc_syntax/src/operator.rs @@ -1,48 +1,6 @@ -use std::fmt; - #[cfg(feature = "serde")] use serde::Serialize; -#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] -#[cfg_attr(feature = "serde", derive(Serialize))] -pub enum Operator { - AssignmentOperator(AssignmentOperator), - BinaryOperator(BinaryOperator), - LogicalOperator(LogicalOperator), - UnaryOperator(UnaryOperator), - UpdateOperator(UpdateOperator), -} - -impl From for Operator { - fn from(op: AssignmentOperator) -> Self { - Self::AssignmentOperator(op) - } -} - -impl From for Operator { - fn from(op: BinaryOperator) -> Self { - Self::BinaryOperator(op) - } -} - -impl From for Operator { - fn from(op: LogicalOperator) -> Self { - Self::LogicalOperator(op) - } -} - -impl From for Operator { - fn from(op: UnaryOperator) -> Self { - Self::UnaryOperator(op) - } -} - -impl From for Operator { - fn from(op: UpdateOperator) -> Self { - Self::UpdateOperator(op) - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] pub enum AssignmentOperator { @@ -135,13 +93,6 @@ impl AssignmentOperator { } } -impl fmt::Display for AssignmentOperator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let operator = self.as_str(); - write!(f, "{operator}") - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] pub enum BinaryOperator { @@ -278,13 +229,6 @@ impl BinaryOperator { } } -impl fmt::Display for BinaryOperator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let operator = self.as_str(); - write!(f, "{operator}") - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] pub enum LogicalOperator { @@ -307,13 +251,6 @@ impl LogicalOperator { } } -impl fmt::Display for LogicalOperator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let operator = self.as_str(); - write!(f, "{operator}") - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] pub enum UnaryOperator { @@ -334,11 +271,6 @@ pub enum UnaryOperator { } impl UnaryOperator { - #[must_use] - pub fn operator(&self) -> Operator { - Operator::UnaryOperator(*self) - } - #[must_use] pub fn is_arithmetic(self) -> bool { matches!(self, Self::UnaryNegation | Self::UnaryPlus) @@ -368,13 +300,6 @@ impl UnaryOperator { } } -impl fmt::Display for UnaryOperator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let operator = self.as_str(); - write!(f, "{operator}") - } -} - #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] #[cfg_attr(feature = "serde", derive(Serialize))] pub enum UpdateOperator { @@ -393,10 +318,3 @@ impl UpdateOperator { } } } - -impl fmt::Display for UpdateOperator { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - let operator = self.as_str(); - write!(f, "{operator}") - } -}