|
| 1 | +import type { AST } from "svelte-eslint-parser" |
| 2 | +import { createRule } from "../utils" |
| 3 | +import type { TSESTree } from "@typescript-eslint/types" |
| 4 | +import { findAttribute, findVariable } from "../utils/ast-utils" |
| 5 | +import { getStaticAttributeValue } from "../utils/ast-utils" |
| 6 | + |
| 7 | +export default createRule("no-loss-of-prop-reactivity", { |
| 8 | + meta: { |
| 9 | + docs: { |
| 10 | + description: "disallow the use of props that potentially lose reactivity", |
| 11 | + category: "Best Practices", |
| 12 | + recommended: false, |
| 13 | + }, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + potentiallyLoseReactivity: |
| 17 | + "Referencing props that potentially lose reactivity should be avoided.", |
| 18 | + }, |
| 19 | + type: "suggestion", |
| 20 | + }, |
| 21 | + create(context) { |
| 22 | + if (!context.parserServices.isSvelte) { |
| 23 | + return {} |
| 24 | + } |
| 25 | + let contextModule: AST.SvelteScriptElement | null = null |
| 26 | + let scriptNode: AST.SvelteScriptElement | null = null |
| 27 | + const reactiveStatements: AST.SvelteReactiveStatement[] = [] |
| 28 | + const functions: TSESTree.FunctionLike[] = [] |
| 29 | + const props = new Set<TSESTree.Identifier>() |
| 30 | + return { |
| 31 | + SvelteScriptElement(node: AST.SvelteScriptElement) { |
| 32 | + const contextAttr = findAttribute(node, "context") |
| 33 | + if (contextAttr && getStaticAttributeValue(contextAttr) === "module") { |
| 34 | + contextModule = node |
| 35 | + return |
| 36 | + } |
| 37 | + |
| 38 | + scriptNode = node |
| 39 | + }, |
| 40 | + SvelteReactiveStatement(node: AST.SvelteReactiveStatement) { |
| 41 | + reactiveStatements.push(node) |
| 42 | + }, |
| 43 | + ":function"(node: TSESTree.FunctionLike) { |
| 44 | + functions.push(node) |
| 45 | + }, |
| 46 | + ExportNamedDeclaration(node: TSESTree.ExportNamedDeclaration) { |
| 47 | + if ( |
| 48 | + contextModule && |
| 49 | + contextModule.range[0] < node.range[0] && |
| 50 | + node.range[1] < contextModule.range[1] |
| 51 | + ) { |
| 52 | + return |
| 53 | + } |
| 54 | + if ( |
| 55 | + node.declaration?.type === "VariableDeclaration" && |
| 56 | + (node.declaration.kind === "let" || node.declaration.kind === "var") |
| 57 | + ) { |
| 58 | + for (const decl of node.declaration.declarations) { |
| 59 | + if (decl.id.type === "Identifier") { |
| 60 | + props.add(decl.id) |
| 61 | + } |
| 62 | + } |
| 63 | + } |
| 64 | + for (const spec of node.specifiers) { |
| 65 | + if (spec.exportKind === "type") { |
| 66 | + continue |
| 67 | + } |
| 68 | + if (isMutableProp(spec.local)) { |
| 69 | + props.add(spec.exported) |
| 70 | + } |
| 71 | + } |
| 72 | + }, |
| 73 | + "Program:exit"() { |
| 74 | + for (const prop of props) { |
| 75 | + const variable = findVariable(context, prop) |
| 76 | + if ( |
| 77 | + !variable || |
| 78 | + // ignore multiple definitions |
| 79 | + variable.defs.length > 1 |
| 80 | + ) { |
| 81 | + return |
| 82 | + } |
| 83 | + for (const reference of variable.references) { |
| 84 | + if (reference.isWrite()) continue |
| 85 | + const id = reference.identifier as TSESTree.Identifier |
| 86 | + if ( |
| 87 | + variable.defs.some( |
| 88 | + (def) => |
| 89 | + def.node.range[0] <= id.range[0] && |
| 90 | + id.range[1] <= def.node.range[1], |
| 91 | + ) |
| 92 | + ) { |
| 93 | + // The reference is in the variable definition. |
| 94 | + continue |
| 95 | + } |
| 96 | + if (isInReactivityScope(id)) continue |
| 97 | + |
| 98 | + context.report({ |
| 99 | + node: id, |
| 100 | + messageId: "potentiallyLoseReactivity", |
| 101 | + }) |
| 102 | + } |
| 103 | + } |
| 104 | + }, |
| 105 | + } |
| 106 | + |
| 107 | + /** Checks whether given prop id is mutable variable or not */ |
| 108 | + function isMutableProp(id: TSESTree.Identifier) { |
| 109 | + const variable = findVariable(context, id) |
| 110 | + if (!variable || variable.defs.length === 0) { |
| 111 | + return false |
| 112 | + } |
| 113 | + return variable.defs.every((def) => { |
| 114 | + if (def.type !== "Variable") { |
| 115 | + return false |
| 116 | + } |
| 117 | + return def.parent.kind === "let" || def.parent.kind === "var" |
| 118 | + }) |
| 119 | + } |
| 120 | + |
| 121 | + /** Checks whether given id is in potentially reactive scope or not */ |
| 122 | + function isInReactivityScope(id: TSESTree.Identifier) { |
| 123 | + if ( |
| 124 | + !scriptNode || |
| 125 | + id.range[1] <= scriptNode.range[0] || |
| 126 | + scriptNode.range[1] <= id.range[0] |
| 127 | + ) { |
| 128 | + // The reference is in the template. |
| 129 | + return true |
| 130 | + } |
| 131 | + if ( |
| 132 | + reactiveStatements.some( |
| 133 | + (node) => |
| 134 | + node.range[0] <= id.range[0] && id.range[1] <= node.range[1], |
| 135 | + ) |
| 136 | + ) { |
| 137 | + // The reference is in the reactive statement. |
| 138 | + return true |
| 139 | + } |
| 140 | + if ( |
| 141 | + functions.some( |
| 142 | + (node) => |
| 143 | + node.range[0] <= id.range[0] && id.range[1] <= node.range[1], |
| 144 | + ) |
| 145 | + ) { |
| 146 | + // The reference is in the function. |
| 147 | + return true |
| 148 | + } |
| 149 | + return false |
| 150 | + } |
| 151 | + }, |
| 152 | +}) |
0 commit comments