Skip to content

Commit

Permalink
fix(core): handle optional types correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Nov 26, 2024
1 parent a0e2ff9 commit 1d33b64
Show file tree
Hide file tree
Showing 8 changed files with 153 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/small-lizards-float.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typedoc-plugin-markdown': patch
---

- Handle Optional types correctly (#719).
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
IntersectionType,
IntrinsicType,
NamedTupleMember,
OptionalType,
QueryType,
ReferenceType,
ReflectionType,
Expand Down Expand Up @@ -79,6 +80,10 @@ export function someType(this: MarkdownThemeContext, model?: SomeType): string {
return this.partials.namedTupleType(model);
}

if (model instanceof OptionalType) {
return this.partials.someType(model.elementType) + '?';
}

if (model.toString() == 'null') {
return backTicks('null');
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -206,3 +206,14 @@ export let curriedFunction: {
) => boolean;
<Value>(searchElement: unknown, iterable: Iterable<Value>): string;
};

export type Stuff = { a: string; b: string };
/**
* Comments for array of stuff
*/
export const functionWithArrayOfStuff = (a: [Stuff]) => {};

/**
* Comments for array of stuff?
*/
export const functionWithArrayOfOptionalStuff = (a: [Stuff?]) => {};
Original file line number Diff line number Diff line change
Expand Up @@ -4481,6 +4481,11 @@ exports[`Navigation should gets Navigation Json for single entry point: (Output
"kind": 2097152,
"path": "type-aliases/StringLiteralType.md"
},
{
"title": "Stuff",
"kind": 2097152,
"path": "type-aliases/Stuff.md"
},
{
"title": "TupleType",
"kind": 2097152,
Expand Down Expand Up @@ -4591,6 +4596,16 @@ exports[`Navigation should gets Navigation Json for single entry point: (Output
"kind": 64,
"path": "functions/functionReturningAUnionType.md"
},
{
"title": "functionWithArrayOfOptionalStuff",
"kind": 64,
"path": "functions/functionWithArrayOfOptionalStuff.md"
},
{
"title": "functionWithArrayOfStuff",
"kind": 64,
"path": "functions/functionWithArrayOfStuff.md"
},
{
"title": "functionWithComplexParams",
"kind": 64,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -434,6 +434,98 @@ http://abc.com
"
`;
exports[`Function Reflection should compile function with array of stuff optional: (Output File Strategy "members") (Option Group "1") 1`] = `
"# Function: functionWithArrayOfOptionalStuff()
> **functionWithArrayOfOptionalStuff**(\`a\`): \`void\`
Comments for array of stuff?
## Parameters
• **a**: [[\`Stuff\`](../type-aliases/Stuff.md)?]
## Returns
\`void\`
## Source
[functions.ts:1](http://source-url)
"
`;
exports[`Function Reflection should compile function with array of stuff optional: (Output File Strategy "members") (Option Group "2") 1`] = `
"# Function: functionWithArrayOfOptionalStuff()
\`\`\`ts
function functionWithArrayOfOptionalStuff(a: [Stuff?]): void
\`\`\`
Comments for array of stuff?
## Parameters
| Parameter | Type |
| :------ | :------ |
| \`a\` | [[\`Stuff\`](../type-aliases/Stuff.md)?] |
## Returns
\`void\`
## Source
[functions.ts:1](http://source-url)
"
`;
exports[`Function Reflection should compile function with array of stuff: (Output File Strategy "members") (Option Group "1") 1`] = `
"# Function: functionWithArrayOfStuff()
> **functionWithArrayOfStuff**(\`a\`): \`void\`
Comments for array of stuff
## Parameters
• **a**: [[\`Stuff\`](../type-aliases/Stuff.md)]
## Returns
\`void\`
## Source
[functions.ts:1](http://source-url)
"
`;
exports[`Function Reflection should compile function with array of stuff: (Output File Strategy "members") (Option Group "2") 1`] = `
"# Function: functionWithArrayOfStuff()
\`\`\`ts
function functionWithArrayOfStuff(a: [Stuff]): void
\`\`\`
Comments for array of stuff
## Parameters
| Parameter | Type |
| :------ | :------ |
| \`a\` | [[\`Stuff\`](../type-aliases/Stuff.md)] |
## Returns
\`void\`
## Source
[functions.ts:1](http://source-url)
"
`;
exports[`Function Reflection should compile function with complex params: (Output File Strategy "members") (Option Group "1") 1`] = `
"# Function: functionWithComplexParams()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,7 @@ Module commments
| [QueryType](type-aliases/QueryType.md) | Comments for query type |
| [ReadonlyMappedType](type-aliases/ReadonlyMappedType.md) | Comments for ReadonlyMapedType |
| [StringLiteralType](type-aliases/StringLiteralType.md) | Comments for StringLiteralType |
| [Stuff](type-aliases/Stuff.md) | - |
| [TupleType](type-aliases/TupleType.md) | Comments for TupleType |
| [TypeWithExternalSymbolLinkMapping](type-aliases/TypeWithExternalSymbolLinkMapping.md) | Comments for TypeWithExternalSymbolLinkMapping |
| [TypeWithReturns](type-aliases/TypeWithReturns.md) | Comments for TypeWithReturns |
Expand Down Expand Up @@ -116,6 +117,8 @@ Module commments
| [functionReturningAPromise](functions/functionReturningAPromise.md) | Comments for function |
| [functionReturningAString](functions/functionReturningAString.md) | Comments for function |
| [functionReturningAUnionType](functions/functionReturningAUnionType.md) | Comments for function |
| [functionWithArrayOfOptionalStuff](functions/functionWithArrayOfOptionalStuff.md) | Comments for array of stuff? |
| [functionWithArrayOfStuff](functions/functionWithArrayOfStuff.md) | Comments for array of stuff |
| [functionWithComplexParams](functions/functionWithComplexParams.md) | Function with function parmas |
| [functionWithDefaultParameters](functions/functionWithDefaultParameters.md) | This is a function with a parameter that has a default value. |
| [functionWithMultipleSignatures](functions/functionWithMultipleSignatures.md) | Main function comment. |
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,8 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"functions/functionReturningAString.md",
"functions/functionReturningAUnionType.md",
"functions/functionReturningAnObject.md",
"functions/functionWithArrayOfOptionalStuff.md",
"functions/functionWithArrayOfStuff.md",
"functions/functionWithComplexParams.md",
"functions/functionWithDefaultParameters.md",
"functions/functionWithMultipleSignatures.md",
Expand Down Expand Up @@ -840,6 +842,7 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"type-aliases/QueryType.md",
"type-aliases/ReadonlyMappedType.md",
"type-aliases/StringLiteralType.md",
"type-aliases/Stuff.md",
"type-aliases/TupleType.md",
"type-aliases/TypeDeclarationWithSpecialCharacters.md",
"type-aliases/TypeWithExternalSymbolLinkMapping.md",
Expand Down Expand Up @@ -891,6 +894,8 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"functions/functionReturningAString.md",
"functions/functionReturningAUnionType.md",
"functions/functionReturningAnObject.md",
"functions/functionWithArrayOfOptionalStuff.md",
"functions/functionWithArrayOfStuff.md",
"functions/functionWithComplexParams.md",
"functions/functionWithDefaultParameters.md",
"functions/functionWithMultipleSignatures.md",
Expand Down Expand Up @@ -923,6 +928,7 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"type-aliases/QueryType.md",
"type-aliases/ReadonlyMappedType.md",
"type-aliases/StringLiteralType.md",
"type-aliases/Stuff.md",
"type-aliases/TupleType.md",
"type-aliases/TypeDeclarationWithSpecialCharacters.md",
"type-aliases/TypeWithExternalSymbolLinkMapping.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -120,4 +120,20 @@ describe(`Function Reflection`, () => {
'functions/functionWithComplexParams.md',
);
});

test(`should compile function with array of stuff`, () => {
expectFileToEqual(
'reflections',
'members',
'functions/functionWithArrayOfStuff.md',
);
});

test(`should compile function with array of stuff optional`, () => {
expectFileToEqual(
'reflections',
'members',
'functions/functionWithArrayOfOptionalStuff.md',
);
});
});

0 comments on commit 1d33b64

Please sign in to comment.