{"record":{"id":"48035abcea8c5b2e","repo":"oxc-project/oxc","slug":"state-initialization-should-be-in-a-class-property","errorCode":null,"errorMessage":"State initialization should be in a class property","messagePattern":"State initialization should be in a class property","errorType":"validation","errorClass":"OxcDiagnostic","httpStatus":null,"severity":"error","filePath":"crates/oxc_linter/src/rules/react/state_in_constructor.rs","lineNumber":20,"sourceCode":"use 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>);\n\ndeclare_oxc_lint!(","sourceCodeStart":2,"sourceCodeEnd":38,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/react/state_in_constructor.rs#L2-L38","documentation":"Raised by react/state_in_constructor when the rule is configured to prefer class property initializers and state is assigned inside the constructor (`this.state = {...}`). At the throw site the constructor assignment should be a class field (`state = {...}`), the modern idiomatic form that avoids constructor boilerplate. state_in_constructor_diagnostic selects this message when is_state_init_constructor is false, i.e. the state initialization was found in the constructor while the config mandates class-property style.","triggerScenarios":"Thrown at crates/oxc_linter/src/rules/react/state_in_constructor.rs:20 when the library encounters an invalid state.","commonSituations":"See trigger scenarios.","solutions":["Declare state as a class property: state = {...} directly on the class.","Change the rule mode to 'constructor' if constructor 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"}