Skip to content

Commit

Permalink
fix(core): fix reflection flags
Browse files Browse the repository at this point in the history
  • Loading branch information
tgreyuk committed Jun 16, 2024
1 parent 9b00dca commit e06b849
Show file tree
Hide file tree
Showing 19 changed files with 154 additions and 49 deletions.
8 changes: 4 additions & 4 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
"ts-morph": "^22.0.0",
"ts-node": "^10.9.2",
"tsc-alias": "^1.8.10",
"typedoc": "^0.26.0-beta.3",
"typedoc": "^0.26.0-beta.4",
"typescript": "^5.4.5",
"unified-prettier": "^2.0.1"
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,7 @@ async function writeResourcesFile() {
'ReferenceType',
'Reflection',
'ReflectionCategory',
'ReflectionFlags',
'ReflectionGroup',
'ReflectionKind',
'ReflectionType',
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from './get-comment-flags';
export * from './get-comment-parts';
export * from './get-declaration-type';
export * from './get-description-for-reflection';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { camelToTitleCase } from 'libs/utils';
import { MarkdownThemeContext } from 'theme';
import { DeclarationReflection, SignatureReflection } from 'typedoc';

export function getCommentFlags(
this: MarkdownThemeContext,
reflection: DeclarationReflection | SignatureReflection,
): string[] {
const flagsNotRendered: `@${string}`[] = [
'@showCategories',
'@showGroups',
'@hideCategories',
'@hideGroups',
];
const flags: string[] = [];
if (reflection.comment) {
for (const tag of reflection.comment.modifierTags) {
if (!flagsNotRendered.includes(tag)) {
flags.push(camelToTitleCase(tag.substring(1)));
}
}
}
return flags;
}
Original file line number Diff line number Diff line change
@@ -1,24 +1,32 @@
import { camelToTitleCase } from 'libs/utils';
import { backTicks } from 'libs/markdown';
import { MarkdownThemeContext } from 'theme';
import { DeclarationReflection, SignatureReflection } from 'typedoc';
import { ReflectionFlags } from 'typedoc';

export function getReflectionFlags(
this: MarkdownThemeContext,
reflection: DeclarationReflection | SignatureReflection,
): string[] {
const flagsNotRendered: `@${string}`[] = [
'@showCategories',
'@showGroups',
'@hideCategories',
'@hideGroups',
];
const flags: string[] = [];
if (reflection.comment) {
for (const tag of reflection.comment.modifierTags) {
if (!flagsNotRendered.includes(tag)) {
flags.push(camelToTitleCase(tag.substring(1)));
}
}
reflectionFlags: ReflectionFlags,
): string {
const result: string[] = [];
if (reflectionFlags.isAbstract) {
result.push(backTicks('abstract'));
}
return flags;
if (reflectionFlags.isConst) {
result.push(backTicks('const'));
}
if (reflectionFlags.isPrivate) {
result.push(backTicks('private'));
}
if (reflectionFlags.isProtected) {
result.push(backTicks('protected'));
}
if (reflectionFlags.isReadonly) {
result.push(backTicks('readonly'));
}
if (reflectionFlags.isStatic) {
result.push(backTicks('static'));
}
if (reflectionFlags.isOptional) {
result.push(backTicks('optional'));
}
return result.join(' ');
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function declaration(

md.push(
this.helpers
.getReflectionFlags(model)
.getCommentFlags(model)
.map((item) => backTicks(item))
.join(' '),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ export function declarationTitle(

const prefix: string[] = [];

if (model.flags.length) {
prefix.push(
model.flags?.map((flag) => backTicks(flag.toLowerCase())).join(' '),
);
const flagsString = this.helpers.getReflectionFlags(model.flags);

if (flagsString.length) {
prefix.push(flagsString);
}

if (model.flags.isRest) {
Expand All @@ -36,8 +36,10 @@ export function declarationTitle(
prefix.push(keyword);
}

if (prefix.length) {
md.push(prefix.join(' ') + ' ');
const prefixes = prefix.filter((prefix) => prefix.length > 0);

if (prefixes.length) {
md.push(prefixes.join(' ') + ' ');
}

const name: string[] = [];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ export function indexSignature(
model: SignatureReflection,
): string {
const md = [''];

const params = model.parameters
? model.parameters.map((parameter) => {
return parameter.type
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,8 @@ export function memberTitle(
const md: string[] = [];
const name: string[] = [];

if (
model?.kind === ReflectionKind.Class &&
model.flags?.includes('Abstract')
) {
name.push(backTicks('abstract') + ' ');
if (model?.kind === ReflectionKind.Class && model.flags?.isAbstract) {
name.push(this.helpers.getReflectionFlags(model.flags) + ' ');
}

name.push(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,12 @@ export function propertiesTable(
nameColumn.push(backTicks(propertyName));
}

const flags = this.helpers.getReflectionFlags(property);
const commentFlags = this.helpers.getCommentFlags(property);

if (flags?.length) {
nameColumn.push(`(${flags.map((flag) => `*${flag}*`).join(', ')})`);
if (commentFlags?.length) {
nameColumn.push(
`(${commentFlags.map((flag) => `*${flag}*`).join(', ')})`,
);
}

row.push(nameColumn.join(' '));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ export function signature(

md.push(
this.helpers
.getReflectionFlags(model)
.getCommentFlags(model)
.map((item) => backTicks(item))
.join(' '),
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,11 @@ export function signatureTitle(
md.push(backTicks(options?.accessor) + ' ');
}

if (model.parent && model.parent.flags?.length > 0) {
md.push(
model.parent.flags
.map((flag) => backTicks(flag.toLowerCase()))
.join(' ') + ' ',
);
if (model.parent) {
const flagsString = this.helpers.getReflectionFlags(model.parent?.flags);
if (flagsString.length) {
md.push(this.helpers.getReflectionFlags(model.parent.flags) + ' ');
}
}

if (!['__call', '__type'].includes(model.name)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import {
ReferenceType,
Reflection,
ReflectionCategory,
ReflectionFlags,
ReflectionGroup,
ReflectionKind,
ReflectionType,
Expand Down Expand Up @@ -558,6 +559,9 @@ There is no association list partial for properties as these are handled as a st

export const resourceHelpers = (context: MarkdownThemeContext) => {
return {
getCommentFlags: (
reflection: DeclarationReflection | SignatureReflection,
) => helpers.getCommentFlags.apply(context, [reflection]) as string[],
getCommentParts: (model: CommentDisplayPart[]) =>
helpers.getCommentParts.apply(context, [model]) as string,
getDeclarationType: (model: DeclarationReflection) =>
Expand Down Expand Up @@ -605,9 +609,8 @@ export const resourceHelpers = (context: MarkdownThemeContext) => {
]) as string,
getPropertyDefaultValue: (model: DeclarationReflection) =>
helpers.getPropertyDefaultValue.apply(context, [model]) as string | null,
getReflectionFlags: (
reflection: DeclarationReflection | SignatureReflection,
) => helpers.getReflectionFlags.apply(context, [reflection]) as string[],
getReflectionFlags: (reflectionFlags: ReflectionFlags) =>
helpers.getReflectionFlags.apply(context, [reflectionFlags]) as string,
getReturnType: (model?: SomeType | undefined) =>
helpers.getReturnType.apply(context, [model]) as string,
isGroupKind: (model: DeclarationReflection | SignatureReflection) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,20 @@ export interface IndexableInterface {
prop: string;
}

export interface MultipleIndexableInterface {
/**
* First index signature
*/
[key: string]: string;
/**
* Second index signature
*/
[index: number]: string;
/**
* Prop
*/
prop: string;
}
export interface CustomEventInterface<T> {
detail: string;
target: T;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4192,6 +4192,11 @@ exports[`Navigation should gets Navigation Json for single entry point: (Output
"title": "InterfaceWithTypeParameters",
"kind": 256,
"path": "interfaces/InterfaceWithTypeParameters.md"
},
{
"title": "MultipleIndexableInterface",
"kind": 256,
"path": "interfaces/MultipleIndexableInterface.md"
}
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -860,3 +860,43 @@ Comments for InterfaceWithTypeParameters
| \`prop\` | \`A\` | Comments for prop |
"
`;

exports[`Interface Reflection should compile multiple indexable interface: (Output File Strategy "members") (Option Group "1") 1`] = `
"# Interface: MultipleIndexableInterface
## Indexable
\\[\`key\`: \`string\`\\]: \`string\`
\\[\`index\`: \`number\`\\]: \`string\`
## Properties
### prop
> **prop**: \`string\`
Prop
#### Source
[interfaces.ts:1](http://source-url)
"
`;

exports[`Interface Reflection should compile multiple indexable interface: (Output File Strategy "members") (Option Group "2") 1`] = `
"# Interface: MultipleIndexableInterface
## Indexable
\\[\`key\`: \`string\`\\]: \`string\`
\\[\`index\`: \`number\`\\]: \`string\`
## Properties
| Property | Type | Description |
| :------ | :------ | :------ |
| \`prop\` | \`string\` | Prop |
"
`;
Original file line number Diff line number Diff line change
Expand Up @@ -813,6 +813,7 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"interfaces/InterfaceWithEventProperties.md",
"interfaces/InterfaceWithFlags.md",
"interfaces/InterfaceWithTypeParameters.md",
"interfaces/MultipleIndexableInterface.md",
"type-aliases/ArrayOfStuff.md",
"type-aliases/ArrayType.md",
"type-aliases/ConditionalType.md",
Expand Down Expand Up @@ -889,6 +890,7 @@ exports[`Urls should gets Urls for single entry points: outputFileStrategy: memb
"interfaces/InterfaceWithEventProperties.md",
"interfaces/InterfaceWithFlags.md",
"interfaces/InterfaceWithTypeParameters.md",
"interfaces/MultipleIndexableInterface.md",
"type-aliases/ArrayOfStuff.md",
"type-aliases/ArrayType.md",
"type-aliases/ConditionalType.md",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,14 @@ describe(`Interface Reflection`, () => {
);
});

test(`should compile multiple indexable interface`, () => {
expectFileToEqual(
'reflections',
'members',
'interfaces/MultipleIndexableInterface.md',
);
});

test(`should compile interface with event properties`, () => {
expectFileToEqual(
'reflections',
Expand Down

0 comments on commit e06b849

Please sign in to comment.