mirror of
https://github.com/danbulant/oxc
synced 2026-05-24 12:21:58 +00:00
refactor(syntax): clean up Operator
This commit is contained in:
parent
8161ac2800
commit
b94fe5ebfa
5 changed files with 46 additions and 100 deletions
|
|
@ -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;
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -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);
|
||||
|
|
|
|||
|
|
@ -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"
|
||||
// "-- >" => "-- >"
|
||||
// "< ! --" => "<! --"
|
||||
let bin_op_add = Operator::BinaryOperator(BinaryOperator::Addition);
|
||||
let bin_op_sub = Operator::BinaryOperator(BinaryOperator::Subtraction);
|
||||
let un_op_pos = Operator::UnaryOperator(UnaryOperator::UnaryPlus);
|
||||
let un_op_pre_inc = Operator::UpdateOperator(UpdateOperator::Increment);
|
||||
let un_op_neg = Operator::UnaryOperator(UnaryOperator::UnaryNegation);
|
||||
let un_op_pre_dec = Operator::UpdateOperator(UpdateOperator::Decrement);
|
||||
let un_op_post_dec = Operator::UpdateOperator(UpdateOperator::Decrement);
|
||||
let bin_op_gt = Operator::BinaryOperator(BinaryOperator::GreaterThan);
|
||||
let un_op_not = Operator::UnaryOperator(UnaryOperator::LogicalNot);
|
||||
let bin_op_add = Operator::Binary(BinaryOperator::Addition);
|
||||
let bin_op_sub = Operator::Binary(BinaryOperator::Subtraction);
|
||||
let un_op_pos = Operator::Unary(UnaryOperator::UnaryPlus);
|
||||
let un_op_pre_inc = Operator::Update(UpdateOperator::Increment);
|
||||
let un_op_neg = Operator::Unary(UnaryOperator::UnaryNegation);
|
||||
let un_op_pre_dec = Operator::Update(UpdateOperator::Decrement);
|
||||
let un_op_post_dec = Operator::Update(UpdateOperator::Decrement);
|
||||
let bin_op_gt = Operator::Binary(BinaryOperator::GreaterThan);
|
||||
let un_op_not = Operator::Unary(UnaryOperator::LogicalNot);
|
||||
if ((prev == bin_op_add || prev == un_op_pos)
|
||||
&& (next == bin_op_add || next == un_op_pos || next == un_op_pre_inc))
|
||||
|| ((prev == bin_op_sub || prev == un_op_neg)
|
||||
|
|
|
|||
26
crates/oxc_minifier/src/printer/operator.rs
Normal file
26
crates/oxc_minifier/src/printer/operator.rs
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
use oxc_syntax::operator::{BinaryOperator, UnaryOperator, UpdateOperator};
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
|
||||
pub enum Operator {
|
||||
Binary(BinaryOperator),
|
||||
Unary(UnaryOperator),
|
||||
Update(UpdateOperator),
|
||||
}
|
||||
|
||||
impl From<BinaryOperator> for Operator {
|
||||
fn from(op: BinaryOperator) -> Self {
|
||||
Self::Binary(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UnaryOperator> for Operator {
|
||||
fn from(op: UnaryOperator) -> Self {
|
||||
Self::Unary(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UpdateOperator> for Operator {
|
||||
fn from(op: UpdateOperator) -> Self {
|
||||
Self::Update(op)
|
||||
}
|
||||
}
|
||||
|
|
@ -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<AssignmentOperator> for Operator {
|
||||
fn from(op: AssignmentOperator) -> Self {
|
||||
Self::AssignmentOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<BinaryOperator> for Operator {
|
||||
fn from(op: BinaryOperator) -> Self {
|
||||
Self::BinaryOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<LogicalOperator> for Operator {
|
||||
fn from(op: LogicalOperator) -> Self {
|
||||
Self::LogicalOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UnaryOperator> for Operator {
|
||||
fn from(op: UnaryOperator) -> Self {
|
||||
Self::UnaryOperator(op)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<UpdateOperator> 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}")
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Reference in a new issue