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