Skip to content

Commit

Permalink
feat: Documentation.toJson() can handle nested submodules (#1190)
Browse files Browse the repository at this point in the history
Allow users to create documentation for nested modules. Suppose a project has the following submodule structure:

```
my-project
└── foo
    └── bar
```

where `foo` and `bar` are submodules. To generate documentation for the `bar` module, call `documentation.toJson('foo.bar')`.
  • Loading branch information
otaviomacedo authored Nov 3, 2023
1 parent 673b5bf commit 9036833
Show file tree
Hide file tree
Showing 4 changed files with 6,753 additions and 11 deletions.
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}`);
}

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

private async createAssembly(
Expand Down
Loading

0 comments on commit 9036833

Please sign in to comment.