-
-
Notifications
You must be signed in to change notification settings - Fork 38
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
svelte/no-dom-manipulating #300
Comments
Thank you for the rule suggestions! That rule sounds good to me. I think the reason eslint-plugin-vue doesn't have that rule is because no one has suggested it. At least there hasn't been a suggestion for that rule since I started maintaining it. |
Ohh. |
What if Svelte keeps a reference to a text node and you set textContent on the parent? |
I tried it. It seems that updating the https://svelte.dev/repl/2b02b103ace347b8aaf2d45311829f4d?version=3.52.0 <script>
let div;
let show;
// ✓ GOOD
const toggle = () => show = !show;
// ✗ BAD
const update = () => div.textContent = 'foo';
</script>
<div bind:this="{div}">
{#if show}
div
{/if}
</div>
<button on:click="{() => toggle()}">Click Me (Good)</button>
<button on:click="{() => update()}">Click Me (Bad)</button> So I think it's reasonable to ignore DOM manipulations by directives ( However, I still don't know if it should be reported if the user is using typescript and the element is specified as an argument to the function. function fn(element: HTMLElement) {
element.remove(); // We don't know if the `element` passed through `bind:this`.
} |
I started implementing this rule. #302 |
Thank you @ota-meshi! |
Motivation
Many Svelte users face
Uncaught (in promise) TypeError: Cannot read property 'removeChild' of null
.sveltejs/svelte#6037
This is because userland code (or library code) manipulates DOM directly.
Then between the actual DOM and Svelte runtime expected DOM is different and such an error occurs.
Some user wants to use a library that includes DOM manipulation, so I think the Svelte runtime also need to update.
But at the same time, always actual DOM and Svelte expected DOM also should be same as much as possible.
Therefore we should not use DOM manipulating functions.
Description
In general, DOM manipulating should delegate to Svelte runtime. If you manipulate the DOM directly, the Svelte runtime may confuse because there is a difference between the actual DOM and the Svelte runtime's expected DOM.
Therefore this rule reports where you use DOM manipulating function.
We don't recommend but If you intentionally manipulate the DOM, simply you can ignore this ESLint report.
Examples
Additional comments
I think Vue also can face a similar situation but I couldn't find a such rule.
(I tried to run with Vue.js at playground and the situation is the same.)
I would like to know why Vue.js doesn't need to care about it.
The text was updated successfully, but these errors were encountered: