Skip to content

Commit aa3531f

Browse files
committed
fix(language-core): tolerance for incomplete root template tag
close #4893
1 parent 64f5da2 commit aa3531f

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

packages/language-core/lib/plugins/file-vue.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,24 @@ const plugin: VueLanguagePlugin = ({ vueCompilerOptions }) => {
1919
if (languageId !== 'vue') {
2020
return;
2121
}
22-
return parse(content);
22+
const sfc = parse(content);
23+
for (const error of sfc.errors) {
24+
// Handle 'Element is missing end tag.' error, see #4893
25+
if (
26+
'code' in error && error.code === 24 && sfc.descriptor.template
27+
&& error.loc?.start.line === sfc.descriptor.template.loc.start.line
28+
) {
29+
const template = sfc.descriptor.template;
30+
const templateText = template.content;
31+
const endTagOffset = templateText.lastIndexOf('<');
32+
const endTagText = templateText.slice(endTagOffset).trimEnd();
33+
if ('</template>'.startsWith(endTagText)) {
34+
sfc.descriptor.template.loc.end.offset = template.loc.start.offset + endTagOffset;
35+
template.content = templateText.slice(0, endTagOffset);
36+
}
37+
}
38+
}
39+
return sfc;
2340
},
2441

2542
updateSFC(sfc, change) {

0 commit comments

Comments
 (0)