Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(language-core): type support for useAttrs and $attrs #5011

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/language-core/lib/codegen/script/scriptSetup.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string, unknown>)`],
define.end,
define.end
])
}
}
if (scriptSetupRanges.cssModules.length) {
for (const { define } of scriptSetupRanges.cssModules) {
setupCodeModifies.push([
Expand Down
4 changes: 3 additions & 1 deletion packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
if (options.propsAssignName) {
ctx.addLocalVariable(options.propsAssignName);
}
ctx.addLocalVariable("$attrs");
ctx.addLocalVariable('$el');
ctx.addLocalVariable('$refs');

Expand Down Expand Up @@ -91,11 +92,12 @@ function* generateSlotsType(options: TemplateCodegenOptions, ctx: TemplateCodege
}

function* generateInheritedAttrs(ctx: TemplateCodegenContext): Generator<Code> {
yield 'var __VLS_inheritedAttrs!: {}';
yield 'let __VLS_inheritedAttrs!: {}';
for (const varName of ctx.inheritedAttrVars) {
yield ` & typeof ${varName}`;
}
yield endOfLine;
yield `var $attrs!: Partial<typeof __VLS_inheritedAttrs> & Record<string, unknown>${endOfLine}`;
}

function* generateRefs(ctx: TemplateCodegenContext): Generator<Code> {
Expand Down
25 changes: 18 additions & 7 deletions packages/language-core/lib/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,9 @@ export function parseScriptSetupRanges(
name?: string;
inheritAttrs?: string;
} = {};
const attrs: {
define: ReturnType<typeof parseDefineFunction>;
}[] = [];
const cssModules: {
define: ReturnType<typeof parseDefineFunction>;
}[] = [];
Expand Down Expand Up @@ -127,6 +130,7 @@ export function parseScriptSetupRanges(
emits,
expose,
options,
attrs,
cssModules,
defineProp,
templateRefs,
Expand Down Expand Up @@ -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
});
}
Expand All @@ -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);
Expand Down
1 change: 1 addition & 0 deletions packages/language-core/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ export interface VueCompilerOptions {
withDefaults: string[];
};
composibles: {
useAttrs: string[];
useCssModule: string[];
useTemplateRef: string[];
};
Expand Down
1 change: 1 addition & 0 deletions packages/language-core/lib/utils/ts.ts
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,7 @@ export function resolveVueCompilerOptions(vueOptions: Partial<VueCompilerOptions
...vueOptions.macros,
},
composibles: {
useAttrs: ['useAttrs'],
useCssModule: ['useCssModule'],
useTemplateRef: ['useTemplateRef', 'templateRef'],
...vueOptions.composibles,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<script setup lang="ts">
import { type AnchorHTMLAttributes, type ReservedProps, useAttrs } from 'vue';
import { exactType } from '../../shared';

type InheritedAttrs = Partial<AnchorHTMLAttributes & ReservedProps> & Record<string, unknown>;

const attrs = useAttrs();
exactType(attrs, {} as InheritedAttrs);
</script>

<template>
<a>{{ exactType($attrs, {} as InheritedAttrs) }}</a>
</template>