Skip to content

Commit 81854db

Browse files
committed
refactor: simplify
1 parent e992097 commit 81854db

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';
@@ -448,27 +449,15 @@ function* generateComponentProps(
448449
yield `}${endOfLine}`;
449450
}
450451

451-
yield `type __VLS_PublicProps = `;
452+
const propTypes: Code[] = [];
452453
if (scriptSetupRanges.defineSlots && options.vueCompilerOptions.jsxSlots) {
453-
if (ctx.generatedPropsType) {
454-
yield ` & `;
455-
}
456-
ctx.generatedPropsType = true;
457-
yield `${ctx.localTypes.PropsChildren}<__VLS_Slots>`;
454+
propTypes.push(`${ctx.localTypes.PropsChildren}<__VLS_Slots>`);
458455
}
459456
if (scriptSetupRanges.defineProps?.typeArg) {
460-
if (ctx.generatedPropsType) {
461-
yield ` & `;
462-
}
463-
ctx.generatedPropsType = true;
464-
yield `__VLS_Props`;
457+
propTypes.push(`__VLS_Props`);
465458
}
466459
if (scriptSetupRanges.defineModel.length) {
467-
if (ctx.generatedPropsType) {
468-
yield ` & `;
469-
}
470-
ctx.generatedPropsType = true;
471-
yield `{${newLine}`;
460+
yield `type __VLS_ModelProps = {${newLine}`;
472461
for (const defineModel of scriptSetupRanges.defineModel) {
473462
const [propName, localName] = getPropAndLocalName(scriptSetup, defineModel);
474463

@@ -499,12 +488,15 @@ function* generateComponentProps(
499488
yield `'${modifierName}'?: Partial<Record<${modifierType}, true>>,${newLine}`;
500489
}
501490
}
502-
yield `}`;
491+
yield `}${endOfLine}`;
492+
propTypes.push(`__VLS_ModelProps`);
503493
}
504-
if (!ctx.generatedPropsType) {
505-
yield `{}`;
494+
if (propTypes.length) {
495+
ctx.generatedPropsType = true;
496+
yield `type __VLS_PublicProps = `;
497+
yield* generateIntersectMerge(propTypes);
498+
yield endOfLine;
506499
}
507-
yield endOfLine;
508500
}
509501

510502
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)