From edf161a9c9832cc5963ea5bdbdd91309b48d35ca Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Thu, 19 Dec 2024 05:56:33 +0800 Subject: [PATCH] fix(language-core): do not generate useTemplateRef parameter repeatedly fixes https://github.com/vuejs/language-tools/issues/5009#issuecomment-2534832312 --- .../language-core/lib/codegen/globalTypes.ts | 1 + .../lib/codegen/script/scriptSetup.ts | 21 +++++++++++++------ .../language-server/tests/renaming.spec.ts | 16 +++++++------- 3 files changed, 24 insertions(+), 14 deletions(-) diff --git a/packages/language-core/lib/codegen/globalTypes.ts b/packages/language-core/lib/codegen/globalTypes.ts index 279b243316..d34f4e924a 100644 --- a/packages/language-core/lib/codegen/globalTypes.ts +++ b/packages/language-core/lib/codegen/globalTypes.ts @@ -15,6 +15,7 @@ export function generateGlobalTypes(lib: string, target: number, strictTemplates const __VLS_intrinsicElements: __VLS_IntrinsicElements; const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any }; const __VLS_unref: typeof import('${lib}').unref; + const __VLS_placeholder: any; const __VLS_nativeElements = { ...{} as SVGElementTagNameMap, diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index 4f8a973244..bd4b7345e8 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -226,17 +226,19 @@ function* generateSetupFunction( } const isTs = options.lang !== 'js' && options.lang !== 'jsx'; for (const { callExp, exp, arg } of scriptSetupRanges.useTemplateRef) { - const templateRefType = arg ? [ - `__VLS_TemplateResult['refs'][`, - generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation), - `]` - ] : [`unknown`]; + const templateRefType = arg + ? [ + `__VLS_TemplateResult['refs'][`, + generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.all), + `]` + ] + : [`unknown`]; if (isTs) { setupCodeModifies.push([ [ `<`, ...templateRefType, - `, keyof __VLS_TemplateResult['refs']>` + `>` ], exp.end, exp.end @@ -257,6 +259,13 @@ function* generateSetupFunction( callExp.end ]); } + if (arg) { + setupCodeModifies.push([ + [`(__VLS_placeholder)`], + arg.start, + arg.end + ]); + } } setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]); diff --git a/packages/language-server/tests/renaming.spec.ts b/packages/language-server/tests/renaming.spec.ts index d01491c9b1..09cc6cf7c4 100644 --- a/packages/language-server/tests/renaming.spec.ts +++ b/packages/language-server/tests/renaming.spec.ts @@ -939,12 +939,12 @@ describe('Renaming', async () => { "newText": "bar", "range": { "end": { - "character": 34, - "line": 7, + "character": 16, + "line": 2, }, "start": { - "character": 31, - "line": 7, + "character": 13, + "line": 2, }, }, }, @@ -952,12 +952,12 @@ describe('Renaming', async () => { "newText": "bar", "range": { "end": { - "character": 16, - "line": 2, + "character": 34, + "line": 7, }, "start": { - "character": 13, - "line": 2, + "character": 31, + "line": 7, }, }, },