forked from mui/material-ui
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateDescribableProp.ts
More file actions
61 lines (53 loc) · 1.5 KB
/
createDescribableProp.ts
File metadata and controls
61 lines (53 loc) · 1.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import * as doctrine from 'doctrine';
import { PropItem, PropItemType } from 'react-docgen-typescript';
export interface DescribablePropDescriptor {
annotation: doctrine.Annotation;
defaultValue: string | null;
required: boolean;
type: PropItemType;
tags: PropItem['tags'];
}
export type CreateDescribablePropSettings = {
/**
* Names of props that do not check if the annotations equal runtime default.
*/
propsWithoutDefaultVerification?: string[];
};
/**
* Returns `null` if the prop should be ignored.
* Throws if it is invalid.
* @param prop
* @param propName
*/
export default function createDescribableProp(
prop: PropItem,
propName: string,
): DescribablePropDescriptor | null {
const { defaultValue, description, required, type, tags } = prop;
const renderedDefaultValue = defaultValue?.value.replace(/\r?\n/g, '');
const renderDefaultValue = Boolean(
renderedDefaultValue &&
// Ignore "large" default values that would break the table layout.
renderedDefaultValue.length <= 150,
);
if (description === undefined) {
throw new Error(`The "${propName}" prop is missing a description.`);
}
const annotation = doctrine.parse(description, {
sloppy: true,
});
if (
description.trim() === '' ||
// @ts-expect-error empty object type
tags?.ignore
) {
return null;
}
return {
annotation,
defaultValue: renderDefaultValue ? renderedDefaultValue! : null,
required: Boolean(required),
type,
tags: prop.tags,
};
}