Check for superfluous types in a packages index.ts and throw if they exist

This commit is contained in:
Fabio Berger
2018-08-15 16:50:23 -07:00
parent 9e7657ac5d
commit baab0f27b5

View File

@@ -185,6 +185,16 @@ export async function generateAndUploadDocsAsync(packageName: string, isStaging:
const referenceNamesWithDuplicates = getAllReferenceNames(propertyName, finalTypeDocOutput, []);
const referenceNames = _.uniq(referenceNamesWithDuplicates);
const exportedTypes = getAllTypeNames(finalTypeDocOutput, []);
const excessiveReferences = _.difference(exportedTypes, referenceNames);
if (!_.isEmpty(excessiveReferences)) {
throw new Error(
`${packageName} package exports BUT does not need: \n${excessiveReferences.join(
'\n',
)} \nin it\'s index.ts. Remove them then try again.`,
);
}
const missingReferences: string[] = [];
_.each(referenceNames, referenceName => {
if (!_.includes(allExportedItems, referenceName) && _.isUndefined(EXTERNAL_TYPE_TO_LINK[referenceName])) {
@@ -230,6 +240,27 @@ export async function generateAndUploadDocsAsync(packageName: string, isStaging:
});
}
function getAllTypeNames(node: any, typeNames: string[]): string[] {
if (!_.isObject(node)) {
return typeNames;
}
const typeKindStrings = ['Interface', 'Enumeration', 'Type alias'];
if (_.includes(typeKindStrings, node.kindString)) {
return [...typeNames, node.name];
}
let updatedTypeNames = typeNames;
_.each(node, nodeValue => {
if (_.isArray(nodeValue)) {
_.each(nodeValue, aNode => {
updatedTypeNames = getAllTypeNames(aNode, updatedTypeNames);
});
} else if (_.isObject(nodeValue)) {
updatedTypeNames = getAllTypeNames(nodeValue, updatedTypeNames);
}
});
return updatedTypeNames;
}
function getAllReferenceNames(propertyName: string, node: any, referenceNames: string[]): string[] {
let updatedReferenceNames = referenceNames;
if (!_.isObject(node)) {