From d8b612c038978f8bfc5914eb9c96f558e370bc1e Mon Sep 17 00:00:00 2001 From: IWANABETHATGUY Date: Thu, 12 Sep 2024 23:43:24 +0800 Subject: [PATCH] refactor(oxc_linter): prefer pass Enum instead of str `no_plus_plus` (#5730) 1. remove redundant branch. --- .../src/rules/eslint/no_plusplus.rs | 26 +++++++++++-------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/crates/oxc_linter/src/rules/eslint/no_plusplus.rs b/crates/oxc_linter/src/rules/eslint/no_plusplus.rs index aaf0c91f68597..60b66c94e3576 100644 --- a/crates/oxc_linter/src/rules/eslint/no_plusplus.rs +++ b/crates/oxc_linter/src/rules/eslint/no_plusplus.rs @@ -1,21 +1,25 @@ -use oxc_ast::AstKind; +use oxc_ast::{ast::UpdateOperator, AstKind}; use oxc_diagnostics::OxcDiagnostic; use oxc_macros::declare_oxc_lint; use oxc_span::Span; use crate::{context::LintContext, rule::Rule, AstNode}; -fn no_plusplus_diagnostic(span: Span, operator: &str) -> OxcDiagnostic { - let diagnostic = - OxcDiagnostic::warn(format!("Unary operator '{operator}' used.")).with_label(span); +fn no_plusplus_diagnostic(span: Span, operator: UpdateOperator) -> OxcDiagnostic { + let diagnostic = OxcDiagnostic::warn(format!( + "Unary operator '{operator}' used.", + operator = operator.as_str() + )) + .with_label(span); - if operator == "++" { - return diagnostic.with_help("Use the assignment operator `+=` instead."); - } else if operator == "--" { - return diagnostic.with_help("Use the assignment operator `-=` instead."); + match operator { + UpdateOperator::Increment => { + diagnostic.with_help("Use the assignment operator `+=` instead.") + } + UpdateOperator::Decrement => { + diagnostic.with_help("Use the assignment operator `-=` instead.") + } } - - diagnostic } #[derive(Debug, Default, Clone)] @@ -98,7 +102,7 @@ impl Rule for NoPlusplus { return; } - ctx.diagnostic(no_plusplus_diagnostic(expr.span, expr.operator.as_str())); + ctx.diagnostic(no_plusplus_diagnostic(expr.span, expr.operator)); } }