UNPKG

vue-router

Version:
100 lines (99 loc) • 5.92 kB
/*! * vue-router v5.5.1 * (c) 2026 Eduardo San Martin Morote * @license MIT */ let muggle_string = require("muggle-string"); let pathe = require("pathe"); // src/volar/utils/augment-vls-ctx.ts /** * Augments the VLS context (volar) with additianal type information. * * @param content - content retrieved from the volar pluign * @param codes - codes to add to the VLS context */ function augmentVlsCtx(content, codes) { let from = -1; for (let i = 0; i < content.length; i++) { const code = content[i]; if (typeof code !== "string") continue; if (from === -1 && code.startsWith(`const __VLS_ctx`)) from = i; else if (from !== -1) { if (code === `}`) { content.splice(i, 0, ...codes.map((code) => `...${code},\n`)); break; } else if (code === `;\n`) { content.splice(from + 1, i - from, `{\n`, `...`, ...content.slice(from + 1, i), `,\n`, ...codes.map((code) => `...${code},\n`), `}`, `;\n`); break; } } } } // src/volar/entries/sfc-typed-router.ts const plugin = ({ compilerOptions, modules: { typescript: ts }, config: { options } }) => { const rootDir = options?.rootDir ?? compilerOptions.rootDir; if (!rootDir) console.warn("[vue-router] No rootDir specified. Set it in the Volar plugin options or tsconfig compilerOptions.rootDir for proper typed routes."); const RE = { DOLLAR_ROUTE: { /** * When using `$route` in a template, it is referred * to as `__VLS_ctx.$route` in the virtual file. */ VLS_CTX: /\b__VLS_ctx.\$route\b/g } }; return { version: 2.1, resolveEmbeddedCode(fileName, sfc, embeddedCode) { if (!embeddedCode.id.startsWith("script_")) return; const escapedFilePath = (rootDir ? (0, pathe.relative)(rootDir, fileName) : fileName).replace(/\\/g, "\\\\").replace(/'/g, "\\'"); const useRouteNameTypeParam = `<${`import('vue-router/auto-routes')._RouteNamesForFilePath<'${escapedFilePath}'>`}>`; const escapedSelfImport = `./${(0, pathe.basename)(fileName)}`.replace(/\\/g, "\\\\").replace(/'/g, "\\'"); const definePageTypeParams = `<'${escapedFilePath}'>`; const definePageWithPropsTypeParams = `<'${escapedFilePath}', typeof import('${escapedSelfImport}').default>`; const routePropsType = `import('vue-router/experimental')._RoutePropsForFilePath<'${escapedFilePath}'>`; const hasPropsTrue = !!sfc.scriptSetup && !sfc.scriptSetup.lang.startsWith("js") && sfc.scriptSetup.ast.statements.some((statement) => ts.isExpressionStatement(statement) && isDefinePageCall(statement.expression) && getPropsOption(statement.expression)?.kind === ts.SyntaxKind.TrueKeyword); if (sfc.scriptSetup) visit(sfc.scriptSetup.ast); function visit(node) { if (ts.isCallExpression(node) && ts.isIdentifier(node.expression) && ts.idText(node.expression) === "useRoute" && !node.typeArguments && !node.arguments.length) { if (!sfc.scriptSetup.lang.startsWith("js")) (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, node.expression.end, node.expression.end, useRouteNameTypeParam); else { const { start, end } = getStartEnd(node, sfc.scriptSetup.ast); (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, start, start, `(`); (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, end, end, ` as ReturnType<typeof useRoute${useRouteNameTypeParam}>)`); } } else if (ts.isTypeQueryNode(node) && ts.isIdentifier(node.exprName) && ts.idText(node.exprName) === "useRoute" && !node.typeArguments && !sfc.scriptSetup.lang.startsWith("js")) (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, node.exprName.end, node.exprName.end, useRouteNameTypeParam); else if (isDefinePageCall(node) && !node.typeArguments && node.arguments.length === 1 && !sfc.scriptSetup.lang.startsWith("js")) { const props = getPropsOption(node); (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, node.expression.end, node.expression.end, props && props.kind !== ts.SyntaxKind.FalseKeyword ? definePageWithPropsTypeParams : definePageTypeParams); } else if (hasPropsTrue && ts.isCallExpression(node) && ts.isIdentifier(node.expression) && ts.idText(node.expression) === "defineProps" && !node.arguments.length) { const [typeArg] = node.typeArguments ?? []; if (typeArg) { const { start } = getStartEnd(typeArg, sfc.scriptSetup.ast); (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, start, start, `${routePropsType} & (`); (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, typeArg.end, typeArg.end, `)`); } else (0, muggle_string.replaceSourceRange)(embeddedCode.content, sfc.scriptSetup.name, node.expression.end, node.expression.end, `<${routePropsType}>`); } else ts.forEachChild(node, visit); } const contentStr = (0, muggle_string.toString)(embeddedCode.content); const vlsCtxAugmentations = []; if (contentStr.match(RE.DOLLAR_ROUTE.VLS_CTX)) vlsCtxAugmentations.push(`{} as { $route: ReturnType<typeof import('vue-router').useRoute${useRouteNameTypeParam}> }`); if (vlsCtxAugmentations.length) augmentVlsCtx(embeddedCode.content, vlsCtxAugmentations); } }; function isDefinePageCall(node) { return ts.isCallExpression(node) && ts.isIdentifier(node.expression) && ts.idText(node.expression) === "definePage"; } /** * Returns the value of `props` in `definePage({ props })`. */ function getPropsOption(definePageCall) { const [arg] = definePageCall.arguments; if (!arg || !ts.isObjectLiteralExpression(arg)) return void 0; for (const prop of arg.properties) if (ts.isPropertyAssignment(prop) && ts.isIdentifier(prop.name) && ts.idText(prop.name) === "props") return prop.initializer; } function getStartEnd(node, ast) { return { start: ts.getTokenPosOfNode(node, ast), end: node.end }; } }; module.exports = plugin;