Skip to content

Commit

Permalink
feat: generate docs for jsii submodules (#172)
Browse files Browse the repository at this point in the history
Fixes #1 

- Adds docs for all classes, structs, interfaces, and enums in submodules
- Changes class initialization examples to include submodule paths, e.g. `new foo.MyClass(...)` instead of `new MyClass(...)`
- Updates class docs to include a "submodule" section if the class has an associated namespace
- Modifies tsconfig to allow using flatMap

I tested this with docs for [projen](https://github.com/projen/projen), and the would-be updated docs are here: https://gist.github.com/Chriscbr/41f7f7e7d1d358dc6ebf0f8cb277b937
  • Loading branch information
Chriscbr committed Dec 14, 2020
1 parent 61286da commit 692c07f
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .projenrc.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const { TypeScriptLibraryProject, Semver, Jest, Eslint } = require('projen');
const { TypeScriptLibraryProject, Semver } = require('projen');

const jsii = '1.9.0';

Expand Down
15 changes: 10 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Home } from './render/home';
import { ClassPage, InterfacePage } from './render/klass';
import { elementAnchorLink } from './render/links';
import { Page, RenderContext, JsiiEntity } from './render/page';
import { flatMap } from './render/util';

/**
* Renders markdown files into an output directory for a jsii typesystem.
Expand Down Expand Up @@ -85,12 +86,16 @@ export async function renderPages(typesystem: jsiiReflect.TypeSystem, ctx: Rende
return result;
}

function documentAssembly(ctx: RenderContext, assembly: jsiiReflect.Assembly): Page[] {
function documentAssembly(ctx: RenderContext, asm: jsiiReflect.Assembly): Page[] {
const classes = [...asm.classes, ...flatMap(asm.submodules, submod => [...submod.classes])];
const interfaces = [...asm.interfaces, ...flatMap(asm.submodules, submod => [...submod.interfaces])];
const enums = [...asm.enums, ...flatMap(asm.submodules, submod => [...submod.enums])];

return [
new Home(ctx, assembly),
...assembly.classes.map(c => new ClassPage(ctx, c)),
...assembly.interfaces.map(i => new InterfacePage(ctx, i)),
...assembly.enums.map(e => new EnumPage(ctx, e)),
new Home(ctx, asm),
...classes.map(c => new ClassPage(ctx, c)),
...interfaces.map(i => new InterfacePage(ctx, i)),
...enums.map(e => new EnumPage(ctx, e)),
];
}

13 changes: 9 additions & 4 deletions src/render/home.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as jsiiReflect from 'jsii-reflect';
import { Page, RenderContext } from './page';
import { flatMap } from './util';

export class Home extends Page {
constructor(ctx: RenderContext, private readonly assembly: jsiiReflect.Assembly) {
Expand All @@ -22,10 +23,14 @@ export class Home extends Page {
lines.push('# API Reference');
}

addSection('Classes', assembly.classes);
addSection('Structs', assembly.interfaces.filter(i => i.isDataType()));
addSection('Interfaces', assembly.interfaces.filter(i => !i.isDataType()));
addSection('Enums', assembly.enums);
const classes = [...assembly.classes, ...flatMap(assembly.submodules, submod => [...submod.classes])];
const interfaces = [...assembly.interfaces, ...flatMap(assembly.submodules, submod => [...submod.interfaces])];
const enums = [...assembly.enums, ...flatMap(assembly.submodules, submod => [...submod.enums])];

addSection('Classes', classes);
addSection('Structs', interfaces.filter(i => i.isDataType()));
addSection('Interfaces', interfaces.filter(i => !i.isDataType()));
addSection('Enums', enums);

return lines;

Expand Down
7 changes: 7 additions & 0 deletions src/render/klass.ts
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,7 @@ export class ClassPage extends Base {
klass.docs.toString(),
'',
...this.renderImplements(klass),
...this.renderNamespace(klass),
...this.renderExtends(klass),
...this.renderImplementors(klass),
...this.renderFactories(klass),
Expand All @@ -182,6 +183,12 @@ export class ClassPage extends Base {
];
}

private renderNamespace(obj: jsiiReflect.ClassType): string[] {
return obj.namespace ? [
`${this.strong('Submodule')}: ${obj.namespace}\n`,
] : [];
}

private renderImplements(c: jsiiReflect.ClassType) {
const ifaces = c.getInterfaces(true);
if (ifaces.length === 0) { return []; }
Expand Down
7 changes: 5 additions & 2 deletions src/render/page.ts
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,10 @@ export abstract class Page {
const showVisibility = options.long || method.protected;

if (method instanceof jsiiReflect.Initializer) {
return `new ${method.parentType.name}(${parameters})`;
const qualifiedName = method.parentType.namespace
? `${method.parentType.namespace}.${method.parentType.name}`
: method.parentType.name;
return `new ${qualifiedName}(${parameters})`;
} else {
return `${showVisibility ? visibility : ''}${staticDecl}${name}(${parameters})${returnDecl}`;
}
Expand All @@ -157,7 +160,7 @@ export abstract class Page {
* Turn the given type into a link
*/
protected typeLink(type: jsiiReflect.Type): string {
const display = type.name;
const display = type.namespace ? `${type.namespace}.${type.name}` : type.name;
const url = this.ctx.links.renderLink(type);

if (!url) {
Expand Down

0 comments on commit 692c07f

Please sign in to comment.