Skip to content

Commit 562aecd

Browse files
committed
fix: complete codegen of slot name prop
1 parent 07a1baa commit 562aecd

File tree

3 files changed

+54
-16
lines changed

3 files changed

+54
-16
lines changed

Diff for: packages/language-core/lib/codegen/template/context.ts

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ const _codeFeatures = {
3838
navigation: true,
3939
completion: { isAdditional: true },
4040
} as VueCodeInformation,
41+
navigationAndVerification: {
42+
navigation: true,
43+
verification: true,
44+
} as VueCodeInformation,
4145
withoutNavigation: {
4246
verification: true,
4347
completion: true,

Diff for: packages/language-core/lib/codegen/template/elementProps.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ export function* generateElementProps(
271271
}
272272
}
273273

274-
function* generatePropExp(
274+
export function* generatePropExp(
275275
options: TemplateCodegenOptions,
276276
ctx: TemplateCodegenContext,
277277
prop: CompilerDOM.DirectiveNode,

Diff for: packages/language-core/lib/codegen/template/slotOutlet.ts

+49-15
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@ import { createVBindShorthandInlayHintInfo } from '../inlayHints';
44
import { endOfLine, newLine, wrapWith } from '../utils';
55
import type { TemplateCodegenContext } from './context';
66
import { generateElementChildren } from './elementChildren';
7-
import { generateElementProps } from './elementProps';
7+
import { generateElementProps, generatePropExp } from './elementProps';
88
import type { TemplateCodegenOptions } from './index';
99
import { generateInterpolation } from './interpolation';
10+
import { generatePropertyAccess } from './propertyAccess';
1011

1112
export function* generateSlotOutlet(
1213
options: TemplateCodegenOptions,
@@ -30,23 +31,56 @@ export function* generateSlotOutlet(
3031

3132
if (options.hasDefineSlots) {
3233
yield `__VLS_normalizeSlot(`;
33-
yield* wrapWith(
34-
node.loc.start.offset,
35-
node.loc.end.offset,
36-
ctx.codeFeatures.verification,
37-
`${options.slotsAssignName ?? '__VLS_slots'}[`,
38-
...wrapWith(
34+
if (nameProp) {
35+
let codes: Generator<Code> | Code[];
36+
if (nameProp.type === CompilerDOM.NodeTypes.ATTRIBUTE && nameProp.value) {
37+
let { source, start: { offset } } = nameProp.value.loc;
38+
if (source.startsWith('"') || source.startsWith("'")) {
39+
source = source.slice(1, -1);
40+
offset++;
41+
}
42+
codes = generatePropertyAccess(
43+
options,
44+
ctx,
45+
source,
46+
offset,
47+
ctx.codeFeatures.navigationAndVerification
48+
);
49+
}
50+
else if (nameProp.type === CompilerDOM.NodeTypes.DIRECTIVE && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION) {
51+
codes = [
52+
`[`,
53+
...generatePropExp(
54+
options,
55+
ctx,
56+
nameProp,
57+
nameProp.exp,
58+
ctx.codeFeatures.all,
59+
true
60+
),
61+
`]`
62+
];
63+
}
64+
else {
65+
codes = [`['default']`];
66+
}
67+
68+
yield* wrapWith(
69+
nameProp.loc.start.offset,
70+
nameProp.loc.end.offset,
71+
ctx.codeFeatures.verification,
72+
`${options.slotsAssignName ?? '__VLS_slots'}`,
73+
...codes
74+
);
75+
}
76+
else {
77+
yield* wrapWith(
3978
node.loc.start.offset,
4079
node.loc.end.offset,
4180
ctx.codeFeatures.verification,
42-
nameProp?.type === CompilerDOM.NodeTypes.ATTRIBUTE && nameProp.value
43-
? `'${nameProp.value.content}'`
44-
: nameProp?.type === CompilerDOM.NodeTypes.DIRECTIVE && nameProp.exp?.type === CompilerDOM.NodeTypes.SIMPLE_EXPRESSION
45-
? nameProp.exp.content
46-
: `('default' as const)`
47-
),
48-
`]`
49-
);
81+
`${options.slotsAssignName ?? '__VLS_slots'}['default']`
82+
);
83+
}
5084
yield `)?.(`;
5185
yield* wrapWith(
5286
startTagOffset,

0 commit comments

Comments
 (0)