Skip to content

Commit ebc8710

Browse files
authored
fix(language-core): generate ref as identifier instead of interpolation (#4688)
1 parent 22eea60 commit ebc8710

File tree

1 file changed

+25
-15
lines changed
  • packages/language-core/lib/codegen/template

1 file changed

+25
-15
lines changed

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

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -583,19 +583,24 @@ function* generateReferencesForElements(
583583
&& prop.name === 'ref'
584584
&& prop.value
585585
) {
586+
const [content, startOffset] = normalizeAttributeValue(prop.value);
587+
586588
yield `// @ts-ignore${newLine}`;
587-
yield* generateInterpolation(
589+
yield `__VLS_ctx`;
590+
yield* generatePropertyAccess(
588591
options,
589592
ctx,
590-
prop.value.content,
591-
prop.value.loc,
592-
prop.value.loc.start.offset + 1,
593+
content,
594+
startOffset,
593595
ctx.codeFeatures.navigation,
594-
'(',
595-
')'
596+
prop.value.loc
596597
);
597598
yield endOfLine;
598599

600+
if (variableNameRegex.test(content)) {
601+
ctx.accessExternalVariable(content, startOffset);
602+
}
603+
599604
const refName = CompilerDOM.toValidAssetId(prop.value.content, '_VLS_refs' as any);
600605
options.templateRefNames.set(prop.value.content, refName);
601606
return refName;
@@ -631,16 +636,8 @@ function* generateReferencesForScopedCssClasses(
631636
}
632637
}
633638
else {
634-
let startOffset = prop.value.loc.start.offset;
635-
let content = prop.value.loc.source;
636639
let isWrapped = false;
637-
if (
638-
(content.startsWith(`'`) && content.endsWith(`'`))
639-
|| (content.startsWith(`"`) && content.endsWith(`"`))
640-
) {
641-
content = content.slice(1, -1);
642-
isWrapped = true;
643-
}
640+
const [content, startOffset] = normalizeAttributeValue(prop.value);
644641
if (content) {
645642
const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
646643
ctx.scopedClasses.push(...classes);
@@ -746,6 +743,19 @@ function getTagRenameApply(oldName: string) {
746743
return oldName === hyphenateTag(oldName) ? hyphenateTag : undefined;
747744
}
748745

746+
function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, number] {
747+
let offset = node.loc.start.offset;
748+
let content = node.loc.source;
749+
if (
750+
(content.startsWith(`'`) && content.endsWith(`'`))
751+
|| (content.startsWith(`"`) && content.endsWith(`"`))
752+
) {
753+
offset++;
754+
content = content.slice(1, -1);
755+
}
756+
return [content, offset];
757+
}
758+
749759
function collectClasses(content: string, startOffset = 0) {
750760
const classes: {
751761
source: string;

0 commit comments

Comments
 (0)