Skip to content

Commit 079b8fd

Browse files
committed
fix(volar): fix volar plugin
1 parent c073083 commit 079b8fd

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

volar/index.js

Lines changed: 50 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -31,70 +31,68 @@ const plugin = _ => ({
3131
}
3232

3333
// Add TemplateTags typings to autocomplete root in `css()`
34-
const templateTags = {}
35-
walkElementNodes(
36-
sfc.templateAst,
37-
({ tag }) => {
38-
templateTags[tag] = true
39-
}
40-
)
41-
42-
embeddedFile.codeGen.addText(`type ComponentTemplateTags__VLS = {\n${Object.entries(templateTags).map(
43-
([tag]) => {
44-
return ` /**
45-
* The \`<${tag}>\` tag from the Vue template.
46-
*/
47-
${tag}: true,\n`
34+
if (sfc.template && sfc.template.content) {
35+
const templateTags = {}
36+
walkElementNodes(
37+
sfc.templateAst,
38+
({ tag }) => {
39+
templateTags[tag] = true
4840
}
49-
).join('')}
50-
}\n`)
41+
)
42+
embeddedFile.codeGen.addText(`type ComponentTemplateTags__VLS = {\n${Object.entries(templateTags).map(([tag]) => ` /**\n * The \`<${tag}>\` tag from the Vue template.\n */\n ${tag}: true,\n`).join('')}}\n`)
5143

52-
// $variantProps()
53-
embeddedFile.codeGen.addText('\ndeclare const $variantsProps: (key: keyof ComponentTemplateTags__VLS) => {}\n')
44+
// $variantProps()
45+
embeddedFile.codeGen.addText('\ndeclare const $variantsProps: (key: keyof ComponentTemplateTags__VLS) => {}\n')
5446

55-
const templateDtMatches = sfc.template.content.match(dtRegex)
56-
if (templateDtMatches) {
57-
sfc.template.content.replace(
58-
dtRegex,
59-
(match, dtKey, index) => addDt(match, dtKey, index, sfc.template.tag)
60-
)
47+
const templateDtMatches = sfc.template.content.match(dtRegex)
48+
if (templateDtMatches) {
49+
sfc.template.content.replace(
50+
dtRegex,
51+
(match, dtKey, index) => addDt(match, dtKey, index, sfc.template.tag)
52+
)
53+
}
54+
} else {
55+
embeddedFile.codeGen.addText('type ComponentTemplateTags__VLS = {}')
6156
}
6257

6358
// Grab `css()` function and type it.
6459
for (let i = 0; i < sfc.styles.length; i++) {
6560
const style = sfc.styles[i]
66-
const cssMatches = style.content.match(/css\(([\s\S]*)\)/)
67-
const dtMatches = style.content.match(dtRegex)
6861

69-
if (cssMatches) {
70-
embeddedFile.codeGen.addText('declare const css: (declaration: import(\'@nuxtjs/design-tokens\').CSS<ComponentTemplateTags__VLS, import(\'@nuxtjs/design-tokens\').NuxtStyleTheme>) => any')
62+
if (style?.content) {
63+
const cssMatches = style.content.match(/css\(([\s\S]*)\)/)
64+
const dtMatches = style.content.match(dtRegex)
65+
66+
if (cssMatches) {
67+
embeddedFile.codeGen.addText('declare const css: (declaration: import(\'@nuxtjs/design-tokens\').CSS<ComponentTemplateTags__VLS, import(\'@nuxtjs/design-tokens\').NuxtStyleTheme>) => any')
7168

72-
embeddedFile.codeGen.addText('\nconst __VLS_css = ')
73-
embeddedFile.codeGen.addCode2(
74-
cssMatches[0],
75-
cssMatches.index,
76-
{
77-
vueTag: 'style',
78-
vueTagIndex: i,
79-
capabilities: {
80-
basic: true,
81-
references: true,
82-
definitions: true,
83-
diagnostic: true,
84-
rename: true,
85-
completion: true,
86-
semanticTokens: true
69+
embeddedFile.codeGen.addText('\nconst __VLS_css = ')
70+
embeddedFile.codeGen.addCode2(
71+
cssMatches[0],
72+
cssMatches.index,
73+
{
74+
vueTag: 'style',
75+
vueTagIndex: i,
76+
capabilities: {
77+
basic: true,
78+
references: true,
79+
definitions: true,
80+
diagnostic: true,
81+
rename: true,
82+
completion: true,
83+
semanticTokens: true
84+
}
8785
}
88-
}
89-
)
90-
embeddedFile.codeGen.addText('\n')
91-
}
86+
)
87+
embeddedFile.codeGen.addText('\n')
88+
}
9289

93-
if (dtMatches) {
94-
style.content.replace(
95-
dtRegex,
96-
(match, dtKey, index) => addDt(match, dtKey, index, style.tag, i)
97-
)
90+
if (dtMatches) {
91+
style.content.replace(
92+
dtRegex,
93+
(match, dtKey, index) => addDt(match, dtKey, index, style.tag, i)
94+
)
95+
}
9896
}
9997
}
10098
}

0 commit comments

Comments
 (0)