Skip to content

Commit

Permalink
refactor: don't generate additional virtual code for defineEmits
Browse files Browse the repository at this point in the history
related to #3581
  • Loading branch information
johnsoncodehk committed Sep 23, 2023
1 parent cd1e0aa commit 02ae1e9
Show file tree
Hide file tree
Showing 4 changed files with 46 additions and 65 deletions.
72 changes: 29 additions & 43 deletions packages/vue-language-core/src/generators/script.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,14 +50,13 @@ export function generate(
scriptSetupRanges = {
bindings: [],
emitsAssignName: undefined,
emitsRuntimeArg: undefined,
emitsTypeArg: undefined,
emitsTypeNums: 0,
exposeRuntimeArg: undefined,
leadingCommentEndOffset: 0,
importSectionEndOffset: 0,
defineProps: undefined,
defineSlots: undefined,
defineEmits: undefined,
slotsAssignName: undefined,
propsAssignName: undefined,
propsRuntimeArg: undefined,
Expand Down Expand Up @@ -391,23 +390,6 @@ export function generate(
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');
}
//#endregion

codes.push('return {} as {\n');
codes.push(`props: typeof __VLS_props,\n`);
codes.push('expose(exposed: typeof __VLS_exposed): void,\n');
Expand Down Expand Up @@ -489,10 +471,27 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:

const scriptSetupGeneratedOffset = muggle.getLength(codes) - scriptSetupRanges.importSectionEndOffset;

let setupCodeModifies: [string, number][] = [];
if (scriptSetupRanges.defineSlots && !scriptSetupRanges.slotsAssignName) {
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, scriptSetupRanges.defineSlots.start);
codes.push(`const __VLS_slots = `);
addVirtualCode('scriptSetup', scriptSetupRanges.defineSlots.start);
setupCodeModifies.push([`const __VLS_slots = `, scriptSetupRanges.defineSlots.start]);
}
if (scriptSetupRanges.defineEmits && !scriptSetupRanges.emitsAssignName) {
setupCodeModifies.push([`const __VLS_emit = `, scriptSetupRanges.defineEmits.start]);
}
setupCodeModifies = setupCodeModifies.sort((a, b) => a[1] - b[1]);

if (setupCodeModifies.length) {
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset, setupCodeModifies[0][1]);
while (setupCodeModifies.length) {
const [code, start] = setupCodeModifies.shift()!;
codes.push(code);
if (setupCodeModifies.length) {
addVirtualCode('scriptSetup', start, setupCodeModifies[0][1]);
}
else {
addVirtualCode('scriptSetup', start);
}
}
}
else {
addVirtualCode('scriptSetup', scriptSetupRanges.importSectionEndOffset);
Expand Down Expand Up @@ -591,16 +590,13 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
}
codes.push(`},\n`);
}
if (scriptSetupRanges.emitsTypeArg) {
if (scriptSetupRanges.defineEmits) {
usedHelperTypes.EmitsTypeHelpers = true;
codes.push(`emits: ({} as __VLS_UnionToIntersection<__VLS_NormalizeEmits<`);
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsTypeArg.start, scriptSetupRanges.emitsTypeArg.end);
codes.push(`>>),\n`);
}
else if (scriptSetupRanges.emitsRuntimeArg) {
codes.push(`emits: (`);
addExtraReferenceVirtualCode('scriptSetup', scriptSetupRanges.emitsRuntimeArg.start, scriptSetupRanges.emitsRuntimeArg.end);
codes.push(`),\n`);
codes.push(
`emits: ({} as __VLS_UnionToIntersection<__VLS_NormalizeEmits<typeof `,
scriptSetupRanges.emitsAssignName ?? '__VLS_emit',
`>>),\n`,
);
}
}

Expand All @@ -622,18 +618,8 @@ declare function defineProp<T>(value?: T | (() => T), required?: boolean, rest?:
codes.push(`)),\n`);
}
// fill $emit
if (scriptSetupRanges.emitsAssignName) {
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName},\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.defineEmits) {
codes.push(`$emit: ${scriptSetupRanges.emitsAssignName ?? '__VLS_emit'},\n`);
}
}

Expand Down
27 changes: 8 additions & 19 deletions packages/vue-language-core/src/parsers/scriptSetupRanges.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,9 @@ export function parseScriptSetupRanges(
let propsRuntimeArg: TextRange | undefined;
let propsTypeArg: TextRange | undefined;
let defineSlots: TextRange | undefined;
let defineEmits: TextRange | undefined;
let slotsAssignName: string | undefined;
let emitsAssignName: string | undefined;
let emitsRuntimeArg: TextRange | undefined;
let emitsTypeArg: TextRange | undefined;
let exposeRuntimeArg: TextRange | undefined;
let emitsTypeNums = -1;

Expand Down Expand Up @@ -67,13 +66,12 @@ export function parseScriptSetupRanges(
withDefaultsArg,
defineProps,
defineSlots,
defineEmits,
propsAssignName,
propsRuntimeArg,
propsTypeArg,
slotsAssignName,
emitsAssignName,
emitsRuntimeArg,
emitsTypeArg,
emitsTypeNums,
exposeRuntimeArg,
defineProp,
Expand Down Expand Up @@ -178,6 +176,12 @@ export function parseScriptSetupRanges(
slotsAssignName = parent.name.getText(ast);
}
}
if (vueCompilerOptions.macros.defineEmits.includes(callText)) {
defineEmits = _getStartEnd(node);
if (ts.isVariableDeclaration(parent)) {
emitsAssignName = parent.name.getText(ast);
}
}
if (node.arguments.length) {
const runtimeArg = node.arguments[0];
if (vueCompilerOptions.macros.defineProps.includes(callText)) {
Expand All @@ -186,12 +190,6 @@ export function parseScriptSetupRanges(
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);
}
Expand All @@ -204,15 +202,6 @@ export function parseScriptSetupRanges(
propsAssignName = parent.name.getText(ast);
}
}
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);
}
}
}
}
else if (vueCompilerOptions.macros.withDefaults.includes(callText)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@ const ScriptSetupExact = defineComponent({
props: {
foo: String,
},
emits: ['change', 'delete'],
emits: {
change: (..._payload: any[]) => { },
delete: (..._payload: any[]) => { },
},
setup() {
return {};
},
Expand Down
7 changes: 5 additions & 2 deletions packages/vue-tsc/tests/__snapshots__/dts.spec.ts.snap
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@
exports[`vue-tsc-dts > Input: components/script-setup.vue, Output: components/script-setup.vue.d.ts 1`] = `
"declare const _default: import(\\"vue\\").DefineComponent<{
foo: StringConstructor;
}, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, (\\"change\\" | \\"delete\\")[], \\"change\\" | \\"delete\\", import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<{
}, {}, unknown, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {
change: (...payload: any[]) => void;
delete: (...payload: any[]) => void;
}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<{
foo: StringConstructor;
}>> & {
onChange?: (...args: any[]) => any;
Expand Down Expand Up @@ -133,7 +136,7 @@ export default _default;
exports[`vue-tsc-dts > Input: defineEmits-new-syntax/main.vue, Output: defineEmits-new-syntax/main.vue.d.ts 1`] = `
"declare const _default: import(\\"vue\\").DefineComponent<{}, {}, {}, {}, {}, import(\\"vue\\").ComponentOptionsMixin, import(\\"vue\\").ComponentOptionsMixin, {
foo: (args_0: string) => void;
foo: (payload_0: string) => void;
}, string, import(\\"vue\\").VNodeProps & import(\\"vue\\").AllowedComponentProps & import(\\"vue\\").ComponentCustomProps, Readonly<import(\\"vue\\").ExtractPropTypes<{}>> & {
onFoo?: (args_0: string) => any;
}, {}, {}>;
Expand Down

0 comments on commit 02ae1e9

Please sign in to comment.