From 28ed0d8388caebb63e5740a74afdc8ed7285b6fd Mon Sep 17 00:00:00 2001 From: KazariEX <1364035137@qq.com> Date: Tue, 13 Aug 2024 21:37:27 +0800 Subject: [PATCH 1/2] fix: generate `ref` as identifier instead of interpolation --- .../lib/codegen/template/element.ts | 37 +++++++++++-------- 1 file changed, 22 insertions(+), 15 deletions(-) diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index c9bd547e13..c849f097b5 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -542,18 +542,20 @@ function* generateReferencesForElements( && prop.name === 'ref' && prop.value ) { + let [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; + ctx.accessExternalVariable(content, startOffset); } } } @@ -568,15 +570,7 @@ function* generateReferencesForScopedCssClasses( && prop.name === 'class' && prop.value ) { - let startOffset = prop.value.loc.start.offset; - let content = prop.value.loc.source; - if ( - (content.startsWith(`'`) && content.endsWith(`'`)) - || (content.startsWith(`"`) && content.endsWith(`"`)) - ) { - startOffset++; - content = content.slice(1, -1); - } + let [content, startOffset] = normalizeAttributeValue(prop.value); if (content) { let currentClassName = ''; for (const char of (content + ' ')) { @@ -622,3 +616,16 @@ function camelizeComponentName(newName: string) { 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]; +} \ No newline at end of file From df1ac34a5c844dbaa1fb78a4997ef2a2a9555c5c Mon Sep 17 00:00:00 2001 From: Johnson Chu Date: Mon, 26 Aug 2024 07:17:56 +0800 Subject: [PATCH 2/2] Update element.ts --- .../language-core/lib/codegen/template/element.ts | 11 ++--------- 1 file changed, 2 insertions(+), 9 deletions(-) diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index 1b3461c041..f9ce74932e 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -583,7 +583,7 @@ function* generateReferencesForElements( && prop.name === 'ref' && prop.value ) { - let [content, startOffset] = normalizeAttributeValue(prop.value); + const [content, startOffset] = normalizeAttributeValue(prop.value); yield `// @ts-ignore${newLine}`; yield `__VLS_ctx`; @@ -636,15 +636,8 @@ function* generateReferencesForScopedCssClasses( } } else { - let [content, startOffset] = normalizeAttributeValue(prop.value); 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);