Skip to content

Commit ed750e2

Browse files
committed
refactor(language-core): #5766 for pug
1 parent 4a9932c commit ed750e2

File tree

3 files changed

+22
-26
lines changed

3 files changed

+22
-26
lines changed

packages/language-core/lib/plugins/vue-template-html.ts

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,13 @@ type Node =
1616
const shouldAddSuffix = /(?<=<[^>/]+)$/;
1717

1818
const plugin: VueLanguagePlugin = ({ modules }) => {
19+
const CompilerDOM = modules['@vue/compiler-dom'];
20+
1921
return {
2022
version: 2.2,
2123

2224
compileSFCTemplate(lang, template, options) {
2325
if (lang === 'html' || lang === 'md') {
24-
const compiler = modules['@vue/compiler-dom'];
25-
2626
let addedSuffix = false;
2727

2828
// #4583
@@ -31,29 +31,18 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
3131
addedSuffix = true;
3232
}
3333

34-
const ast = compiler.parse(template, {
34+
const ast = CompilerDOM.parse(template, {
3535
...options,
3636
comments: true,
3737
});
38+
CompilerDOM.transform(ast, options);
3839

39-
const [nodeTransforms, directiveTransforms] = compiler.getBaseTransformPreset();
40-
compiler.transform(ast, {
41-
...options,
42-
nodeTransforms: [
43-
...nodeTransforms,
44-
...options.nodeTransforms ?? [],
45-
],
46-
directiveTransforms: {
47-
...directiveTransforms,
48-
...options.directiveTransforms,
49-
},
50-
});
51-
52-
// @ts-expect-error
5340
return {
5441
ast,
42+
code: '',
43+
preamble: '',
5544
__addedSuffix: addedSuffix,
56-
} as CompilerDOM.CodegenResult;
45+
};
5746
}
5847
},
5948

@@ -70,8 +59,6 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
7059
}
7160
}
7261

73-
const CompilerDOM = modules['@vue/compiler-dom'];
74-
7562
const lengthDiff = change.newText.length - (change.end - change.start);
7663
let hitNodes: Node[] = [];
7764

packages/language-core/lib/virtualFile/computedSfc.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type * as CompilerDOM from '@vue/compiler-dom';
1+
import * as CompilerDOM from '@vue/compiler-dom';
22
import type { SFCBlock, SFCParseResult } from '@vue/compiler-sfc';
33
import { computed, setActiveSub } from 'alien-signals';
44
import type * as ts from 'typescript';
@@ -281,10 +281,13 @@ export function computedSfc(
281281

282282
const errors: CompilerDOM.CompilerError[] = [];
283283
const warnings: CompilerDOM.CompilerError[] = [];
284+
const [nodeTransforms, directiveTransforms] = CompilerDOM.getBaseTransformPreset();
284285
let options: CompilerDOM.CompilerOptions = {
285-
onError: (err: CompilerDOM.CompilerError) => errors.push(err),
286-
onWarn: (err: CompilerDOM.CompilerError) => warnings.push(err),
286+
onError: err => errors.push(err),
287+
onWarn: err => warnings.push(err),
287288
expressionPlugins: ['typescript'],
289+
nodeTransforms,
290+
directiveTransforms,
288291
};
289292

290293
for (const plugin of plugins) {

packages/language-plugin-pug/index.ts

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ import * as pug from 'volar-service-pug/lib/languageService';
66
const classRegex = /^class\s*=/;
77

88
const plugin: VueLanguagePlugin = ({ modules }) => {
9+
const CompilerDOM = modules['@vue/compiler-dom'];
10+
911
return {
1012
name: require('./package.json').name,
1113

@@ -61,8 +63,7 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
6163
}
6264

6365
const map = new SourceMap(pugFile.mappings);
64-
const compiler = modules['@vue/compiler-dom'];
65-
const completed = compiler.compile(pugFile.htmlCode, {
66+
const ast = CompilerDOM.parse(pugFile.htmlCode, {
6667
...options,
6768
comments: true,
6869
onWarn(warning) {
@@ -79,8 +80,13 @@ const plugin: VueLanguagePlugin = ({ modules }) => {
7980
options.onError?.(createProxyObject(error));
8081
},
8182
});
83+
CompilerDOM.transform(ast, options);
8284

83-
return createProxyObject(completed);
85+
return {
86+
ast: createProxyObject(ast),
87+
code: '',
88+
preamble: '',
89+
};
8490

8591
function createProxyObject(target: any) {
8692
const proxys = new WeakMap();

0 commit comments

Comments
 (0)