Skip to content

Commit 688a427

Browse files
authored
refactor(language-core): more robust code features resolution (#5177)
1 parent 4c2a482 commit 688a427

File tree

12 files changed

+137
-155
lines changed

12 files changed

+137
-155
lines changed
Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,72 @@
1+
import type { VueCodeInformation } from '../types';
2+
3+
const raw = {
4+
all: {
5+
verification: true,
6+
completion: true,
7+
semantic: true,
8+
navigation: true,
9+
},
10+
none: {},
11+
verification: {
12+
verification: true,
13+
},
14+
completion: {
15+
completion: true,
16+
},
17+
additionalCompletion: {
18+
completion: { isAdditional: true },
19+
},
20+
withoutCompletion: {
21+
verification: true,
22+
semantic: true,
23+
navigation: true,
24+
},
25+
navigation: {
26+
navigation: true,
27+
},
28+
navigationWithoutRename: {
29+
navigation: { shouldRename: () => false },
30+
},
31+
navigationAndCompletion: {
32+
navigation: true,
33+
completion: true,
34+
},
35+
navigationAndAdditionalCompletion: {
36+
navigation: true,
37+
completion: { isAdditional: true },
38+
},
39+
navigationAndVerification: {
40+
navigation: true,
41+
verification: true,
42+
},
43+
withoutNavigation: {
44+
verification: true,
45+
completion: true,
46+
semantic: true,
47+
},
48+
withoutHighlight: {
49+
semantic: { shouldHighlight: () => false },
50+
verification: true,
51+
navigation: true,
52+
completion: true,
53+
},
54+
withoutHighlightAndNavigation: {
55+
semantic: { shouldHighlight: () => false },
56+
verification: true,
57+
completion: true,
58+
},
59+
withoutHighlightAndCompletion: {
60+
semantic: { shouldHighlight: () => false },
61+
verification: true,
62+
navigation: true,
63+
},
64+
withoutHighlightAndCompletionAndNavigation: {
65+
semantic: { shouldHighlight: () => false },
66+
verification: true,
67+
},
68+
} satisfies Record<string, VueCodeInformation>;
69+
70+
export const codeFeatures = raw as {
71+
[K in keyof typeof raw]: VueCodeInformation;
72+
};

packages/language-core/lib/codegen/script/component.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
11
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
22
import type { Code, Sfc } from '../../types';
3+
import { codeFeatures } from '../codeFeatures';
34
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
45
import type { ScriptCodegenContext } from './context';
5-
import { ScriptCodegenOptions, codeFeatures } from './index';
6+
import type { ScriptCodegenOptions } from './index';
67

78
export function* generateComponent(
89
options: ScriptCodegenOptions,

packages/language-core/lib/codegen/script/componentSelf.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import * as path from 'path-browserify';
22
import type { Code } from '../../types';
3+
import { codeFeatures } from '../codeFeatures';
34
import type { TemplateCodegenContext } from '../template/context';
45
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
56
import { generateComponentSetupReturns, generateEmitsOption, generatePropsOption } from './component';
67
import type { ScriptCodegenContext } from './context';
7-
import { codeFeatures, type ScriptCodegenOptions } from './index';
8+
import type { ScriptCodegenOptions } from './index';
89
import { getTemplateUsageVars } from './template';
910

1011
export function* generateComponentSelf(

packages/language-core/lib/codegen/script/index.ts

Lines changed: 2 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ import * as path from 'path-browserify';
33
import type * as ts from 'typescript';
44
import type { ScriptRanges } from '../../parsers/scriptRanges';
55
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
6-
import type { Code, Sfc, VueCodeInformation, VueCompilerOptions } from '../../types';
6+
import type { Code, Sfc, VueCompilerOptions } from '../../types';
7+
import { codeFeatures } from '../codeFeatures';
78
import { generateGlobalTypes, getGlobalTypesFileName } from '../globalTypes';
89
import type { TemplateCodegenContext } from '../template/context';
910
import { endOfLine, generateSfcBlockSection, newLine } from '../utils';
@@ -14,29 +15,6 @@ import { generateSrc } from './src';
1415
import { generateStyleModulesType } from './styleModulesType';
1516
import { generateTemplate } from './template';
1617

17-
export const codeFeatures = {
18-
all: {
19-
verification: true,
20-
completion: true,
21-
semantic: true,
22-
navigation: true,
23-
} as VueCodeInformation,
24-
none: {} as VueCodeInformation,
25-
verification: {
26-
verification: true,
27-
} as VueCodeInformation,
28-
navigation: {
29-
navigation: true,
30-
} as VueCodeInformation,
31-
navigationWithoutRename: {
32-
navigation: {
33-
shouldRename() {
34-
return false;
35-
},
36-
},
37-
} as VueCodeInformation,
38-
};
39-
4018
export interface ScriptCodegenOptions {
4119
ts: typeof ts;
4220
compilerOptions: ts.CompilerOptions;

packages/language-core/lib/codegen/script/scriptSetup.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
import type { ScriptSetupRanges } from '../../parsers/scriptSetupRanges';
22
import type { Code, Sfc, TextRange } from '../../types';
3+
import { codeFeatures } from '../codeFeatures';
34
import { combineLastMapping, endOfLine, generateSfcBlockSection, newLine } from '../utils';
45
import { generateComponent, generateEmitsOption } from './component';
56
import { generateComponentSelf } from './componentSelf';
67
import type { ScriptCodegenContext } from './context';
7-
import { ScriptCodegenOptions, codeFeatures, generateScriptSectionPartiallyEnding } from './index';
8+
import { ScriptCodegenOptions, generateScriptSectionPartiallyEnding } from './index';
89
import { generateTemplate } from './template';
910

1011
export function* generateScriptSetupImports(

packages/language-core/lib/codegen/script/src.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Code, Sfc } from '../../types';
2+
import { codeFeatures } from '../codeFeatures';
23
import { endOfLine } from '../utils';
3-
import { codeFeatures } from './index';
44

55
export function* generateSrc(
66
script: NonNullable<Sfc['script']>,

packages/language-core/lib/codegen/script/styleModulesType.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
import type { Code } from '../../types';
2+
import { codeFeatures } from '../codeFeatures';
23
import { endOfLine, newLine } from '../utils';
34
import type { ScriptCodegenContext } from './context';
4-
import { ScriptCodegenOptions, codeFeatures } from './index';
5+
import type { ScriptCodegenOptions } from './index';
56
import { generateCssClassProperty } from './template';
67

78
export function* generateStyleModulesType(

packages/language-core/lib/codegen/script/template.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
import type { Code } from '../../types';
22
import { hyphenateTag } from '../../utils/shared';
3+
import { codeFeatures } from '../codeFeatures';
34
import { TemplateCodegenContext, createTemplateCodegenContext } from '../template/context';
45
import { generateInterpolation } from '../template/interpolation';
56
import { generateStyleScopedClassReferences } from '../template/styleScopedClasses';
67
import { endOfLine, newLine } from '../utils';
78
import type { ScriptCodegenContext } from './context';
8-
import { codeFeatures, type ScriptCodegenOptions } from './index';
9+
import type { ScriptCodegenOptions } from './index';
910

1011
export function* generateTemplate(
1112
options: ScriptCodegenOptions,

packages/language-core/lib/codegen/template/context.ts

Lines changed: 31 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,74 +1,10 @@
11
import type * as CompilerDOM from '@vue/compiler-dom';
22
import type { Code, VueCodeInformation } from '../../types';
3+
import { codeFeatures } from '../codeFeatures';
34
import { InlayHintInfo } from '../inlayHints';
45
import { endOfLine, newLine, wrapWith } from '../utils';
56
import type { TemplateCodegenOptions } from './index';
67

7-
const _codeFeatures = {
8-
all: {
9-
verification: true,
10-
completion: true,
11-
semantic: true,
12-
navigation: true,
13-
} as VueCodeInformation,
14-
verification: {
15-
verification: true,
16-
} as VueCodeInformation,
17-
completion: {
18-
completion: true,
19-
} as VueCodeInformation,
20-
additionalCompletion: {
21-
completion: { isAdditional: true },
22-
} as VueCodeInformation,
23-
navigation: {
24-
navigation: true,
25-
} as VueCodeInformation,
26-
navigationWithoutRename: {
27-
navigation: {
28-
shouldRename() {
29-
return false;
30-
},
31-
},
32-
} as VueCodeInformation,
33-
navigationAndCompletion: {
34-
navigation: true,
35-
completion: true,
36-
} as VueCodeInformation,
37-
navigationAndAdditionalCompletion: {
38-
navigation: true,
39-
completion: { isAdditional: true },
40-
} as VueCodeInformation,
41-
navigationAndVerification: {
42-
navigation: true,
43-
verification: true,
44-
} as VueCodeInformation,
45-
withoutNavigation: {
46-
verification: true,
47-
completion: true,
48-
semantic: true,
49-
} as VueCodeInformation,
50-
withoutHighlight: {
51-
semantic: { shouldHighlight: () => false },
52-
verification: true,
53-
navigation: true,
54-
completion: true,
55-
} as VueCodeInformation,
56-
withoutHighlightAndNavigation: {
57-
semantic: { shouldHighlight: () => false },
58-
verification: true,
59-
completion: true,
60-
} as VueCodeInformation,
61-
withoutHighlightAndCompletion: {
62-
semantic: { shouldHighlight: () => false },
63-
verification: true,
64-
navigation: true,
65-
} as VueCodeInformation,
66-
withoutHighlightAndCompletionAndNavigation: {
67-
semantic: { shouldHighlight: () => false },
68-
verification: true,
69-
} as VueCodeInformation,
70-
};
71-
728
export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;
739

7410
export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>) {
@@ -83,34 +19,30 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
8319
} | undefined;
8420
let variableId = 0;
8521

86-
const codeFeatures = new Proxy(_codeFeatures, {
87-
get(target, key: keyof typeof _codeFeatures) {
88-
const data = target[key];
89-
if (data.verification) {
90-
if (ignoredError) {
91-
return {
92-
...data,
93-
verification: false,
94-
};
95-
}
96-
if (expectErrorToken) {
97-
const token = expectErrorToken;
98-
if (typeof data.verification !== 'object' || !data.verification.shouldReport) {
99-
return {
100-
...data,
101-
verification: {
102-
shouldReport: () => {
103-
token.errors++;
104-
return false;
105-
},
106-
},
107-
};
108-
}
109-
}
22+
function resolveCodeFeatures(features: VueCodeInformation) {
23+
if (features.verification) {
24+
if (ignoredError) {
25+
return {
26+
...features,
27+
verification: false,
28+
};
11029
}
111-
return data;
112-
},
113-
});
30+
if (expectErrorToken) {
31+
const token = expectErrorToken;
32+
return {
33+
...features,
34+
verification: {
35+
shouldReport: () => {
36+
token.errors++;
37+
return false;
38+
}
39+
},
40+
};
41+
}
42+
}
43+
return features;
44+
}
45+
11446
const localVars = new Map<string, number>();
11547
const specialVars = new Set<string>();
11648
const accessExternalVariables = new Map<string, Set<number>>();
@@ -138,9 +70,15 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
13870
const templateRefs = new Map<string, [varName: string, offset: number]>();
13971

14072
return {
73+
codeFeatures: new Proxy(codeFeatures, {
74+
get(target, key: keyof typeof codeFeatures) {
75+
const data = target[key];
76+
return resolveCodeFeatures(data);
77+
},
78+
}),
79+
resolveCodeFeatures,
14180
slots,
14281
dynamicSlots,
143-
codeFeatures,
14482
specialVars,
14583
accessExternalVariables,
14684
lastGenericComment,

packages/language-core/lib/codegen/template/element.ts

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -134,10 +134,7 @@ export function* generateComponent(
134134
options,
135135
ctx,
136136
'template',
137-
{
138-
...ctx.codeFeatures.all,
139-
completion: false,
140-
},
137+
ctx.codeFeatures.withoutCompletion,
141138
dynamicTagInfo.tag,
142139
dynamicTagInfo.offsets[1],
143140
dynamicTagInfo.astHolder,
@@ -227,13 +224,13 @@ export function* generateComponent(
227224
yield* wrapWith(
228225
node.loc.start.offset,
229226
node.loc.end.offset,
230-
{
227+
ctx.resolveCodeFeatures({
231228
verification: {
232229
shouldReport(_source, code) {
233230
return String(code) !== '6133';
234231
},
235232
}
236-
},
233+
}),
237234
var_componentInstance
238235
);
239236
yield ` = ${var_functionalComponent}`;

0 commit comments

Comments
 (0)