Skip to content

Commit 981c59f

Browse files
committed
refactor: simplify
1 parent 892050c commit 981c59f

File tree

2 files changed

+24
-39
lines changed

2 files changed

+24
-39
lines changed

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { Code, Sfc, TextRange } from '../../types';
44
import { codeFeatures } from '../codeFeatures';
55
import { endOfLine, generateSfcBlockSection, identifierRegex, newLine } from '../utils';
66
import { generateCamelized } from '../utils/camelized';
7+
import { generateIntersectMerge } from '../utils/merge';
78
import { wrapWith } from '../utils/wrapWith';
89
import { generateComponent } from './component';
910
import type { ScriptCodegenContext } from './context';
@@ -451,27 +452,15 @@ function* generateComponentProps(
451452
yield `}${endOfLine}`;
452453
}
453454

454-
yield `type __VLS_PublicProps = `;
455+
const propTypes: Code[] = [];
455456
if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
456-
if (ctx.generatedPropsType) {
457-
yield ` & `;
458-
}
459-
ctx.generatedPropsType = true;
460-
yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
457+
propTypes.push(`${ctx.localTypes.PropsChildren}<__VLS_Slots>`);
461458
}
462459
if (scriptSetupRanges.defineProps?.typeArg) {
463-
if (ctx.generatedPropsType) {
464-
yield ` & `;
465-
}
466-
ctx.generatedPropsType = true;
467-
yield `__VLS_Props`;
460+
propTypes.push(`__VLS_Props`);
468461
}
469462
if (scriptSetupRanges.defineModel.length) {
470-
if (ctx.generatedPropsType) {
471-
yield ` & `;
472-
}
473-
ctx.generatedPropsType = true;
474-
yield `{${newLine}`;
463+
yield `type __VLS_ModelProps = {${newLine}`;
475464
for (const defineModel of scriptSetupRanges.defineModel) {
476465
const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
477466

@@ -502,12 +491,15 @@ function* generateComponentProps(
502491
yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${newLine}`;
503492
}
504493
}
505-
yield `}`;
494+
yield `}${endOfLine}`;
495+
propTypes.push(`__VLS_ModelProps`);
506496
}
507-
if (!ctx.generatedPropsType) {
508-
yield `{}`;
497+
if (propTypes.length) {
498+
ctx.generatedPropsType = true;
499+
yield `type __VLS_PublicProps = `;
500+
yield* generateIntersectMerge(propTypes);
501+
yield endOfLine;
509502
}
510-
yield endOfLine;
511503
}
512504

513505
function* generateModelEmit(

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

Lines changed: 12 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ export function* generateTemplate(
2020
const templateCodegenCtx = createTemplateCodegenContext({
2121
scriptSetupBindingNames: new Set(),
2222
});
23-
yield* generateTemplateCtx(options, ctx);
23+
yield* generateTemplateCtx(options);
2424
yield* generateTemplateElements();
2525
yield* generateTemplateComponents(options);
2626
yield* generateTemplateDirectives(options);
@@ -35,10 +35,7 @@ export function* generateTemplate(
3535
}
3636
}
3737

38-
function* generateTemplateCtx(
39-
options: ScriptCodegenOptions,
40-
ctx: ScriptCodegenContext,
41-
): Generator<Code> {
38+
function* generateTemplateCtx(options: ScriptCodegenOptions): Generator<Code> {
4239
const exps: Code[] = [];
4340

4441
if (options.vueCompilerOptions.petiteVueExtensions.some(ext => options.fileName.endsWith(ext))) {
@@ -63,29 +60,25 @@ function* generateTemplateCtx(
6360
emitTypes.push(`typeof __VLS_modelEmit`);
6461
}
6562
if (emitTypes.length) {
66-
yield `type __VLS_EmitProps = __VLS_EmitsToProps<__VLS_NormalizeEmits<${emitTypes.join(' & ')}>>${endOfLine};`;
63+
yield `type __VLS_EmitProps = __VLS_EmitsToProps<__VLS_NormalizeEmits<${emitTypes.join(' & ')}>>${endOfLine}`;
6764
exps.push(`{} as { $emit: ${emitTypes.join(' & ')} }`);
6865
}
6966

67+
const propTypes: string[] = [];
7068
const { defineProps, withDefaults } = options.scriptSetupRanges ?? {};
7169
const props = defineProps?.arg
7270
? `typeof ${defineProps.name ?? `__VLS_props`}`
73-
: defineProps?.typeArg && withDefaults?.arg
74-
? `__VLS_WithDefaultsGlobal<__VLS_Props, typeof __VLS_defaults>`
71+
: defineProps?.typeArg
72+
? withDefaults?.arg
73+
? `__VLS_WithDefaultsGlobal<__VLS_Props, typeof __VLS_defaults>`
74+
: `__VLS_Props`
7575
: undefined;
76-
77-
const propTypes: string[] = [];
78-
if (ctx.generatedPropsType) {
79-
if (props) {
80-
propTypes.push(`__VLS_SpreadMerge<__VLS_PublicProps, ${props}>`);
81-
}
82-
else {
83-
propTypes.push(`__VLS_PublicProps`);
84-
}
85-
}
86-
else if (props) {
76+
if (props) {
8777
propTypes.push(props);
8878
}
79+
if (options.scriptSetupRanges?.defineModel.length) {
80+
propTypes.push(`__VLS_ModelProps`);
81+
}
8982
if (emitTypes.length) {
9083
propTypes.push(`__VLS_EmitProps`);
9184
}

0 commit comments

Comments
 (0)