Skip to content
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
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ Parameters:
- `params.inputFiles`: The list of files to scan and for which the documentation should be build.
- `params.options`: Optional compiler options to generate the docs

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/docs.ts#L477)
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/docs.ts#L496)

### :gear: documentationToMarkdown

Expand All @@ -164,7 +164,7 @@ Parameters:
- `params.entries`: The entries of the documentation (functions, constants and classes).
- `params.options`: Optional configuration to render the Markdown content. See `types.ts` for details.

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/markdown.ts#L296)
[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/lib/markdown.ts#L335)

### :gear: generateDocumentation

Expand Down
27 changes: 23 additions & 4 deletions src/lib/docs.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,18 @@ const isNodeExportedOrPublic = (node: Node): boolean => {
);
};

/**
* True if node is a method or property and is declaration is static.
*/
const isNodeStatic = (node: Node): boolean | undefined => {
if (!isMethodDeclaration(node) && !isPropertyDeclaration(node)) {
return undefined;
}

const flags = getCombinedModifierFlags(node as Declaration);
return (flags & ModifierFlags.Static) !== 0;
};

/** Serialize a signature (call or construct) */
const serializeSignature = ({
checker,
Expand Down Expand Up @@ -274,17 +286,22 @@ const visit = ({
const addDocEntry = ({
symbol,
doc_type,
isStatic,
node
}: {
symbol: TypeScriptSymbol | undefined;
doc_type: DocEntryType;
node: Node;
}): void => {
} & Pick<DocEntry, 'isStatic'>): void => {
if (!symbol) {
return;
}

const details = serializeSymbol({checker, symbol, doc_type});
const details = {
...serializeSymbol({checker, symbol, doc_type}),
isStatic
};

pushEntry({node, details});
};

Expand Down Expand Up @@ -335,7 +352,8 @@ const visit = ({
forEachChild(node, visitChild);
} else if (isMethodDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name);
addDocEntry({symbol, doc_type: 'method', node});
const isStatic = isNodeStatic(node);
addDocEntry({symbol, doc_type: 'method', isStatic, node});
} else if (isFunctionDeclaration(node)) {
const symbol = checker.getSymbolAtLocation(node.name ?? node);
addDocEntry({symbol, doc_type: 'function', node});
Expand Down Expand Up @@ -365,7 +383,8 @@ const visit = ({
} else if (isPropertyDeclaration(node)) {
// We test for the property after the arrow function because a public property of a class can be an arrow function.
const symbol = checker.getSymbolAtLocation(node.name);
addDocEntry({symbol, doc_type: 'property', node});
const isStatic = isNodeStatic(node);
addDocEntry({symbol, doc_type: 'property', isStatic, node});
} else if (isVariableStatement(node)) {
const {
declarationList: {declarations, flags}
Expand Down
53 changes: 46 additions & 7 deletions src/lib/markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,15 @@ const inlineDocParam = (documentation: string | undefined): string =>
const inlineParams = (params: Params[]): string[] =>
params.map(({name, documentation}) => `* \`${name}\`${inlineDocParam(documentation)}`);

const reduceStatic = (values: DocEntry[]): [DocEntry[], DocEntry[]] =>
values.reduce<[DocEntry[], DocEntry[]]>(
([i, s], value) => [
[...i, ...(value.isStatic !== true ? [value] : [])],
[...s, ...(value.isStatic === true ? [value] : [])]
],
[[], []]
);

const classesToMarkdown = ({
entry,
headingLevel,
Expand Down Expand Up @@ -73,9 +82,19 @@ const classesToMarkdown = ({
markdown.push('\n');
}

if ((methods?.length ?? 0) > 0) {
markdown.push(`${headingLevel}# Methods\n`);
markdown.push(`${tableOfContent({entries: methods ?? [], emoji})}\n`);
const methodsToMarkdown = ({
methods,
titlePrefix
}: {
methods: DocEntry[];
titlePrefix?: string;
}): void => {
if ((methods?.length ?? 0) === 0) {
return;
}

markdown.push(`${headingLevel}# ${titlePrefix ?? ''}Methods\n`);
markdown.push(`${tableOfContent({entries: methods, emoji})}\n`);

// Explicitly do not pass repo to generate the source code link afterwards for the all block
markdown.push(
Expand All @@ -86,10 +105,25 @@ const classesToMarkdown = ({
emoji
})
);
}
};

const [instanceMethods, staticMethods] = reduceStatic(methods ?? []);

if ((properties?.length ?? 0) > 0) {
markdown.push(`${headingLevel}# Properties\n`);
methodsToMarkdown({methods: staticMethods, titlePrefix: 'Static '});
methodsToMarkdown({methods: instanceMethods});

const propertiesToMarkdown = ({
properties,
titlePrefix
}: {
properties: DocEntry[];
titlePrefix?: string;
}): void => {
if ((properties?.length ?? 0) === 0) {
return;
}

markdown.push(`${headingLevel}# ${titlePrefix ?? ''}Properties\n`);
markdown.push(`${tableOfContent({entries: properties ?? [], emoji})}\n`);

// Explicitly do not pass repo to generate the source code link afterwards for the all block
Expand All @@ -101,7 +135,12 @@ const classesToMarkdown = ({
emoji
})
);
}
};

const [instanceProperties, staticProperties] = reduceStatic(properties ?? []);

propertiesToMarkdown({properties: staticProperties, titlePrefix: 'Static '});
propertiesToMarkdown({properties: instanceProperties});

return markdown.join('\n');
};
Expand Down
1 change: 1 addition & 0 deletions src/lib/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export interface DocEntry {
url?: string;
documentation?: string;
type?: string;
isStatic?: boolean;
constructors?: DocEntryConstructor[];
parameters?: DocEntry[];
methods?: DocEntry[];
Expand Down
62 changes: 56 additions & 6 deletions src/test/mock.json
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,8 @@
]
}
],
"doc_type": "method"
"doc_type": "method",
"isStatic": true
},
{
"name": "accountBalance",
Expand Down Expand Up @@ -181,7 +182,8 @@
"documentation": "Public method.",
"type": "() => void",
"jsDocs": [],
"doc_type": "method"
"doc_type": "method",
"isStatic": false
}
],
"properties": [
Expand All @@ -190,7 +192,8 @@
"documentation": "The documentation of the public property.",
"type": "string",
"jsDocs": [],
"doc_type": "property"
"doc_type": "property",
"isStatic": false
}
],
"fileName": "src/test/mock.ts"
Expand Down Expand Up @@ -225,7 +228,8 @@
]
}
],
"doc_type": "method"
"doc_type": "method",
"isStatic": true
},
{
"name": "metadata",
Expand All @@ -251,7 +255,8 @@
"documentation": "Description",
"type": "() => void",
"jsDocs": [],
"doc_type": "method"
"doc_type": "method",
"isStatic": false
}
],
"properties": [],
Expand Down Expand Up @@ -500,5 +505,50 @@
],
"doc_type": "const",
"fileName": "src/test/mock.ts"
},
{
"name": "Number",
"documentation": "Should differentiate methods / properties and static methods / properties",
"type": "typeof Number",
"jsDocs": [],
"doc_type": "class",
"constructors": [],
"methods": [
{
"name": "add",
"documentation": "",
"type": "(n: Number) => Number",
"jsDocs": [],
"doc_type": "method",
"isStatic": false
},
{
"name": "add",
"documentation": "",
"type": "(n1: Number, n2: Number) => Number",
"jsDocs": [],
"doc_type": "method",
"isStatic": true
}
],
"properties": [
{
"name": "hello",
"documentation": "",
"type": "string",
"jsDocs": [],
"doc_type": "property",
"isStatic": false
},
{
"name": "world",
"documentation": "",
"type": "string",
"jsDocs": [],
"doc_type": "property",
"isStatic": true
}
],
"fileName": "src/test/mock.ts"
}
]
]
70 changes: 65 additions & 5 deletions src/test/mock.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,11 +152,9 @@ Parameters:
* `hardwareWallet`


### Methods
### Static Methods

- [create](#gear-create)
- [accountBalance](#gear-accountbalance)
- [shouldBeDocumented](#gear-shouldbedocumented)

#### :gear: create

Expand All @@ -168,6 +166,11 @@ Create a LedgerCanister

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L55)

### Methods

- [accountBalance](#gear-accountbalance)
- [shouldBeDocumented](#gear-shouldbedocumented)

#### :gear: accountBalance

Returns the balance of the specified account identifier.
Expand Down Expand Up @@ -212,10 +215,9 @@ The documentation of the public property.



### Methods
### Static Methods

- [create](#gear-create)
- [metadata](#gear-metadata)

#### :gear: create

Expand All @@ -227,6 +229,10 @@ This create function is public as well.

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L133)

### Methods

- [metadata](#gear-metadata)

#### :gear: metadata

The token metadata (name, symbol, etc.).
Expand Down Expand Up @@ -255,6 +261,60 @@ Description

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L150)

## :factory: Number

Should differentiate methods / properties and static methods / properties

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L315)

### Static Methods

- [add](#gear-add)

#### :gear: add

| Method | Type |
| ---------- | ---------- |
| `add` | `(n1: Number, n2: Number) => Number` |

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L327)

### Methods

- [add](#gear-add)

#### :gear: add

| Method | Type |
| ---------- | ---------- |
| `add` | `(n: Number) => Number` |

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L323)

### Static Properties

- [world](#gear-world)

#### :gear: world

| Property | Type |
| ---------- | ---------- |
| `world` | `string` |

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L318)

### Properties

- [hello](#gear-hello)

#### :gear: hello

| Property | Type |
| ---------- | ---------- |
| `hello` | `string` |

[:link: Source](https://github.com/peterpeterparker/tsdoc-markdown/tree/main/src/test/mock.ts#L317)

## :nut_and_bolt: Enum

- [Time](#gear-time)
Expand Down
Loading