Skip to content

Commit 8cf123f

Browse files
authored
feat(language-core): type support of $attrs (#5076)
1 parent 54f630e commit 8cf123f

File tree

3 files changed

+45
-22
lines changed

3 files changed

+45
-22
lines changed

packages/language-core/lib/codegen/template/context.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,8 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
125125
}[] = [];
126126
const emptyClassOffsets: number[] = [];
127127
const inlayHints: InlayHintInfo[] = [];
128+
const bindingAttrLocs: CompilerDOM.SourceLocation[] = [];
129+
const inheritedAttrVars = new Set<string>();
128130
const templateRefs = new Map<string, [varName: string, offset: number]>();
129131

130132
return {
@@ -140,7 +142,8 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
140142
emptyClassOffsets,
141143
inlayHints,
142144
hasSlot: false,
143-
inheritedAttrVars: new Set(),
145+
bindingAttrLocs,
146+
inheritedAttrVars,
144147
templateRefs,
145148
singleRootElType: undefined as string | undefined,
146149
singleRootNode: undefined as CompilerDOM.ElementNode | undefined,

packages/language-core/lib/codegen/template/elementProps.ts

Lines changed: 26 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -240,28 +240,35 @@ export function* generateElementProps(
240240
&& !prop.arg
241241
&& prop.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
242242
) {
243-
const codes = wrapWith(
244-
prop.exp.loc.start.offset,
245-
prop.exp.loc.end.offset,
246-
ctx.codeFeatures.verification,
247-
`...`,
248-
...generatePropExp(
249-
options,
250-
ctx,
251-
prop,
252-
prop.exp,
253-
ctx.codeFeatures.all,
254-
false,
255-
enableCodeFeatures
256-
)
257-
);
258-
if (enableCodeFeatures) {
259-
yield* codes;
243+
if (prop.exp.loc.source === '$attrs') {
244+
if (enableCodeFeatures) {
245+
ctx.bindingAttrLocs.push(prop.exp.loc);
246+
}
260247
}
261248
else {
262-
yield toString([...codes]);
249+
const codes = wrapWith(
250+
prop.exp.loc.start.offset,
251+
prop.exp.loc.end.offset,
252+
ctx.codeFeatures.verification,
253+
`...`,
254+
...generatePropExp(
255+
options,
256+
ctx,
257+
prop,
258+
prop.exp,
259+
ctx.codeFeatures.all,
260+
false,
261+
enableCodeFeatures
262+
)
263+
);
264+
if (enableCodeFeatures) {
265+
yield* codes;
266+
}
267+
else {
268+
yield toString([...codes]);
269+
}
270+
yield `,${newLine}`;
263271
}
264-
yield `,${newLine}`;
265272
}
266273
}
267274
}

packages/language-core/lib/codegen/template/index.ts

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,8 @@ export function* generateTemplate(options: TemplateCodegenOptions): Generator<Co
3535
if (options.propsAssignName) {
3636
ctx.addLocalVariable(options.propsAssignName);
3737
}
38-
// TODO: circular reference
39-
// ctx.addLocalVariable('$attrs');
4038
ctx.addLocalVariable(getSlotsPropertyName(options.vueCompilerOptions.target));
39+
ctx.addLocalVariable('$attrs');
4140
ctx.addLocalVariable('$refs');
4241
ctx.addLocalVariable('$el');
4342

@@ -100,6 +99,20 @@ function* generateInheritedAttrs(ctx: TemplateCodegenContext): Generator<Code> {
10099
}
101100
yield endOfLine;
102101
yield `var $attrs!: Partial<typeof __VLS_inheritedAttrs> & Record<string, unknown>${endOfLine}`;
102+
103+
if (ctx.bindingAttrLocs.length) {
104+
yield `[`;
105+
for (const loc of ctx.bindingAttrLocs) {
106+
yield [
107+
loc.source,
108+
'template',
109+
loc.start.offset,
110+
ctx.codeFeatures.all
111+
];
112+
yield `,`;
113+
}
114+
yield `]${endOfLine}`;
115+
}
103116
}
104117

105118
function* generateRefs(ctx: TemplateCodegenContext): Generator<Code> {

0 commit comments

Comments
 (0)