From d6faaf1f96c565385622f4e75a63391837f729d7 Mon Sep 17 00:00:00 2001 From: KazariEX Date: Fri, 21 Nov 2025 02:41:39 +0800 Subject: [PATCH] refactor: compat with language tools v3.1 --- src/volar/entries/sfc-typed-router.ts | 97 +++++++++++++++------------ src/volar/utils/augment-vls-ctx.ts | 8 +-- 2 files changed, 58 insertions(+), 47 deletions(-) diff --git a/src/volar/entries/sfc-typed-router.ts b/src/volar/entries/sfc-typed-router.ts index 2f1a91841..9edca0247 100644 --- a/src/volar/entries/sfc-typed-router.ts +++ b/src/volar/entries/sfc-typed-router.ts @@ -1,7 +1,8 @@ import { relative } from 'pathe' import type { VueLanguagePlugin } from '@vue/language-core' -import { replaceAll, toString } from 'muggle-string' +import { replaceSourceRange, toString } from 'muggle-string' import { augmentVlsCtx } from '../utils/augment-vls-ctx' +import type ts from 'typescript' /* Future ideas: @@ -13,21 +14,11 @@ import { augmentVlsCtx } from '../utils/augment-vls-ctx' - (low priority) Enhance typing of `to` route in `beforeEnter` route guards defined in `definePage` */ -const plugin: VueLanguagePlugin = (ctx) => { +const plugin: VueLanguagePlugin = ({ + compilerOptions, + modules: { typescript: ts }, +}) => { const RE = { - USE_ROUTE: { - /** - * Targets the spot between `useRoute` and `()` - */ - BEFORE_PARENTHESES: /(?<=useRoute)(\s*)(?=\(\))/g, - /** - * Targets the spot right before `useRoute()` - */ - BEFORE: /(?=useRoute(\s*)\(\))/g, - /** Targets the spot right after `useRoute()` */ - AFTER: /(?<=useRoute(\s*)\(\))/g, - }, - DOLLAR_ROUTE: { /** * When using `$route` in a template, it is referred @@ -39,7 +30,7 @@ const plugin: VueLanguagePlugin = (ctx) => { return { version: 2.1, - resolveEmbeddedCode(fileName, _sfc, embeddedCode) { + resolveEmbeddedCode(fileName, sfc, embeddedCode) { if (!embeddedCode.id.startsWith('script_')) { return } @@ -47,31 +38,54 @@ const plugin: VueLanguagePlugin = (ctx) => { // TODO: Do we want to apply this to EVERY .vue file or only to components that the user wrote themselves? // NOTE: this might not work if different from the root passed to VueRouter unplugin - const relativeFilePath = ctx.compilerOptions.baseUrl - ? relative(ctx.compilerOptions.baseUrl, fileName) + const relativeFilePath = compilerOptions.baseUrl + ? relative(compilerOptions.baseUrl, fileName) : fileName const useRouteNameType = `import('vue-router/auto-routes')._RouteNamesForFilePath<'${relativeFilePath}'>` const useRouteNameTypeParam = `<${useRouteNameType}>` const typedCall = `useRoute${useRouteNameTypeParam}` - if (embeddedCode.id.startsWith('script_ts')) { - // Inserts type param into `useRoute()` calls. - // We only apply this mutation on