unicorn/prefer-modern-dom-apis Style
What it does
Enforces the use of:
childNode.replaceWith(newNode)overparentNode.replaceChild(newNode, oldNode)referenceNode.before(newNode)overparentNode.insertBefore(newNode, referenceNode)referenceNode.before('text')overreferenceNode.insertAdjacentText('beforebegin', 'text')referenceNode.before(newNode)overreferenceNode.insertAdjacentElement('beforebegin', newNode)
Why is this bad?
There are some advantages of using the newer DOM APIs, like:
- Traversing to the parent node is not necessary.
- Appending multiple nodes at once.
- Both
DOMStringand DOM node objects can be manipulated.
Examples
Examples of incorrect code for this rule:
javascript
oldChildNode.replaceWith(newChildNode);Examples of correct code for this rule:
javascript
parentNode.replaceChild(newChildNode, oldChildNode);How to use
To enable this rule in the CLI or using the config file, you can use:
bash
oxlint --deny unicorn/prefer-modern-dom-apisjson
{
"rules": {
"unicorn/prefer-modern-dom-apis": "error"
}
}