@storybook/react 7.0.0-alpha.4 → 7.0.0-alpha.41
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-WO7CMEGS.mjs +1 -0
- package/dist/config.d.ts +23 -0
- package/dist/config.js +3 -0
- package/dist/config.mjs +3 -0
- package/dist/index.d.ts +185 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/types-7e2d88a6.d.ts +10 -0
- package/package.json +47 -19
- package/preview.js +1 -1
- package/LICENSE +0 -21
- package/dist/cjs/config.js +0 -43
- package/dist/cjs/docs/config.js +0 -25
- package/dist/cjs/docs/extractArgTypes.js +0 -47
- package/dist/cjs/docs/extractProps.js +0 -57
- package/dist/cjs/docs/jsxDecorator.js +0 -210
- package/dist/cjs/docs/lib/captions.js +0 -18
- package/dist/cjs/docs/lib/componentTypes.js +0 -14
- package/dist/cjs/docs/lib/defaultValues/createDefaultValue.js +0 -86
- package/dist/cjs/docs/lib/defaultValues/createFromRawDefaultProp.js +0 -206
- package/dist/cjs/docs/lib/defaultValues/generateArray.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/generateObject.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/index.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/prettyIdentifier.js +0 -38
- package/dist/cjs/docs/lib/generateCode.js +0 -74
- package/dist/cjs/docs/lib/index.js +0 -57
- package/dist/cjs/docs/lib/inspection/acornParser.js +0 -230
- package/dist/cjs/docs/lib/inspection/index.js +0 -31
- package/dist/cjs/docs/lib/inspection/inspectValue.js +0 -24
- package/dist/cjs/docs/lib/inspection/types.js +0 -19
- package/dist/cjs/docs/lib/isHtmlTag.js +0 -14
- package/dist/cjs/docs/propTypes/createType.js +0 -458
- package/dist/cjs/docs/propTypes/generateFuncSignature.js +0 -68
- package/dist/cjs/docs/propTypes/handleProp.js +0 -52
- package/dist/cjs/docs/propTypes/rawDefaultPropResolvers.js +0 -43
- package/dist/cjs/docs/propTypes/sortProps.js +0 -21
- package/dist/cjs/docs/react-argtypes.stories.js +0 -67
- package/dist/cjs/docs/typeScript/handleProp.js +0 -38
- package/dist/cjs/index.js +0 -98
- package/dist/cjs/preview/config.js +0 -19
- package/dist/cjs/preview/globals.js +0 -13
- package/dist/cjs/preview/index.js +0 -44
- package/dist/cjs/preview/render.js +0 -165
- package/dist/cjs/preview/types-6-0.js +0 -5
- package/dist/cjs/preview/types-6-3.js +0 -18
- package/dist/cjs/preview/types-7-0.js +0 -5
- package/dist/cjs/preview/types.js +0 -5
- package/dist/cjs/testing/index.js +0 -113
- package/dist/esm/config.js +0 -6
- package/dist/esm/docs/config.js +0 -12
- package/dist/esm/docs/extractArgTypes.js +0 -37
- package/dist/esm/docs/extractProps.js +0 -42
- package/dist/esm/docs/jsxDecorator.js +0 -170
- package/dist/esm/docs/lib/captions.js +0 -6
- package/dist/esm/docs/lib/componentTypes.js +0 -2
- package/dist/esm/docs/lib/defaultValues/createDefaultValue.js +0 -72
- package/dist/esm/docs/lib/defaultValues/createFromRawDefaultProp.js +0 -183
- package/dist/esm/docs/lib/defaultValues/generateArray.js +0 -21
- package/dist/esm/docs/lib/defaultValues/generateObject.js +0 -21
- package/dist/esm/docs/lib/defaultValues/index.js +0 -2
- package/dist/esm/docs/lib/defaultValues/prettyIdentifier.js +0 -26
- package/dist/esm/docs/lib/generateCode.js +0 -59
- package/dist/esm/docs/lib/index.js +0 -4
- package/dist/esm/docs/lib/inspection/acornParser.js +0 -211
- package/dist/esm/docs/lib/inspection/index.js +0 -2
- package/dist/esm/docs/lib/inspection/inspectValue.js +0 -15
- package/dist/esm/docs/lib/inspection/types.js +0 -12
- package/dist/esm/docs/lib/isHtmlTag.js +0 -4
- package/dist/esm/docs/propTypes/createType.js +0 -447
- package/dist/esm/docs/propTypes/generateFuncSignature.js +0 -57
- package/dist/esm/docs/propTypes/handleProp.js +0 -39
- package/dist/esm/docs/propTypes/rawDefaultPropResolvers.js +0 -31
- package/dist/esm/docs/propTypes/sortProps.js +0 -14
- package/dist/esm/docs/react-argtypes.stories.js +0 -54
- package/dist/esm/docs/typeScript/handleProp.js +0 -28
- package/dist/esm/index.js +0 -5
- package/dist/esm/preview/config.js +0 -1
- package/dist/esm/preview/globals.js +0 -8
- package/dist/esm/preview/index.js +0 -21
- package/dist/esm/preview/render.js +0 -141
- package/dist/esm/preview/types-6-0.js +0 -1
- package/dist/esm/preview/types-6-3.js +0 -10
- package/dist/esm/preview/types-7-0.js +0 -1
- package/dist/esm/preview/types.js +0 -1
- package/dist/esm/testing/index.js +0 -97
- package/dist/types/config.d.ts +0 -10
- package/dist/types/docs/config.d.ts +0 -10
- package/dist/types/docs/extractArgTypes.d.ts +0 -2
- package/dist/types/docs/extractProps.d.ts +0 -9
- package/dist/types/docs/jsxDecorator.d.ts +0 -23
- package/dist/types/docs/lib/captions.d.ts +0 -6
- package/dist/types/docs/lib/componentTypes.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/createDefaultValue.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/createFromRawDefaultProp.d.ts +0 -11
- package/dist/types/docs/lib/defaultValues/generateArray.d.ts +0 -3
- package/dist/types/docs/lib/defaultValues/generateObject.d.ts +0 -3
- package/dist/types/docs/lib/defaultValues/index.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/prettyIdentifier.d.ts +0 -4
- package/dist/types/docs/lib/generateCode.d.ts +0 -3
- package/dist/types/docs/lib/index.d.ts +0 -4
- package/dist/types/docs/lib/inspection/acornParser.d.ts +0 -7
- package/dist/types/docs/lib/inspection/index.d.ts +0 -2
- package/dist/types/docs/lib/inspection/inspectValue.d.ts +0 -2
- package/dist/types/docs/lib/inspection/types.d.ts +0 -50
- package/dist/types/docs/lib/isHtmlTag.d.ts +0 -1
- package/dist/types/docs/propTypes/createType.d.ts +0 -2
- package/dist/types/docs/propTypes/generateFuncSignature.d.ts +0 -4
- package/dist/types/docs/propTypes/handleProp.d.ts +0 -5
- package/dist/types/docs/propTypes/rawDefaultPropResolvers.d.ts +0 -1
- package/dist/types/docs/propTypes/sortProps.d.ts +0 -4
- package/dist/types/docs/react-argtypes.stories.d.ts +0 -1
- package/dist/types/docs/typeScript/handleProp.d.ts +0 -3
- package/dist/types/index.d.ts +0 -4
- package/dist/types/preview/config.d.ts +0 -1
- package/dist/types/preview/globals.d.ts +0 -1
- package/dist/types/preview/index.d.ts +0 -25
- package/dist/types/preview/render.d.ts +0 -5
- package/dist/types/preview/types-6-0.d.ts +0 -35
- package/dist/types/preview/types-6-3.d.ts +0 -40
- package/dist/types/preview/types-7-0.d.ts +0 -21
- package/dist/types/preview/types.d.ts +0 -16
- package/dist/types/testing/index.d.ts +0 -84
|
@@ -1,211 +0,0 @@
|
|
|
1
|
-
import { Parser } from 'acorn';
|
|
2
|
-
import jsx from 'acorn-jsx';
|
|
3
|
-
import * as acornWalk from 'acorn-walk';
|
|
4
|
-
import { InspectionType } from './types';
|
|
5
|
-
const ACORN_WALK_VISITORS = Object.assign({}, acornWalk.base, {
|
|
6
|
-
JSXElement: () => {}
|
|
7
|
-
});
|
|
8
|
-
const acornParser = Parser.extend(jsx()); // Cannot use "estree.Identifier" type because this function also support "JSXIdentifier".
|
|
9
|
-
|
|
10
|
-
function extractIdentifierName(identifierNode) {
|
|
11
|
-
return identifierNode != null ? identifierNode.name : null;
|
|
12
|
-
}
|
|
13
|
-
|
|
14
|
-
function filterAncestors(ancestors) {
|
|
15
|
-
return ancestors.filter(x => x.type === 'ObjectExpression' || x.type === 'ArrayExpression');
|
|
16
|
-
}
|
|
17
|
-
|
|
18
|
-
function calculateNodeDepth(node) {
|
|
19
|
-
const depths = [];
|
|
20
|
-
acornWalk.ancestor( // @ts-ignore
|
|
21
|
-
node, {
|
|
22
|
-
ObjectExpression(_, ancestors) {
|
|
23
|
-
depths.push(filterAncestors(ancestors).length);
|
|
24
|
-
},
|
|
25
|
-
|
|
26
|
-
ArrayExpression(_, ancestors) {
|
|
27
|
-
depths.push(filterAncestors(ancestors).length);
|
|
28
|
-
}
|
|
29
|
-
|
|
30
|
-
}, ACORN_WALK_VISITORS);
|
|
31
|
-
return Math.max(...depths);
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function parseIdentifier(identifierNode) {
|
|
35
|
-
return {
|
|
36
|
-
inferredType: {
|
|
37
|
-
type: InspectionType.IDENTIFIER,
|
|
38
|
-
identifier: extractIdentifierName(identifierNode)
|
|
39
|
-
},
|
|
40
|
-
ast: identifierNode
|
|
41
|
-
};
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function parseLiteral(literalNode) {
|
|
45
|
-
return {
|
|
46
|
-
inferredType: {
|
|
47
|
-
type: InspectionType.LITERAL
|
|
48
|
-
},
|
|
49
|
-
ast: literalNode
|
|
50
|
-
};
|
|
51
|
-
}
|
|
52
|
-
|
|
53
|
-
function parseFunction(funcNode) {
|
|
54
|
-
let innerJsxElementNode; // If there is at least a JSXElement in the body of the function, then it's a React component.
|
|
55
|
-
|
|
56
|
-
acornWalk.simple( // @ts-ignore
|
|
57
|
-
funcNode.body, {
|
|
58
|
-
JSXElement(node) {
|
|
59
|
-
innerJsxElementNode = node;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
}, ACORN_WALK_VISITORS);
|
|
63
|
-
const isJsx = innerJsxElementNode != null;
|
|
64
|
-
const inferredType = {
|
|
65
|
-
type: isJsx ? InspectionType.ELEMENT : InspectionType.FUNCTION,
|
|
66
|
-
params: funcNode.params,
|
|
67
|
-
hasParams: funcNode.params.length !== 0
|
|
68
|
-
};
|
|
69
|
-
const identifierName = extractIdentifierName(funcNode.id);
|
|
70
|
-
|
|
71
|
-
if (identifierName != null) {
|
|
72
|
-
inferredType.identifier = identifierName;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
return {
|
|
76
|
-
inferredType,
|
|
77
|
-
ast: funcNode
|
|
78
|
-
};
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
function parseClass(classNode) {
|
|
82
|
-
let innerJsxElementNode; // If there is at least a JSXElement in the body of the class, then it's a React component.
|
|
83
|
-
|
|
84
|
-
acornWalk.simple( // @ts-ignore
|
|
85
|
-
classNode.body, {
|
|
86
|
-
JSXElement(node) {
|
|
87
|
-
innerJsxElementNode = node;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}, ACORN_WALK_VISITORS);
|
|
91
|
-
const inferredType = {
|
|
92
|
-
type: innerJsxElementNode != null ? InspectionType.ELEMENT : InspectionType.CLASS,
|
|
93
|
-
identifier: extractIdentifierName(classNode.id)
|
|
94
|
-
};
|
|
95
|
-
return {
|
|
96
|
-
inferredType,
|
|
97
|
-
ast: classNode
|
|
98
|
-
};
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
function parseJsxElement(jsxElementNode) {
|
|
102
|
-
const inferredType = {
|
|
103
|
-
type: InspectionType.ELEMENT
|
|
104
|
-
};
|
|
105
|
-
const identifierName = extractIdentifierName(jsxElementNode.openingElement.name);
|
|
106
|
-
|
|
107
|
-
if (identifierName != null) {
|
|
108
|
-
inferredType.identifier = identifierName;
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
return {
|
|
112
|
-
inferredType,
|
|
113
|
-
ast: jsxElementNode
|
|
114
|
-
};
|
|
115
|
-
}
|
|
116
|
-
|
|
117
|
-
function parseCall(callNode) {
|
|
118
|
-
const identifierNode = callNode.callee.type === 'MemberExpression' ? callNode.callee.property : callNode.callee;
|
|
119
|
-
const identifierName = extractIdentifierName(identifierNode);
|
|
120
|
-
|
|
121
|
-
if (identifierName === 'shape') {
|
|
122
|
-
return parseObject(callNode.arguments[0]);
|
|
123
|
-
}
|
|
124
|
-
|
|
125
|
-
return null;
|
|
126
|
-
}
|
|
127
|
-
|
|
128
|
-
function parseObject(objectNode) {
|
|
129
|
-
return {
|
|
130
|
-
inferredType: {
|
|
131
|
-
type: InspectionType.OBJECT,
|
|
132
|
-
depth: calculateNodeDepth(objectNode)
|
|
133
|
-
},
|
|
134
|
-
ast: objectNode
|
|
135
|
-
};
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
function parseArray(arrayNode) {
|
|
139
|
-
return {
|
|
140
|
-
inferredType: {
|
|
141
|
-
type: InspectionType.ARRAY,
|
|
142
|
-
depth: calculateNodeDepth(arrayNode)
|
|
143
|
-
},
|
|
144
|
-
ast: arrayNode
|
|
145
|
-
};
|
|
146
|
-
} // Cannot set "expression" type to "estree.Expression" because the type doesn't include JSX.
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
function parseExpression(expression) {
|
|
150
|
-
switch (expression.type) {
|
|
151
|
-
case 'Identifier':
|
|
152
|
-
return parseIdentifier(expression);
|
|
153
|
-
|
|
154
|
-
case 'Literal':
|
|
155
|
-
return parseLiteral(expression);
|
|
156
|
-
|
|
157
|
-
case 'FunctionExpression':
|
|
158
|
-
case 'ArrowFunctionExpression':
|
|
159
|
-
return parseFunction(expression);
|
|
160
|
-
|
|
161
|
-
case 'ClassExpression':
|
|
162
|
-
return parseClass(expression);
|
|
163
|
-
|
|
164
|
-
case 'JSXElement':
|
|
165
|
-
return parseJsxElement(expression);
|
|
166
|
-
|
|
167
|
-
case 'CallExpression':
|
|
168
|
-
return parseCall(expression);
|
|
169
|
-
|
|
170
|
-
case 'ObjectExpression':
|
|
171
|
-
return parseObject(expression);
|
|
172
|
-
|
|
173
|
-
case 'ArrayExpression':
|
|
174
|
-
return parseArray(expression);
|
|
175
|
-
|
|
176
|
-
default:
|
|
177
|
-
return null;
|
|
178
|
-
}
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
export function parse(value) {
|
|
182
|
-
const ast = acornParser.parse(`(${value})`);
|
|
183
|
-
let parsingResult = {
|
|
184
|
-
inferredType: {
|
|
185
|
-
type: InspectionType.UNKNOWN
|
|
186
|
-
},
|
|
187
|
-
ast
|
|
188
|
-
};
|
|
189
|
-
|
|
190
|
-
if (ast.body[0] != null) {
|
|
191
|
-
const rootNode = ast.body[0];
|
|
192
|
-
|
|
193
|
-
switch (rootNode.type) {
|
|
194
|
-
case 'ExpressionStatement':
|
|
195
|
-
{
|
|
196
|
-
const expressionResult = parseExpression(rootNode.expression);
|
|
197
|
-
|
|
198
|
-
if (expressionResult != null) {
|
|
199
|
-
parsingResult = expressionResult;
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
break;
|
|
203
|
-
}
|
|
204
|
-
|
|
205
|
-
default:
|
|
206
|
-
break;
|
|
207
|
-
}
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
return parsingResult;
|
|
211
|
-
}
|
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { parse } from './acornParser';
|
|
2
|
-
import { InspectionType } from './types';
|
|
3
|
-
export function inspectValue(value) {
|
|
4
|
-
try {
|
|
5
|
-
const parsingResult = parse(value);
|
|
6
|
-
return Object.assign({}, parsingResult);
|
|
7
|
-
} catch (e) {// do nothing.
|
|
8
|
-
}
|
|
9
|
-
|
|
10
|
-
return {
|
|
11
|
-
inferredType: {
|
|
12
|
-
type: InspectionType.UNKNOWN
|
|
13
|
-
}
|
|
14
|
-
};
|
|
15
|
-
}
|
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export let InspectionType;
|
|
2
|
-
|
|
3
|
-
(function (InspectionType) {
|
|
4
|
-
InspectionType["IDENTIFIER"] = "Identifier";
|
|
5
|
-
InspectionType["LITERAL"] = "Literal";
|
|
6
|
-
InspectionType["OBJECT"] = "Object";
|
|
7
|
-
InspectionType["ARRAY"] = "Array";
|
|
8
|
-
InspectionType["FUNCTION"] = "Function";
|
|
9
|
-
InspectionType["CLASS"] = "Class";
|
|
10
|
-
InspectionType["ELEMENT"] = "Element";
|
|
11
|
-
InspectionType["UNKNOWN"] = "Unknown";
|
|
12
|
-
})(InspectionType || (InspectionType = {}));
|
|
@@ -1,447 +0,0 @@
|
|
|
1
|
-
import { createSummaryValue, isTooLongForTypeSummary } from '@storybook/docs-tools';
|
|
2
|
-
import { generateFuncSignature, generateShortFuncSignature, toMultilineSignature } from './generateFuncSignature';
|
|
3
|
-
import { OBJECT_CAPTION, ARRAY_CAPTION, CLASS_CAPTION, FUNCTION_CAPTION, ELEMENT_CAPTION, CUSTOM_CAPTION, isHtmlTag, generateObjectCode, generateCode } from '../lib';
|
|
4
|
-
import { InspectionType, inspectValue } from '../lib/inspection';
|
|
5
|
-
const MAX_FUNC_LENGTH = 150;
|
|
6
|
-
var PropTypesType;
|
|
7
|
-
|
|
8
|
-
(function (PropTypesType) {
|
|
9
|
-
PropTypesType["CUSTOM"] = "custom";
|
|
10
|
-
PropTypesType["ANY"] = "any";
|
|
11
|
-
PropTypesType["FUNC"] = "func";
|
|
12
|
-
PropTypesType["SHAPE"] = "shape";
|
|
13
|
-
PropTypesType["OBJECT"] = "object";
|
|
14
|
-
PropTypesType["INSTANCEOF"] = "instanceOf";
|
|
15
|
-
PropTypesType["OBJECTOF"] = "objectOf";
|
|
16
|
-
PropTypesType["UNION"] = "union";
|
|
17
|
-
PropTypesType["ENUM"] = "enum";
|
|
18
|
-
PropTypesType["ARRAYOF"] = "arrayOf";
|
|
19
|
-
PropTypesType["ELEMENT"] = "element";
|
|
20
|
-
PropTypesType["ELEMENTTYPE"] = "elementType";
|
|
21
|
-
PropTypesType["NODE"] = "node";
|
|
22
|
-
})(PropTypesType || (PropTypesType = {}));
|
|
23
|
-
|
|
24
|
-
function createTypeDef({
|
|
25
|
-
name,
|
|
26
|
-
short,
|
|
27
|
-
compact,
|
|
28
|
-
full,
|
|
29
|
-
inferredType
|
|
30
|
-
}) {
|
|
31
|
-
return {
|
|
32
|
-
name,
|
|
33
|
-
short,
|
|
34
|
-
compact,
|
|
35
|
-
full: full != null ? full : short,
|
|
36
|
-
inferredType
|
|
37
|
-
};
|
|
38
|
-
}
|
|
39
|
-
|
|
40
|
-
function cleanPropTypes(value) {
|
|
41
|
-
return value.replace(/PropTypes./g, '').replace(/.isRequired/g, '');
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
function splitIntoLines(value) {
|
|
45
|
-
return value.split(/\r?\n/);
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
function prettyObject(ast, compact = false) {
|
|
49
|
-
return cleanPropTypes(generateObjectCode(ast, compact));
|
|
50
|
-
}
|
|
51
|
-
|
|
52
|
-
function prettyArray(ast, compact = false) {
|
|
53
|
-
return cleanPropTypes(generateCode(ast, compact));
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
function getCaptionForInspectionType(type) {
|
|
57
|
-
switch (type) {
|
|
58
|
-
case InspectionType.OBJECT:
|
|
59
|
-
return OBJECT_CAPTION;
|
|
60
|
-
|
|
61
|
-
case InspectionType.ARRAY:
|
|
62
|
-
return ARRAY_CAPTION;
|
|
63
|
-
|
|
64
|
-
case InspectionType.CLASS:
|
|
65
|
-
return CLASS_CAPTION;
|
|
66
|
-
|
|
67
|
-
case InspectionType.FUNCTION:
|
|
68
|
-
return FUNCTION_CAPTION;
|
|
69
|
-
|
|
70
|
-
case InspectionType.ELEMENT:
|
|
71
|
-
return ELEMENT_CAPTION;
|
|
72
|
-
|
|
73
|
-
default:
|
|
74
|
-
return CUSTOM_CAPTION;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
|
|
78
|
-
function generateTypeFromString(value, originalTypeName) {
|
|
79
|
-
const {
|
|
80
|
-
inferredType,
|
|
81
|
-
ast
|
|
82
|
-
} = inspectValue(value);
|
|
83
|
-
const {
|
|
84
|
-
type
|
|
85
|
-
} = inferredType;
|
|
86
|
-
let short;
|
|
87
|
-
let compact;
|
|
88
|
-
let full;
|
|
89
|
-
|
|
90
|
-
switch (type) {
|
|
91
|
-
case InspectionType.IDENTIFIER:
|
|
92
|
-
case InspectionType.LITERAL:
|
|
93
|
-
short = value;
|
|
94
|
-
compact = value;
|
|
95
|
-
break;
|
|
96
|
-
|
|
97
|
-
case InspectionType.OBJECT:
|
|
98
|
-
{
|
|
99
|
-
const {
|
|
100
|
-
depth
|
|
101
|
-
} = inferredType;
|
|
102
|
-
short = OBJECT_CAPTION;
|
|
103
|
-
compact = depth === 1 ? prettyObject(ast, true) : null;
|
|
104
|
-
full = prettyObject(ast);
|
|
105
|
-
break;
|
|
106
|
-
}
|
|
107
|
-
|
|
108
|
-
case InspectionType.ELEMENT:
|
|
109
|
-
{
|
|
110
|
-
const {
|
|
111
|
-
identifier
|
|
112
|
-
} = inferredType;
|
|
113
|
-
short = identifier != null && !isHtmlTag(identifier) ? identifier : ELEMENT_CAPTION;
|
|
114
|
-
compact = splitIntoLines(value).length === 1 ? value : null;
|
|
115
|
-
full = value;
|
|
116
|
-
break;
|
|
117
|
-
}
|
|
118
|
-
|
|
119
|
-
case InspectionType.ARRAY:
|
|
120
|
-
{
|
|
121
|
-
const {
|
|
122
|
-
depth
|
|
123
|
-
} = inferredType;
|
|
124
|
-
short = ARRAY_CAPTION;
|
|
125
|
-
compact = depth <= 2 ? prettyArray(ast, true) : null;
|
|
126
|
-
full = prettyArray(ast);
|
|
127
|
-
break;
|
|
128
|
-
}
|
|
129
|
-
|
|
130
|
-
default:
|
|
131
|
-
short = getCaptionForInspectionType(type);
|
|
132
|
-
compact = splitIntoLines(value).length === 1 ? value : null;
|
|
133
|
-
full = value;
|
|
134
|
-
break;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return createTypeDef({
|
|
138
|
-
name: originalTypeName,
|
|
139
|
-
short,
|
|
140
|
-
compact,
|
|
141
|
-
full,
|
|
142
|
-
inferredType: type
|
|
143
|
-
});
|
|
144
|
-
}
|
|
145
|
-
|
|
146
|
-
function generateCustom({
|
|
147
|
-
raw
|
|
148
|
-
}) {
|
|
149
|
-
if (raw != null) {
|
|
150
|
-
return generateTypeFromString(raw, PropTypesType.CUSTOM);
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
return createTypeDef({
|
|
154
|
-
name: PropTypesType.CUSTOM,
|
|
155
|
-
short: CUSTOM_CAPTION,
|
|
156
|
-
compact: CUSTOM_CAPTION
|
|
157
|
-
});
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
function generateFunc(extractedProp) {
|
|
161
|
-
const {
|
|
162
|
-
jsDocTags
|
|
163
|
-
} = extractedProp;
|
|
164
|
-
|
|
165
|
-
if (jsDocTags != null) {
|
|
166
|
-
if (jsDocTags.params != null || jsDocTags.returns != null) {
|
|
167
|
-
return createTypeDef({
|
|
168
|
-
name: PropTypesType.FUNC,
|
|
169
|
-
// @ts-ignore
|
|
170
|
-
short: generateShortFuncSignature(jsDocTags.params, jsDocTags.returns),
|
|
171
|
-
compact: null,
|
|
172
|
-
// @ts-ignore
|
|
173
|
-
full: generateFuncSignature(jsDocTags.params, jsDocTags.returns)
|
|
174
|
-
});
|
|
175
|
-
}
|
|
176
|
-
}
|
|
177
|
-
|
|
178
|
-
return createTypeDef({
|
|
179
|
-
name: PropTypesType.FUNC,
|
|
180
|
-
short: FUNCTION_CAPTION,
|
|
181
|
-
compact: FUNCTION_CAPTION
|
|
182
|
-
});
|
|
183
|
-
}
|
|
184
|
-
|
|
185
|
-
function generateShape(type, extractedProp) {
|
|
186
|
-
const fields = Object.keys(type.value).map(key => `${key}: ${generateType(type.value[key], extractedProp).full}`).join(', ');
|
|
187
|
-
const {
|
|
188
|
-
inferredType,
|
|
189
|
-
ast
|
|
190
|
-
} = inspectValue(`{ ${fields} }`);
|
|
191
|
-
const {
|
|
192
|
-
depth
|
|
193
|
-
} = inferredType;
|
|
194
|
-
return createTypeDef({
|
|
195
|
-
name: PropTypesType.SHAPE,
|
|
196
|
-
short: OBJECT_CAPTION,
|
|
197
|
-
compact: depth === 1 && ast ? prettyObject(ast, true) : null,
|
|
198
|
-
full: ast ? prettyObject(ast) : null
|
|
199
|
-
});
|
|
200
|
-
}
|
|
201
|
-
|
|
202
|
-
function objectOf(of) {
|
|
203
|
-
return `objectOf(${of})`;
|
|
204
|
-
}
|
|
205
|
-
|
|
206
|
-
function generateObjectOf(type, extractedProp) {
|
|
207
|
-
const {
|
|
208
|
-
short,
|
|
209
|
-
compact,
|
|
210
|
-
full
|
|
211
|
-
} = generateType(type.value, extractedProp);
|
|
212
|
-
return createTypeDef({
|
|
213
|
-
name: PropTypesType.OBJECTOF,
|
|
214
|
-
short: objectOf(short),
|
|
215
|
-
compact: compact != null ? objectOf(compact) : null,
|
|
216
|
-
full: full ? objectOf(full) : full
|
|
217
|
-
});
|
|
218
|
-
}
|
|
219
|
-
|
|
220
|
-
function generateUnion(type, extractedProp) {
|
|
221
|
-
if (Array.isArray(type.value)) {
|
|
222
|
-
const values = type.value.reduce((acc, v) => {
|
|
223
|
-
const {
|
|
224
|
-
short,
|
|
225
|
-
compact,
|
|
226
|
-
full
|
|
227
|
-
} = generateType(v, extractedProp);
|
|
228
|
-
acc.short.push(short);
|
|
229
|
-
acc.compact.push(compact);
|
|
230
|
-
acc.full.push(full);
|
|
231
|
-
return acc;
|
|
232
|
-
}, {
|
|
233
|
-
short: [],
|
|
234
|
-
compact: [],
|
|
235
|
-
full: []
|
|
236
|
-
});
|
|
237
|
-
return createTypeDef({
|
|
238
|
-
name: PropTypesType.UNION,
|
|
239
|
-
short: values.short.join(' | '),
|
|
240
|
-
compact: values.compact.every(x => x != null) ? values.compact.join(' | ') : null,
|
|
241
|
-
full: values.full.join(' | ')
|
|
242
|
-
});
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
return createTypeDef({
|
|
246
|
-
name: PropTypesType.UNION,
|
|
247
|
-
short: type.value,
|
|
248
|
-
compact: null
|
|
249
|
-
});
|
|
250
|
-
}
|
|
251
|
-
|
|
252
|
-
function generateEnumValue({
|
|
253
|
-
value,
|
|
254
|
-
computed
|
|
255
|
-
}) {
|
|
256
|
-
return computed ? generateTypeFromString(value, 'enumvalue') : createTypeDef({
|
|
257
|
-
name: 'enumvalue',
|
|
258
|
-
short: value,
|
|
259
|
-
compact: value
|
|
260
|
-
});
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
function generateEnum(type) {
|
|
264
|
-
if (Array.isArray(type.value)) {
|
|
265
|
-
const values = type.value.reduce((acc, v) => {
|
|
266
|
-
const {
|
|
267
|
-
short,
|
|
268
|
-
compact,
|
|
269
|
-
full
|
|
270
|
-
} = generateEnumValue(v);
|
|
271
|
-
acc.short.push(short);
|
|
272
|
-
acc.compact.push(compact);
|
|
273
|
-
acc.full.push(full);
|
|
274
|
-
return acc;
|
|
275
|
-
}, {
|
|
276
|
-
short: [],
|
|
277
|
-
compact: [],
|
|
278
|
-
full: []
|
|
279
|
-
});
|
|
280
|
-
return createTypeDef({
|
|
281
|
-
name: PropTypesType.ENUM,
|
|
282
|
-
short: values.short.join(' | '),
|
|
283
|
-
compact: values.compact.every(x => x != null) ? values.compact.join(' | ') : null,
|
|
284
|
-
full: values.full.join(' | ')
|
|
285
|
-
});
|
|
286
|
-
}
|
|
287
|
-
|
|
288
|
-
return createTypeDef({
|
|
289
|
-
name: PropTypesType.ENUM,
|
|
290
|
-
short: type.value,
|
|
291
|
-
compact: type.value
|
|
292
|
-
});
|
|
293
|
-
}
|
|
294
|
-
|
|
295
|
-
function braceAfter(of) {
|
|
296
|
-
return `${of}[]`;
|
|
297
|
-
}
|
|
298
|
-
|
|
299
|
-
function braceAround(of) {
|
|
300
|
-
return `[${of}]`;
|
|
301
|
-
}
|
|
302
|
-
|
|
303
|
-
function createArrayOfObjectTypeDef(short, compact, full) {
|
|
304
|
-
return createTypeDef({
|
|
305
|
-
name: PropTypesType.ARRAYOF,
|
|
306
|
-
short: braceAfter(short),
|
|
307
|
-
compact: compact != null ? braceAround(compact) : null,
|
|
308
|
-
full: full ? braceAround(full) : full
|
|
309
|
-
});
|
|
310
|
-
}
|
|
311
|
-
|
|
312
|
-
function generateArray(type, extractedProp) {
|
|
313
|
-
const {
|
|
314
|
-
name,
|
|
315
|
-
short,
|
|
316
|
-
compact,
|
|
317
|
-
full,
|
|
318
|
-
inferredType
|
|
319
|
-
} = generateType(type.value, extractedProp);
|
|
320
|
-
|
|
321
|
-
if (name === PropTypesType.CUSTOM) {
|
|
322
|
-
if (inferredType === InspectionType.OBJECT) {
|
|
323
|
-
return createArrayOfObjectTypeDef(short, compact, full);
|
|
324
|
-
}
|
|
325
|
-
} else if (name === PropTypesType.SHAPE) {
|
|
326
|
-
return createArrayOfObjectTypeDef(short, compact, full);
|
|
327
|
-
}
|
|
328
|
-
|
|
329
|
-
return createTypeDef({
|
|
330
|
-
name: PropTypesType.ARRAYOF,
|
|
331
|
-
short: braceAfter(short),
|
|
332
|
-
compact: braceAfter(short)
|
|
333
|
-
});
|
|
334
|
-
}
|
|
335
|
-
|
|
336
|
-
function generateType(type, extractedProp) {
|
|
337
|
-
try {
|
|
338
|
-
switch (type.name) {
|
|
339
|
-
case PropTypesType.CUSTOM:
|
|
340
|
-
return generateCustom(type);
|
|
341
|
-
|
|
342
|
-
case PropTypesType.FUNC:
|
|
343
|
-
return generateFunc(extractedProp);
|
|
344
|
-
|
|
345
|
-
case PropTypesType.SHAPE:
|
|
346
|
-
return generateShape(type, extractedProp);
|
|
347
|
-
|
|
348
|
-
case PropTypesType.INSTANCEOF:
|
|
349
|
-
return createTypeDef({
|
|
350
|
-
name: PropTypesType.INSTANCEOF,
|
|
351
|
-
short: type.value,
|
|
352
|
-
compact: type.value
|
|
353
|
-
});
|
|
354
|
-
|
|
355
|
-
case PropTypesType.OBJECTOF:
|
|
356
|
-
return generateObjectOf(type, extractedProp);
|
|
357
|
-
|
|
358
|
-
case PropTypesType.UNION:
|
|
359
|
-
return generateUnion(type, extractedProp);
|
|
360
|
-
|
|
361
|
-
case PropTypesType.ENUM:
|
|
362
|
-
return generateEnum(type);
|
|
363
|
-
|
|
364
|
-
case PropTypesType.ARRAYOF:
|
|
365
|
-
return generateArray(type, extractedProp);
|
|
366
|
-
|
|
367
|
-
default:
|
|
368
|
-
return createTypeDef({
|
|
369
|
-
name: type.name,
|
|
370
|
-
short: type.name,
|
|
371
|
-
compact: type.name
|
|
372
|
-
});
|
|
373
|
-
}
|
|
374
|
-
} catch (e) {
|
|
375
|
-
// eslint-disable-next-line no-console
|
|
376
|
-
console.error(e);
|
|
377
|
-
}
|
|
378
|
-
|
|
379
|
-
return createTypeDef({
|
|
380
|
-
name: 'unknown',
|
|
381
|
-
short: 'unknown',
|
|
382
|
-
compact: 'unknown'
|
|
383
|
-
});
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
export function createType(extractedProp) {
|
|
387
|
-
const {
|
|
388
|
-
type
|
|
389
|
-
} = extractedProp.docgenInfo; // A type could be null if a defaultProp has been provided without a type definition.
|
|
390
|
-
|
|
391
|
-
if (type == null) {
|
|
392
|
-
return null;
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
try {
|
|
396
|
-
switch (type.name) {
|
|
397
|
-
case PropTypesType.CUSTOM:
|
|
398
|
-
case PropTypesType.SHAPE:
|
|
399
|
-
case PropTypesType.INSTANCEOF:
|
|
400
|
-
case PropTypesType.OBJECTOF:
|
|
401
|
-
case PropTypesType.UNION:
|
|
402
|
-
case PropTypesType.ENUM:
|
|
403
|
-
case PropTypesType.ARRAYOF:
|
|
404
|
-
{
|
|
405
|
-
const {
|
|
406
|
-
short,
|
|
407
|
-
compact,
|
|
408
|
-
full
|
|
409
|
-
} = generateType(type, extractedProp);
|
|
410
|
-
|
|
411
|
-
if (compact != null) {
|
|
412
|
-
if (!isTooLongForTypeSummary(compact)) {
|
|
413
|
-
return createSummaryValue(compact);
|
|
414
|
-
}
|
|
415
|
-
}
|
|
416
|
-
|
|
417
|
-
return full ? createSummaryValue(short, full) : createSummaryValue(short);
|
|
418
|
-
}
|
|
419
|
-
|
|
420
|
-
case PropTypesType.FUNC:
|
|
421
|
-
{
|
|
422
|
-
const {
|
|
423
|
-
short,
|
|
424
|
-
full
|
|
425
|
-
} = generateType(type, extractedProp);
|
|
426
|
-
let summary = short;
|
|
427
|
-
let detail;
|
|
428
|
-
|
|
429
|
-
if (full && full.length < MAX_FUNC_LENGTH) {
|
|
430
|
-
summary = full;
|
|
431
|
-
} else if (full) {
|
|
432
|
-
detail = toMultilineSignature(full);
|
|
433
|
-
}
|
|
434
|
-
|
|
435
|
-
return createSummaryValue(summary, detail);
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
default:
|
|
439
|
-
return null;
|
|
440
|
-
}
|
|
441
|
-
} catch (e) {
|
|
442
|
-
// eslint-disable-next-line no-console
|
|
443
|
-
console.error(e);
|
|
444
|
-
}
|
|
445
|
-
|
|
446
|
-
return null;
|
|
447
|
-
}
|