diff --git a/packages/language-core/lib/codegen/globalTypes.ts b/packages/language-core/lib/codegen/globalTypes.ts index c25ccbfc8f..c782d0f3ad 100644 --- a/packages/language-core/lib/codegen/globalTypes.ts +++ b/packages/language-core/lib/codegen/globalTypes.ts @@ -6,12 +6,11 @@ export function getGlobalTypesFileName(options: VueCompilerOptions) { return [ options.lib, options.target, - options.checkUnknownProps, - ].map(v => (typeof v === 'boolean' ? Number(v) : v)).join('_') + '.d.ts'; + ].join('_') + '.d.ts'; } export function generateGlobalTypes(options: VueCompilerOptions) { - const { lib, target, checkUnknownProps } = options; + const { lib, target } = options; let text = `// @ts-nocheck${newLine}`; text += `export {}${endOfLine}`; @@ -24,7 +23,7 @@ export function generateGlobalTypes(options: VueCompilerOptions) { } text += `declare global { - ${checkUnknownProps ? '' : `var ${names.PROPS_FALLBACK}: Record;`} + var ${names.PROPS_FALLBACK}: Record; const __VLS_directiveBindingRestFields: { instance: null, oldValue: null, modifiers: any, dir: any }; const ${names.placeholder}: any; @@ -61,9 +60,20 @@ export function generateGlobalTypes(options: VueCompilerOptions) { ? K extends { __ctx?: { props?: infer P } } ? NonNullable

: never : T extends (props: infer P, ...args: any) => any ? P : {}; - type __VLS_FunctionalComponent = (props: (T extends { $props: infer Props } ? Props : {})${ - checkUnknownProps ? '' : ' & Record' - }, ctx?: any) => ${ + type __VLS_FunctionalComponent0 = (props: (T extends { $props: infer Props } ? Props : {}), ctx?: any) => ${ + target >= 3.3 + ? `import('${lib}/jsx-runtime').JSX.Element` + : `globalThis.JSX.Element` + } & { + __ctx?: { + attrs?: any; + slots?: T extends { $slots: infer Slots } ? Slots : Record; + emit?: T extends { $emit: infer Emit } ? Emit : {}; + props?: typeof props; + expose?: (exposed: T) => void; + }; + }; + type __VLS_FunctionalComponent1 = (props: (T extends { $props: infer Props } ? Props : {}) & Record, ctx?: any) => ${ target >= 3.3 ? `import('${lib}/jsx-runtime').JSX.Element` : `globalThis.JSX.Element` @@ -153,15 +163,19 @@ export function generateGlobalTypes(options: VueCompilerOptions) { : T extends (...args: any) => any ? T : (arg1: unknown, arg2: unknown, arg3: unknown, arg4: unknown) => void; - function __VLS_asFunctionalComponent any ? InstanceType : unknown>(t: T, instance?: K): - T extends new (...args: any) => any ? __VLS_FunctionalComponent + function __VLS_asFunctionalComponent0 any ? InstanceType : unknown>(t: T, instance?: K): + T extends new (...args: any) => any ? __VLS_FunctionalComponent0 + : T extends () => any ? (props: {}, ctx?: any) => ReturnType + : T extends (...args: any) => any ? T + : __VLS_FunctionalComponent0<{}>; + function __VLS_asFunctionalComponent1 any ? InstanceType : unknown>(t: T, instance?: K): + T extends new (...args: any) => any ? __VLS_FunctionalComponent1 : T extends () => any ? (props: {}, ctx?: any) => ReturnType : T extends (...args: any) => any ? T - : __VLS_FunctionalComponent<{}>; + : __VLS_FunctionalComponent1<{}>; function __VLS_functionalComponentArgsRest any>(t: T): 2 extends Parameters['length'] ? [any] : []; - function __VLS_asFunctionalElement(tag: T, endTag?: T): (attrs: T${ - checkUnknownProps ? '' : ' & Record' - }) => void; + function __VLS_asFunctionalElement0(tag: T, endTag?: T): (attrs: T) => void; + function __VLS_asFunctionalElement1(tag: T, endTag?: T): (attrs: T & Record) => void; function __VLS_asFunctionalSlot(slot: S): S extends () => infer R ? (props: {}) => R : NonNullable; function __VLS_tryAsConstant(t: T): T; }${newLine}`; diff --git a/packages/language-core/lib/codegen/script/scriptSetup.ts b/packages/language-core/lib/codegen/script/scriptSetup.ts index fba66c763c..d267bf9535 100644 --- a/packages/language-core/lib/codegen/script/scriptSetup.ts +++ b/packages/language-core/lib/codegen/script/scriptSetup.ts @@ -96,7 +96,11 @@ export function* generateGeneric( + ` & import('${vueCompilerOptions.lib}').AllowedComponentProps` + ` & import('${vueCompilerOptions.lib}').ComponentCustomProps` : `globalThis.JSX.IntrinsicAttributes` - } & (typeof globalThis extends { ${names.PROPS_FALLBACK}: infer P } ? P : {})${endOfLine}`; + }`; + if (!vueCompilerOptions.checkUnknownProps) { + yield ` & (typeof globalThis extends { ${names.PROPS_FALLBACK}: infer P } ? P : {})`; + } + yield endOfLine; yield ` expose: (exposed: `; yield scriptSetupRanges.defineExpose ? `import('${vueCompilerOptions.lib}').ShallowUnwrapRef` diff --git a/packages/language-core/lib/codegen/template/element.ts b/packages/language-core/lib/codegen/template/element.ts index d1b25472a4..02651f5120 100644 --- a/packages/language-core/lib/codegen/template/element.ts +++ b/packages/language-core/lib/codegen/template/element.ts @@ -185,7 +185,9 @@ function* generateComponentBody( const propsVar = ctx.getInternalVariable(); yield `// @ts-ignore${newLine}`; - yield `const ${functionalVar} = __VLS_asFunctionalComponent(${componentVar}, new ${componentVar}({${newLine}`; + yield `const ${functionalVar} = ${ + options.vueCompilerOptions.checkUnknownProps ? '__VLS_asFunctionalComponent0' : '__VLS_asFunctionalComponent1' + }(${componentVar}, new ${componentVar}({${newLine}`; yield* toString(propCodes); yield `}))${endOfLine}`; @@ -274,7 +276,9 @@ export function* generateElement( const [startTagOffset, endTagOffset] = getElementTagOffsets(node, options.template); const failedPropExps: FailGeneratedExpression[] = []; - yield `__VLS_asFunctionalElement(${names.intrinsics}`; + yield `${ + options.vueCompilerOptions.checkUnknownProps ? `__VLS_asFunctionalElement0` : `__VLS_asFunctionalElement1` + }(${names.intrinsics}`; yield* generatePropertyAccess( options, ctx, diff --git a/packages/language-core/lib/compilerOptions.ts b/packages/language-core/lib/compilerOptions.ts index b69f370f20..483d3b257c 100644 --- a/packages/language-core/lib/compilerOptions.ts +++ b/packages/language-core/lib/compilerOptions.ts @@ -113,7 +113,7 @@ export function createParsedCommandLine( } export class CompilerOptionsResolver { - options: Omit = {}; + options: Omit = {}; target: number | undefined; globalTypesPath: string | undefined; plugins: VueLanguagePlugin[] = []; @@ -133,6 +133,14 @@ export class CompilerOptionsResolver { this.target = options[key]; } break; + case 'strictTemplates': + const strict = !!options.strictTemplates; + this.options.strictVModel ??= strict; + this.options.checkUnknownProps ??= strict; + this.options.checkUnknownEvents ??= strict; + this.options.checkUnknownDirectives ??= strict; + this.options.checkUnknownComponents ??= strict; + break; case 'globalTypesPath': if (options[key] !== undefined) { this.globalTypesPath = path.join(rootDir, options[key]); @@ -169,12 +177,11 @@ export class CompilerOptionsResolver { } } - build(defaults?: VueCompilerOptions) { - defaults ??= getDefaultCompilerOptions(this.target, this.options.lib, this.options.strictTemplates); - + build(defaults = getDefaultCompilerOptions()) { const resolvedOptions: VueCompilerOptions = { ...defaults, ...this.options, + target: this.target ?? defaults.target, plugins: this.plugins, macros: { ...defaults.macros, @@ -275,8 +282,8 @@ export function getDefaultCompilerOptions(target = 99, lib = 'vue', strictTempla vitePressExtensions: [], petiteVueExtensions: [], jsxSlots: false, - strictVModel: strictTemplates, strictCssModules: false, + strictVModel: strictTemplates, checkUnknownProps: strictTemplates, checkUnknownEvents: strictTemplates, checkUnknownDirectives: strictTemplates, diff --git a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#2726/main.vue b/test-workspace/tsc/passedFixtures/vue3/#2726/main.vue similarity index 87% rename from test-workspace/tsc/passedFixtures/vue3_strictTemplate/#2726/main.vue rename to test-workspace/tsc/passedFixtures/vue3/#2726/main.vue index 4098ffef0d..835abfda69 100644 --- a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#2726/main.vue +++ b/test-workspace/tsc/passedFixtures/vue3/#2726/main.vue @@ -1,3 +1,5 @@ + + diff --git a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/MyInput.vue b/test-workspace/tsc/passedFixtures/vue3/#3152/MyInput.vue similarity index 64% rename from test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/MyInput.vue rename to test-workspace/tsc/passedFixtures/vue3/#3152/MyInput.vue index dd3a6450d6..3aef029416 100644 --- a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/MyInput.vue +++ b/test-workspace/tsc/passedFixtures/vue3/#3152/MyInput.vue @@ -1,3 +1,5 @@ + + diff --git a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/main.vue b/test-workspace/tsc/passedFixtures/vue3/#3152/main.vue similarity index 83% rename from test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/main.vue rename to test-workspace/tsc/passedFixtures/vue3/#3152/main.vue index d190deeb02..77e2a3f575 100644 --- a/test-workspace/tsc/passedFixtures/vue3_strictTemplate/#3152/main.vue +++ b/test-workspace/tsc/passedFixtures/vue3/#3152/main.vue @@ -1,3 +1,5 @@ + +