Skip to content

Commit

Permalink
new: alpha beta experimental modifiers (#53)
Browse files Browse the repository at this point in the history
* new: alpha, beta, and experimental modifiers

This adds badges to entities that are marked with stabilitity modifiers. I'm open to
suggestions on better ways of rendering this information.

* move labels around
  • Loading branch information
chadhietala committed Jul 18, 2022
1 parent 922f406 commit f32fa95
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 9 deletions.
46 changes: 43 additions & 3 deletions fixtures/monorepo/standard/src/index.ts
@@ -1,10 +1,50 @@
/**
* a type
* @beta
*/
export type Type = 'standard';

/**
* newy new guy
* @param a a thing
* @param b b thing
* @alpha
*/
export function bizz(a: string, b: string): string;

/**
* newy new guy
* @param a a thing
* @param b b thing
* @param c c thing
* @beta
*/
export function bizz(a: string, b: string, c: string): string;

/**
* a thing for a thing
* @param a id
* @returns returns the param
* @beta
*/
export function bizz(...args: string[]): string {
return args[0];
}

/**
* thing for a thing
* @beta
*/
export function bizz(a: string): string {
return a;
}
export interface Foo {
/**
* very experimental
* @alpha
*/
foo: string;

/**
* very experimental
* @experimental
*/
a: string;
}
40 changes: 40 additions & 0 deletions packages/plugin/src/components/CommentBadges.tsx
@@ -0,0 +1,40 @@
import React from 'react';
import { JSONOutput } from 'typedoc';

function getModifierClassName(tag: string) {
switch (tag) {
case '@beta':
case '@experimental':
return 'warning';
case '@alpha':
return 'danger';
default:
return 'info';
}
}

export type CommentWithModifiers = Pick<JSONOutput.Comment, 'blockTags' | 'summary'> &
Required<Pick<JSONOutput.Comment, 'modifierTags'>>;

export function isCommentWithModifiers(
comment?: JSONOutput.Comment,
): comment is CommentWithModifiers {
return !!comment && !!comment.modifierTags && comment.modifierTags.length > 0;
}

interface CommentBadgesProps {
comment: CommentWithModifiers;
}

export function CommentBadges({ comment }: CommentBadgesProps) {
const { modifierTags } = comment;
return (
<div className="badge-group">
{modifierTags.map((tag) => (
<span key={tag} className={`badge badge--${getModifierClassName(tag)}`}>
{tag.slice(1)}
</span>
))}
</div>
);
}
4 changes: 3 additions & 1 deletion packages/plugin/src/components/Member.tsx
Expand Up @@ -6,6 +6,7 @@ import { useReflection } from '../hooks/useReflection';
import { useReflectionMap } from '../hooks/useReflectionMap';
import { hasOwnDocument } from '../utils/visibility';
import { AnchorLink } from './AnchorLink';
import { CommentBadges, isCommentWithModifiers } from './CommentBadges';
import { Flags } from './Flags';
import { MemberDeclaration } from './MemberDeclaration';
import { MemberGetterSetter } from './MemberGetterSetter';
Expand All @@ -20,7 +21,7 @@ export interface MemberProps {
export function Member({ id }: MemberProps) {
const reflections = useReflectionMap();
const reflection = useReflection(id)!;

const { comment } = reflection;
let content: React.ReactNode = null;

if (reflection.signatures) {
Expand All @@ -46,6 +47,7 @@ export function Member({ id }: MemberProps) {
<SourceLink sources={reflection.sources} />
<Flags flags={reflection.flags} />
{reflection.name}
{isCommentWithModifiers(comment) && <CommentBadges comment={comment} />}
</h3>

{content}
Expand Down
11 changes: 7 additions & 4 deletions packages/plugin/src/components/MemberSignatureBody.tsx
Expand Up @@ -4,6 +4,7 @@ import React from 'react';
import type { JSONOutput } from 'typedoc';
import { useMinimalLayout } from '../hooks/useMinimalLayout';
import { Comment, hasComment } from './Comment';
import { CommentBadges, isCommentWithModifiers } from './CommentBadges';
import { DefaultValue } from './DefaultValue';
import { Flags } from './Flags';
import { hasSources, MemberSources } from './MemberSources';
Expand Down Expand Up @@ -34,11 +35,11 @@ export interface MemberSignatureBodyProps {
sig: JSONOutput.SignatureReflection;
}

function excludeBlockTags(comment?: JSONOutput.Comment): JSONOutput.Comment | undefined {
function excludeBlockTags(comment?: JSONOutput.Comment): JSONOutput.Comment | undefined {
if (comment) {
const { blockTags, ...rest } = comment;
return rest;
}
}
return undefined;
}

Expand All @@ -50,8 +51,8 @@ function intoReturnComment(comment?: JSONOutput.Comment): JSONOutput.Comment | u
const index = tags.indexOf('@returns');

return {
summary: comment.blockTags[index].content
}
summary: comment.blockTags[index].content,
};
}
}

Expand All @@ -69,6 +70,8 @@ export function MemberSignatureBody({ hideSources, sig }: MemberSignatureBodyPro
<>
{!hideSources && <MemberSources reflection={sig} />}

{isCommentWithModifiers(sig.comment) && <CommentBadges comment={sig.comment} />}

<Comment comment={excludeBlockTags(sig.comment)} />

{hasComment(sig.comment) && (showTypes || showParams || showReturn) && (
Expand Down
3 changes: 2 additions & 1 deletion packages/plugin/src/components/Reflection.tsx
Expand Up @@ -4,6 +4,7 @@ import React, { useMemo } from 'react';
import type { JSONOutput } from 'typedoc';
import { createHierarchy } from '../utils/hierarchy';
import { Comment, hasComment } from './Comment';
import { CommentBadges, isCommentWithModifiers } from './CommentBadges';
import { Hierarchy } from './Hierarchy';
import { Icon } from './Icon';
import { Index } from './Index';
Expand All @@ -19,13 +20,13 @@ export interface ReflectionProps {
| JSONOutput.Reflection
| JSONOutput.SignatureReflection;
}

// eslint-disable-next-line complexity
export function Reflection({ reflection }: ReflectionProps) {
const hierarchy = useMemo(() => createHierarchy(reflection), [reflection]);

return (
<>
{isCommentWithModifiers(reflection.comment) && <CommentBadges comment={reflection.comment} />}
{hasComment(reflection.comment) && <Comment root comment={reflection.comment} />}

{'typeParameter' in reflection &&
Expand Down
17 changes: 17 additions & 0 deletions packages/plugin/src/components/styles.css
Expand Up @@ -204,6 +204,23 @@ html[data-theme='light'] .tsd-panel-content {
vertical-align: middle;
}

/* MODIFIERS */

.badge-group {
font-size: var(--tsd-font-small);
vertical-align: middle;
}

.tsd-panel-header .badge-group {
display: inline-block;
margin-left: .25em;
}

.badge-group > span {
margin-right: 0.25rem;
}


/* SYNTAX */

.tsd-generics {
Expand Down

0 comments on commit f32fa95

Please sign in to comment.