oxc-project/oxc · error · OxcDiagnostic

Do not assign `this` to `{ident_name}`

Error message

Do not assign `this` to `{ident_name}`

What it means

Raised by unicorn/no-this-assignment when `this` is captured into a variable (`const self = this`) instead of using arrow functions or bind. The assigned identifier name is interpolated; the faulting input is the `this` assignment at the labeled span.

Source

Thrown at crates/oxc_linter/src/rules/unicorn/no_this_assignment.rs:12

use oxc_ast::{
    AstKind,
    ast::{AssignmentTarget, BindingPattern, Expression},
};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;

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

fn no_this_assignment_diagnostic(span: Span, ident_name: &str) -> OxcDiagnostic {
    OxcDiagnostic::warn(format!("Do not assign `this` to `{ident_name}`"))
        .with_help("Reference `this` directly instead of assigning it to a variable.")
        .with_label(span)
}

#[derive(Debug, Default, Clone)]
pub struct NoThisAssignment;

declare_oxc_lint!(
    /// ### What it does
    ///
    /// Disallow assigning `this` to a variable.
    ///
    /// ### Why is this bad?
    ///
    /// Assigning `this` to a variable is unnecessary and confusing.
    ///
    /// ### Examples
    ///

View on GitHub (pinned to e1e7af627c)

Solutions

  1. Reference `this` directly at the use site
  2. Use an arrow function (which captures `this` lexically) instead of a callback that needs the saved variable
  3. Use `.bind(this)` on the callback
Defensive patterns

Strategy: validation

When it happens

Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/no_this_assignment.rs:12 when the library encounters an invalid state.

Common situations: See trigger scenarios.


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