Skip to content

Commit

Permalink
fix: cannot recognize namespace component in template
Browse files Browse the repository at this point in the history
close #1726
  • Loading branch information
johnsoncodehk committed Aug 27, 2022
1 parent 28e8f92 commit 3d84c0d
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
1 change: 1 addition & 0 deletions packages/vue-language-core/src/generators/script.ts
Expand Up @@ -652,6 +652,7 @@ export function generate(

codeGen.addText(`/* CSS variable injection */\n`);
const cssIds = writeCssVars();
codeGen.addText(`/* CSS variable injection end */\n`);

if (htmlGen) {
mergeCodeGen(codeGen, htmlGen.codeGen);
Expand Down
20 changes: 9 additions & 11 deletions packages/vue-language-core/src/generators/template.ts
Expand Up @@ -82,6 +82,7 @@ export function generate(
const tagOffsetsMap: Record<string, number[]> = {};
const tagResolves: Record<string, {
component: string,
isNamespacedTag: boolean,
emit: string,
offsets: number[],
} | undefined> = {};
Expand Down Expand Up @@ -119,18 +120,16 @@ export function generate(
const tagRanges = tagOffsets.map(offset => ({ start: offset, end: offset + tagName.length }));
const isNamespacedTag = tagName.indexOf('.') >= 0;

const var_componentVar = capitalize(camelize(tagName.replace(/:/g, '-')));
const var_componentVar = isNamespacedTag ? `__VLS_ctx.${tagName}` : capitalize(camelize(tagName.replace(/:/g, '-')));
const var_emit = `__VLS_${elementIndex++}`;

if (isNamespacedTag) {

identifiers.add(tagName.split('.')[0]);

for (let i = 0; i < tagRanges.length; i++) {
const tagRange = tagRanges[i];
if (i === 0) {
tsCodeGen.addText(`declare const ${var_componentVar}: typeof __VLS_ctx.`);
}
else {
tsCodeGen.addText(`declare const __VLS_${elementIndex++}: typeof __VLS_ctx.`);
}
tsCodeGen.addText(`declare const __VLS_${elementIndex++}: typeof __VLS_ctx.`);
writeCode(
tagName,
tagRange,
Expand Down Expand Up @@ -211,6 +210,7 @@ export function generate(

tagResolves[tagName] = {
component: var_componentVar,
isNamespacedTag,
emit: var_emit,
offsets: tagOffsets,
};
Expand Down Expand Up @@ -489,10 +489,8 @@ export function generate(
const fullTagStart = tsCodeGen.getText().length;
const tagCapabilities = {
...capabilitiesSet.diagnosticOnly,
...capabilitiesSet.tagHover,
...(_isIntrinsicElement ? {
...capabilitiesSet.tagReference,
} : {})
...(tagResolves[node.tag]?.isNamespacedTag ? {} : capabilitiesSet.tagHover),
...(_isIntrinsicElement ? capabilitiesSet.tagReference : {})
};
const endTagOffset = !node.isSelfClosing && sourceLang === 'html' ? node.loc.start.offset + node.loc.source.lastIndexOf(node.tag) : undefined;

Expand Down
@@ -0,0 +1,2 @@
<script lang="ts" setup>
</script>
@@ -0,0 +1,9 @@
<template>
<Namespace.Comp />
</template>

<script setup lang="ts">
import Comp from './component.vue';
const Namespace = { Comp };
</script>

0 comments on commit 3d84c0d

Please sign in to comment.