-
Notifications
You must be signed in to change notification settings - Fork 471
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(addon-doc): add type reference handler for improve documentation (…
- Loading branch information
Showing
14 changed files
with
215 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
8 changes: 0 additions & 8 deletions
8
projects/addon-doc/components/documentation/pipes/content-tooltip.pipe.ts
This file was deleted.
Oops, something went wrong.
31 changes: 31 additions & 0 deletions
31
projects/addon-doc/components/documentation/pipes/type-reference.pipe.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import {Inject, Pipe, PipeTransform} from '@angular/core'; | ||
import { | ||
TUI_DOC_TYPE_REFERENCE_HANDLER, | ||
TUI_DOC_TYPE_REFERENCE_PARSER, | ||
} from '@taiga-ui/addon-doc/tokens'; | ||
import type {TuiDocTypeReferenceParsed} from '@taiga-ui/addon-doc/utils'; | ||
import {TuiHandler, TuiStringHandler} from '@taiga-ui/cdk'; | ||
|
||
@Pipe({name: `tuiDocTypeReference`}) | ||
export class TuiDocTypeReferencePipe implements PipeTransform { | ||
constructor( | ||
@Inject(TUI_DOC_TYPE_REFERENCE_PARSER) | ||
private readonly parser: TuiHandler<string, TuiDocTypeReferenceParsed>, | ||
@Inject(TUI_DOC_TYPE_REFERENCE_HANDLER) | ||
private readonly linkHandler?: TuiStringHandler<string | null> | null, | ||
) {} | ||
|
||
transform(original: string): ReadonlyArray<{ | ||
type: string; | ||
extracted: string; | ||
reference: string | null; | ||
}> { | ||
return this.parser(original) | ||
.map(({type, extracted}) => ({ | ||
type, | ||
extracted, | ||
reference: this.linkHandler?.(extracted) ?? null, | ||
})) | ||
.sort((a, b) => b.reference?.localeCompare(a.reference ?? ``) ?? -1); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
import { | ||
TuiDocTypeReferenceParsed, | ||
tuiTypeReferenceParser, | ||
} from '@taiga-ui/addon-doc/utils'; | ||
import {tuiCreateToken, TuiHandler, TuiStringHandler} from '@taiga-ui/cdk'; | ||
|
||
export const TUI_DOC_TYPE_REFERENCE_HANDLER = tuiCreateToken<TuiStringHandler< | ||
string | null | ||
> | null>(null); | ||
|
||
export const TUI_DOC_TYPE_REFERENCE_PARSER = | ||
tuiCreateToken<TuiHandler<string, TuiDocTypeReferenceParsed>>(tuiTypeReferenceParser); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
58 changes: 58 additions & 0 deletions
58
projects/addon-doc/utils/test/type-reference-parser.spec.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
import {tuiTypeReferenceParser} from '@taiga-ui/addon-doc'; | ||
|
||
describe(`tuiTypeReferenceParser`, () => { | ||
it(`parse`, () => { | ||
expect( | ||
tuiTypeReferenceParser(` | ||
PolymorpheusContent<TuiValueContentContext<T | G, K | V>> | ||
| Map<T, boolean> | ||
| ReadonlyArray<string | null> | ||
| readonly string[] | ||
| T | ||
| ReadonlyArray<readonly number[]> | ||
| [TuiDay, number][] | ||
| TuiStringHandler<number> | ||
| null | ||
| readonly [number, number] | ||
| 'positive' | ||
| 'negative' | ||
| -1 | ||
| 2 | ||
| 0 | ||
| void | ||
| CustomEvent<TuiIconError> | ||
| TuiComparator<T> | ||
| readonly T[] | ||
| TuiDay | ||
| [TuiTime, TuiTime] | ||
| T[][] | ||
`), | ||
).toEqual([ | ||
{ | ||
type: `PolymorpheusContent<TuiValueContentContext<T | G, K | V>>`, | ||
extracted: `PolymorpheusContent`, | ||
}, | ||
{type: `Map<T, boolean>`, extracted: `Map`}, | ||
{type: `ReadonlyArray<string | null>`, extracted: `string`}, | ||
{type: `readonly string[]`, extracted: `string`}, | ||
{type: `T`, extracted: `unknown`}, | ||
{type: `ReadonlyArray<readonly number[]>`, extracted: `number`}, | ||
{type: `[TuiDay, number][]`, extracted: `TuiDay`}, | ||
{type: `TuiStringHandler<number>`, extracted: `TuiStringHandler`}, | ||
{type: `null`, extracted: `null`}, | ||
{type: `readonly [number, number]`, extracted: `number`}, | ||
{type: `'positive'`, extracted: `string`}, | ||
{type: `'negative'`, extracted: `string`}, | ||
{type: `-1`, extracted: `number`}, | ||
{type: `2`, extracted: `number`}, | ||
{type: `0`, extracted: `number`}, | ||
{type: `void`, extracted: `void`}, | ||
{type: `CustomEvent<TuiIconError>`, extracted: `CustomEvent`}, | ||
{type: `TuiComparator<T>`, extracted: `TuiComparator`}, | ||
{type: `readonly T[]`, extracted: `unknown`}, | ||
{type: `TuiDay`, extracted: `TuiDay`}, | ||
{type: `[TuiTime, TuiTime]`, extracted: `TuiTime`}, | ||
{type: `T[][]`, extracted: `unknown`}, | ||
]); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
export type TuiDocTypeReferenceParsed = ReadonlyArray<{type: string; extracted: string}>; | ||
|
||
export function tuiTypeReferenceParser(types: string): TuiDocTypeReferenceParsed { | ||
const generics = types.match(/<([^>]+)>/g) ?? []; | ||
|
||
const escaped = generics | ||
.reduce( | ||
(result, current) => result.replace(current, current.replace(/\|/g, `&`)), | ||
types, | ||
) | ||
.split(`|`) | ||
.map(item => item.trim()); | ||
|
||
return escaped.reduce<TuiDocTypeReferenceParsed>((result, type) => { | ||
let extracted = type | ||
.trim() | ||
.replace(/readonly /g, ``) | ||
.replace(/\[\]/g, ``); | ||
|
||
extracted = | ||
extracted.match(/ReadonlyArray<([^>]+)>/)?.[1]?.split(`&`)?.[0] ?? extracted; | ||
extracted = extracted.match(/\[([^\]]+)\]/)?.[1]?.split(`,`)?.[0] ?? extracted; | ||
extracted = (extracted.split(`<`)?.[0] ?? extracted)?.trim() ?? ``; | ||
extracted = Number.isNaN(parseFloat(extracted)) ? extracted : `number`; | ||
extracted = /^'(.+)'$|^"(.+)"$|^`(.+)`$/.test(extracted) ? `string` : extracted; | ||
extracted = extracted.length === 1 ? `unknown` : extracted; | ||
|
||
return result.concat({type: type.replace(/&/g, `|`), extracted}); | ||
}, []); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters