Skip to content

Commit

Permalink
fix(docgen): typescript: sort APIs, methods and properties
Browse files Browse the repository at this point in the history
  • Loading branch information
drauggres committed Dec 18, 2019
1 parent 618a379 commit 452fc9e
Showing 1 changed file with 30 additions and 8 deletions.
38 changes: 30 additions & 8 deletions packages/titanium-docgen/generators/typescript_generator.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,27 @@ function removeMembers(typeInfo, interfaceNode) {
typeInfo.methods = filterMembers(typeInfo.methods, interfaceNode.methods);
}

/**
* @param {Object} a method, property or event
* @param {Object} b method, property or event
* @return {number}
*/
function sortByName(a, b) {
if (a.name > b.name) {
return 1;
}
if (a.name < b.name) {
return -1;
}
if (a.type) {
return -1;
}
if (b.type) {
return 1;
}
return 0;
}

/**
* Parses the prepared API type docs and creates a syntax tree that can be used
* to generate a TypeScript type definition file.
Expand Down Expand Up @@ -128,21 +149,22 @@ class DocsParser {
this.viewInterface = new InterfaceNode(this.apis['Titanium.UI.View']);
uiNamespace.addInterface(this.viewInterface);

for (const fullyQualifiedTypeName in this.apis) {
const namesList = Object.keys(this.apis).sort();
namesList.forEach(fullyQualifiedTypeName => {
if (fullyQualifiedTypeName.startsWith('__')) {
continue;
return;
}

const typeInfo = this.apis[fullyQualifiedTypeName];
const namespaceParts = typeInfo.name.split('.');
namespaceParts.pop();

if (namespaceParts[0] !== 'Titanium' && namespaceParts.length > 0) {
continue;
return;
} else if (typeInfo.name === 'Global') {
continue;
return;
} else if (typeInfo.name === 'Dictionary') {
continue;
return;
}

const parentNamespace = this.findOrCreateNamespace(namespaceParts);
Expand All @@ -164,7 +186,7 @@ class DocsParser {
} else {
console.warn(`Unhandled type ${typeInfo.name}`);
}
}
});
}

/**
Expand Down Expand Up @@ -674,7 +696,7 @@ class MemberNode {
return !propertyDoc.__hide;
});

this.properties = properties.map(propertyDoc => {
this.properties = properties.sort(sortByName).map(propertyDoc => {
// Make all properties of global interfaces optional by default
if (this.fullyQualifiedName.indexOf('.') === -1 && propertyDoc.optional === undefined) {
propertyDoc.optional = true;
Expand All @@ -695,7 +717,7 @@ class MemberNode {
}

let filteredMethods = [];
methods.forEach(methodDoc => {
methods.sort(sortByName).forEach(methodDoc => {
if (methodDoc.__hide) {
return;
}
Expand Down

0 comments on commit 452fc9e

Please sign in to comment.