@storybook/react 6.5.0-alpha.9 → 6.5.0-beta.0

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