oxc-project/oxc · error · OxcDiagnostic
Prefer `childNode.remove()` over `parentNode.removeChild(chi
Error message
Prefer `childNode.remove()` over `parentNode.removeChild(childNode)`.
What it means
Diagnostic from the unicorn/prefer-dom-node-remove lint rule. It fires when a call matches `parentNode.removeChild(childNode)` where the parent expression is the same node the child belongs to; the child's own `remove()` method achieves the same effect without naming the parent, and ignores errors when the node is already detached. It is a lint suggestion, not a runtime error.
Source
Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_remove.rs:14
use oxc_ast::{AstKind, ast::Expression};
use oxc_diagnostics::OxcDiagnostic;
use oxc_macros::declare_oxc_lint;
use oxc_span::Span;
use crate::{
AstNode,
ast_util::{call_expr_method_callee_info, is_method_call},
context::LintContext,
rule::Rule,
};
fn prefer_dom_node_remove_diagnostic(span: Span) -> OxcDiagnostic {
OxcDiagnostic::warn("Prefer `childNode.remove()` over `parentNode.removeChild(childNode)`.")
.with_help("Replace `parentNode.removeChild(childNode)` with `childNode.remove()`.")
.with_label(span)
.with_note("https://developer.mozilla.org/en-US/docs/Web/API/Element/remove")
}
#[derive(Debug, Default, Clone)]
pub struct PreferDomNodeRemove;
declare_oxc_lint!(
/// ### What it does
///
/// Prefers the use of `child.remove()` over `parentNode.removeChild(child)`.
///
/// ### Why is this bad?
///
/// The DOM function [`Node#remove()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove) is preferred
/// over the indirect removal of an object with [`Node#removeChild()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild).
///View on GitHub (pinned to e1e7af627c)
Solutions
- Replace `parentNode.removeChild(childNode)` with `childNode.remove()`
- Apply the rule's auto-fix, which rewrites the call to `childNode.remove()`
- Verify the parent expression was indeed the child's direct parent before rewriting
Defensive patterns
Strategy: validation
When it happens
Trigger: Thrown at crates/oxc_linter/src/rules/unicorn/prefer_dom_node_remove.rs:14 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/50b37afdeeabf24f.
Report an issue: GitHub.