Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Merged
merged 2 commits into from
Sep 12, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 15 additions & 11 deletions crates/oxc_linter/src/rules/eslint/no_plusplus.rs
Original file line number Diff line number Diff line change
@@ -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)]
Expand Down Expand Up @@ -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));
}
}

Expand Down
Loading