Skip to content

Commit 66f3e4e

Browse files
committed
feat: add checkUnknownDirectives option
1 parent 07a1baa commit 66f3e4e

15 files changed

+158
-156
lines changed

Diff for: packages/language-core/lib/codegen/codeFeatures.ts

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

Diff for: packages/language-core/lib/codegen/script/component.ts

+2-1
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,

Diff for: packages/language-core/lib/codegen/script/componentSelf.ts

+2-1
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(

Diff for: packages/language-core/lib/codegen/script/index.ts

+2-24
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;

Diff for: packages/language-core/lib/codegen/script/scriptSetup.ts

+2-1
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 { type ScriptCodegenOptions, generateScriptSectionPartiallyEnding } from './index';
89
import { generateTemplate } from './template';
910

1011
export function* generateScriptSetupImports(

Diff for: packages/language-core/lib/codegen/script/src.ts

+1-1
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']>,

Diff for: packages/language-core/lib/codegen/script/styleModulesType.ts

+2-1
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(

Diff for: packages/language-core/lib/codegen/script/template.ts

+2-1
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,

Diff for: packages/language-core/lib/codegen/template/context.ts

+31-84
Original file line numberDiff line numberDiff line change
@@ -1,65 +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-
withoutNavigation: {
42-
verification: true,
43-
completion: true,
44-
semantic: true,
45-
} as VueCodeInformation,
46-
withoutHighlight: {
47-
semantic: { shouldHighlight: () => false },
48-
verification: true,
49-
navigation: true,
50-
completion: true,
51-
} as VueCodeInformation,
52-
withoutHighlightAndCompletion: {
53-
semantic: { shouldHighlight: () => false },
54-
verification: true,
55-
navigation: true,
56-
} as VueCodeInformation,
57-
withoutHighlightAndCompletionAndNavigation: {
58-
semantic: { shouldHighlight: () => false },
59-
verification: true,
60-
} as VueCodeInformation,
61-
};
62-
638
export type TemplateCodegenContext = ReturnType<typeof createTemplateCodegenContext>;
649

6510
export function createTemplateCodegenContext(options: Pick<TemplateCodegenOptions, 'scriptSetupBindingNames' | 'edited'>) {
@@ -74,34 +19,30 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
7419
} | undefined;
7520
let variableId = 0;
7621

77-
const codeFeatures = new Proxy(_codeFeatures, {
78-
get(target, key: keyof typeof _codeFeatures) {
79-
const data = target[key];
80-
if (data.verification) {
81-
if (ignoredError) {
82-
return {
83-
...data,
84-
verification: false,
85-
};
86-
}
87-
if (expectErrorToken) {
88-
const token = expectErrorToken;
89-
if (typeof data.verification !== 'object' || !data.verification.shouldReport) {
90-
return {
91-
...data,
92-
verification: {
93-
shouldReport: () => {
94-
token.errors++;
95-
return false;
96-
},
97-
},
98-
};
99-
}
100-
}
22+
function resolveCodeFeatures(features: VueCodeInformation) {
23+
if (features.verification) {
24+
if (ignoredError) {
25+
return {
26+
...features,
27+
verification: false,
28+
};
10129
}
102-
return data;
103-
},
104-
});
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+
10546
const localVars = new Map<string, number>();
10647
const specialVars = new Set<string>();
10748
const accessExternalVariables = new Map<string, Set<number>>();
@@ -129,9 +70,15 @@ export function createTemplateCodegenContext(options: Pick<TemplateCodegenOption
12970
const templateRefs = new Map<string, [varName: string, offset: number]>();
13071

13172
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,
13280
slots,
13381
dynamicSlots,
134-
codeFeatures,
13582
specialVars,
13683
accessExternalVariables,
13784
lastGenericComment,

Diff for: packages/language-core/lib/codegen/template/element.ts

+3-6
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)