{"record":{"id":"3fb31e59193f3c56","repo":"oxc-project/oxc","slug":"expected-method-method-name-str-to-have-this","errorCode":null,"errorMessage":"Expected method{method_name_str} to have this.","messagePattern":"Expected method(.+?) to have this\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"warning","filePath":"crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs","lineNumber":25,"sourceCode":"use oxc_ast::{\n    AstKind,\n    ast::{\n        AccessorProperty, ArrowFunctionBody, Expression, FunctionBody, PropertyDefinition,\n        TSAccessibility,\n    },\n};\nuse oxc_ast_visit::Visit;\nuse oxc_diagnostics::OxcDiagnostic;\nuse oxc_macros::declare_oxc_lint;\nuse oxc_semantic::AstNode;\nuse oxc_span::{GetSpan, Span};\nuse oxc_str::CompactStr;\n\nuse crate::{LintContext, rule::Rule};\n\nfn class_methods_use_this_diagnostic(span: Span, name: Option<Cow<'_, str>>) -> OxcDiagnostic {\n    let method_name_str = name.map_or(String::new(), |name| format!(\" `{name}`\"));\n    OxcDiagnostic::warn(format!(\"Expected method{method_name_str} to have this.\"))\n        .with_help(format!(\"Consider converting method{method_name_str} to a static method.\"))\n        .with_label(span)\n}\n\n#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]\n#[serde(rename_all = \"camelCase\", default, deny_unknown_fields)]\npub struct ClassMethodsUseThisConfig {\n    /// List of method names to exempt from this rule. Names can include the hash for private methods.\n    /// Example: `save`, `#rerender`\n    #[schemars(with = \"Vec<String>\")]\n    except_methods: Vec<MethodException>,\n    /// Enforce this rule for class fields that are functions.\n    enforce_for_class_fields: bool,\n    /// Whether to ignore methods that are overridden.\n    ignore_override_methods: bool,\n    /// Whether to ignore classes that implement interfaces.\n    ignore_classes_with_implements: Option<IgnoreClassWithImplements>,\n}","sourceCodeStart":7,"sourceCodeEnd":43,"githubUrl":"https://github.com/oxc-project/oxc/blob/e1e7af627c8843ab64044ed466b128fcc21a035b/crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs#L7-L43","documentation":"Oxlint's port of the ESLint rule class-methods-use-this. The diagnostic is built in class_methods_use_this_diagnostic (crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs:14) and interpolates the method name, or an empty string when the method is anonymous. The rule flags class methods whose body never references `this`, because such methods do not depend on instance state and are normally better as static methods. Function-valued class fields are also checked when `enforceForFields` is enabled, and the config's `exceptMethods` list can exempt specific names, including `#private` methods.","triggerScenarios":"Run oxlint with the class-methods-use-this rule enabled over a class where a method body contains no read or write of `this`; also fires for arrow/function class fields when enforceForFields is true, and for private methods like `#save` unless listed in exceptMethods.","commonSituations":"Utility or formatting helpers hung off a class purely for namespacing; code migrated from plain modules into classes; enabling the eslint recommended presets on an existing codebase; forgetting that exceptMethods needs the `#` prefix for private method names.","solutions":["Mark the method `static` — it uses no instance state, so static matches the intent.","If the method genuinely depends on the instance, use `this` inside its body (often it was meant to read a field).","Add the method name to the rule's `exceptMethods` option in .oxlintrc.json (include `#` for private methods, e.g. \"#rerender\").","Suppress the single line with `// oxlint-disable-line class-methods-use-this` or disable the rule for the file."],"exampleFix":"// before\nclass Report {\n  format(data) {\n    return JSON.stringify(data);\n  }\n}\n\n// after\nclass Report {\n  static format(data) {\n    return JSON.stringify(data);\n  }\n}","handlingStrategy":"validation","validationCode":"// .oxlintrc.json — exempt known stateless helpers before adopting the rule\n{\n  \"rules\": {\n    \"class-methods-use-this\": [\"warn\", {\n      \"exceptMethods\": [\"save\", \"#rerender\"],\n      \"enforceForFields\": false\n    }]\n  }\n}","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Default to `static` for helpers that never touch instance state.","Use exceptMethods (with `#` for private names) instead of scattering inline disables.","Decide enforceForFields explicitly when adopting the rule so arrow-function fields do not surprise you."],"tags":["eslint","oxlint","class","static-method","style"],"backgroundTag":"unused-this-in-class-method","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"}