refactor(oxc_linter): prefer pass Enum instead of str no_plus_plus (#5730)

1. remove redundant branch.
This commit is contained in:
IWANABETHATGUY 2024-09-12 23:43:24 +08:00 committed by GitHub
parent 6436524ff1
commit d8b612c038
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -1,21 +1,25 @@
use oxc_ast::AstKind; use oxc_ast::{ast::UpdateOperator, AstKind};
use oxc_diagnostics::OxcDiagnostic; use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint; use oxc_macros::declare_oxc_lint;
use oxc_span::Span; use oxc_span::Span;
use crate::{context::LintContext, rule::Rule, AstNode}; use crate::{context::LintContext, rule::Rule, AstNode};
fn no_plusplus_diagnostic(span: Span, operator: &str) -> OxcDiagnostic { fn no_plusplus_diagnostic(span: Span, operator: UpdateOperator) -> OxcDiagnostic {
let diagnostic = let diagnostic = OxcDiagnostic::warn(format!(
OxcDiagnostic::warn(format!("Unary operator '{operator}' used.")).with_label(span); "Unary operator '{operator}' used.",
operator = operator.as_str()
))
.with_label(span);
if operator == "++" { match operator {
return diagnostic.with_help("Use the assignment operator `+=` instead."); UpdateOperator::Increment => {
} else if operator == "--" { diagnostic.with_help("Use the assignment operator `+=` instead.")
return diagnostic.with_help("Use the assignment operator `-=` instead."); }
UpdateOperator::Decrement => {
diagnostic.with_help("Use the assignment operator `-=` instead.")
}
} }
diagnostic
} }
#[derive(Debug, Default, Clone)] #[derive(Debug, Default, Clone)]
@ -98,7 +102,7 @@ impl Rule for NoPlusplus {
return; return;
} }
ctx.diagnostic(no_plusplus_diagnostic(expr.span, expr.operator.as_str())); ctx.diagnostic(no_plusplus_diagnostic(expr.span, expr.operator));
} }
} }