Skip to content

Commit f5c458c

Browse files
committed
dont emit multiple payload interfaces, use arg name instead of options
1 parent bf6301e commit f5c458c

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

utils/generate_types/index.js

+39-8
Original file line numberDiff line numberDiff line change
@@ -117,21 +117,52 @@ function argNameForType(type) {
117117
return type[0].toLowerCase() + type.slice(1);
118118
}
119119

120+
/**
121+
* @param {Documentation.Class} classDesc
122+
*/
123+
function hasUniqueEvents(classDesc) {
124+
if (!classDesc.events.size)
125+
return false;
126+
const parent = parentClass(classDesc);
127+
if (!parent)
128+
return true;
129+
return Array.from(classDesc.events.keys()).some(eventName => !parent.events.has(eventName));
130+
}
131+
132+
/**
133+
* @param {Documentation.Class} classDesc
134+
*/
135+
function createEventDescriptions(classDesc) {
136+
if (!hasUniqueEvents(classDesc))
137+
return [];
138+
const descriptions = [];
139+
for (const [eventName, value] of classDesc.events) {
140+
const type = typeToString(value && value.type, classDesc.name, eventName, 'payload');
141+
const argName = argNameForType(type);
142+
const params = argName ? `${argName} : ${type}` : '';
143+
descriptions.push({
144+
params,
145+
eventName,
146+
comment: value.comment
147+
});
148+
}
149+
return descriptions;
150+
}
151+
120152
/**
121153
* @param {Documentation.Class} classDesc
122154
*/
123155
function classBody(classDesc) {
124156
const parts = [];
157+
const eventDescriptions = createEventDescriptions(classDesc);
125158
for (const method of ['on', 'once', 'addListener']) {
126-
for (const [eventName, value] of classDesc.events) {
127-
if (value.comment)
128-
parts.push(writeComment(value.comment, ' '));
129-
const type = typeToString(value && value.type, classDesc.name, eventName, 'payload');
130-
const argName = argNameForType(type);
131-
const params = argName ? `${argName} : ${type}` : '';
132-
parts.push(` ${method}(event: '${eventName}', listener: (${params}) => void): this;\n`);
159+
for (const {eventName, params, comment} of eventDescriptions) {
160+
if (comment)
161+
parts.push(writeComment(comment, ' '));
162+
parts.push(` ${method}(event: '${eventName}', listener: (${params}) => void): this;\n`);
133163
}
134164
}
165+
135166
const members = classDesc.membersArray.filter(member => member.kind !== 'event');
136167
parts.push(members.map(member => {
137168
if (member.kind === 'event')
@@ -312,7 +343,7 @@ function matchingBracket(str, open, close) {
312343
function argsFromMember(member, ...namespace) {
313344
if (member.kind === 'property')
314345
return '';
315-
return '(' + member.argsArray.map(arg => `${nameForProperty(arg)}: ${typeToString(arg.type, ...namespace, member.name, 'options')}`).join(', ') + ')';
346+
return '(' + member.argsArray.map(arg => `${nameForProperty(arg)}: ${typeToString(arg.type, ...namespace, member.name, arg.name)}`).join(', ') + ')';
316347
}
317348
/**
318349
* @param {Documentation.Member} member

0 commit comments

Comments
 (0)