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

fix(language-core): generate ref as identifier instead of interpolation #4688

Merged
merged 3 commits into from
Aug 25, 2024
Merged
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
40 changes: 25 additions & 15 deletions packages/language-core/lib/codegen/template/element.ts
Original file line number Diff line number Diff line change
Expand Up @@ -583,19 +583,24 @@ function* generateReferencesForElements(
&& prop.name === 'ref'
&& prop.value
) {
const [content, startOffset] = normalizeAttributeValue(prop.value);

yield `// @ts-ignore${newLine}`;
yield* generateInterpolation(
yield `__VLS_ctx`;
yield* generatePropertyAccess(
options,
ctx,
prop.value.content,
prop.value.loc,
prop.value.loc.start.offset + 1,
content,
startOffset,
ctx.codeFeatures.navigation,
'(',
')'
prop.value.loc
);
yield endOfLine;

if (variableNameRegex.test(content)) {
ctx.accessExternalVariable(content, startOffset);
}

const refName = CompilerDOM.toValidAssetId(prop.value.content, '_VLS_refs' as any);
options.templateRefNames.set(prop.value.content, refName);
return refName;
Expand Down Expand Up @@ -631,16 +636,8 @@ function* generateReferencesForScopedCssClasses(
}
}
else {
let startOffset = prop.value.loc.start.offset;
let content = prop.value.loc.source;
let isWrapped = false;
if (
(content.startsWith(`'`) && content.endsWith(`'`))
|| (content.startsWith(`"`) && content.endsWith(`"`))
) {
content = content.slice(1, -1);
isWrapped = true;
}
const [content, startOffset] = normalizeAttributeValue(prop.value);
if (content) {
const classes = collectClasses(content, startOffset + (isWrapped ? 1 : 0));
ctx.scopedClasses.push(...classes);
Expand Down Expand Up @@ -746,6 +743,19 @@ function getTagRenameApply(oldName: string) {
return oldName === hyphenateTag(oldName) ? hyphenateTag : undefined;
}

function normalizeAttributeValue(node: CompilerDOM.TextNode): [string, number] {
let offset = node.loc.start.offset;
let content = node.loc.source;
if (
(content.startsWith(`'`) && content.endsWith(`'`))
|| (content.startsWith(`"`) && content.endsWith(`"`))
) {
offset++;
content = content.slice(1, -1);
}
return [content, offset];
}

function collectClasses(content: string, startOffset = 0) {
const classes: {
source: string;
Expand Down