Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Documentation.toJson() can handle nested submodules #1190

Merged
merged 4 commits into from
Nov 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 18 additions & 11 deletions src/docgen/view/documentation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -312,22 +312,29 @@ export class Documentation {
}

/**
* Lookup a submodule by a submodule name.
* Lookup a submodule by a submodule name. To look up a nested submodule, encode it as a
* dot-separated path, e.g., 'top-level-module.nested-module.another-nested-one'.
*/
private findSubmodule(assembly: reflect.Assembly, submodule: string): reflect.Submodule {
const submodules = assembly.submodules.filter(
(s) => s.name === submodule,
);
type ReflectSubmodules = typeof assembly.submodules;
return recurse(submodule.split('.'), assembly.submodules);

if (submodules.length === 0) {
throw new Error(`Submodule ${submodule} not found in assembly ${assembly.name}@${assembly.version}`);
}
function recurse(names: string[], submodules: ReflectSubmodules): reflect.Submodule {
const [head, ...tail] = names;
const found = submodules.filter(
(s) => s.name === head,
);

if (submodules.length > 1) {
throw new Error(`Found multiple submodules with name: ${submodule} in assembly ${assembly.name}@${assembly.version}`);
}
if (found.length === 0) {
throw new Error(`Submodule ${submodule} not found in assembly ${assembly.name}@${assembly.version}`);
}

return submodules[0];
if (found.length > 1) {
throw new Error(`Found multiple submodules with name: ${submodule} in assembly ${assembly.name}@${assembly.version}`);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm pretty sure this can never happen, but okay.

}

return tail.length === 0 ? found[0] : recurse(tail, found[0].submodules);
}
}

private async createAssembly(
Expand Down
Loading