|
| 1 | +import type { ESLintExtendedProgram } from ".."; |
1 | 2 | import type { Context } from "../../context";
|
| 3 | +import { traverseNodes } from "../../traverse"; |
| 4 | +import { parseScriptWithoutAnalyzeScope } from "../script"; |
2 | 5 |
|
3 | 6 | /**
|
4 |
| - * Append store type declarations. |
| 7 | + * Append type declarations for svelte variables. |
| 8 | + * - Append TypeScript code like |
| 9 | + * `declare let $foo: Parameters<Parameters<(typeof foo)["subscribe"]>[0]>[0];` |
| 10 | + * to define the type information for like `$foo` variable. |
| 11 | + * - Append TypeScript code like `let foo = bar;` to define the type information for like `$: foo = bar` variable. |
| 12 | + */ |
| 13 | +export function appendDeclareSvelteVarsTypes(ctx: Context): void { |
| 14 | + const vcode = ctx.sourceCode.scripts.vcode; |
| 15 | + |
| 16 | + if (/\$\s*:\s*[\p{ID_Start}$(_]/u.test(vcode)) { |
| 17 | + // Probably have a reactive variable, so we will need to parse TypeScript once to extract the reactive variables. |
| 18 | + const result = parseScriptWithoutAnalyzeScope( |
| 19 | + vcode, |
| 20 | + ctx.sourceCode.scripts.attrs, |
| 21 | + { |
| 22 | + ...ctx.parserOptions, |
| 23 | + // Without typings |
| 24 | + project: null, |
| 25 | + } |
| 26 | + ); |
| 27 | + appendDeclareSvelteVarsTypesFromAST(result, vcode, ctx); |
| 28 | + } else { |
| 29 | + appendDeclareStoreTypesFromText(vcode, ctx); |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +/** |
| 34 | + * Append type declarations for svelte variables from AST. |
| 35 | + */ |
| 36 | +function appendDeclareSvelteVarsTypesFromAST( |
| 37 | + result: ESLintExtendedProgram, |
| 38 | + code: string, |
| 39 | + ctx: Context |
| 40 | +) { |
| 41 | + const maybeStores = new Set<string>(); |
| 42 | + |
| 43 | + traverseNodes(result.ast, { |
| 44 | + visitorKeys: result.visitorKeys, |
| 45 | + enterNode: (node, parent) => { |
| 46 | + if (node.type === "Identifier") { |
| 47 | + if (!node.name.startsWith("$") || node.name.length <= 1) { |
| 48 | + return; |
| 49 | + } |
| 50 | + maybeStores.add(node.name.slice(1)); |
| 51 | + } else if (node.type === "LabeledStatement") { |
| 52 | + if ( |
| 53 | + node.label.name !== "$" || |
| 54 | + parent !== result.ast || |
| 55 | + node.body.type !== "ExpressionStatement" || |
| 56 | + node.body.expression.type !== "AssignmentExpression" |
| 57 | + ) { |
| 58 | + return; |
| 59 | + } |
| 60 | + // It is reactive variable declaration. |
| 61 | + const text = code.slice(...node.body.expression.range!); |
| 62 | + ctx.scriptLet.appendDeclareReactiveVar(text); |
| 63 | + } |
| 64 | + }, |
| 65 | + leaveNode() { |
| 66 | + /* noop */ |
| 67 | + }, |
| 68 | + }); |
| 69 | + ctx.scriptLet.appendDeclareMaybeStores(maybeStores); |
| 70 | +} |
| 71 | + |
| 72 | +/** |
| 73 | + * Append type declarations for store access. |
5 | 74 | * Append TypeScript code like
|
6 | 75 | * `declare let $foo: Parameters<Parameters<(typeof foo)["subscribe"]>[0]>[0];`
|
7 |
| - * to define the type information for like $foo variable. |
| 76 | + * to define the type information for like `$foo` variable. |
8 | 77 | */
|
9 |
| -export function appendDeclareStoreTypes(ctx: Context): void { |
10 |
| - const vcode = ctx.sourceCode.scripts.vcode; |
| 78 | +function appendDeclareStoreTypesFromText(vcode: string, ctx: Context): void { |
11 | 79 | const extractStoreRe = /\$[\p{ID_Start}$_][\p{ID_Continue}$\u200c\u200d]*/giu;
|
12 | 80 | let m;
|
13 | 81 | const maybeStores = new Set<string>();
|
|
0 commit comments