Skip to content

Commit b7f82fc

Browse files
committed
refactor(linter): improve unicorn/error-message (#9560)
Related to #6050 - Improve the documentation - Better code style
1 parent 4b61372 commit b7f82fc

File tree

1 file changed

+24
-30
lines changed

1 file changed

+24
-30
lines changed

crates/oxc_linter/src/rules/unicorn/error_message.rs

Lines changed: 24 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,15 @@ pub struct ErrorMessage;
2727
declare_oxc_lint!(
2828
/// ### What it does
2929
///
30-
/// This rule enforces a `message` value to be passed in when creating an instance of a built-in `Error` object, which leads to more readable and debuggable code.
30+
/// Enforces providing a `message` when creating built-in `Error` objects to
31+
/// improve readability and debugging.
3132
///
3233
/// ### Why is this bad?
3334
///
35+
/// Throwing an `Error` without a message, like `throw new Error()`, provides no context
36+
/// on what went wrong, making debugging harder. A clear error message improves
37+
/// code clarity and helps developers quickly identify issues.
38+
///
3439
/// ### Examples
3540
///
3641
/// Examples of **incorrect** code for this rule:
@@ -71,48 +76,37 @@ impl Rule for ErrorMessage {
7176
return;
7277
}
7378

74-
let constructor_name = &callee.name;
75-
let message_argument_idx = usize::from(constructor_name.as_str() == "AggregateError");
76-
77-
// If message is `SpreadElement` or there is `SpreadElement` before message
78-
if args
79-
.iter()
80-
.enumerate()
81-
.any(|(i, arg)| i <= message_argument_idx && matches!(arg, Argument::SpreadElement(_)))
82-
{
79+
// If there is `SpreadElement` before message
80+
if matches!(args.first(), Some(Argument::SpreadElement(_))) {
8381
return;
8482
}
8583

84+
let constructor_name = &callee.name;
85+
let message_argument_idx = usize::from(callee.name == "AggregateError");
8686
let message_argument = args.get(message_argument_idx);
8787

8888
let Some(arg) = message_argument else {
89-
return ctx.diagnostic(missing_message(constructor_name.as_str(), span));
89+
return ctx.diagnostic(missing_message(constructor_name, span));
9090
};
9191

92-
match arg {
93-
Argument::StringLiteral(lit) => {
94-
if lit.value.is_empty() {
95-
ctx.diagnostic(empty_message(lit.span));
96-
}
97-
}
98-
Argument::TemplateLiteral(template_lit) => {
99-
if template_lit.span.source_text(ctx.source_text()).len() == 2 {
100-
ctx.diagnostic(empty_message(template_lit.span));
101-
}
102-
}
103-
Argument::ObjectExpression(object_expr) => {
104-
ctx.diagnostic(not_string(object_expr.span));
92+
let diagnostic = match arg {
93+
Argument::ArrayExpression(array_expr) => not_string(array_expr.span),
94+
Argument::ObjectExpression(object_expr) => not_string(object_expr.span),
95+
Argument::StringLiteral(lit) if lit.value.is_empty() => empty_message(lit.span),
96+
Argument::TemplateLiteral(template_lit)
97+
if template_lit.span.source_text(ctx.source_text()).len() == 2 =>
98+
{
99+
empty_message(template_lit.span)
105100
}
106-
Argument::ArrayExpression(array_expr) => {
107-
ctx.diagnostic(not_string(array_expr.span));
108-
}
109-
_ => {}
110-
}
101+
_ => return,
102+
};
103+
104+
ctx.diagnostic(diagnostic);
111105
}
112106
}
113107

114108
// https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error#Error_types
115-
const BUILT_IN_ERRORS: &[&str] = &[
109+
const BUILT_IN_ERRORS: [&str; 9] = [
116110
"Error",
117111
"EvalError",
118112
"RangeError",

0 commit comments

Comments
 (0)