{"record":{"id":"1eee2970f9bb8ec1","repo":"oxc-project/oxc","slug":"the-name-property-should-be-set-to-name","errorCode":null,"errorMessage":"The `name` property should be set to `{name}`.","messagePattern":"The `name` property should be set to `(.+?)`\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs","lineNumber":24,"sourceCode":"    },\n    match_member_expression,\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, ast_util, context::LintContext, rule::Rule};\n\nfn invalid_class_name_diagnostic(span: Span, expected: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"Invalid class name, use `{expected}`.\")).with_label(span)\n}\n\nfn missing_super_call_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Missing call to `super()` in constructor.\").with_label(span)\n}\n\nfn invalid_name_property_diagnostic(span: Span, name: &str) -> OxcDiagnostic {\n    OxcDiagnostic::warn(format!(\"The `name` property should be set to `{name}`.\")).with_label(span)\n}\n\nfn pass_message_to_super_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Pass the error message to `super()` instead of setting `this.message`.\")\n        .with_label(span)\n}\n\nfn invalid_export_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\"Exported error name should match error class\").with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct CustomErrorDefinition;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Enforces the only valid way of Error subclassing. It works with any super class that ends in Error.","sourceCodeStart":6,"sourceCodeEnd":42,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/custom_error_definition.rs#L6-L42","documentation":"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.","triggerScenarios":"`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.","commonSituations":"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.","solutions":["Set `this.name = 'ClassName'` (or field `name = 'ClassName'`) with the literal exactly matching the class name.","Replace `this.name = this.constructor.name` with the literal so minifiers cannot break it.","If the constructor has no name assignment at all, add the literal assignment."],"exampleFix":"// before\nclass CustomError extends Error {\n  constructor(message) {\n    super(message);\n    this.name = 'MyError';\n  }\n}\n// after\nclass CustomError extends Error {\n  constructor(message) {\n    super(message);\n    this.name = 'CustomError';\n  }\n}","handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":["Set `this.name` (or a `name` field) to a string literal exactly matching the class name in every error subclass.","Never use `this.constructor.name` — minifiers rename classes and break the value.","When renaming an error class, grep for its old name string so the `name` literal is updated too."],"tags":["oxlint","lint","unicorn","errors","classes","naming"],"backgroundTag":"custom-error-subclassing","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T00:17:10.932Z"}