oxc-project/oxc · warning

Expected method{method_name_str} to have this.

Error message

Expected method{method_name_str} to have this.

What it means

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.

Source

Thrown at crates/oxc_linter/src/rules/eslint/class_methods_use_this.rs:25

use oxc_ast::{
    AstKind,
    ast::{
        AccessorProperty, ArrowFunctionBody, Expression, FunctionBody, PropertyDefinition,
        TSAccessibility,
    },
};
use oxc_ast_visit::Visit;
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_semantic::AstNode;
use oxc_span::{GetSpan, Span};
use oxc_str::CompactStr;

use crate::{LintContext, rule::Rule};

fn class_methods_use_this_diagnostic(span: Span, name: Option<Cow<'_, str>>) -> OxcDiagnostic {
    let method_name_str = name.map_or(String::new(), |name| format!(" `{name}`"));
    OxcDiagnostic::warn(format!("Expected method{method_name_str} to have this."))
        .with_help(format!("Consider converting method{method_name_str} to a static method."))
        .with_label(span)
}

#[derive(Debug, Clone, JsonSchema, Serialize, Deserialize)]
#[serde(rename_all = "camelCase", default, deny_unknown_fields)]
pub struct ClassMethodsUseThisConfig {
    /// List of method names to exempt from this rule. Names can include the hash for private methods.
    /// Example: `save`, `#rerender`
    #[schemars(with = "Vec<String>")]
    except_methods: Vec<MethodException>,
    /// Enforce this rule for class fields that are functions.
    enforce_for_class_fields: bool,
    /// Whether to ignore methods that are overridden.
    ignore_override_methods: bool,
    /// Whether to ignore classes that implement interfaces.
    ignore_classes_with_implements: Option<IgnoreClassWithImplements>,
}

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Mark the method `static` — it uses no instance state, so static matches the intent.
  2. If the method genuinely depends on the instance, use `this` inside its body (often it was meant to read a field).
  3. Add the method name to the rule's `exceptMethods` option in .oxlintrc.json (include `#` for private methods, e.g. "#rerender").
  4. Suppress the single line with `// oxlint-disable-line class-methods-use-this` or disable the rule for the file.

Example fix

// before
class Report {
  format(data) {
    return JSON.stringify(data);
  }
}

// after
class Report {
  static format(data) {
    return JSON.stringify(data);
  }
}
Defensive patterns

Strategy: validation

Validate before calling

// .oxlintrc.json — exempt known stateless helpers before adopting the rule
{
  "rules": {
    "class-methods-use-this": ["warn", {
      "exceptMethods": ["save", "#rerender"],
      "enforceForFields": false
    }]
  }
}

Prevention

When it happens

Trigger: 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.

Common situations: 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.

Related errors


AI-assisted analysis of oxc-project/oxc@e1e7af627c (2026-08-20). Data as JSON: /api/errors/3fb31e59193f3c56. Report an issue: GitHub.