Skip to content

Commit

Permalink
chore(core): fix invalid typescript syntax for type aliases inside co…
Browse files Browse the repository at this point in the history
…de blocks
  • Loading branch information
tgreyuk committed Dec 18, 2024
1 parent 3e0d41a commit f91ce05
Show file tree
Hide file tree
Showing 11 changed files with 234 additions and 187 deletions.
5 changes: 5 additions & 0 deletions .changeset/pink-kings-divide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'typedoc-plugin-markdown': patch
---

- Fix invalid typescript syntax for type aliases inside declaration code blocks when "useCodeBlocks" is true (#741).
271 changes: 129 additions & 142 deletions package-lock.json

Large diffs are not rendered by default.

16 changes: 8 additions & 8 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@
"prepare": "husky"
},
"devDependencies": {
"@changesets/cli": "^2.27.10",
"@commitlint/cli": "^19.6.0",
"@changesets/cli": "^2.27.11",
"@commitlint/cli": "^19.6.1",
"@commitlint/config-conventional": "^19.6.0",
"@eslint/js": "^9.16.0",
"@eslint/js": "^9.17.0",
"@types/eslint__js": "^8.42.3",
"@types/fs-extra": "^11.0.4",
"@types/jest": "^29.5.14",
"@types/node": "^22.10.1",
"@types/node": "^22.10.2",
"consola": "^3.2.3",
"copyfiles": "^2.4.1",
"eslint": "^9.16.0",
"eslint": "^9.17.0",
"fs-extra": "^11.2.0",
"glob": "^11.0.0",
"husky": "^9.1.7",
"jest": "^29.7.0",
"markdownlint": "^0.37.0",
"markdownlint": "^0.37.1",
"p-limit": "^6.1.0",
"prettier": "^3.4.2",
"prettier-plugin-organize-imports": "^4.1.0",
Expand All @@ -52,8 +52,8 @@
"ts-morph": "^24.0.0",
"tsc-alias": "^1.8.10",
"tsx": "^4.19.2",
"typedoc": "^0.27.3",
"typedoc": "^0.27.5",
"typescript": "^5.7.2",
"typescript-eslint": "^8.17.0"
"typescript-eslint": "^8.18.1"
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { backTicks, bold, codeBlock } from '@plugin/libs/markdown/index.js';
import { encodeAngleBrackets, escapeChars } from '@plugin/libs/utils/index.js';
import { MarkdownThemeContext } from '@plugin/theme/index.js';
import { DeclarationReflection } from 'typedoc';
import { DeclarationReflection, ReflectionKind } from 'typedoc';

export function declarationTitle(
this: MarkdownThemeContext,
Expand Down Expand Up @@ -73,7 +73,9 @@ export function declarationTitle(
}

if (declarationType) {
name.push(': ');
const delimiter =
useCodeBlocks && model.kind === ReflectionKind.TypeAlias ? ' = ' : ': ';
name.push(delimiter);
}

md.push(name.join(''));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,12 @@ export function unionType(
model: UnionType,
): string {
const useCodeBlocks = this.options.getValue('useCodeBlocks');
const shouldFormat = useCodeBlocks && model.types.length > 4;
const md = model.types
.map((unionType) =>
this.partials.someType(unionType, { forceCollapse: true }),
)
.join(shouldFormat ? `\n \\| ` : ` \\| `);
const typesOut = model.types.map((unionType) =>
this.partials.someType(unionType, { forceCollapse: true }),
);
const shouldFormat =
useCodeBlocks &&
(typesOut?.join('').length > 70 || typesOut?.join('').includes('\n'));
const md = typesOut.join(shouldFormat ? `\n \\| ` : ` \\| `);
return shouldFormat ? `\n \\| ` + md : md;
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,3 +34,5 @@ export const variableWithChars = {
export function some_prettier_function(param: string) {
return param;
}

export type SomePrettierTypeAlias = string | { x: 1 } | boolean | { y: 2 };
Original file line number Diff line number Diff line change
Expand Up @@ -482,7 +482,7 @@ exports[`Objects And Params should compile intersection type: (Output File Strat
"# Type Alias: IntersectionType
\`\`\`ts
type IntersectionType: TupleType & ArrayType & {
type IntersectionType = TupleType & ArrayType & {
bar: number;
};
\`\`\`
Expand Down Expand Up @@ -644,7 +644,7 @@ exports[`Objects And Params should compile literal type: (Output File Strategy "
"# Type Alias: LiteralType
\`\`\`ts
type LiteralType: {
type LiteralType = {
someFunctionWithArrow: () => string;
x: string;
y: {
Expand Down Expand Up @@ -826,7 +826,10 @@ exports[`Objects And Params should compile union type: (Output File Strategy "me
"# Type Alias: UnionType
\`\`\`ts
type UnionType: string | boolean | {
type UnionType =
| string
| boolean
| {
z: string;
};
\`\`\`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1330,20 +1330,26 @@ exports[`Function Reflection should compile function with union params: (Output
\`\`\`ts
function functionWithUnionParams(
primitiveUnions: string | number,
objectUnions: {
objectUnions:
| {
a: string;
b: 1;
} | {
}
| {
a: number;
b: 1;
c: {
x: string;
};
},
mixedUnions: string | number | {
mixedUnions:
| string
| number
| {
a: string;
b: string;
} | {
}
| {
a: {
y: string;
z: string;
Expand All @@ -1357,8 +1363,8 @@ function functionWithUnionParams(
| Parameter | Type | Description |
| :------ | :------ | :------ |
| \`primitiveUnions\` | \`string\` \\| \`number\` | Comments for primitiveUnions |
| \`objectUnions\` | \\{ \`a\`: \`string\`; \`b\`: \`1\`; \\} \\| \\{ \`a\`: \`number\`; \`b\`: \`1\`; \`c\`: \\{ \`x\`: \`string\`; \\}; \\} | Comments for objectUnionsUseful |
| \`mixedUnions\` | \`string\` \\| \`number\` \\| \\{ \`a\`: \`string\`; \`b\`: \`string\`; \\} \\| \\{ \`a\`: \\{ \`y\`: \`string\`; \`z\`: \`string\`; \\}; \\} | Comments for mixedUnions |
| \`objectUnions\` | \\| \\{ \`a\`: \`string\`; \`b\`: \`1\`; \\} \\| \\{ \`a\`: \`number\`; \`b\`: \`1\`; \`c\`: \\{ \`x\`: \`string\`; \\}; \\} | Comments for objectUnionsUseful |
| \`mixedUnions\` | \\| \`string\` \\| \`number\` \\| \\{ \`a\`: \`string\`; \`b\`: \`string\`; \\} \\| \\{ \`a\`: \\{ \`y\`: \`string\`; \`z\`: \`string\`; \\}; \\} | Comments for mixedUnions |
| \`noUnions\` | \`string\` | Comments for noUnions |
## Returns
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ exports[`Type Alias Reflection should compile array of objects type: (Output Fil
"# Type Alias: ArrayOfObjectsType
\`\`\`ts
type ArrayOfObjectsType: object[];
type ArrayOfObjectsType = object[];
\`\`\`
## Type declaration
Expand Down Expand Up @@ -60,7 +60,7 @@ exports[`Type Alias Reflection should compile array type: (Output File Strategy
"# Type Alias: ArrayType
\`\`\`ts
type ArrayType: string[];
type ArrayType = string[];
\`\`\`
Comments for ArrayType
Expand Down Expand Up @@ -92,7 +92,7 @@ exports[`Type Alias Reflection should compile conditional type: (Output File Str
"# Type Alias: ConditionalType\\<T\\>
\`\`\`ts
type ConditionalType<T>: T extends string ? "string" : T extends number ? "number" : T extends boolean ? "boolean" : T extends undefined ? "undefined" : "object";
type ConditionalType<T> = T extends string ? "string" : T extends number ? "number" : T extends boolean ? "boolean" : T extends undefined ? "undefined" : "object";
\`\`\`
Comments for ConditionalType
Expand Down Expand Up @@ -126,7 +126,7 @@ exports[`Type Alias Reflection should compile external link symbol type: (Output
"# Type Alias: TypeWithExternalSymbolLinkMapping
\`\`\`ts
type TypeWithExternalSymbolLinkMapping: Application;
type TypeWithExternalSymbolLinkMapping = Application;
\`\`\`
Comments for TypeWithExternalSymbolLinkMapping
Expand Down Expand Up @@ -286,7 +286,7 @@ exports[`Type Alias Reflection should compile external literal type: (Output Fil
"# Type Alias: LiteralType
\`\`\`ts
type LiteralType: object;
type LiteralType = object;
\`\`\`
Comments for LiteralType
Expand Down Expand Up @@ -331,7 +331,7 @@ exports[`Type Alias Reflection should compile external reference type: (Output F
"# Type Alias: ExternalReferenceType
\`\`\`ts
type ExternalReferenceType: ClassWithTypeParameters<"x", "y">;
type ExternalReferenceType = ClassWithTypeParameters<"x", "y">;
\`\`\`
Comments for ExternalReferenceType
Expand Down Expand Up @@ -380,7 +380,7 @@ exports[`Type Alias Reflection should compile function type: (Output File Strate
"# Type Alias: FunctionType()
\`\`\`ts
type FunctionType: (name: string, value: unknown) => void;
type FunctionType = (name: string, value: unknown) => void;
\`\`\`
Comments for FunctionType
Expand Down Expand Up @@ -426,7 +426,7 @@ exports[`Type Alias Reflection should compile index access type: (Output File St
"# Type Alias: IndexAccessType
\`\`\`ts
type IndexAccessType: ArrayOfObjectsType[number];
type IndexAccessType = ArrayOfObjectsType[number];
\`\`\`
Comments for IndexAccessType
Expand Down Expand Up @@ -460,7 +460,7 @@ exports[`Type Alias Reflection should compile intersection type: (Output File St
"# Type Alias: IntersectionType
\`\`\`ts
type IntersectionType: TupleType & ArrayType & object;
type IntersectionType = TupleType & ArrayType & object;
\`\`\`
Comments for IntersectionType
Expand Down Expand Up @@ -498,7 +498,7 @@ exports[`Type Alias Reflection should compile partial mapped type: (Output File
"# Type Alias: PartialMappedType\\<T\\>
\`\`\`ts
type PartialMappedType<T>: { [P in keyof T]?: T[P] };
type PartialMappedType<T> = { [P in keyof T]?: T[P] };
\`\`\`
Comments for PartialMappedType
Expand Down Expand Up @@ -532,7 +532,7 @@ exports[`Type Alias Reflection should compile primitive type: (Output File Strat
"# Type Alias: PrimitiveType
\`\`\`ts
type PrimitiveType: boolean;
type PrimitiveType = boolean;
\`\`\`
Comments for PrimitiveType
Expand Down Expand Up @@ -560,7 +560,7 @@ exports[`Type Alias Reflection should compile query type: (Output File Strategy
"# Type Alias: QueryType
\`\`\`ts
type QueryType: typeof someQuery;
type QueryType = typeof someQuery;
\`\`\`
Comments for query type
Expand Down Expand Up @@ -592,7 +592,7 @@ exports[`Type Alias Reflection should compile readonly mapped type: (Output File
"# Type Alias: ReadonlyMappedType\\<T\\>
\`\`\`ts
type ReadonlyMappedType<T>: { readonly [P in keyof T]: T[P] };
type ReadonlyMappedType<T> = { readonly [P in keyof T]: T[P] };
\`\`\`
Comments for ReadonlyMapedType
Expand Down Expand Up @@ -626,7 +626,7 @@ exports[`Type Alias Reflection should compile string literal type: (Output File
"# Type Alias: StringLiteralType
\`\`\`ts
type StringLiteralType:
type StringLiteralType =
| " "
| "string"
| "string|with|pipes"
Expand Down Expand Up @@ -660,7 +660,7 @@ exports[`Type Alias Reflection should compile string tuple type: (Output File St
"# Type Alias: TupleType
\`\`\`ts
type TupleType: [string, number];
type TupleType = [string, number];
\`\`\`
Comments for TupleType
Expand Down Expand Up @@ -694,7 +694,7 @@ exports[`Type Alias Reflection should compile type parameter type: (Output File
"# Type Alias: TypeWithTypeParams\\<T, R\\>
\`\`\`ts
type TypeWithTypeParams<T, R>: [T, R];
type TypeWithTypeParams<T, R> = [T, R];
\`\`\`
Comments for TypeWithTypeParams
Expand Down Expand Up @@ -733,7 +733,7 @@ exports[`Type Alias Reflection should compile type with returns: (Output File St
"# Type Alias: TypeWithReturns
\`\`\`ts
type TypeWithReturns: string;
type TypeWithReturns = string;
\`\`\`
Comments for TypeWithReturns
Expand Down Expand Up @@ -765,7 +765,10 @@ exports[`Type Alias Reflection should compile union type with template strings:
"# Type Alias: UnionTypeWithTemplateStrings
\`\`\`ts
type UnionTypeWithTemplateStrings: \`v\${number}\` | \`v\${number}.\${number}\` | \`v\${number}.\${number}.\${number}\`;
type UnionTypeWithTemplateStrings =
| \`v\${number}\`
| \`v\${number}.\${number}\`
| \`v\${number}.\${number}.\${number}\`;
\`\`\`
Union with template strings
Expand Down Expand Up @@ -793,7 +796,10 @@ exports[`Type Alias Reflection should compile union type: (Output File Strategy
"# Type Alias: UnionType
\`\`\`ts
type UnionType: string | boolean | {
type UnionType =
| string
| boolean
| {
z: string;
};
\`\`\`
Expand Down Expand Up @@ -837,7 +843,10 @@ exports[`Type Alias Reflection should compile useful union type: (Output File St
"# Type Alias: UsefulUnionType
\`\`\`ts
type UsefulUnionType: string | boolean | {
type UsefulUnionType =
| string
| boolean
| {
z: string;
};
\`\`\`
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,13 @@ const originalDoubleQuote = "hello"
"
`;
exports[`Utils should get prettified function: (Output File Strategy "members") (Option Group "1") 2`] = `
"# Type Alias: SomePrettierTypeAlias
> **SomePrettierTypeAlias**: \`string\` \\| \\{ \`x\`: \`1\`; \\} \\| \`boolean\` \\| \\{ \`y\`: \`2\`; \\}
"
`;
exports[`Utils should get prettified function: (Output File Strategy "members") (Option Group "2") 1`] = `
"# Function: some_prettier_function()
Expand Down Expand Up @@ -202,6 +209,23 @@ const originalDoubleQuote = "hello";
"
`;
exports[`Utils should get prettified function: (Output File Strategy "members") (Option Group "2") 2`] = `
"# Type Alias: SomePrettierTypeAlias
\`\`\`ts
type SomePrettierTypeAlias =
| string
| {
x: 1;
}
| boolean
| {
y: 2;
};
\`\`\`
"
`;
exports[`Utils should get variable with brackets: (Output File Strategy "members") (Option Group "1") 1`] = `
"# Variable: variableWithChars
Expand Down
Loading

0 comments on commit f91ce05

Please sign in to comment.