sindresorhus/eslint-plugin-unicorn

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

Open

#2,474 opened on 2024年10月8日

GitHub で見る
 (2 comments) (3 reactions) (0 assignees)JavaScript (5,022 stars) (468 forks)user submission
docshelp wantedtypes

説明

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.

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