-
Notifications
You must be signed in to change notification settings - Fork 474
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(addon-copy): add type reference handler for improve documentation
- Loading branch information
Showing
14 changed files
with
191 additions
and
33 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.
26 changes: 26 additions & 0 deletions
26
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,26 @@ | ||
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 {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, readonly string[]>, | ||
@Inject(TUI_DOC_TYPE_REFERENCE_HANDLER) | ||
private readonly linkHandler?: TuiStringHandler<string | null> | null, | ||
) {} | ||
|
||
transform(original: string): ReadonlyArray<{ | ||
type: string; | ||
original: string; | ||
reference: string | null; | ||
}> { | ||
return this.parser(original) | ||
.map(type => ({type, original, reference: this.linkHandler?.(type) ?? 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,9 @@ | ||
import {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, readonly string[]>>(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
53 changes: 53 additions & 0 deletions
53
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,53 @@ | ||
import {tuiTypeReferenceParser} from '@taiga-ui/addon-doc'; | ||
|
||
describe(`tuiTypeReferenceParser`, () => { | ||
it(`parse`, () => { | ||
expect( | ||
tuiTypeReferenceParser(` | ||
PolymorpheusContent<TuiValueContentContext<T | G, K>> | ||
| 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] | ||
`), | ||
).toEqual([ | ||
`PolymorpheusContent`, | ||
`Map`, | ||
`string`, | ||
`string`, | ||
`unknown`, | ||
`number`, | ||
`TuiDay`, | ||
`TuiStringHandler`, | ||
`null`, | ||
`number`, | ||
`string`, | ||
`string`, | ||
`number`, | ||
`number`, | ||
`number`, | ||
`void`, | ||
`CustomEvent`, | ||
`TuiComparator`, | ||
`unknown`, | ||
`TuiDay`, | ||
`TuiTime`, | ||
]); | ||
}); | ||
}); |
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,24 @@ | ||
export function tuiTypeReferenceParser(types: string): readonly string[] { | ||
const generics = types.match(/<([^>]+)>/g) ?? []; | ||
const escapedSymbol = `,`; | ||
|
||
const escaped = generics.reduce( | ||
(result, current) => result.replace(current, current.replace(`|`, escapedSymbol)), | ||
Check failure Code scanning / CodeQL Incomplete string escaping or encoding High
This replaces only the first occurrence of |.
|
||
types, | ||
); | ||
|
||
return escaped | ||
.split(`|`) | ||
.map(item => item.trim().replace(`readonly `, ``).replace(`[]`, ``)) | ||
.map( | ||
item => | ||
item.match(/ReadonlyArray<([^>]+)>/)?.[1]?.split(escapedSymbol)?.[0] ?? | ||
item, | ||
) | ||
.map(item => item.match(/\[([^\]]+)\]/)?.[1]?.split(escapedSymbol)?.[0] ?? item) | ||
.map(item => item.split(`<`)?.[0] ?? item) | ||
.map(item => item.trim()) | ||
.map(item => (Number.isNaN(parseFloat(item)) ? item : `number`)) | ||
.map(item => (/^'(.+)'$|^"(.+)"$|^`(.+)`$/.test(item) ? `string` : item)) | ||
.map(item => (item.length === 1 ? `unknown` : item)); | ||
} |
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