sindresorhus/eslint-plugin-unicorn

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

Open

#2,474 创建于 2024年10月8日

在 GitHub 查看
 (2 评论) (3 反应) (0 负责人)JavaScript (5,022 star) (468 fork)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.

贡献者指南

`prefer-dom-node-append`: Property 'append' does not exist on type 'ChildNode'. · sindresorhus/eslint-plugin-unicorn#2474 | Good First Issue