|
| 1 | +import { createRule } from "../utils" |
| 2 | +import { extractContextReferences } from "./reference-helpers/svelte-context" |
| 3 | +import type { TSESTree } from "@typescript-eslint/types" |
| 4 | + |
| 5 | +export default createRule("valid-context-access", { |
| 6 | + meta: { |
| 7 | + docs: { |
| 8 | + description: |
| 9 | + "context functions must be called during component initialization.", |
| 10 | + category: "Possible Errors", |
| 11 | + // TODO Switch to recommended in the major version. |
| 12 | + recommended: false, |
| 13 | + }, |
| 14 | + schema: [], |
| 15 | + messages: { |
| 16 | + unexpected: |
| 17 | + "Do not call {{function}} except during component initialization.", |
| 18 | + }, |
| 19 | + type: "problem", |
| 20 | + }, |
| 21 | + create(context) { |
| 22 | + const scopeManager = context.getSourceCode().scopeManager |
| 23 | + const toplevelScope = |
| 24 | + scopeManager.globalScope?.childScopes.find( |
| 25 | + (scope) => scope.type === "module", |
| 26 | + ) || scopeManager.globalScope |
| 27 | + |
| 28 | + /** report ESLint error */ |
| 29 | + function report(node: TSESTree.CallExpression) { |
| 30 | + context.report({ |
| 31 | + loc: node.loc, |
| 32 | + messageId: "unexpected", |
| 33 | + data: { |
| 34 | + function: |
| 35 | + node.callee.type === "Identifier" |
| 36 | + ? node.callee.name |
| 37 | + : "context function", |
| 38 | + }, |
| 39 | + }) |
| 40 | + } |
| 41 | + |
| 42 | + /** Get nodes where the variable is used */ |
| 43 | + function getReferences(id: TSESTree.Identifier | TSESTree.BindingName) { |
| 44 | + const variable = toplevelScope?.variables.find((v) => { |
| 45 | + if (id.type === "Identifier") { |
| 46 | + return v.identifiers.includes(id) |
| 47 | + } |
| 48 | + return false |
| 49 | + }) |
| 50 | + if (variable) { |
| 51 | + return variable.references.filter((r) => r.identifier !== id) |
| 52 | + } |
| 53 | + return [] |
| 54 | + } |
| 55 | + |
| 56 | + /** Let's lint! */ |
| 57 | + function doLint( |
| 58 | + visitedCallExpressions: TSESTree.CallExpression[], |
| 59 | + contextCallExpression: TSESTree.CallExpression, |
| 60 | + currentNode: TSESTree.CallExpression, |
| 61 | + ) { |
| 62 | + let { parent } = currentNode |
| 63 | + if (parent?.type !== "ExpressionStatement") { |
| 64 | + report(contextCallExpression) |
| 65 | + return |
| 66 | + } |
| 67 | + while (parent) { |
| 68 | + parent = parent.parent |
| 69 | + if ( |
| 70 | + parent?.type === "VariableDeclaration" || |
| 71 | + parent?.type === "FunctionDeclaration" |
| 72 | + ) { |
| 73 | + const references = |
| 74 | + parent.type === "VariableDeclaration" |
| 75 | + ? getReferences(parent.declarations[0].id) |
| 76 | + : parent.id |
| 77 | + ? getReferences(parent.id) |
| 78 | + : [] |
| 79 | + |
| 80 | + for (const reference of references) { |
| 81 | + if (reference.identifier?.parent?.type === "CallExpression") { |
| 82 | + if ( |
| 83 | + !visitedCallExpressions.includes(reference.identifier.parent) |
| 84 | + ) { |
| 85 | + visitedCallExpressions.push(reference.identifier.parent) |
| 86 | + doLint( |
| 87 | + visitedCallExpressions, |
| 88 | + contextCallExpression, |
| 89 | + reference.identifier?.parent, |
| 90 | + ) |
| 91 | + } |
| 92 | + } |
| 93 | + } |
| 94 | + } else if (parent?.type === "ExpressionStatement") { |
| 95 | + if (parent.expression.type !== "CallExpression") { |
| 96 | + report(contextCallExpression) |
| 97 | + } else if (parent.expression.callee.type === "Identifier") { |
| 98 | + report(contextCallExpression) |
| 99 | + } |
| 100 | + } |
| 101 | + } |
| 102 | + } |
| 103 | + |
| 104 | + return { |
| 105 | + Program() { |
| 106 | + for (const { node } of extractContextReferences(context)) { |
| 107 | + const visitedCallExpressions: TSESTree.CallExpression[] = [] |
| 108 | + doLint(visitedCallExpressions, node, node) |
| 109 | + } |
| 110 | + }, |
| 111 | + } |
| 112 | + }, |
| 113 | +}) |
0 commit comments