Skip to content
Merged
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,9 @@ use crate::{
};

fn no_nonoctal_decimal_escape_diagnostic(escape_sequence: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use '{escape_sequence}' escape sequence.")).with_label(span)
OxcDiagnostic::warn(format!("Don't use '{escape_sequence}' escape sequence."))
.with_help("Use the actual character or a valid escape sequence instead.")
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
30 changes: 25 additions & 5 deletions crates/oxc_linter/src/rules/eslint/operator_assignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,28 @@ use serde_json::Value;

use crate::{AstNode, context::LintContext, rule::Rule, utils::is_same_member_expression};

fn operator_assignment_diagnostic(mode: Mode, span: Span, operator: &str) -> OxcDiagnostic {
fn operator_assignment_diagnostic(
mode: Mode,
span: Span,
operator: &str,
can_fix: bool,
) -> OxcDiagnostic {
let msg = if Mode::Never == mode {
format!("Unexpected operator assignment ({operator}) shorthand.")
} else {
format!("Assignment (=) can be replaced with operator assignment ({operator}).")
};
OxcDiagnostic::warn(msg).with_label(span)
let mut diagnostic = OxcDiagnostic::warn(msg).with_label(span);

if !can_fix {
diagnostic = diagnostic.with_note(if Mode::Never == mode {
format!("Replace '{operator}' with a regular '=' assignment.")
} else {
format!("Use '{operator}' shorthand instead of '='.")
});
}

diagnostic
}

#[derive(Debug, Default, PartialEq, Clone, Copy, Serialize, JsonSchema)]
Expand Down Expand Up @@ -129,7 +144,7 @@ fn verify(expr: &AssignmentExpression, mode: Mode, ctx: &LintContext) {
let replace_operator = format!("{}=", binary_operator.as_str());
if check_is_same_reference(left, &binary_expr.left, ctx) {
ctx.diagnostic_with_fix(
operator_assignment_diagnostic(mode, expr.span, &replace_operator),
operator_assignment_diagnostic(mode, expr.span, &replace_operator, true),
|fixer| {
if !can_be_fixed(left) {
return fixer.noop();
Expand Down Expand Up @@ -162,15 +177,20 @@ fn verify(expr: &AssignmentExpression, mode: Mode, ctx: &LintContext) {
);
} else if check_is_same_reference(left, &binary_expr.right, ctx) && is_commutative_operator
{
ctx.diagnostic(operator_assignment_diagnostic(mode, expr.span, &replace_operator));
ctx.diagnostic(operator_assignment_diagnostic(
mode,
expr.span,
&replace_operator,
false,
));
}
}
}

fn prohibit(expr: &AssignmentExpression, mode: Mode, ctx: &LintContext) {
if !expr.operator.is_assign() && !expr.operator.is_logical() {
ctx.diagnostic_with_dangerous_fix(
operator_assignment_diagnostic(mode, expr.span, expr.operator.as_str()),
operator_assignment_diagnostic(mode, expr.span, expr.operator.as_str(), true),
|fixer| {
if !can_be_fixed(&expr.left) {
return fixer.noop();
Expand Down
Loading
Loading