Skip to content

Commit 26fa104

Browse files
authored
refactor(language-core): use shouldReport to handle unknown events and components checking (#5537)
1 parent 78fcfdd commit 26fa104

File tree

5 files changed

+72
-62
lines changed

5 files changed

+72
-62
lines changed

packages/language-core/lib/codegen/codeFeatures.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,26 @@ const raw = {
7272
navigation: true,
7373
completion: true,
7474
},
75+
doNotReportTs2339AndTs2551: {
76+
verification: {
77+
// https://typescript.tv/errors/#ts2339
78+
// https://typescript.tv/errors/#ts2551
79+
shouldReport: (_source, code) => String(code) !== '2339' && String(code) !== '2551',
80+
},
81+
},
82+
doNotReportTs2353AndTs2561: {
83+
verification: {
84+
// https://typescript.tv/errors/#ts2353
85+
// https://typescript.tv/errors/#ts2561
86+
shouldReport: (_source, code) => String(code) !== '2353' && String(code) !== '2561',
87+
},
88+
},
89+
doNotReportTs6133: {
90+
verification: {
91+
// https://typescript.tv/errors/#ts6133
92+
shouldReport: (_source, code) => String(code) !== '6133',
93+
},
94+
},
7595
} satisfies Record<string, VueCodeInformation>;
7696

7797
export const codeFeatures = raw as {

packages/language-core/lib/codegen/globalTypes.ts

Lines changed: 28 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,29 +1,17 @@
11
import type { VueCompilerOptions } from '../types';
22
import { getSlotsPropertyName } from '../utils/shared';
33

4-
export function getGlobalTypesFileName({
5-
lib,
6-
target,
7-
checkUnknownProps,
8-
checkUnknownEvents,
9-
checkUnknownComponents,
10-
}: VueCompilerOptions) {
4+
export function getGlobalTypesFileName(options: VueCompilerOptions) {
115
return [
12-
lib,
13-
target,
14-
checkUnknownProps,
15-
checkUnknownEvents,
16-
checkUnknownComponents,
6+
options.lib,
7+
options.target,
8+
options.checkUnknownProps,
179
].map(v => (typeof v === 'boolean' ? Number(v) : v)).join('_') + '.d.ts';
1810
}
1911

20-
export function generateGlobalTypes({
21-
lib,
22-
target,
23-
checkUnknownProps,
24-
checkUnknownEvents,
25-
checkUnknownComponents,
26-
}: VueCompilerOptions) {
12+
export function generateGlobalTypes(options: VueCompilerOptions) {
13+
const { lib, target, checkUnknownProps } = options;
14+
2715
const fnPropsType = `(T extends { $props: infer Props } ? Props : {})${
2816
checkUnknownProps ? '' : ' & Record<string, unknown>'
2917
}`;
@@ -69,7 +57,7 @@ export function generateGlobalTypes({
6957
N1 extends keyof __VLS_GlobalComponents ? N1 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N1] } :
7058
N2 extends keyof __VLS_GlobalComponents ? N2 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N2] } :
7159
N3 extends keyof __VLS_GlobalComponents ? N3 extends N0 ? Pick<__VLS_GlobalComponents, N0 extends keyof __VLS_GlobalComponents ? N0 : never> : { [K in N0]: __VLS_GlobalComponents[N3] } :
72-
${checkUnknownComponents ? '{}' : '{ [K in N0]: unknown }'};
60+
{};
7361
type __VLS_FunctionalComponentCtx<T, K> = __VLS_PickNotAny<'__ctx' extends keyof __VLS_PickNotAny<K, {}>
7462
? K extends { __ctx?: infer Ctx } ? NonNullable<Ctx> : never : any
7563
, T extends (props: any, ctx: infer Ctx) => any ? Ctx : any
@@ -80,12 +68,12 @@ export function generateGlobalTypes({
8068
: {};
8169
type __VLS_FunctionalComponent<T> = (props: ${fnPropsType}, ctx?: any) => __VLS_Element & {
8270
__ctx?: {
83-
attrs?: any,
84-
slots?: T extends { ${getSlotsPropertyName(target)}: infer Slots } ? Slots : Record<string, any>,
85-
emit?: T extends { $emit: infer Emit } ? Emit : {},
86-
props?: ${fnPropsType},
87-
expose?: (exposed: T) => void,
88-
}
71+
attrs?: any;
72+
slots?: T extends { ${getSlotsPropertyName(target)}: infer Slots } ? Slots : Record<string, any>;
73+
emit?: T extends { $emit: infer Emit } ? Emit : {};
74+
props?: ${fnPropsType};
75+
expose?: (exposed: T) => void;
76+
};
8977
};
9078
type __VLS_IsFunction<T, K> = K extends keyof T
9179
? __VLS_IsAny<T[K]> extends false
@@ -94,15 +82,19 @@ export function generateGlobalTypes({
9482
: true
9583
: false
9684
: false;
97-
type __VLS_NormalizeComponentEvent<Props, Emits, onEvent extends keyof Props, Event extends keyof Emits, CamelizedEvent extends keyof Emits> = (
98-
__VLS_IsFunction<Props, onEvent> extends true
99-
? Props
100-
: __VLS_IsFunction<Emits, Event> extends true
101-
? { [K in onEvent]?: Emits[Event] }
102-
: __VLS_IsFunction<Emits, CamelizedEvent> extends true
103-
? { [K in onEvent]?: Emits[CamelizedEvent] }
104-
: Props
105-
)${checkUnknownEvents ? '' : ' & Record<string, unknown>'};
85+
type __VLS_NormalizeComponentEvent<
86+
Props,
87+
Emits,
88+
onEvent extends keyof Props,
89+
Event extends keyof Emits,
90+
CamelizedEvent extends keyof Emits,
91+
> = __VLS_IsFunction<Props, onEvent> extends true
92+
? Props
93+
: __VLS_IsFunction<Emits, Event> extends true
94+
? { [K in onEvent]?: Emits[Event] }
95+
: __VLS_IsFunction<Emits, CamelizedEvent> extends true
96+
? { [K in onEvent]?: Emits[CamelizedEvent] }
97+
: Props;
10698
// fix https://github.com/vuejs/language-tools/issues/926
10799
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never;
108100
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R
@@ -174,7 +166,7 @@ export function generateGlobalTypes({
174166
: __VLS_FunctionalComponent<{}>;
175167
function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): 2 extends Parameters<T>['length'] ? [any] : [];
176168
function __VLS_asFunctionalElement<T>(tag: T, endTag?: T): (attrs: T${
177-
checkUnknownComponents ? '' : ' & Record<string, unknown>'
169+
checkUnknownProps ? '' : ' & Record<string, unknown>'
178170
}) => void;
179171
function __VLS_asFunctionalSlot<S>(slot: S): S extends () => infer R ? (props: {}) => R : NonNullable<S>;
180172
function __VLS_tryAsConstant<const T>(t: T): T;

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

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,12 @@ export function* generateComponent(
152152
yield* generateCanonicalComponentName(
153153
node.tag,
154154
tagOffsets[0],
155-
ctx.codeFeatures.withoutHighlightAndCompletionAndNavigation,
155+
ctx.resolveCodeFeatures({
156+
...codeFeatures.semanticWithoutHighlight,
157+
...options.vueCompilerOptions.checkUnknownComponents
158+
? codeFeatures.verification
159+
: codeFeatures.doNotReportTs2339AndTs2551,
160+
}),
156161
);
157162
yield `${endOfLine}`;
158163

@@ -207,14 +212,7 @@ export function* generateComponent(
207212
yield* wrapWith(
208213
node.loc.start.offset,
209214
node.loc.end.offset,
210-
ctx.resolveCodeFeatures({
211-
verification: {
212-
shouldReport(_source, code) {
213-
// https://typescript.tv/errors/#ts6133
214-
return String(code) !== '6133';
215-
},
216-
},
217-
}),
215+
ctx.codeFeatures.doNotReportTs6133,
218216
componentVNodeVar,
219217
);
220218
yield ` = ${componentFunctionalVar}`;

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

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as CompilerDOM from '@vue/compiler-dom';
22
import { camelize, capitalize } from '@vue/shared';
33
import type * as ts from 'typescript';
44
import type { Code, VueCodeInformation } from '../../types';
5+
import { codeFeatures } from '../codeFeatures';
56
import { combineLastMapping, createTsAst, endOfLine, identifierRegex, newLine } from '../utils';
67
import { generateCamelized } from '../utils/camelized';
78
import { wrapWith } from '../utils/wrapWith';
@@ -61,12 +62,12 @@ export function* generateElementEvents(
6162
yield `const ${ctx.getInternalVariable()}: __VLS_NormalizeComponentEvent<typeof ${propsVar}, typeof ${emitsVar}, '${propName}', '${emitName}', '${camelizedEmitName}'> = (${newLine}`;
6263
if (prop.name === 'on') {
6364
yield `{ `;
64-
yield* generateEventArg(ctx, source, start!, emitPrefix.slice(0, -1), ctx.codeFeatures.navigation);
65+
yield* generateEventArg(options, ctx, source, start!, emitPrefix.slice(0, -1), ctx.codeFeatures.navigation);
6566
yield `: {} as any } as typeof ${emitsVar},${newLine}`;
6667
}
6768
yield `{ `;
6869
if (prop.name === 'on') {
69-
yield* generateEventArg(ctx, source, start!, propPrefix.slice(0, -1));
70+
yield* generateEventArg(options, ctx, source, start!, propPrefix.slice(0, -1));
7071
yield `: `;
7172
yield* generateEventExpression(options, ctx, prop);
7273
}
@@ -80,15 +81,21 @@ export function* generateElementEvents(
8081
}
8182

8283
export function* generateEventArg(
84+
options: TemplateCodegenOptions,
8385
ctx: TemplateCodegenContext,
8486
name: string,
8587
start: number,
8688
directive = 'on',
87-
features: VueCodeInformation = {
88-
...ctx.codeFeatures.withoutHighlightAndCompletion,
89-
...ctx.codeFeatures.navigationWithoutRename,
90-
},
89+
features?: VueCodeInformation,
9190
): Generator<Code> {
91+
features ??= ctx.resolveCodeFeatures({
92+
...codeFeatures.semanticWithoutHighlight,
93+
...codeFeatures.navigationWithoutRename,
94+
...options.vueCompilerOptions.checkUnknownEvents
95+
? codeFeatures.verification
96+
: codeFeatures.doNotReportTs2353AndTs2561,
97+
});
98+
9299
if (directive.length) {
93100
name = capitalize(name);
94101
}

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

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ export function* generateElementProps(
4646
) {
4747
if (!isComponent) {
4848
yield `...{ `;
49-
yield* generateEventArg(ctx, prop.arg.loc.source, prop.arg.loc.start.offset);
49+
yield* generateEventArg(options, ctx, prop.arg.loc.source, prop.arg.loc.start.offset);
5050
yield `: `;
5151
yield* generateEventExpression(options, ctx, prop);
5252
yield `},`;
@@ -378,16 +378,9 @@ function getPropsCodeInfo(
378378
): VueCodeInformation {
379379
return ctx.resolveCodeFeatures({
380380
...codeFeatures.withoutHighlightAndCompletion,
381-
verification: strictPropsCheck || {
382-
shouldReport(_source, code) {
383-
// https://typescript.tv/errors/#ts2353
384-
// https://typescript.tv/errors/#ts2561
385-
if (String(code) === '2353' || String(code) === '2561') {
386-
return false;
387-
}
388-
return true;
389-
},
390-
},
381+
...strictPropsCheck
382+
? codeFeatures.verification
383+
: codeFeatures.doNotReportTs2353AndTs2561,
391384
});
392385
}
393386

0 commit comments

Comments
 (0)