Skip to content

Commit

Permalink
refactor(linter): use meaningful names for diagnostic parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
DonIsaac committed Sep 6, 2024
1 parent be3a432 commit 7721566
Show file tree
Hide file tree
Showing 147 changed files with 921 additions and 757 deletions.
20 changes: 12 additions & 8 deletions crates/oxc_linter/src/rules/eslint/array_callback_return/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,20 @@ use crate::{
AstNode,
};

fn expect_return(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Missing return on some path for array method {x0:?}"))
.with_help(format!("Array method {x0:?} needs to have valid return on all code paths"))
.with_label(span1)
fn expect_return(method_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Missing return on some path for array method {method_name:?}"))
.with_help(format!(
"Array method {method_name:?} needs to have valid return on all code paths"
))
.with_label(span)
}

fn expect_no_return(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected return for array method {x0:?}"))
.with_help(format!("Array method {x0:?} expects no useless return from the function"))
.with_label(span1)
fn expect_no_return(method_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected return for array method {method_name:?}"))
.with_help(format!(
"Array method {method_name:?} expects no useless return from the function"
))
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/for_direction.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,11 @@ use oxc_syntax::operator::{AssignmentOperator, BinaryOperator, UnaryOperator, Up

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

fn for_direction_diagnostic(span0: Span, span1: Span) -> OxcDiagnostic {
fn for_direction_diagnostic(span: Span, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("The update clause in this loop moves the variable in the wrong direction")
.with_help("Use while loop for intended infinite loop")
.with_labels([
span0.label("This test moves in the wrong direction"),
span.label("This test moves in the wrong direction"),
span1.label("with this update"),
])
}
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_class_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};

fn no_class_assign_diagnostic(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected re-assignment of class {x0}")).with_labels([
span1.label(format!("{x0} is declared as class here")),
span2.label(format!("{x0} is re-assigned here")),
fn no_class_assign_diagnostic(name: &str, decl_span: Span, assign_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected re-assignment of class {name}")).with_labels([
decl_span.label(format!("{name} is declared as class here")),
assign_span.label(format!("{name} is re-assigned here")),
])
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_compare_neg_zero.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,10 @@ use oxc_syntax::operator::{BinaryOperator, UnaryOperator};

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

fn no_compare_neg_zero_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Do not use the {x0} operator to compare against -0."))
fn no_compare_neg_zero_diagnostic(operator: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Do not use the {operator} operator to compare against -0."))
.with_help("Use Object.is(x, -0) to test equality with -0 and use 0 for other cases")
.with_label(span1)
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_const_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};

fn no_const_assign_diagnostic(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected re-assignment of const variable {x0}")).with_labels([
span1.label(format!("{x0} is declared here as const")),
span2.label(format!("{x0} is re-assigned here")),
fn no_const_assign_diagnostic(name: &str, decl_span: Span, assign_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Unexpected re-assignment of const variable {name}")).with_labels([
decl_span.label(format!("{name} is declared here as const")),
assign_span.label(format!("{name} is re-assigned here")),
])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,20 @@ declare_oxc_lint!(
correctness
);

fn constant_short_circuit(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
fn constant_short_circuit(lhs_name: &str, expr_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"Unexpected constant {x0:?} on the left-hand side of a {x1:?} expression"
"Unexpected constant {lhs_name:?} on the left-hand side of a {expr_name:?} expression"
))
.with_help("This expression always evaluates to the constant on the left-hand side")
.with_label(span2)
.with_label(span)
}

fn constant_binary_operand(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
fn constant_binary_operand(left_or_right: &str, operator: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected constant binary expression")
.with_help(format!("This compares constantly with the {x0}-hand side of the {x1}"))
.with_label(span2)
.with_help(format!(
"This compares constantly with the {left_or_right}-hand side of the {operator}"
))
.with_label(span)
}

fn constant_always_new(span: Span) -> OxcDiagnostic {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_control_regex.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,10 +10,10 @@ use regex::{Matches, Regex};

use crate::{ast_util::extract_regex_flags, context::LintContext, rule::Rule, AstNode};

fn no_control_regex_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
fn no_control_regex_diagnostic(regex: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Unexpected control character(s)")
.with_help(format!("Unexpected control character(s) in regular expression: \"{x0}\""))
.with_label(span1)
.with_help(format!("Unexpected control character(s) in regular expression: \"{regex}\""))
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
12 changes: 6 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_dupe_class_members.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,15 @@ use rustc_hash::FxHashMap;
use crate::{context::LintContext, rule::Rule};

fn no_dupe_class_members_diagnostic(
x0: &str, /*Class member name */
span1: Span,
span2: Span,
member_name: &str, /*Class member name */
decl_span: Span,
re_decl_span: Span,
) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Duplicate class member: {x0:?}"))
OxcDiagnostic::warn(format!("Duplicate class member: {member_name:?}"))
.with_help("The last declaration overwrites previous ones, remove one of them or rename if both should be retained")
.with_labels([
span1.label(format!("{x0:?} is previously declared here")),
span2.label(format!("{x0:?} is re-declared here")),
decl_span.label(format!("{member_name:?} is previously declared here")),
re_decl_span.label(format!("{member_name:?} is re-declared here")),
])
}

Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_dupe_else_if.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,10 @@ use oxc_syntax::operator::LogicalOperator;

use crate::{ast_util::calculate_hash, context::LintContext, rule::Rule, AstNode};

fn no_dupe_else_if_diagnostic(span0: Span, span1: Span) -> OxcDiagnostic {
fn no_dupe_else_if_diagnostic(first_test: Span, second_test: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("duplicate conditions in if-else-if chains")
.with_help("This branch can never execute. Its condition is a duplicate or covered by previous conditions in the if-else-if chain")
.with_labels([span0, span1])
.with_labels([first_test, second_test])
}

#[derive(Debug, Default, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_empty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use oxc_span::Span;

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

fn no_empty_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
fn no_empty_diagnostic(stmt_kind: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow empty block statements")
.with_help(format!("Add comment inside empty {x0} statement"))
.with_label(span1.label(format!("Empty {x0} statement")))
.with_help(format!("Add comment inside empty {stmt_kind} statement"))
.with_label(span.label(format!("Empty {stmt_kind} statement")))
}

#[derive(Debug, Default, Clone)]
Expand Down
4 changes: 2 additions & 2 deletions crates/oxc_linter/src/rules/eslint/no_empty_pattern.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use oxc_span::Span;

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

fn no_empty_pattern_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
fn no_empty_pattern_diagnostic(pattern_type: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow empty destructuring patterns.")
.with_help("Passing `null` or `undefined` will result in runtime error because `null` and `undefined` cannot be destructured.")
.with_label(
span1.label(format!("Empty {x0} binding pattern")),
span.label(format!("Empty {pattern_type} binding pattern")),
)
}

Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_func_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@ use oxc_span::Span;

use crate::{context::LintContext, rule::Rule};

fn no_func_assign_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("'{x0}' is a function."))
.with_label(span1.label(format!("{x0} is re-assigned here")))
fn no_func_assign_diagnostic(name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("'{name}' is a function."))
.with_label(span.label(format!("{name} is re-assigned here")))
}

#[derive(Debug, Default, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_global_assign.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ use oxc_span::{CompactStr, Span};

use crate::{context::LintContext, rule::Rule};

fn no_global_assign_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Read-only global '{x0}' should not be modified."))
.with_label(span1.label(format!("Read-only global '{x0}' should not be modified.")))
fn no_global_assign_diagnostic(global_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Read-only global '{global_name}' should not be modified."))
.with_label(span.label(format!("Read-only global '{global_name}' should not be modified.")))
}

#[derive(Debug, Default, Clone)]
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_inner_declarations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use oxc_span::Span;

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

fn no_inner_declarations_diagnostic(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
fn no_inner_declarations_diagnostic(decl_type: &str, body: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Variable or `function` declarations are not allowed in nested blocks")
.with_help(format!("Move {x0} declaration to {x1} root"))
.with_label(span2)
.with_help(format!("Move {decl_type} declaration to {body} root"))
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
8 changes: 4 additions & 4 deletions crates/oxc_linter/src/rules/eslint/no_label_var.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use oxc_span::Span;

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

fn no_label_var_diagnostic(x0: &str, span0: Span, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Found identifier '{x0}' with the same name as a label."))
fn no_label_var_diagnostic(name: &str, id_span: Span, label_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Found identifier '{name}' with the same name as a label."))
.with_labels([
span0.label(format!("Identifier '{x0}' found here.")),
span1.label("Label with the same name."),
id_span.label(format!("Identifier '{name}' found here.")),
label_span.label("Label with the same name."),
])
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,8 @@ use oxc_span::Span;

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

fn no_new_native_nonconstructor_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("`{x0}` cannot be called as a constructor.")).with_label(span1)
fn no_new_native_nonconstructor_diagnostic(fn_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("`{fn_name}` cannot be called as a constructor.")).with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
8 changes: 5 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_new_wrappers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,12 @@ use oxc_span::Span;

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

fn no_new_wrappers_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
fn no_new_wrappers_diagnostic(builtin_name: &str, new_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow new operators with the String, Number, and Boolean objects")
.with_help(format!("do not use {x0} as a constructor, consider removing the new operator."))
.with_label(span1)
.with_help(format!(
"do not use {builtin_name} as a constructor, consider removing the new operator."
))
.with_label(new_span)
}

#[derive(Debug, Default, Clone)]
Expand Down
19 changes: 10 additions & 9 deletions crates/oxc_linter/src/rules/eslint/no_nonoctal_decimal_escape.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ use regex::{Captures, Regex};

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

fn replacement(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use '{x0}' escape sequence."))
.with_help(format!("Replace '{x0}' with '{x1}'. This maintains the current functionality."))
.with_label(span2)
fn replacement(escape_sequence: &str, replacement: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use '{escape_sequence}' escape sequence."))
.with_help(format!("Replace '{escape_sequence}' with '{replacement}'. This maintains the current functionality."))
.with_label(span)
}

fn escape_backslash(x0: &str, x1: &str, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use '{x0}' escape sequence."))
.with_help(format!("Replace '{x0}' with '{x1}' to include the actual backslash character."))
.with_label(span2)
fn escape_backslash(escape_sequence: &str, replacement: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("Don't use '{escape_sequence}' escape sequence."))
.with_help(format!("Replace '{escape_sequence}' with '{replacement}' to include the actual backslash character."))
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand All @@ -39,7 +39,8 @@ declare_oxc_lint!(
/// "\\9"
/// ```
NoNonoctalDecimalEscape,
correctness
correctness,
pending
);

impl Rule for NoNonoctalDecimalEscape {
Expand Down
6 changes: 3 additions & 3 deletions crates/oxc_linter/src/rules/eslint/no_obj_calls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@ use crate::{context::LintContext, rule::Rule};
const GLOBAL_THIS: &str = "globalThis";
const NON_CALLABLE_GLOBALS: [&str; 5] = ["Atomics", "Intl", "JSON", "Math", "Reflect"];

fn no_obj_calls_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
fn no_obj_calls_diagnostic(obj_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Disallow calling some global objects as functions")
.with_help(format!("{x0} is not a function."))
.with_label(span1)
.with_help(format!("{obj_name} is not a function."))
.with_label(span)
}

#[derive(Debug, Clone, PartialEq, Eq)]
Expand Down
14 changes: 8 additions & 6 deletions crates/oxc_linter/src/rules/eslint/no_prototype_builtins.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ use oxc_span::{GetSpan, Span};

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

fn no_prototype_builtins_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("do not access Object.prototype method {x0:?} from target object"))
.with_help(format!(
"to avoid prototype pollution, use `Object.prototype.{x0}.call` instead"
))
.with_label(span1)
fn no_prototype_builtins_diagnostic(method_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"do not access Object.prototype method {method_name:?} from target object"
))
.with_help(format!(
"to avoid prototype pollution, use `Object.prototype.{method_name}.call` instead"
))
.with_label(span)
}

#[derive(Debug, Default, Clone)]
Expand Down
22 changes: 12 additions & 10 deletions crates/oxc_linter/src/rules/eslint/no_redeclare.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,20 @@ use oxc_syntax::symbol::SymbolId;

use crate::{context::LintContext, rule::Rule};

fn no_redeclare_diagnostic(x0: &str, span1: Span, span2: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("'{x0}' is already defined.")).with_labels([
span1.label(format!("'{x0}' is already defined.")),
span2.label("It can not be redeclare here."),
fn no_redeclare_diagnostic(id_name: &str, decl_span: Span, re_decl_span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("'{id_name}' is already defined.")).with_labels([
decl_span.label(format!("'{id_name}' is already defined.")),
re_decl_span.label("It can not be redeclare here."),
])
}

fn no_redeclare_as_builti_in_diagnostic(x0: &str, span1: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!("'{x0}' is already defined as a built-in global variable."))
.with_label(
span1.label(format!("'{x0}' is already defined as a built-in global variable.")),
)
fn no_redeclare_as_builtin_in_diagnostic(builtin_name: &str, span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn(format!(
"'{builtin_name}' is already defined as a built-in global variable."
))
.with_label(
span.label(format!("'{builtin_name}' is already defined as a built-in global variable.")),
)
}

#[derive(Debug, Default, Clone)]
Expand Down Expand Up @@ -89,7 +91,7 @@ impl Rule for NoRedeclare {
impl NoRedeclare {
fn report_diagnostic(&self, ctx: &LintContext, span: Span, ident: &BindingIdentifier) {
if self.built_in_globals && ctx.env_contains_var(&ident.name) {
ctx.diagnostic(no_redeclare_as_builti_in_diagnostic(ident.name.as_str(), ident.span));
ctx.diagnostic(no_redeclare_as_builtin_in_diagnostic(ident.name.as_str(), ident.span));
} else {
ctx.diagnostic(no_redeclare_diagnostic(ident.name.as_str(), ident.span, span));
}
Expand Down
Loading

0 comments on commit 7721566

Please sign in to comment.