sindresorhus/eslint-plugin-unicorn

`prefer-dom-node-append`: Property 'append' does not exist on type 'ChildNode'.

クローズ

#2,474 opened on 2024/10/08

 (2 件のコメント) (3 件のリアクション) (0 人の担当者)JavaScript (468 件のフォーク)user submission
docshelp wantedtypes

Repository metrics

Stars
 (5,022 個のスター)
PR merge metrics
 (平均マージ 4h 30m) (30d で 26 merged PRs)

説明

Description

Code using appendChild on a Node should not be autofixed to use append. I think this can be improved by using type information and only raising an issue when (or at the very least not autofixing unless) it is known to be an Element.

This relates to https://github.com/sindresorhus/eslint-plugin-unicorn/issues/347

Fail

These are currently working fine and should still work

const foo = (element: Element, htmlElement: HTMLElement) => {
  element.appendChild(node)
  htmlElement.appendChild(node)
}
document.getElementsByName("name")[0]?.appendChild(document.createTextNode("foo"))

Pass

const foo = (node: Node, childNode: ChildNode, untyped,  element: Element) => {
  node.appendChild(node) // Current false positive
  childNode.appendChild(node) // Current false positive
  childNode.appendChild(node) // Current false positive
  element.append(node)
}
document.body.childNodes[0]?.appendChild(document.createTextNode("foo")) // Current false positive

Additional Info

These examples are oversimplified just to get the right type.

コントリビューターガイド