From 2a729b81686848cf15d12e411366bf50e98df43f Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Sun, 17 Nov 2024 15:09:22 +0800 Subject: [PATCH 1/3] feat: type support for `useAttrs` --- .../lib/codegen/script/scriptSetup.ts | 13 ++++++++++ .../lib/parsers/scriptSetupRanges.ts | 25 +++++++++++++------ packages/language-core/lib/types.ts | 1 + packages/language-core/lib/utils/ts.ts | 1 + .../useAttrs/main.vue | 11 ++++++++ 5 files changed, 44 insertions(+), 7 deletions(-) create mode 100644 test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue 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/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'; + +const attrs = useAttrs(); +exactType(attrs, {} as Partial & Record); + + + \ No newline at end of file From 105d1d7ec4b275daccf11e976efd7172b6dc31f7 Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Sun, 17 Nov 2024 15:10:30 +0800 Subject: [PATCH 2/3] chore: lint --- .../fallthroughAttributes_strictTemplate/useAttrs/main.vue | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue b/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue index 7bd62653e4..d4b035b510 100644 --- a/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue +++ b/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue @@ -1,4 +1,4 @@ - \ No newline at end of file From 15a8ddc8299bf13a5b6a99337fc8b7f31523772e Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Tue, 19 Nov 2024 12:41:57 +0800 Subject: [PATCH 3/3] feat: `$attrs` --- packages/language-core/lib/codegen/template/index.ts | 4 +++- .../fallthroughAttributes_strictTemplate/useAttrs/main.vue | 6 ++++-- 2 files changed, 7 insertions(+), 3 deletions(-) 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/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue b/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue index d4b035b510..3bad154af3 100644 --- a/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue +++ b/test-workspace/tsc/passedFixtures/fallthroughAttributes_strictTemplate/useAttrs/main.vue @@ -2,10 +2,12 @@ import { type AnchorHTMLAttributes, type ReservedProps, useAttrs } from 'vue'; import { exactType } from '../../shared'; +type InheritedAttrs = Partial & Record; + const attrs = useAttrs(); -exactType(attrs, {} as Partial & Record); +exactType(attrs, {} as InheritedAttrs); \ No newline at end of file