|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import type { TSESTree } from "@typescript-eslint/types" |
| 3 | +import { createRule } from "../utils" |
| 4 | +import { findVariable, getNodeName } from "../utils/ast-utils" |
| 5 | +import type { Variable } from "@typescript-eslint/scope-manager" |
| 6 | +import { getPropertyName } from "eslint-utils" |
| 7 | + |
| 8 | +const DOM_MANIPULATING_METHODS = new Set([ |
| 9 | + "appendChild", // https://developer.mozilla.org/en-US/docs/Web/API/Node/appendChild |
| 10 | + "insertBefore", // https://developer.mozilla.org/en-US/docs/Web/API/Node/insertBefore |
| 11 | + "normalize", // https://developer.mozilla.org/en-US/docs/Web/API/Node/normalize |
| 12 | + "removeChild", // https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild |
| 13 | + "replaceChild", // https://developer.mozilla.org/en-US/docs/Web/API/Node/replaceChild |
| 14 | + "after", // https://developer.mozilla.org/en-US/docs/Web/API/Element/after |
| 15 | + "append", // https://developer.mozilla.org/en-US/docs/Web/API/Element/append |
| 16 | + "before", // https://developer.mozilla.org/en-US/docs/Web/API/Element/before |
| 17 | + "insertAdjacentElement", // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentElement |
| 18 | + "insertAdjacentHTML", // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentHTML |
| 19 | + "insertAdjacentText", // https://developer.mozilla.org/en-US/docs/Web/API/Element/insertAdjacentText |
| 20 | + "prepend", // https://developer.mozilla.org/en-US/docs/Web/API/Element/prepend |
| 21 | + "remove", // https://developer.mozilla.org/en-US/docs/Web/API/Element/remove |
| 22 | + "replaceChildren", // https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceChildren |
| 23 | + "replaceWith", // https://developer.mozilla.org/en-US/docs/Web/API/Element/replaceWith |
| 24 | +]) |
| 25 | +const DOM_MANIPULATING_PROPERTIES = new Set([ |
| 26 | + "textContent", // https://developer.mozilla.org/en-US/docs/Web/API/Node/textContent |
| 27 | + "innerHTML", // https://developer.mozilla.org/en-US/docs/Web/API/Element/innerHTML |
| 28 | + "outerHTML", // https://developer.mozilla.org/en-US/docs/Web/API/Element/outerHTML |
| 29 | + "innerText", // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/innerText |
| 30 | + "outerText", // https://developer.mozilla.org/en-US/docs/Web/API/HTMLElement/outerText |
| 31 | +]) |
| 32 | + |
| 33 | +export default createRule("no-dom-manipulating", { |
| 34 | + meta: { |
| 35 | + docs: { |
| 36 | + description: "disallow DOM manipulating", |
| 37 | + category: "Possible Errors", |
| 38 | + recommended: false, |
| 39 | + }, |
| 40 | + schema: [], |
| 41 | + messages: { |
| 42 | + disallowManipulateDOM: |
| 43 | + "Don't manipulate the DOM directly. The Svelte runtime can get confused if there is a difference between the actual DOM and the DOM expected by the Svelte runtime.", |
| 44 | + }, |
| 45 | + type: "problem", |
| 46 | + }, |
| 47 | + create(context) { |
| 48 | + const domVariables = new Set<Variable>() |
| 49 | + |
| 50 | + /** |
| 51 | + * Verify DOM variable identifier node |
| 52 | + */ |
| 53 | + function verifyIdentifier( |
| 54 | + node: TSESTree.Identifier | TSESTree.JSXIdentifier, |
| 55 | + ) { |
| 56 | + const member = node.parent |
| 57 | + if (member?.type !== "MemberExpression" || member.object !== node) { |
| 58 | + return |
| 59 | + } |
| 60 | + const name = getPropertyName(member) |
| 61 | + if (!name) { |
| 62 | + return |
| 63 | + } |
| 64 | + let target: TSESTree.Expression = member |
| 65 | + let parent = target.parent |
| 66 | + while (parent?.type === "ChainExpression") { |
| 67 | + target = parent |
| 68 | + parent = parent.parent |
| 69 | + } |
| 70 | + if (!parent) { |
| 71 | + return |
| 72 | + } |
| 73 | + if (parent.type === "CallExpression") { |
| 74 | + if (parent.callee !== target || !DOM_MANIPULATING_METHODS.has(name)) { |
| 75 | + return |
| 76 | + } |
| 77 | + } else if (parent.type === "AssignmentExpression") { |
| 78 | + if (parent.left !== target || !DOM_MANIPULATING_PROPERTIES.has(name)) { |
| 79 | + return |
| 80 | + } |
| 81 | + } |
| 82 | + context.report({ |
| 83 | + node: member, |
| 84 | + messageId: "disallowManipulateDOM", |
| 85 | + }) |
| 86 | + } |
| 87 | + |
| 88 | + return { |
| 89 | + "SvelteDirective[kind='Binding']"(node: AST.SvelteBindingDirective) { |
| 90 | + if ( |
| 91 | + node.key.name.name !== "this" || |
| 92 | + !node.expression || |
| 93 | + node.expression.type !== "Identifier" |
| 94 | + ) { |
| 95 | + // not bind:this={id} |
| 96 | + return |
| 97 | + } |
| 98 | + const element = node.parent.parent |
| 99 | + if (element.type !== "SvelteElement" || !isHTMLElement(element)) { |
| 100 | + // not HTML element |
| 101 | + return |
| 102 | + } |
| 103 | + const variable = findVariable(context, node.expression) |
| 104 | + if ( |
| 105 | + !variable || |
| 106 | + (variable.scope.type !== "module" && variable.scope.type !== "global") |
| 107 | + ) { |
| 108 | + return |
| 109 | + } |
| 110 | + domVariables.add(variable) |
| 111 | + }, |
| 112 | + "Program:exit"() { |
| 113 | + for (const variable of domVariables) { |
| 114 | + for (const reference of variable.references) { |
| 115 | + verifyIdentifier(reference.identifier) |
| 116 | + } |
| 117 | + } |
| 118 | + }, |
| 119 | + } |
| 120 | + |
| 121 | + /** |
| 122 | + * Checks whether the given node is a HTML element or not. |
| 123 | + */ |
| 124 | + function isHTMLElement(node: AST.SvelteElement) { |
| 125 | + return ( |
| 126 | + node.kind === "html" || |
| 127 | + (node.kind === "special" && getNodeName(node) === "svelte:element") |
| 128 | + ) |
| 129 | + } |
| 130 | + }, |
| 131 | +}) |
0 commit comments