{"record":{"id":"a843800a99a92461","repo":"oxc-project/oxc","slug":"state-initialization-should-be-in-a-constructor","errorCode":null,"errorMessage":"State initialization should be in a constructor","messagePattern":"State initialization should be in a constructor","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/react/state_in_constructor.rs","lineNumber":18,"sourceCode":"use oxc_ast::AstKind;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::NodeId;\nuse oxc_span::Span;\nuse serde::Deserialize;\n\nuse crate::{\n    AstNode,\n    ast_util::get_outer_member_expression,\n    context::LintContext,\n    rule::{DefaultRuleConfig, Rule},\n    utils::{AlwaysNever, is_es6_component, is_state_member_expression},\n};\n\nfn state_in_constructor_diagnostic(span: Span, is_state_init_constructor: bool) -> OxcDiagnostic {\n    let message = if is_state_init_constructor {\n        \"State initialization should be in a constructor\"\n    } else {\n        \"State initialization should be in a class property\"\n    };\n    OxcDiagnostic::warn(message).with_label(span)\n}\n\nimpl StateInConstructor {\n    pub const fn is_always(&self) -> bool {\n        matches!(*self.0, AlwaysNever::Always)\n    }\n\n    pub const fn is_never(&self) -> bool {\n        matches!(*self.0, AlwaysNever::Never)\n    }\n}\n\n#[derive(Debug, Default, Clone, Deserialize)]\npub struct StateInConstructor(Box<AlwaysNever>);","sourceCodeStart":1,"sourceCodeEnd":36,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/react/state_in_constructor.rs#L1-L36","documentation":"Raised by react/state_in_constructor when a class component initializes state via `this.state = {...}` inside a method/constructor placement disallowed by the rule's mode: the message variant fires when the rule (configured `always`) requires state to live in the constructor but found the assignment elsewhere. At the throw site assigning this.state outside the constructor bypasses the canonical initialization point and can overwrite constructor-initialized state. state_in_constructor_diagnostic fires from run() with is_state_init_constructor telling which message branch applies.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/react/state_in_constructor.rs:18 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Move the state assignment into the constructor: this.state = {...} in constructor().","Change the rule mode to 'class-property' if property initialization is preferred."],"exampleFix":null,"handlingStrategy":"validation","validationCode":null,"typeGuard":null,"tryCatchPattern":null,"preventionTips":[],"tags":[],"backgroundTag":null,"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"}