oxc-project/oxc · warning · OxcDiagnostic

The `name` property should be set to `{name}`.

Error message

The `name` property should be set to `{name}`.

What it means

Diagnostic from oxlint's `unicorn/custom-error-definition` rule (pending category). The `name` property of a custom error class must be set to a string literal exactly equal to the class name — via `this.name = '...'` in the constructor or a `name = '...'` class field. This message fires when the value is a different string, a non-literal expression such as `this.constructor.name` (which breaks after minification), or when no name assignment exists at all (then the span covers the constructor body or the class). The `{name}` placeholder is filled with the class name.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs:24

    },
    match_member_expression,
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::{GetSpan, Span};

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

fn invalid_class_name_diagnostic(span: Span, expected: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Invalid class name, use `{expected}`.")).with_label(span)
}

fn missing_super_call_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Missing call to `super()` in constructor.").with_label(span)
}

fn invalid_name_property_diagnostic(span: Span, name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("The `name` property should be set to `{name}`.")).with_label(span)
}

fn pass_message_to_super_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Pass the error message to `super()` instead of setting `this.message`.")
        .with_label(span)
}

fn invalid_export_diagnostic(span: Span) -> OxcDiagnostic {
    OxcDiagnostic::warn("Exported error name should match error class").with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct CustomErrorDefinition;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Enforces the only valid way of Error subclassing. It works with any super class that ends in Error.

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Set `this.name = 'ClassName'` (or field `name = 'ClassName'`) with the literal exactly matching the class name.
  2. Replace `this.name = this.constructor.name` with the literal so minifiers cannot break it.
  3. If the constructor has no name assignment at all, add the literal assignment.

Example fix

// before
class CustomError extends Error {
  constructor(message) {
    super(message);
    this.name = 'MyError';
  }
}
// after
class CustomError extends Error {
  constructor(message) {
    super(message);
    this.name = 'CustomError';
  }
}
Defensive patterns

Strategy: validation

Prevention

When it happens

Trigger: `this.name = 'MyError'` inside `class CustomError`, `name = this.constructor.name` as a class field, or a custom error constructor that never sets `name` — all inside classes extending a valid `*Error` superclass.

Common situations: Copy-pasted error classes where the name string was never updated after renaming the class; minification breaking `this.constructor.name`-based names; error hierarchies relying on the default `Error` name.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/1eee2970f9bb8ec1. Report an issue: GitHub.