@vue/language-core
Advanced tools
Comparing version 1.8.13 to 1.8.14
@@ -31,3 +31,2 @@ "use strict"; | ||
const path_1 = require("path"); | ||
const sharedTypes = __importStar(require("../utils/globalTypes")); | ||
const shared_1 = require("../utils/shared"); | ||
@@ -59,5 +58,2 @@ const transform_1 = require("../utils/transform"); | ||
emitsAssignName: undefined, | ||
emitsRuntimeArg: undefined, | ||
emitsTypeArg: undefined, | ||
emitsTypeNums: 0, | ||
exposeRuntimeArg: undefined, | ||
@@ -67,6 +63,9 @@ leadingCommentEndOffset: 0, | ||
defineProps: undefined, | ||
defineSlots: undefined, | ||
defineEmits: undefined, | ||
defineExpose: undefined, | ||
slotsAssignName: undefined, | ||
propsAssignName: undefined, | ||
propsRuntimeArg: undefined, | ||
propsTypeArg: undefined, | ||
slotsTypeArg: undefined, | ||
withDefaultsArg: undefined, | ||
@@ -77,11 +76,14 @@ defineProp: [], | ||
//#endregion | ||
const bindingNames = new Set([ | ||
...scriptRanges?.bindings.map(range => sfc.script.content.substring(range.start, range.end)) ?? [], | ||
...scriptSetupRanges?.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end)) ?? [], | ||
]); | ||
const bypassDefineComponent = lang === 'js' || lang === 'jsx'; | ||
const usedHelperTypes = { | ||
DefinePropsToOptions: false, | ||
mergePropDefaults: false, | ||
EmitsTypeHelpers: false, | ||
MergePropDefaults: false, | ||
WithTemplateSlots: false, | ||
PropsChildren: false, | ||
}; | ||
codes.push(`/* ${Object.entries(vueCompilerOptions).map(([key, value]) => `${key}: ${JSON.stringify(value)}`).join(', ')} */\n`); | ||
codes.push(`/* __placeholder__ */\n`); | ||
let generatedTemplate = false; | ||
@@ -95,3 +97,3 @@ generateSrc(); | ||
if (!generatedTemplate) { | ||
generateTemplate(); | ||
generateTemplate(false); | ||
} | ||
@@ -113,3 +115,2 @@ if (sfc.scriptSetup) { | ||
function generateHelperTypes() { | ||
let usedPrettify = false; | ||
if (usedHelperTypes.DefinePropsToOptions) { | ||
@@ -124,3 +125,3 @@ if (compilerOptions.exactOptionalPropertyTypes) { | ||
} | ||
if (usedHelperTypes.mergePropDefaults) { | ||
if (usedHelperTypes.MergePropDefaults) { | ||
codes.push(`type __VLS_WithDefaults<P, D> = { | ||
@@ -132,19 +133,3 @@ // use 'keyof Pick<P, keyof P>' instead of 'keyof P' to keep props jsdoc | ||
};\n`); | ||
usedPrettify = true; | ||
} | ||
if (usedHelperTypes.EmitsTypeHelpers) { | ||
// fix https://github.com/vuejs/language-tools/issues/926 | ||
codes.push('type __VLS_UnionToIntersection<U> = __VLS_Prettify<(U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never>;\n'); | ||
usedPrettify = true; | ||
if (scriptSetupRanges && scriptSetupRanges.emitsTypeNums !== -1) { | ||
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads', scriptSetupRanges.emitsTypeNums)); | ||
} | ||
else { | ||
codes.push(sharedTypes.genConstructorOverloads('__VLS_ConstructorOverloads')); | ||
} | ||
codes.push(`type __VLS_NormalizeEmits<T> = __VLS_ConstructorOverloads<T> & { | ||
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never | ||
}\n`); | ||
; | ||
} | ||
if (usedHelperTypes.WithTemplateSlots) { | ||
@@ -161,5 +146,2 @@ codes.push(`type __VLS_WithTemplateSlots<T, S> = T & { new(): {\n`, `$slots: S;\n`); | ||
} | ||
if (usedPrettify) { | ||
codes.push(`type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {};\n`); | ||
} | ||
} | ||
@@ -305,18 +287,19 @@ function generateSrc() { | ||
codes.push('(\n'); | ||
codes.push(`__VLS_props: Awaited<typeof __VLS_setup>['props']`, `& import('${vueCompilerOptions.lib}').VNodeProps`, `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, `& import('${vueCompilerOptions.lib}').ComponentCustomProps,\n`); | ||
codes.push(`__VLS_ctx?: Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>,\n`); | ||
codes.push(`__VLS_props: Awaited<typeof __VLS_setup>['props'],\n`); | ||
codes.push(`__VLS_ctx?: __VLS_Prettify<Pick<Awaited<typeof __VLS_setup>, 'attrs' | 'emit' | 'slots'>>,\n`); // use __VLS_Prettify for less dts code | ||
codes.push(`__VLS_expose?: NonNullable<Awaited<typeof __VLS_setup>>['expose'],\n`); | ||
codes.push('__VLS_setup = (async () => {\n'); | ||
scriptSetupGeneratedOffset = generateSetupFunction(true, 'none', definePropMirrors); | ||
//#region exposed | ||
codes.push(`const __VLS_exposed = `); | ||
if (scriptSetupRanges.exposeRuntimeArg) { | ||
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end); | ||
//#region props | ||
codes.push(`const __VLS_fnComponent = `); | ||
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`); | ||
if (scriptSetupRanges.propsRuntimeArg) { | ||
codes.push(`props: `); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end); | ||
codes.push(`,\n`); | ||
} | ||
else { | ||
codes.push(`{}`); | ||
if (scriptSetupRanges.defineEmits) { | ||
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`); | ||
} | ||
codes.push(';\n'); | ||
//#endregion | ||
//#region props | ||
codes.push(`});\n`); | ||
if (scriptSetupRanges.defineProp.length) { | ||
@@ -339,6 +322,3 @@ codes.push(`const __VLS_defaults = {\n`); | ||
} | ||
codes.push(`let __VLS_props!: {}`); | ||
if (scriptSetupRanges.propsRuntimeArg) { | ||
codes.push(` & InstanceType<typeof __VLS_publicComponent>['$props']`); | ||
} | ||
codes.push(`let __VLS_fnPropsTypeOnly!: {}`); // TODO: reuse __VLS_fnPropsTypeOnly even without generic, and remove __VLS_propsOption_defineProp | ||
if (scriptSetupRanges.propsTypeArg) { | ||
@@ -373,29 +353,19 @@ codes.push(` & `); | ||
} | ||
if (scriptSetupRanges.slotsTypeArg && vueCompilerOptions.jsxSlots) { | ||
codes.push(`;\n`); | ||
codes.push(`let __VLS_fnPropsDefineComponent!: InstanceType<typeof __VLS_fnComponent>['$props']`); | ||
codes.push(`;\n`); | ||
codes.push(`let __VLS_fnPropsSlots!: `); | ||
if (scriptSetupRanges.defineSlots && vueCompilerOptions.jsxSlots) { | ||
usedHelperTypes.PropsChildren = true; | ||
codes.push(` & __VLS_PropsChildren<`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end); | ||
codes.push('>'); | ||
codes.push(`__VLS_PropsChildren<typeof __VLS_slots>`); | ||
} | ||
codes.push(`;\n`); | ||
//#endregion | ||
//#region emits | ||
codes.push(`const __VLS_emit = `); | ||
if (scriptSetupRanges.emitsTypeArg) { | ||
codes.push('{} as '); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end); | ||
codes.push(';\n'); | ||
} | ||
else if (scriptSetupRanges.emitsRuntimeArg) { | ||
codes.push(`defineEmits(`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end); | ||
codes.push(');\n'); | ||
} | ||
else { | ||
codes.push('{} as any;\n'); | ||
codes.push(`{}`); | ||
} | ||
codes.push(`;\n`); | ||
codes.push(`let __VLS_defaultProps!: `, `import('${vueCompilerOptions.lib}').VNodeProps`, `& import('${vueCompilerOptions.lib}').AllowedComponentProps`, `& import('${vueCompilerOptions.lib}').ComponentCustomProps`, `;\n`); | ||
//#endregion | ||
codes.push('return {} as {\n'); | ||
codes.push(`props: typeof __VLS_props,\n`); | ||
codes.push('expose(exposed: typeof __VLS_exposed): void,\n'); | ||
codes.push(`props: __VLS_Prettify<Omit<typeof __VLS_fnPropsDefineComponent & typeof __VLS_fnPropsTypeOnly, keyof typeof __VLS_defaultProps>> & typeof __VLS_fnPropsSlots & typeof __VLS_defaultProps,\n`); | ||
codes.push(`expose(exposed: ${scriptSetupRanges.exposeRuntimeArg ? 'typeof __VLS_exposed' : '{}'}): void,\n`); | ||
codes.push('attrs: any,\n'); | ||
@@ -448,13 +418,6 @@ codes.push('slots: ReturnType<typeof __VLS_template>,\n'); | ||
if (vueCompilerOptions.target >= 3.3) { | ||
const bindings = new Set(scriptSetupRanges.bindings.map(range => sfc.scriptSetup.content.substring(range.start, range.end))); | ||
codes.push('const { '); | ||
for (const [macro, aliases] of Object.entries(vueCompilerOptions.macros)) { | ||
for (const alias of aliases) { | ||
if (!bindings.has(alias)) { | ||
codes.push(macro); | ||
if (alias !== macro) { | ||
codes.push(` : ${alias}`); | ||
} | ||
codes.push(`, `); | ||
} | ||
for (const macro of Object.keys(vueCompilerOptions.macros)) { | ||
if (!bindingNames.has(macro)) { | ||
codes.push(macro, ', '); | ||
} | ||
@@ -479,3 +442,37 @@ } | ||
const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset; | ||
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset); | ||
let setupCodeModifies = []; | ||
if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) { | ||
setupCodeModifies.push([() => codes.push(`const __VLS_slots = `), scriptSetupRanges.defineSlots.start, scriptSetupRanges.defineSlots.start]); | ||
} | ||
if (scriptSetupRanges.defineEmits && !scriptSetupRanges.emitsAssignName) { | ||
setupCodeModifies.push([() => codes.push(`const __VLS_emit = `), scriptSetupRanges.defineEmits.start, scriptSetupRanges.defineEmits.start]); | ||
} | ||
if (scriptSetupRanges.defineExpose && scriptSetupRanges.exposeRuntimeArg) { | ||
setupCodeModifies.push([() => { | ||
codes.push(`const __VLS_exposed = `); | ||
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end); | ||
codes.push(`;`); | ||
addVirtualCode('scriptSetup', scriptSetupRanges.defineExpose.start, scriptSetupRanges.exposeRuntimeArg.start); | ||
codes.push(`__VLS_exposed`); | ||
addVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.end, scriptSetupRanges.defineExpose.end); | ||
}, scriptSetupRanges.defineExpose.start, scriptSetupRanges.defineExpose.end]); | ||
} | ||
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]); | ||
if (setupCodeModifies.length) { | ||
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]); | ||
while (setupCodeModifies.length) { | ||
const [generate, _, end] = setupCodeModifies.shift(); | ||
generate(); | ||
if (setupCodeModifies.length) { | ||
const nextStart = setupCodeModifies[0][1]; | ||
addVirtualCode('scriptSetup', end, nextStart); | ||
} | ||
else { | ||
addVirtualCode('scriptSetup', end); | ||
} | ||
} | ||
} | ||
else { | ||
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset); | ||
} | ||
if (scriptSetupRanges.propsTypeArg && scriptSetupRanges.withDefaultsArg) { | ||
@@ -521,5 +518,28 @@ // fix https://github.com/vuejs/language-tools/issues/1187 | ||
} | ||
generateTemplate(functional); | ||
if (mode === 'return' || mode === 'export') { | ||
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.defineSlots)) { | ||
usedHelperTypes.WithTemplateSlots = true; | ||
codes.push(`const __VLS_component = `); | ||
generateComponent(functional); | ||
codes.push(`;\n`); | ||
codes.push(mode === 'return' ? 'return ' : 'export default '); | ||
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_component, ReturnType<typeof __VLS_template>>;\n`); | ||
} | ||
else { | ||
codes.push(mode === 'return' ? 'return ' : 'export default '); | ||
generateComponent(functional); | ||
codes.push(`;\n`); | ||
} | ||
} | ||
if (mode === 'export') { | ||
generateExportDefaultEndMapping(); | ||
} | ||
return scriptSetupGeneratedOffset; | ||
} | ||
function generateComponent(functional) { | ||
if (!scriptSetupRanges) | ||
return; | ||
if (scriptRanges?.exportDefault && scriptRanges.exportDefault.expression.start !== scriptRanges.exportDefault.args.start) { | ||
// use defineComponent() from user space code if it exist | ||
codes.push(`const __VLS_publicComponent = `); | ||
addVirtualCode('script', scriptRanges.exportDefault.expression.start, scriptRanges.exportDefault.args.start); | ||
@@ -529,5 +549,20 @@ codes.push(`{\n`); | ||
else { | ||
codes.push(`const __VLS_publicComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`); | ||
codes.push(`(await import('${vueCompilerOptions.lib}')).defineComponent({\n`); | ||
} | ||
if (!bypassDefineComponent) { | ||
generateComponentOptions(functional); | ||
codes.push(`setup() {\n`); | ||
codes.push(`return {\n`); | ||
generateSetupReturns(); | ||
if (scriptSetupRanges.exposeRuntimeArg) { | ||
codes.push(`...__VLS_exposed,\n`); | ||
} | ||
codes.push(`};\n`); | ||
codes.push(`},\n`); | ||
if (scriptRanges?.exportDefault?.args) { | ||
addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1); | ||
} | ||
codes.push(`})`); | ||
} | ||
function generateComponentOptions(functional) { | ||
if (scriptSetupRanges && !bypassDefineComponent) { | ||
if (scriptSetupRanges.propsRuntimeArg || scriptSetupRanges.propsTypeArg || (!functional && scriptSetupRanges.defineProp.length)) { | ||
@@ -544,3 +579,3 @@ codes.push(`props: {\n`); | ||
if (scriptSetupRanges.withDefaultsArg) { | ||
usedHelperTypes.mergePropDefaults = true; | ||
usedHelperTypes.MergePropDefaults = true; | ||
codes.push(`__VLS_WithDefaults<`); | ||
@@ -550,3 +585,3 @@ } | ||
if (functional) { | ||
codes.push(`typeof __VLS_props`); | ||
codes.push(`typeof __VLS_fnPropsTypeOnly`); | ||
} | ||
@@ -568,17 +603,9 @@ else { | ||
} | ||
if (scriptSetupRanges.emitsTypeArg) { | ||
usedHelperTypes.EmitsTypeHelpers = true; | ||
codes.push(`emits: ({} as __VLS_UnionToIntersection<__VLS_NormalizeEmits<`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end); | ||
codes.push(`>>),\n`); | ||
if (scriptSetupRanges.defineEmits) { | ||
codes.push(`emits: ({} as __VLS_NormalizeEmits<typeof `, scriptSetupRanges.emitsAssignName ?? '__VLS_emit', `>),\n`); | ||
} | ||
else if (scriptSetupRanges.emitsRuntimeArg) { | ||
codes.push(`emits: (`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end); | ||
codes.push(`),\n`); | ||
} | ||
} | ||
codes.push(`setup() {\n`); | ||
codes.push(`return {\n`); | ||
if (bypassDefineComponent) { | ||
} | ||
function generateSetupReturns() { | ||
if (scriptSetupRanges && bypassDefineComponent) { | ||
// fill $props | ||
@@ -598,49 +625,8 @@ if (scriptSetupRanges.propsTypeArg) { | ||
// fill $emit | ||
if (scriptSetupRanges.emitsAssignName) { | ||
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName},\n`); | ||
if (scriptSetupRanges.defineEmits) { | ||
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName ?? '__VLS_emit'},\n`); | ||
} | ||
else if (scriptSetupRanges.emitsTypeArg) { | ||
codes.push(`$emit: defineEmits<`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end); | ||
codes.push(`>(),\n`); | ||
} | ||
else if (scriptSetupRanges.emitsRuntimeArg) { | ||
codes.push(`$emit: defineEmits(`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end); | ||
codes.push(`),\n`); | ||
} | ||
} | ||
if (scriptSetupRanges.exposeRuntimeArg) { | ||
codes.push(`...(`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.exposeRuntimeArg.start, scriptSetupRanges.exposeRuntimeArg.end); | ||
codes.push(`),\n`); | ||
} | ||
codes.push(`};\n`); | ||
codes.push(`},\n`); | ||
if (scriptRanges?.exportDefault?.args) { | ||
addVirtualCode('script', scriptRanges.exportDefault.args.start + 1, scriptRanges.exportDefault.args.end - 1); | ||
} | ||
codes.push(`});\n`); | ||
generateTemplate(); | ||
if (mode === 'return') { | ||
codes.push(`return `); | ||
} | ||
else if (mode === 'export') { | ||
codes.push('export default '); | ||
} | ||
if (mode === 'return' || mode === 'export') { | ||
if (!vueCompilerOptions.skipTemplateCodegen && (htmlGen?.hasSlot || scriptSetupRanges?.slotsTypeArg)) { | ||
usedHelperTypes.WithTemplateSlots = true; | ||
codes.push(`{} as __VLS_WithTemplateSlots<typeof __VLS_publicComponent, ReturnType<typeof __VLS_template>>;`); | ||
} | ||
else { | ||
codes.push(`{} as typeof __VLS_publicComponent;`); | ||
} | ||
} | ||
if (mode === 'export') { | ||
generateExportDefaultEndMapping(); | ||
} | ||
return scriptSetupGeneratedOffset; | ||
} | ||
function generateTemplate() { | ||
function generateTemplate(functional) { | ||
generatedTemplate = true; | ||
@@ -650,12 +636,6 @@ if (!vueCompilerOptions.skipTemplateCodegen) { | ||
generateConstNameOption(); | ||
if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) { | ||
codes.push(`var __VLS_slots!: `); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end); | ||
codes.push(';\n'); | ||
} | ||
; | ||
codes.push(`function __VLS_template() {\n`); | ||
const templateGened = generateTemplateContext(); | ||
codes.push(`}\n`); | ||
generateComponentForTemplateUsage(templateGened.cssIds); | ||
generateComponentForTemplateUsage(functional, templateGened.cssIds); | ||
} | ||
@@ -671,25 +651,9 @@ else { | ||
} | ||
function generateComponentForTemplateUsage(cssIds) { | ||
function generateComponentForTemplateUsage(functional, cssIds) { | ||
if (sfc.scriptSetup && scriptSetupRanges) { | ||
codes.push(`const __VLS_internalComponent = (await import('${vueCompilerOptions.lib}')).defineComponent({\n`); | ||
generateComponentOptions(functional); | ||
codes.push(`setup() {\n`); | ||
codes.push(`return {\n`); | ||
// fill ctx from props | ||
if (bypassDefineComponent) { | ||
if (scriptSetupRanges.propsAssignName) { | ||
codes.push(`...${scriptSetupRanges.propsAssignName},\n`); | ||
} | ||
else if (scriptSetupRanges.withDefaultsArg && scriptSetupRanges.propsTypeArg) { | ||
codes.push(`...withDefaults(defineProps<`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsTypeArg.start, scriptSetupRanges.propsTypeArg.end); | ||
codes.push(`>(), `); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.withDefaultsArg.start, scriptSetupRanges.withDefaultsArg.end); | ||
codes.push(`),\n`); | ||
} | ||
else if (scriptSetupRanges.propsRuntimeArg) { | ||
codes.push(`...defineProps(`); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.propsRuntimeArg.start, scriptSetupRanges.propsRuntimeArg.end); | ||
codes.push(`),\n`); | ||
} | ||
} | ||
generateSetupReturns(); | ||
// bindings | ||
@@ -775,5 +739,2 @@ const templateUsageVars = getTemplateUsageVars(); | ||
codes.push(`let __VLS_ctx!: ${useGlobalThisTypeInCtx ? 'typeof globalThis &' : ''}`); | ||
if (sfc.scriptSetup) { | ||
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_publicComponent, new () => {}>> & `); | ||
} | ||
codes.push(`InstanceType<__VLS_PickNotAny<typeof __VLS_internalComponent, new () => {}>> & {\n`); | ||
@@ -795,3 +756,3 @@ /* CSS Module */ | ||
codes.push(`let __VLS_otherComponents!: NonNullable<typeof __VLS_internalComponent extends { components: infer C } ? C : {}> & typeof __VLS_componentsOption;\n`); | ||
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & typeof __VLS_publicComponent & (new () => { ${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: typeof __VLS_slots })>;\n`); | ||
codes.push(`let __VLS_own!: __VLS_SelfComponent<typeof __VLS_name, typeof __VLS_internalComponent & (new () => { ${(0, shared_1.getSlotsPropertyName)(vueCompilerOptions.target)}: typeof ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'} })>;\n`); | ||
codes.push(`let __VLS_localComponents!: typeof __VLS_otherComponents & Omit<typeof __VLS_own, keyof typeof __VLS_otherComponents>;\n`); | ||
@@ -827,12 +788,7 @@ codes.push(`let __VLS_components!: typeof __VLS_localComponents & __VLS_GlobalComponents & typeof __VLS_ctx;\n`); // for html completion, TS references... | ||
codes.push(`// no template\n`); | ||
if (scriptSetupRanges?.slotsTypeArg && sfc.scriptSetup) { | ||
codes.push(`let __VLS_slots!: `); | ||
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.slotsTypeArg.start, scriptSetupRanges.slotsTypeArg.end); | ||
codes.push(`;\n`); | ||
} | ||
else { | ||
if (!scriptSetupRanges?.defineSlots) { | ||
codes.push(`const __VLS_slots = {};\n`); | ||
} | ||
} | ||
codes.push(`return __VLS_slots;\n`); | ||
codes.push(`return ${scriptSetupRanges?.slotsAssignName ?? '__VLS_slots'};\n`); | ||
return { cssIds }; | ||
@@ -902,9 +858,2 @@ function generateCssClassProperty(styleIndex, className, classRange, propertyType, optional, referencesCodeLens) { | ||
if (htmlGen) { | ||
let bindingNames = []; | ||
if (scriptSetupRanges) { | ||
bindingNames = bindingNames.concat(scriptSetupRanges.bindings.map(range => sfc.scriptSetup?.content.substring(range.start, range.end) ?? '')); | ||
} | ||
if (scriptRanges) { | ||
bindingNames = bindingNames.concat(scriptRanges.bindings.map(range => sfc.script?.content.substring(range.start, range.end) ?? '')); | ||
} | ||
// fix import components unused report | ||
@@ -911,0 +860,0 @@ for (const varName of bindingNames) { |
@@ -8,3 +8,3 @@ import { FileRangeCapabilities } from '@volar/language-core'; | ||
type Code = Segment<FileRangeCapabilities>; | ||
export declare function generate(ts: typeof import('typescript/lib/tsserverlibrary'), compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions, sourceTemplate: string, sourceLang: string, sfc: Sfc, hasScriptSetupSlots: boolean, codegenStack: boolean): { | ||
export declare function generate(ts: typeof import('typescript/lib/tsserverlibrary'), compilerOptions: ts.CompilerOptions, vueCompilerOptions: VueCompilerOptions, sourceTemplate: string, sourceLang: string, sfc: Sfc, hasScriptSetupSlots: boolean, slotsAssignName: string | undefined, codegenStack: boolean): { | ||
codes: Code[]; | ||
@@ -11,0 +11,0 @@ codeStacks: muggle.StackNode[]; |
@@ -11,10 +11,10 @@ import type * as ts from 'typescript/lib/tsserverlibrary'; | ||
defineProps: TextRange | undefined; | ||
defineSlots: TextRange | undefined; | ||
defineEmits: TextRange | undefined; | ||
defineExpose: TextRange | undefined; | ||
propsAssignName: string | undefined; | ||
propsRuntimeArg: TextRange | undefined; | ||
propsTypeArg: TextRange | undefined; | ||
slotsTypeArg: TextRange | undefined; | ||
slotsAssignName: string | undefined; | ||
emitsAssignName: string | undefined; | ||
emitsRuntimeArg: TextRange | undefined; | ||
emitsTypeArg: TextRange | undefined; | ||
emitsTypeNums: number; | ||
exposeRuntimeArg: TextRange | undefined; | ||
@@ -21,0 +21,0 @@ defineProp: { |
@@ -12,8 +12,8 @@ "use strict"; | ||
let propsTypeArg; | ||
let slotsTypeArg; | ||
let defineSlots; | ||
let defineEmits; | ||
let defineExpose; | ||
let slotsAssignName; | ||
let emitsAssignName; | ||
let emitsRuntimeArg; | ||
let emitsTypeArg; | ||
let exposeRuntimeArg; | ||
let emitsTypeNums = -1; | ||
const definePropProposalA = vueCompilerOptions.experimentalDefinePropProposal === 'kevinEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=kevinEdition'); | ||
@@ -51,10 +51,10 @@ const definePropProposalB = vueCompilerOptions.experimentalDefinePropProposal === 'johnsonEdition' || ast.getFullText().trimStart().startsWith('// @experimentalDefinePropProposal=johnsonEdition'); | ||
defineProps, | ||
defineSlots, | ||
defineEmits, | ||
defineExpose, | ||
propsAssignName, | ||
propsRuntimeArg, | ||
propsTypeArg, | ||
slotsTypeArg, | ||
slotsAssignName, | ||
emitsAssignName, | ||
emitsRuntimeArg, | ||
emitsTypeArg, | ||
emitsTypeNums, | ||
exposeRuntimeArg, | ||
@@ -145,47 +145,30 @@ defineProp, | ||
} | ||
if (vueCompilerOptions.macros.defineProps.includes(callText) | ||
|| vueCompilerOptions.macros.defineSlots.includes(callText) | ||
|| vueCompilerOptions.macros.defineEmits.includes(callText) | ||
|| vueCompilerOptions.macros.defineExpose.includes(callText)) { | ||
if (vueCompilerOptions.macros.defineProps.includes(callText)) { | ||
defineProps = _getStartEnd(node); | ||
else if (vueCompilerOptions.macros.defineSlots.includes(callText)) { | ||
defineSlots = _getStartEnd(node); | ||
if (ts.isVariableDeclaration(parent)) { | ||
slotsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) { | ||
defineEmits = _getStartEnd(node); | ||
if (ts.isVariableDeclaration(parent)) { | ||
emitsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) { | ||
defineExpose = _getStartEnd(node); | ||
if (node.arguments.length) { | ||
const runtimeArg = node.arguments[0]; | ||
if (vueCompilerOptions.macros.defineProps.includes(callText)) { | ||
propsRuntimeArg = _getStartEnd(runtimeArg); | ||
if (ts.isVariableDeclaration(parent)) { | ||
propsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) { | ||
emitsRuntimeArg = _getStartEnd(runtimeArg); | ||
if (ts.isVariableDeclaration(parent)) { | ||
emitsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
else if (vueCompilerOptions.macros.defineExpose.includes(callText)) { | ||
exposeRuntimeArg = _getStartEnd(runtimeArg); | ||
} | ||
exposeRuntimeArg = _getStartEnd(node.arguments[0]); | ||
} | ||
} | ||
else if (vueCompilerOptions.macros.defineProps.includes(callText)) { | ||
defineProps = _getStartEnd(node); | ||
if (ts.isVariableDeclaration(parent)) { | ||
propsAssignName = parent.name.getText(ast); | ||
} | ||
if (node.arguments.length) { | ||
propsRuntimeArg = _getStartEnd(node.arguments[0]); | ||
} | ||
if (node.typeArguments?.length) { | ||
const typeArg = node.typeArguments[0]; | ||
if (vueCompilerOptions.macros.defineProps.includes(callText)) { | ||
propsTypeArg = _getStartEnd(typeArg); | ||
if (ts.isVariableDeclaration(parent)) { | ||
propsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
if (vueCompilerOptions.macros.defineSlots.includes(callText)) { | ||
slotsTypeArg = _getStartEnd(typeArg); | ||
} | ||
else if (vueCompilerOptions.macros.defineEmits.includes(callText)) { | ||
emitsTypeArg = _getStartEnd(typeArg); | ||
if (ts.isTypeLiteralNode(typeArg)) { | ||
emitsTypeNums = typeArg.members.length; | ||
} | ||
if (ts.isVariableDeclaration(parent)) { | ||
emitsAssignName = parent.name.getText(ast); | ||
} | ||
} | ||
propsTypeArg = _getStartEnd(node.typeArguments[0]); | ||
} | ||
@@ -192,0 +175,0 @@ } |
@@ -21,10 +21,10 @@ import { Sfc, VueLanguagePlugin } from '../types'; | ||
defineProps: import("../types").TextRange | undefined; | ||
defineSlots: import("../types").TextRange | undefined; | ||
defineEmits: import("../types").TextRange | undefined; | ||
defineExpose: import("../types").TextRange | undefined; | ||
propsAssignName: string | undefined; | ||
propsRuntimeArg: import("../types").TextRange | undefined; | ||
propsTypeArg: import("../types").TextRange | undefined; | ||
slotsTypeArg: import("../types").TextRange | undefined; | ||
slotsAssignName: string | undefined; | ||
emitsAssignName: string | undefined; | ||
emitsRuntimeArg: import("../types").TextRange | undefined; | ||
emitsTypeArg: import("../types").TextRange | undefined; | ||
emitsTypeNums: number; | ||
exposeRuntimeArg: import("../types").TextRange | undefined; | ||
@@ -31,0 +31,0 @@ defineProp: { |
@@ -141,7 +141,11 @@ "use strict"; | ||
return; | ||
return templateGen.generate(ts, compilerOptions, vueCompilerOptions, _sfc.template?.content ?? '', _sfc.template?.lang ?? 'html', _sfc, hasScriptSetupSlots.value, codegenStack); | ||
return templateGen.generate(ts, compilerOptions, vueCompilerOptions, _sfc.template?.content ?? '', _sfc.template?.lang ?? 'html', _sfc, hasScriptSetupSlots.value, slotsAssignName.value, codegenStack); | ||
}); | ||
const hasScriptSetupSlots = (0, reactivity_1.shallowRef)(false); // remove when https://github.com/vuejs/core/pull/5912 merged | ||
//#region remove when https://github.com/vuejs/core/pull/5912 merged | ||
const hasScriptSetupSlots = (0, reactivity_1.shallowRef)(false); | ||
const slotsAssignName = (0, reactivity_1.shallowRef)(); | ||
//#endregion | ||
const tsxGen = (0, reactivity_1.computed)(() => { | ||
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.slotsTypeArg; | ||
hasScriptSetupSlots.value = !!scriptSetupRanges.value?.defineSlots; | ||
slotsAssignName.value = scriptSetupRanges.value?.slotsAssignName; | ||
return (0, script_1.generate)(ts, fileName, _sfc, lang.value, scriptRanges.value, scriptSetupRanges.value, htmlGen.value, compilerOptions, vueCompilerOptions, codegenStack); | ||
@@ -148,0 +152,0 @@ }); |
import { VueCompilerOptions } from '../types'; | ||
export declare const baseName = "__VLS_types.d.ts"; | ||
export declare function getTypesCode(vueCompilerOptions: VueCompilerOptions): string; | ||
export declare function genConstructorOverloads(name?: string, nums?: number): string; | ||
//# sourceMappingURL=globalTypes.d.ts.map |
"use strict"; | ||
Object.defineProperty(exports, "__esModule", { value: true }); | ||
exports.genConstructorOverloads = exports.getTypesCode = exports.baseName = void 0; | ||
exports.getTypesCode = exports.baseName = void 0; | ||
const shared_1 = require("./shared"); | ||
@@ -16,5 +16,3 @@ exports.baseName = '__VLS_types.d.ts'; | ||
type __VLS_Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
type __VLS_Prettify<T> = { [K in keyof T]: T[K]; } & {}; | ||
@@ -72,22 +70,2 @@ type __VLS_GlobalComponents = | ||
type __VLS_FillingEventArg<E> = E extends (...args: any) => any ? __VLS_FillingEventArg_ParametersLength<E> extends 0 ? ($event?: undefined) => ReturnType<E> : E : E; | ||
type __VLS_EmitEvent<F, E> = | ||
F extends { | ||
(event: E, ...payload: infer P): any | ||
} ? (...payload: P) => void | ||
: F extends { | ||
(event: E, ...payload: infer P): any | ||
(...args: any): any | ||
} ? (...payload: P) => void | ||
: F extends { | ||
(event: E, ...payload: infer P): any | ||
(...args: any): any | ||
(...args: any): any | ||
} ? (...payload: P) => void | ||
: F extends { | ||
(event: E, ...payload: infer P): any | ||
(...args: any): any | ||
(...args: any): any | ||
(...args: any): any | ||
} ? (...payload: P) => void | ||
: unknown | '[Type Warning] Volar could not infer $emit event more than 4 overloads without DefineComponent. see https://github.com/vuejs/language-tools/issues/60'; | ||
declare function __VLS_asFunctionalComponent<T, K = T extends new (...args: any) => any ? InstanceType<T> : unknown>(t: T, instance?: K): | ||
@@ -105,6 +83,6 @@ T extends new (...args: any) => any | ||
declare function __VLS_functionalComponentArgsRest<T extends (...args: any) => any>(t: T): Parameters<T>['length'] extends 2 ? [any] : []; | ||
declare function __VLS_pickEvent<Emit, K, E>(emit: Emit, emitKey: K, event: E): __VLS_FillingEventArg< | ||
declare function __VLS_pickEvent<E1, E2>(emitEvent: E1, propEvent: E2): __VLS_FillingEventArg< | ||
__VLS_PickNotAny< | ||
__VLS_AsFunctionOrAny<E>, | ||
__VLS_AsFunctionOrAny<__VLS_EmitEvent<Emit, K>> | ||
__VLS_AsFunctionOrAny<E2>, | ||
__VLS_AsFunctionOrAny<E1> | ||
> | ||
@@ -123,37 +101,32 @@ > | undefined; | ||
declare function __VLS_normalizeSlot<S>(s: S): S extends () => infer R ? (props: {}) => R : S; | ||
/** | ||
* emit | ||
*/ | ||
// fix https://github.com/vuejs/language-tools/issues/926 | ||
type __VLS_UnionToIntersection<U> = (U extends unknown ? (arg: U) => unknown : never) extends ((arg: infer P) => unknown) ? P : never; | ||
type __VLS_OverloadUnionInner<T, U = unknown> = U & T extends (...args: infer A) => infer R | ||
? U extends T | ||
? never | ||
: __VLS_OverloadUnionInner<T, Pick<T, keyof T> & U & ((...args: A) => R)> | ((...args: A) => R) | ||
: never; | ||
type __VLS_OverloadUnion<T> = Exclude< | ||
__VLS_OverloadUnionInner<(() => never) & T>, | ||
T extends () => never ? never : () => never | ||
>; | ||
type __VLS_ConstructorOverloads<T> = __VLS_OverloadUnion<T> extends infer F | ||
? F extends (event: infer E, ...args: infer A) => infer R | ||
? { [K in E as K extends string ? K : never]: (...args: A) => R; } | ||
: never | ||
: never; | ||
type __VLS_NormalizeEmits<T> = __VLS_Prettify< | ||
__VLS_UnionToIntersection< | ||
__VLS_ConstructorOverloads<T> & { | ||
[K in keyof T]: T[K] extends any[] ? { (...args: T[K]): void } : never | ||
} | ||
> | ||
>; | ||
`.trim(); | ||
} | ||
exports.getTypesCode = getTypesCode; | ||
// TODO: not working for overloads > n (n = 8) | ||
// see: https://github.com/vuejs/language-tools/issues/60 | ||
function genConstructorOverloads(name = 'ConstructorOverloads', nums) { | ||
let code = ''; | ||
code += `type ${name}<T> =\n`; | ||
if (nums === undefined) { | ||
for (let i = 8; i >= 1; i--) { | ||
gen(i); | ||
} | ||
} | ||
else if (nums > 0) { | ||
gen(nums); | ||
} | ||
code += `// 0\n`; | ||
code += `{};\n`; | ||
return code; | ||
function gen(i) { | ||
code += `// ${i}\n`; | ||
code += `T extends {\n`; | ||
for (let j = 1; j <= i; j++) { | ||
code += `(event: infer E${j}, ...payload: infer P${j}): void;\n`; | ||
} | ||
code += `} ? (\n`; | ||
for (let j = 1; j <= i; j++) { | ||
if (j > 1) | ||
code += '& '; | ||
code += `(E${j} extends string ? { [K${j} in E${j}]: (...payload: P${j}) => void } : {})\n`; | ||
} | ||
code += `) :\n`; | ||
} | ||
} | ||
exports.genConstructorOverloads = genConstructorOverloads; | ||
//# sourceMappingURL=globalTypes.js.map |
{ | ||
"name": "@vue/language-core", | ||
"version": "1.8.13", | ||
"version": "1.8.14", | ||
"main": "out/index.js", | ||
@@ -37,3 +37,3 @@ "license": "MIT", | ||
}, | ||
"gitHead": "4d25501776edb5a0712c10c6921fffbdc9454a23" | ||
"gitHead": "5febf2694e80621a7b852464edc76b74f73a5cb6" | ||
} |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
255545
5519