Skip to content

Commit

Permalink
refactor(language-core): clearer variables naming of script setup ranges
Browse files Browse the repository at this point in the history
  • Loading branch information
KazariEX committed Dec 16, 2024
1 parent 1067aa4 commit eee3309
Show file tree
Hide file tree
Showing 9 changed files with 295 additions and 268 deletions.
8 changes: 4 additions & 4 deletions packages/component-meta/lib/base.ts
Original file line number Diff line number Diff line change
Expand Up @@ -724,9 +724,9 @@ function readVueComponentDefaultProps(
const descriptor = vueSourceFile._sfc;
const scriptSetupRanges = descriptor.scriptSetup ? vue.parseScriptSetupRanges(ts, descriptor.scriptSetup.ast, vueCompilerOptions) : undefined;

if (descriptor.scriptSetup && scriptSetupRanges?.props.withDefaults?.arg) {
if (descriptor.scriptSetup && scriptSetupRanges?.withDefaults?.arg) {

const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.props.withDefaults.arg.start, scriptSetupRanges.props.withDefaults.arg.end);
const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.withDefaults.arg.start, scriptSetupRanges.withDefaults.arg.end);
const ast = ts.createSourceFile('/tmp.' + descriptor.scriptSetup.lang, '(' + defaultsText + ')', ts.ScriptTarget.Latest);
const obj = findObjectLiteralExpression(ast);

Expand All @@ -743,8 +743,8 @@ function readVueComponentDefaultProps(
}
}
}
} else if (descriptor.scriptSetup && scriptSetupRanges?.props.define?.arg) {
const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.props.define.arg.start, scriptSetupRanges.props.define.arg.end);
} else if (descriptor.scriptSetup && scriptSetupRanges?.defineProps?.arg) {
const defaultsText = descriptor.scriptSetup.content.slice(scriptSetupRanges.defineProps.arg.start, scriptSetupRanges.defineProps.arg.end);
const ast = ts.createSourceFile('/tmp.' + descriptor.scriptSetup.lang, '(' + defaultsText + ')', ts.ScriptTarget.Latest);
const obj = findObjectLiteralExpression(ast);

Expand Down
30 changes: 15 additions & 15 deletions packages/language-core/lib/codegen/script/component.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ export function* generateComponent(
if (ctx.bypassDefineComponent) {
yield* generateComponentSetupReturns(scriptSetupRanges);
}
if (scriptSetupRanges.expose.define) {
if (scriptSetupRanges.defineExpose) {
yield `...__VLS_exposed,${newLine}`;
}
yield `}${endOfLine}`;
Expand All @@ -40,7 +40,7 @@ export function* generateComponent(
const { args } = options.scriptRanges.exportDefault;
yield generateSfcBlockSection(options.sfc.script, args.start + 1, args.end - 1, codeFeatures.all);
}
if (options.vueCompilerOptions.target >= 3.5 && scriptSetupRanges.templateRefs.length) {
if (options.vueCompilerOptions.target >= 3.5 && scriptSetupRanges.useTemplateRef.length) {
yield `__typeRefs: {} as __VLS_TemplateResult['refs'],${newLine}`;
}
if (options.vueCompilerOptions.target >= 3.5 && options.templateCodegen?.singleRootElType) {
Expand All @@ -51,14 +51,14 @@ export function* generateComponent(

export function* generateComponentSetupReturns(scriptSetupRanges: ScriptSetupRanges): Generator<Code> {
// fill $props
if (scriptSetupRanges.props.define) {
if (scriptSetupRanges.defineProps) {
// NOTE: defineProps is inaccurate for $props
yield `$props: __VLS_makeOptional(${scriptSetupRanges.props.name ?? `__VLS_props`}),${newLine}`;
yield `...${scriptSetupRanges.props.name ?? `__VLS_props`},${newLine}`;
yield `$props: __VLS_makeOptional(${scriptSetupRanges.defineProps.name ?? `__VLS_props`}),${newLine}`;
yield `...${scriptSetupRanges.defineProps.name ?? `__VLS_props`},${newLine}`;
}
// fill $emit
if (scriptSetupRanges.emits.define) {
yield `$emit: ${scriptSetupRanges.emits.name ?? '__VLS_emit'},${newLine}`;
if (scriptSetupRanges.defineEmits) {
yield `$emit: ${scriptSetupRanges.defineEmits.name ?? '__VLS_emit'},${newLine}`;
}
}

Expand All @@ -78,10 +78,10 @@ export function* generateEmitsOption(
typeOptionType: `__VLS_ModelEmit`,
});
}
if (scriptSetupRanges.emits.define) {
const { typeArg, hasUnionTypeArg } = scriptSetupRanges.emits.define;
if (scriptSetupRanges.defineEmits) {
const { name, typeArg, hasUnionTypeArg } = scriptSetupRanges.defineEmits;
codes.push({
optionExp: `{} as __VLS_NormalizeEmits<typeof ${scriptSetupRanges.emits.name ?? '__VLS_emit'}>`,
optionExp: `{} as __VLS_NormalizeEmits<typeof ${name ?? '__VLS_emit'}>`,
typeOptionType: typeArg && !hasUnionTypeArg
? `__VLS_Emit`
: undefined,
Expand Down Expand Up @@ -139,15 +139,15 @@ export function* generatePropsOption(
codes.push({
optionExp: [
`{} as `,
scriptSetupRanges.props.withDefaults?.arg ? `${ctx.localTypes.WithDefaults}<` : '',
scriptSetupRanges.withDefaults?.arg ? `${ctx.localTypes.WithDefaults}<` : '',
`${ctx.localTypes.TypePropsToOption}<__VLS_PublicProps>`,
scriptSetupRanges.props.withDefaults?.arg ? `, typeof __VLS_withDefaultsArg>` : '',
scriptSetupRanges.withDefaults?.arg ? `, typeof __VLS_withDefaultsArg>` : '',
].join(''),
typeOptionExp: `{} as __VLS_PublicProps`,
});
}
if (scriptSetupRanges.props.define?.arg) {
const { arg } = scriptSetupRanges.props.define;
if (scriptSetupRanges.defineProps?.arg) {
const { arg } = scriptSetupRanges.defineProps;
codes.push({
optionExp: generateSfcBlockSection(scriptSetup, arg.start, arg.end, codeFeatures.navigation),
typeOptionExp: undefined,
Expand All @@ -170,7 +170,7 @@ export function* generatePropsOption(
}

const useTypeOption = options.vueCompilerOptions.target >= 3.5 && codes.every(code => code.typeOptionExp);
const useOption = !useTypeOption || scriptSetupRanges.props.withDefaults;
const useOption = !useTypeOption || scriptSetupRanges.withDefaults;

if (useTypeOption) {
if (codes.length === 1) {
Expand Down
Loading

0 comments on commit eee3309

Please sign in to comment.