Skip to content

Commit

Permalink
dont emit multiple payload interfaces, use arg name instead of options
Browse files Browse the repository at this point in the history
  • Loading branch information
JoelEinbinder committed Mar 19, 2020
1 parent bf6301e commit f5c458c
Showing 1 changed file with 39 additions and 8 deletions.
47 changes: 39 additions & 8 deletions utils/generate_types/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,21 +117,52 @@ function argNameForType(type) {
return type[0].toLowerCase() + type.slice(1);
}

/**
* @param {Documentation.Class} classDesc
*/
function hasUniqueEvents(classDesc) {
if (!classDesc.events.size)
return false;
const parent = parentClass(classDesc);
if (!parent)
return true;
return Array.from(classDesc.events.keys()).some(eventName => !parent.events.has(eventName));
}

/**
* @param {Documentation.Class} classDesc
*/
function createEventDescriptions(classDesc) {
if (!hasUniqueEvents(classDesc))
return [];
const descriptions = [];
for (const [eventName, value] of classDesc.events) {
const type = typeToString(value && value.type, classDesc.name, eventName, 'payload');
const argName = argNameForType(type);
const params = argName ? `${argName} : ${type}` : '';
descriptions.push({
params,
eventName,
comment: value.comment
});
}
return descriptions;
}

/**
* @param {Documentation.Class} classDesc
*/
function classBody(classDesc) {
const parts = [];
const eventDescriptions = createEventDescriptions(classDesc);
for (const method of ['on', 'once', 'addListener']) {
for (const [eventName, value] of classDesc.events) {
if (value.comment)
parts.push(writeComment(value.comment, ' '));
const type = typeToString(value && value.type, classDesc.name, eventName, 'payload');
const argName = argNameForType(type);
const params = argName ? `${argName} : ${type}` : '';
parts.push(` ${method}(event: '${eventName}', listener: (${params}) => void): this;\n`);
for (const {eventName, params, comment} of eventDescriptions) {
if (comment)
parts.push(writeComment(comment, ' '));
parts.push(` ${method}(event: '${eventName}', listener: (${params}) => void): this;\n`);
}
}

const members = classDesc.membersArray.filter(member => member.kind !== 'event');
parts.push(members.map(member => {
if (member.kind === 'event')
Expand Down Expand Up @@ -312,7 +343,7 @@ function matchingBracket(str, open, close) {
function argsFromMember(member, ...namespace) {
if (member.kind === 'property')
return '';
return '(' + member.argsArray.map(arg => `${nameForProperty(arg)}: ${typeToString(arg.type, ...namespace, member.name, 'options')}`).join(', ') + ')';
return '(' + member.argsArray.map(arg => `${nameForProperty(arg)}: ${typeToString(arg.type, ...namespace, member.name, arg.name)}`).join(', ') + ')';
}
/**
* @param {Documentation.Member} member
Expand Down

0 comments on commit f5c458c

Please sign in to comment.