Skip to content

Commit

Permalink
feat: support @required tag to denote required prop
Browse files Browse the repository at this point in the history
  • Loading branch information
metonym committed May 19, 2022
1 parent f6ed263 commit c0592ae
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 3 deletions.
5 changes: 5 additions & 0 deletions src/ComponentParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ interface ComponentProp {
description?: string;
isFunction: boolean;
isFunctionDeclaration: boolean;
isRequired: boolean;
reactive: boolean;
}

Expand Down Expand Up @@ -385,6 +386,7 @@ export default class ComponentParser {
value,
isFunction,
isFunctionDeclaration,
isRequired: false,
constant: kind === "const",
reactive: false,
});
Expand Down Expand Up @@ -456,6 +458,7 @@ export default class ComponentParser {
let description: undefined | string = undefined;
let isFunction = false;
let isFunctionDeclaration = false;
let isRequired = false;

if (init != null) {
if (
Expand Down Expand Up @@ -511,6 +514,7 @@ export default class ComponentParser {
}

additional_tags.forEach((tag) => {
isRequired = tag.tag === "required";
description += `${description ? "\n" : ""}@${tag.tag} ${tag.name}${
tag.description ? ` ${tag.description}` : ""
}`;
Expand All @@ -529,6 +533,7 @@ export default class ComponentParser {
value,
isFunction,
isFunctionDeclaration,
isRequired,
constant: kind === "const",
reactive: this.reactive_vars.has(prop_name),
});
Expand Down
4 changes: 2 additions & 2 deletions src/writer/writer-markdown.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { ComponentDocs } from "../rollup-plugin";
import WriterMarkdown, { AppendType } from "./WriterMarkdown";
import { formatTsProps, getTypeDefs } from "./writer-ts-definitions";

const PROP_TABLE_HEADER = `| Prop name | Kind | Reactive | Type | Default value | Description |\n| :- | :- | :- | :- |\n`;
const PROP_TABLE_HEADER = `| Prop name | Required | Kind | Reactive | Type | Default value | Description |\n| :- | :- | :- | :- |\n`;
const SLOT_TABLE_HEADER = `| Slot name | Default | Props | Fallback |\n| :- | :- | :- | :- |\n`;
const EVENT_TABLE_HEADER = `| Event name | Type | Detail |\n| :- | :- | :- |\n`;
const MD_TYPE_UNDEFINED = "--";
Expand Down Expand Up @@ -88,7 +88,7 @@ export default async function writeMarkdown(components: ComponentDocs, options:
.forEach((prop) => {
document.append(
"raw",
`| ${prop.name} | ${`<code>${prop.kind}</code>`} | ${prop.reactive ? "Yes" : "No"} | ${formatPropType(
`| ${prop.name} | ${prop.isRequired ? 'Yes' : 'No'} | ${`<code>${prop.kind}</code>`} | ${prop.reactive ? "Yes" : "No"} | ${formatPropType(
prop.type
)} | ${formatPropValue(prop.value)} | ${formatPropDescription(prop.description)} |\n`
);
Expand Down
2 changes: 1 addition & 1 deletion src/writer/writer-ts-definitions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ function genPropDef(def: Pick<ComponentDocApi, "props" | "rest_props" | "moduleN

return `
${prop_comments.length > 0 ? `/**\n${prop_comments}*/` : EMPTY_STR}
${prop.name}?: ${prop_value};`;
${prop.name}${prop.isRequired ? "" : "?"}: ${prop_value};`;
});

if (def.rest_props?.type === "Element") {
Expand Down

0 comments on commit c0592ae

Please sign in to comment.