{"record":{"id":"89358e7c6d161967","repo":"oxc-project/oxc","slug":"encountered-same-named-class-field-declaration-and","errorCode":null,"errorMessage":"Encountered same-named class field declaration and `this` assignment in constructor.","messagePattern":"Encountered same-named class field declaration and `this` assignment in constructor\\.","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/unicorn/prefer_class_fields.rs","lineNumber":23,"sourceCode":"        MethodDefinitionKind, PropertyDefinitionType, Statement,\n    },\n};\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_span::{GetSpan, Span};\n\nuse crate::{AstNode, context::LintContext, rule::Rule};\n\nfn prefer_class_fields_diagnostic(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Prefer class field declaration over `this` assignment in constructor for static values.\",\n    )\n    .with_help(\"Declare static values as class fields instead of assigning them to `this` in the constructor.\")\n    .with_label(span)\n}\n\nfn prefer_class_fields_suggestion(span: Span) -> OxcDiagnostic {\n    OxcDiagnostic::warn(\n        \"Encountered same-named class field declaration and `this` assignment in constructor.\",\n    )\n    .with_help(\"Replace the class field declaration with the value from `this` assignment.\")\n    .with_label(span)\n}\n\n#[derive(Debug, Default, Clone)]\npub struct PreferClassFields;\n\ndeclare_oxc_lint!(\n    /// ### What it does\n    ///\n    /// Prefers class field declarations over `this` assignments in constructors for static values.\n    ///\n    /// ### Why is this bad?\n    ///\n    /// Class field declarations are more readable and less error-prone than assigning static\n    /// values to `this` in the constructor. Using class fields keeps the constructor cleaner","sourceCodeStart":5,"sourceCodeEnd":41,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/unicorn/prefer_class_fields.rs#L5-L41","documentation":"Lint diagnostic from oxlint's `unicorn/prefer-class-fields` rule (suggestion variant). It fires in the narrower case where the class ALREADY declares a field `x = <something>` AND the constructor assigns `this.x = <other>` with the same name — the field initializer is immediately overwritten, so the declaration value is dead. The suggested fix is to fold the constructor value into the field declaration and drop the assignment.","triggerScenarios":"`class A { count = 0; constructor() { this.count = 1; } }` — same-named PropertyDefinition and `this.` assignment in the constructor. Reported during oxlint analysis of constructors that assign to declared field names.","commonSituations":"Partial migrations: someone added class fields but left old constructor assignments; or subclasses that re-assign inherited fields. The duplicated initialization is a real smell — the field initializer runs first and is discarded. Common during incremental modernization passes flagged in bulk by unicorn.","solutions":["Merge: `count = 1;` as the field and delete `this.count = 1;` from the constructor.","If the assignment is conditional (e.g. `if (opts) this.count = opts.count`), keep the constructor assignment but make the field the documented default; suppress with `// oxlint-disable-next-line unicorn/prefer-class-fields` and a comment.","Run `oxlint --fix` and verify tests, since initialization order in subclasses can change.","Disable the rule only if class-field semantics are deliberately being avoided repo-wide."],"exampleFix":"// before\nclass Counter {\n  count = 0;\n  constructor() {\n    this.count = 1;\n  }\n}\n\n// after\nclass Counter {\n  count = 1;\n}","handlingStrategy":"validation","validationCode":"// oxlint --filter unicorn/prefer-class-fields src/\n// CI gate: oxlint --deny-warnings src/","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Never declare a field AND assign the same `this.<name>` in the constructor — one of the two values is dead.","When the constructor assignment is conditional, encode it as `field = opts?.count ?? defaultValue` instead.","Run tests after merging field values; initialization order matters in inheritance chains."],"tags":["oxlint","unicorn","classes","dead-code","refactor"],"backgroundTag":"lint-rule-violation","analyzedSha":"e1e7af627c8843ab64044ed466b128fcc21a035b","analyzedAt":"2026-08-20T07:01:07.079Z","contentChangedAt":"2026-08-20T07:01:07.079Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}