Skip to content

feat(language-core): type support of $attrs #5076

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

Merged
merged 1 commit into from
Dec 20, 2024
Merged
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
5 changes: 4 additions & 1 deletion packages/language-core/lib/codegen/template/context.ts
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
}[] = [];
const emptyClassOffsets: number[] = [];
const inlayHints: InlayHintInfo[] = [];
const bindingAttrLocs: CompilerDOM.SourceLocation[] = [];
const inheritedAttrVars = new Set<string>();
const templateRefs = new Map<string, [varName: string, offset: number]>();

return {
Expand All @@ -140,7 +142,8 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
emptyClassOffsets,
inlayHints,
hasSlot: false,
inheritedAttrVars: new Set(),
bindingAttrLocs,
inheritedAttrVars,
templateRefs,
singleRootElType: undefined as string | undefined,
singleRootNode: undefined as CompilerDOM.ElementNode | undefined,
Expand Down
45 changes: 26 additions & 19 deletions packages/language-core/lib/codegen/template/elementProps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -240,28 +240,35 @@ export function* generateElementProps(
&& !prop.arg
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
) {
const codes = wrapWith(
prop.exp.loc.start.offset,
prop.exp.loc.end.offset,
ctx.codeFeatures.verification,
`...`,
...generatePropExp(
options,
ctx,
prop,
prop.exp,
ctx.codeFeatures.all,
false,
enableCodeFeatures
)
);
if (enableCodeFeatures) {
yield* codes;
if (prop.exp.loc.source === '$attrs') {
if (enableCodeFeatures) {
ctx.bindingAttrLocs.push(prop.exp.loc);
}
}
else {
yield toString([...codes]);
const codes = wrapWith(
prop.exp.loc.start.offset,
prop.exp.loc.end.offset,
ctx.codeFeatures.verification,
`...`,
...generatePropExp(
options,
ctx,
prop,
prop.exp,
ctx.codeFeatures.all,
false,
enableCodeFeatures
)
);
if (enableCodeFeatures) {
yield* codes;
}
else {
yield toString([...codes]);
}
yield `,${newLine}`;
}
yield `,${newLine}`;
}
}
}
Expand Down
17 changes: 15 additions & 2 deletions packages/language-core/lib/codegen/template/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,8 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
if (options.propsAssignName) {
ctx.addLocalVariable(options.propsAssignName);
}
// TODO: circular reference
// ctx.addLocalVariable('$attrs');
ctx.addLocalVariable(getSlotsPropertyName(options.vueCompilerOptions.target));
ctx.addLocalVariable('$attrs');
ctx.addLocalVariable('$refs');
ctx.addLocalVariable('$el');

Expand Down Expand Up @@ -100,6 +99,20 @@ function* generateInheritedAttrs(ctx: TemplateCodegenContext): Generator<Code> {
}
yield endOfLine;
yield `var $attrs!: Partial<typeof __VLS_inheritedAttrs> & Record<string, unknown>${endOfLine}`;

if (ctx.bindingAttrLocs.length) {
yield `[`;
for (const loc of ctx.bindingAttrLocs) {
yield [
loc.source,
'template',
loc.start.offset,
ctx.codeFeatures.all
];
yield `,`;
}
yield `]${endOfLine}`;
}
}

function* generateRefs(ctx: TemplateCodegenContext): Generator<Code> {
Expand Down
Loading