diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index bc3117e60d..7d97262128 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -192,6 +192,19 @@ function* generateSetupFunction( ]); } } + if (scriptSetupRanges.attrs.length) { + for (const { define } of scriptSetupRanges.attrs) { + setupCodeModifies.push([ + [`(`], + define.start, + define.start + ], [ + [` as __VLS_TemplateResult['attrs'] & Record)`], + define.end, + define.end + ]) + } + } if (scriptSetupRanges.cssModules.length) { for (const { define } of scriptSetupRanges.cssModules) { setupCodeModifies.push([ diff --git a/packages/language-core/lib/codegen/template/index.ts b/packages/language-core/lib/codegen/template/index.ts index a0776541a3..17ddef19d8 100644 --- a/packages/language-core/lib/codegen/template/index.ts +++ b/packages/language-core/lib/codegen/template/index.ts @@ -34,6 +34,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator { - yield 'var __VLS_inheritedAttrs!: {}'; + yield 'let __VLS_inheritedAttrs!: {}'; for (const varName of ctx.inheritedAttrVars) { yield ` & typeof ${varName}`; } yield endOfLine; + yield `var $attrs!: Partial & Record${endOfLine}`; } function* generateRefs(ctx: TemplateCodegenContext): Generator { diff --git a/packages/language-core/lib/parsers/scriptSetupRanges.ts b/packages/language-core/lib/parsers/scriptSetupRanges.ts index 433674d2f1..c4262e6295 100644 --- a/packages/language-core/lib/parsers/scriptSetupRanges.ts +++ b/packages/language-core/lib/parsers/scriptSetupRanges.ts @@ -48,6 +48,9 @@ export function parseScriptSetupRanges( name?: string; inheritAttrs?: string; } = {}; + const attrs: { + define: ReturnType; + }[] = []; const cssModules: { define: ReturnType; }[] = []; @@ -127,6 +130,7 @@ export function parseScriptSetupRanges( emits, expose, options, + attrs, cssModules, defineProp, templateRefs, @@ -377,14 +381,10 @@ export function parseScriptSetupRanges( } } } - } else if (vueCompilerOptions.composibles.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) { + } + else if (vueCompilerOptions.composibles.useAttrs.includes(callText)) { const define = parseDefineFunction(node); - let name; - if (ts.isVariableDeclaration(parent)) { - name = getNodeText(ts, parent.name, ast); - } - templateRefs.push({ - name, + attrs.push({ define }); } @@ -394,6 +394,17 @@ export function parseScriptSetupRanges( define }); } + else if (vueCompilerOptions.composibles.useTemplateRef.includes(callText) && node.arguments.length && !node.typeArguments?.length) { + const define = parseDefineFunction(node); + let name; + if (ts.isVariableDeclaration(parent)) { + name = getNodeText(ts, parent.name, ast); + } + templateRefs.push({ + name, + define + }); + } } ts.forEachChild(node, child => { parents.push(node); diff --git a/packages/language-core/lib/types.ts b/packages/language-core/lib/types.ts index feb015576c..eec3189ee1 100644 --- a/packages/language-core/lib/types.ts +++ b/packages/language-core/lib/types.ts @@ -44,6 +44,7 @@ export interface VueCompilerOptions { withDefaults: string[]; }; composibles: { + useAttrs: string[]; useCssModule: string[]; useTemplateRef: string[]; }; diff --git a/packages/language-core/lib/utils/ts.ts b/packages/language-core/lib/utils/ts.ts index 4b8ebda122..cc6cebb2fa 100644 --- a/packages/language-core/lib/utils/ts.ts +++ b/packages/language-core/lib/utils/ts.ts @@ -252,6 +252,7 @@ export function resolveVueCompilerOptions(vueOptions: Partial +import { type AnchorHTMLAttributes, type ReservedProps, useAttrs } from 'vue'; +import { exactType } from '../../shared'; + +type InheritedAttrs = Partial & Record; + +const attrs = useAttrs(); +exactType(attrs, {} as InheritedAttrs); + + + \ No newline at end of file