@storybook/react 6.5.0-alpha.45 → 6.5.0-alpha.48

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 (146) hide show
  1. package/dist/cjs/client/docs/config.js +29 -0
  2. package/dist/cjs/client/docs/extractArgTypes.js +54 -0
  3. package/dist/cjs/client/docs/extractProps.js +77 -0
  4. package/dist/cjs/client/docs/jsxDecorator.js +277 -0
  5. package/dist/cjs/client/docs/lib/captions.js +18 -0
  6. package/dist/cjs/client/docs/lib/componentTypes.js +24 -0
  7. package/dist/cjs/client/docs/lib/defaultValues/createDefaultValue.js +81 -0
  8. package/dist/cjs/client/docs/lib/defaultValues/createFromRawDefaultProp.js +225 -0
  9. package/dist/cjs/client/docs/lib/defaultValues/generateArray.js +29 -0
  10. package/dist/cjs/client/docs/lib/defaultValues/generateObject.js +29 -0
  11. package/dist/cjs/client/docs/lib/defaultValues/index.js +37 -0
  12. package/dist/cjs/client/docs/lib/defaultValues/prettyIdentifier.js +34 -0
  13. package/dist/cjs/client/docs/lib/generateCode.js +89 -0
  14. package/dist/cjs/client/docs/lib/index.js +63 -0
  15. package/dist/cjs/client/docs/lib/inspection/acornParser.js +254 -0
  16. package/dist/cjs/client/docs/lib/inspection/index.js +37 -0
  17. package/dist/cjs/client/docs/lib/inspection/inspectValue.js +26 -0
  18. package/dist/cjs/client/docs/lib/inspection/types.js +19 -0
  19. package/dist/cjs/client/docs/lib/isHtmlTag.js +18 -0
  20. package/dist/cjs/client/docs/propTypes/createType.js +469 -0
  21. package/dist/cjs/client/docs/propTypes/generateFuncSignature.js +78 -0
  22. package/dist/cjs/client/docs/propTypes/handleProp.js +54 -0
  23. package/dist/cjs/client/docs/propTypes/rawDefaultPropResolvers.js +47 -0
  24. package/dist/cjs/client/docs/propTypes/sortProps.js +37 -0
  25. package/dist/cjs/client/docs/react-argtypes.stories.js +129 -0
  26. package/dist/cjs/client/docs/typeScript/handleProp.js +38 -0
  27. package/dist/cjs/server/{framework-preset-react-docgen.js → framework-preset-react-docs.js} +19 -9
  28. package/dist/cjs/server/preset.js +1 -1
  29. package/dist/esm/client/docs/config.js +16 -0
  30. package/dist/esm/client/docs/extractArgTypes.js +39 -0
  31. package/dist/esm/client/docs/extractProps.js +54 -0
  32. package/dist/esm/client/docs/jsxDecorator.js +218 -0
  33. package/dist/esm/client/docs/lib/captions.js +6 -0
  34. package/dist/esm/client/docs/lib/componentTypes.js +9 -0
  35. package/dist/esm/client/docs/lib/defaultValues/createDefaultValue.js +67 -0
  36. package/dist/esm/client/docs/lib/defaultValues/createFromRawDefaultProp.js +191 -0
  37. package/dist/esm/client/docs/lib/defaultValues/generateArray.js +19 -0
  38. package/dist/esm/client/docs/lib/defaultValues/generateObject.js +19 -0
  39. package/dist/esm/client/docs/lib/defaultValues/index.js +2 -0
  40. package/dist/esm/client/docs/lib/defaultValues/prettyIdentifier.js +22 -0
  41. package/dist/esm/client/docs/lib/generateCode.js +68 -0
  42. package/dist/esm/client/docs/lib/index.js +4 -0
  43. package/dist/esm/client/docs/lib/inspection/acornParser.js +213 -0
  44. package/dist/esm/client/docs/lib/inspection/index.js +2 -0
  45. package/dist/esm/client/docs/lib/inspection/inspectValue.js +16 -0
  46. package/dist/esm/client/docs/lib/inspection/types.js +12 -0
  47. package/dist/esm/client/docs/lib/isHtmlTag.js +6 -0
  48. package/dist/esm/client/docs/propTypes/createType.js +449 -0
  49. package/dist/esm/client/docs/propTypes/generateFuncSignature.js +62 -0
  50. package/dist/esm/client/docs/propTypes/handleProp.js +39 -0
  51. package/dist/esm/client/docs/propTypes/rawDefaultPropResolvers.js +32 -0
  52. package/dist/esm/client/docs/propTypes/sortProps.js +24 -0
  53. package/dist/esm/client/docs/react-argtypes.stories.js +97 -0
  54. package/dist/esm/client/docs/typeScript/handleProp.js +27 -0
  55. package/dist/esm/server/{framework-preset-react-docgen.js → framework-preset-react-docs.js} +13 -9
  56. package/dist/esm/server/preset.js +1 -1
  57. package/dist/modern/client/docs/config.js +14 -0
  58. package/dist/modern/client/docs/extractArgTypes.js +38 -0
  59. package/dist/modern/client/docs/extractProps.js +42 -0
  60. package/dist/modern/client/docs/jsxDecorator.js +177 -0
  61. package/dist/modern/client/docs/lib/captions.js +6 -0
  62. package/dist/modern/client/docs/lib/componentTypes.js +2 -0
  63. package/dist/modern/client/docs/lib/defaultValues/createDefaultValue.js +72 -0
  64. package/dist/modern/client/docs/lib/defaultValues/createFromRawDefaultProp.js +181 -0
  65. package/dist/modern/client/docs/lib/defaultValues/generateArray.js +21 -0
  66. package/dist/modern/client/docs/lib/defaultValues/generateObject.js +21 -0
  67. package/dist/modern/client/docs/lib/defaultValues/index.js +2 -0
  68. package/dist/modern/client/docs/lib/defaultValues/prettyIdentifier.js +24 -0
  69. package/dist/modern/client/docs/lib/generateCode.js +59 -0
  70. package/dist/modern/client/docs/lib/index.js +4 -0
  71. package/dist/modern/client/docs/lib/inspection/acornParser.js +211 -0
  72. package/dist/modern/client/docs/lib/inspection/index.js +2 -0
  73. package/dist/modern/client/docs/lib/inspection/inspectValue.js +15 -0
  74. package/dist/modern/client/docs/lib/inspection/types.js +12 -0
  75. package/dist/modern/client/docs/lib/isHtmlTag.js +4 -0
  76. package/dist/modern/client/docs/propTypes/createType.js +446 -0
  77. package/dist/modern/client/docs/propTypes/generateFuncSignature.js +57 -0
  78. package/dist/modern/client/docs/propTypes/handleProp.js +39 -0
  79. package/dist/modern/client/docs/propTypes/rawDefaultPropResolvers.js +31 -0
  80. package/dist/modern/client/docs/propTypes/sortProps.js +14 -0
  81. package/dist/modern/client/docs/react-argtypes.stories.js +54 -0
  82. package/dist/modern/client/docs/typeScript/handleProp.js +28 -0
  83. package/dist/modern/server/{framework-preset-react-docgen.js → framework-preset-react-docs.js} +13 -9
  84. package/dist/modern/server/preset.js +1 -1
  85. package/dist/ts3.4/client/docs/config.d.ts +13 -0
  86. package/dist/ts3.4/client/docs/extractArgTypes.d.ts +2 -0
  87. package/dist/ts3.4/client/docs/extractProps.d.ts +5 -0
  88. package/dist/ts3.4/client/docs/jsxDecorator.d.ts +23 -0
  89. package/dist/ts3.4/client/docs/lib/captions.d.ts +6 -0
  90. package/dist/ts3.4/client/docs/lib/componentTypes.d.ts +2 -0
  91. package/dist/ts3.4/client/docs/lib/defaultValues/createDefaultValue.d.ts +2 -0
  92. package/dist/ts3.4/client/docs/lib/defaultValues/createFromRawDefaultProp.d.ts +11 -0
  93. package/dist/ts3.4/client/docs/lib/defaultValues/generateArray.d.ts +3 -0
  94. package/dist/ts3.4/client/docs/lib/defaultValues/generateObject.d.ts +3 -0
  95. package/dist/ts3.4/client/docs/lib/defaultValues/index.d.ts +2 -0
  96. package/dist/ts3.4/client/docs/lib/defaultValues/prettyIdentifier.d.ts +4 -0
  97. package/dist/ts3.4/client/docs/lib/generateCode.d.ts +3 -0
  98. package/dist/ts3.4/client/docs/lib/index.d.ts +4 -0
  99. package/dist/ts3.4/client/docs/lib/inspection/acornParser.d.ts +7 -0
  100. package/dist/ts3.4/client/docs/lib/inspection/index.d.ts +2 -0
  101. package/dist/ts3.4/client/docs/lib/inspection/inspectValue.d.ts +2 -0
  102. package/dist/ts3.4/client/docs/lib/inspection/types.d.ts +50 -0
  103. package/dist/ts3.4/client/docs/lib/isHtmlTag.d.ts +1 -0
  104. package/dist/ts3.4/client/docs/propTypes/createType.d.ts +2 -0
  105. package/dist/ts3.4/client/docs/propTypes/generateFuncSignature.d.ts +4 -0
  106. package/dist/ts3.4/client/docs/propTypes/handleProp.d.ts +5 -0
  107. package/dist/ts3.4/client/docs/propTypes/rawDefaultPropResolvers.d.ts +1 -0
  108. package/dist/ts3.4/client/docs/propTypes/sortProps.d.ts +4 -0
  109. package/dist/ts3.4/client/docs/react-argtypes.stories.d.ts +1 -0
  110. package/dist/ts3.4/client/docs/typeScript/handleProp.d.ts +3 -0
  111. package/dist/ts3.4/server/framework-preset-react-docs.d.ts +6 -0
  112. package/dist/ts3.9/client/docs/config.d.ts +13 -0
  113. package/dist/ts3.9/client/docs/extractArgTypes.d.ts +2 -0
  114. package/dist/ts3.9/client/docs/extractProps.d.ts +5 -0
  115. package/dist/ts3.9/client/docs/jsxDecorator.d.ts +23 -0
  116. package/dist/ts3.9/client/docs/lib/captions.d.ts +6 -0
  117. package/dist/ts3.9/client/docs/lib/componentTypes.d.ts +2 -0
  118. package/dist/ts3.9/client/docs/lib/defaultValues/createDefaultValue.d.ts +2 -0
  119. package/dist/ts3.9/client/docs/lib/defaultValues/createFromRawDefaultProp.d.ts +11 -0
  120. package/dist/ts3.9/client/docs/lib/defaultValues/generateArray.d.ts +3 -0
  121. package/dist/ts3.9/client/docs/lib/defaultValues/generateObject.d.ts +3 -0
  122. package/dist/ts3.9/client/docs/lib/defaultValues/index.d.ts +2 -0
  123. package/dist/ts3.9/client/docs/lib/defaultValues/prettyIdentifier.d.ts +4 -0
  124. package/dist/ts3.9/client/docs/lib/generateCode.d.ts +3 -0
  125. package/dist/ts3.9/client/docs/lib/index.d.ts +4 -0
  126. package/dist/ts3.9/client/docs/lib/inspection/acornParser.d.ts +7 -0
  127. package/dist/ts3.9/client/docs/lib/inspection/index.d.ts +2 -0
  128. package/dist/ts3.9/client/docs/lib/inspection/inspectValue.d.ts +2 -0
  129. package/dist/ts3.9/client/docs/lib/inspection/types.d.ts +50 -0
  130. package/dist/ts3.9/client/docs/lib/isHtmlTag.d.ts +1 -0
  131. package/dist/ts3.9/client/docs/propTypes/createType.d.ts +2 -0
  132. package/dist/ts3.9/client/docs/propTypes/generateFuncSignature.d.ts +4 -0
  133. package/dist/ts3.9/client/docs/propTypes/handleProp.d.ts +5 -0
  134. package/dist/ts3.9/client/docs/propTypes/rawDefaultPropResolvers.d.ts +1 -0
  135. package/dist/ts3.9/client/docs/propTypes/sortProps.d.ts +4 -0
  136. package/dist/ts3.9/client/docs/react-argtypes.stories.d.ts +1 -0
  137. package/dist/ts3.9/client/docs/typeScript/handleProp.d.ts +3 -0
  138. package/dist/ts3.9/client/preview/types-6-0.d.ts +3 -3
  139. package/dist/ts3.9/client/preview/types-7-0.d.ts +2 -2
  140. package/dist/ts3.9/server/framework-preset-react-docs.d.ts +6 -0
  141. package/dist/ts3.9/server/options.d.ts +1 -1
  142. package/dist/ts3.9/server/preset.d.ts +1 -1
  143. package/package.json +21 -11
  144. package/types/index.ts +1 -1
  145. package/dist/ts3.4/server/framework-preset-react-docgen.d.ts +0 -5
  146. package/dist/ts3.9/server/framework-preset-react-docgen.d.ts +0 -5
@@ -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
+ }
@@ -0,0 +1,57 @@
1
+ export function generateFuncSignature(params, returns) {
2
+ const hasParams = params != null;
3
+ const hasReturns = returns != null;
4
+
5
+ if (!hasParams && !hasReturns) {
6
+ return '';
7
+ }
8
+
9
+ const funcParts = [];
10
+
11
+ if (hasParams) {
12
+ const funcParams = params.map(x => {
13
+ const prettyName = x.getPrettyName();
14
+ const typeName = x.getTypeName();
15
+
16
+ if (typeName != null) {
17
+ return `${prettyName}: ${typeName}`;
18
+ }
19
+
20
+ return prettyName;
21
+ });
22
+ funcParts.push(`(${funcParams.join(', ')})`);
23
+ } else {
24
+ funcParts.push('()');
25
+ }
26
+
27
+ if (hasReturns) {
28
+ funcParts.push(`=> ${returns.getTypeName()}`);
29
+ }
30
+
31
+ return funcParts.join(' ');
32
+ }
33
+ export function generateShortFuncSignature(params, returns) {
34
+ const hasParams = params != null;
35
+ const hasReturns = returns != null;
36
+
37
+ if (!hasParams && !hasReturns) {
38
+ return '';
39
+ }
40
+
41
+ const funcParts = [];
42
+
43
+ if (hasParams) {
44
+ funcParts.push('( ... )');
45
+ } else {
46
+ funcParts.push('()');
47
+ }
48
+
49
+ if (hasReturns) {
50
+ funcParts.push(`=> ${returns.getTypeName()}`);
51
+ }
52
+
53
+ return funcParts.join(' ');
54
+ }
55
+ export function toMultilineSignature(signature) {
56
+ return signature.replace(/,/g, ',\r\n');
57
+ }
@@ -0,0 +1,39 @@
1
+ import { createType } from './createType';
2
+ import { createDefaultValue, createDefaultValueFromRawDefaultProp } from '../lib/defaultValues';
3
+ import { keepOriginalDefinitionOrder } from './sortProps';
4
+ import { rawDefaultPropTypeResolvers } from './rawDefaultPropResolvers';
5
+ export function enhancePropTypesProp(extractedProp, rawDefaultProp) {
6
+ const {
7
+ propDef
8
+ } = extractedProp;
9
+ const newtype = createType(extractedProp);
10
+
11
+ if (newtype != null) {
12
+ propDef.type = newtype;
13
+ }
14
+
15
+ const {
16
+ defaultValue
17
+ } = extractedProp.docgenInfo;
18
+
19
+ if (defaultValue != null && defaultValue.value != null) {
20
+ const newDefaultValue = createDefaultValue(defaultValue.value);
21
+
22
+ if (newDefaultValue != null) {
23
+ propDef.defaultValue = newDefaultValue;
24
+ }
25
+ } else if (rawDefaultProp != null) {
26
+ const newDefaultValue = createDefaultValueFromRawDefaultProp(rawDefaultProp, propDef, rawDefaultPropTypeResolvers);
27
+
28
+ if (newDefaultValue != null) {
29
+ propDef.defaultValue = newDefaultValue;
30
+ }
31
+ }
32
+
33
+ return propDef;
34
+ }
35
+ export function enhancePropTypesProps(extractedProps, component) {
36
+ const rawDefaultProps = component.defaultProps != null ? component.defaultProps : {};
37
+ const enhancedProps = extractedProps.map(x => enhancePropTypesProp(x, rawDefaultProps[x.propDef.name]));
38
+ return keepOriginalDefinitionOrder(enhancedProps, component);
39
+ }
@@ -0,0 +1,31 @@
1
+ import { createSummaryValue } from '@storybook/docs-tools';
2
+ import { extractFunctionName, createTypeResolvers } from '../lib/defaultValues';
3
+ import { FUNCTION_CAPTION, ELEMENT_CAPTION } from '../lib';
4
+ import { getPrettyElementIdentifier, getPrettyFuncIdentifier } from '../lib/defaultValues/prettyIdentifier';
5
+ import { inspectValue } from '../lib/inspection';
6
+
7
+ const funcResolver = (rawDefaultProp, {
8
+ name,
9
+ type
10
+ }) => {
11
+ const isElement = type.summary === 'element' || type.summary === 'elementType';
12
+ const funcName = extractFunctionName(rawDefaultProp, name);
13
+
14
+ if (funcName != null) {
15
+ // Try to display the name of the component. The body of the component is omitted since the code has been transpiled.
16
+ if (isElement) {
17
+ return createSummaryValue(getPrettyElementIdentifier(funcName));
18
+ }
19
+
20
+ const {
21
+ hasParams
22
+ } = inspectValue(rawDefaultProp.toString()).inferredType;
23
+ return createSummaryValue(getPrettyFuncIdentifier(funcName, hasParams));
24
+ }
25
+
26
+ return createSummaryValue(isElement ? ELEMENT_CAPTION : FUNCTION_CAPTION);
27
+ };
28
+
29
+ export const rawDefaultPropTypeResolvers = createTypeResolvers({
30
+ function: funcResolver
31
+ });
@@ -0,0 +1,14 @@
1
+ // react-docgen doesn't returned the props in the order they were defined in the "propTypes" object of the component.
2
+ // This function re-order them by their original definition order.
3
+ export function keepOriginalDefinitionOrder(extractedProps, component) {
4
+ // eslint-disable-next-line react/forbid-foreign-prop-types
5
+ const {
6
+ propTypes
7
+ } = component;
8
+
9
+ if (propTypes != null) {
10
+ return Object.keys(propTypes).map(x => extractedProps.find(y => y.name === x)).filter(x => x);
11
+ }
12
+
13
+ return extractedProps;
14
+ }
@@ -0,0 +1,54 @@
1
+ import React, { useState } from 'react';
2
+ import mapValues from 'lodash/mapValues';
3
+ import { ArgsTable } from '@storybook/components';
4
+ import { inferControls } from '@storybook/store';
5
+ import { storiesOf } from '..';
6
+ import { extractArgTypes } from './extractArgTypes'; // FIXME
7
+
8
+ const argsTableProps = component => {
9
+ const argTypes = extractArgTypes(component);
10
+ const parameters = {
11
+ __isArgsStory: true
12
+ };
13
+ const rows = inferControls({
14
+ argTypes,
15
+ parameters
16
+ });
17
+ return {
18
+ rows
19
+ };
20
+ };
21
+
22
+ const ArgsStory = ({
23
+ component
24
+ }) => {
25
+ const {
26
+ rows
27
+ } = argsTableProps(component);
28
+ const initialArgs = mapValues(rows, argType => argType.defaultValue);
29
+ const [args, setArgs] = useState(initialArgs);
30
+ return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement("p", null, /*#__PURE__*/React.createElement("b", null, "NOTE:"), " these stories are to help visualise the snapshot tests in", ' ', /*#__PURE__*/React.createElement("code", null, "./react-properties.test.js"), "."), /*#__PURE__*/React.createElement(ArgsTable, {
31
+ rows: rows,
32
+ args: args,
33
+ updateArgs: val => setArgs(Object.assign({}, args, val))
34
+ }), /*#__PURE__*/React.createElement("table", null, /*#__PURE__*/React.createElement("thead", null, /*#__PURE__*/React.createElement("tr", null, /*#__PURE__*/React.createElement("th", null, "arg name"), /*#__PURE__*/React.createElement("th", null, "argType"))), /*#__PURE__*/React.createElement("tbody", null, Object.entries(args).map(([key, val]) => /*#__PURE__*/React.createElement("tr", {
35
+ key: key
36
+ }, /*#__PURE__*/React.createElement("td", null, /*#__PURE__*/React.createElement("code", null, key)), /*#__PURE__*/React.createElement("td", null, /*#__PURE__*/React.createElement("pre", null, JSON.stringify(rows[key]))))))));
37
+ };
38
+
39
+ const issuesFixtures = ['js-class-component', 'js-function-component', 'js-function-component-inline-defaults', 'js-function-component-inline-defaults-no-propTypes', 'ts-function-component', 'ts-function-component-inline-defaults', '9399-js-proptypes-shape', '8663-js-styled-components', '9626-js-default-values', '9668-js-proptypes-no-jsdoc', '8143-ts-react-fc-generics', '8143-ts-imported-types', '8279-js-styled-docgen', '8140-js-prop-types-oneof', '9023-js-hoc', '8740-ts-multi-props', '9556-ts-react-default-exports', '9592-ts-styled-props', '9591-ts-import-types', '9721-ts-deprecated-jsdoc', '9827-ts-default-values', '9586-js-react-memo', '9575-ts-camel-case', '9493-ts-display-name', '8894-9511-ts-forward-ref', '9465-ts-type-props', '8428-js-static-prop-types', '9764-ts-extend-props', '9922-ts-component-props'];
40
+ const issuesStories = storiesOf('ArgTypes/Issues', module);
41
+ issuesFixtures.forEach(fixture => {
42
+ // eslint-disable-next-line import/no-dynamic-require, global-require
43
+ const {
44
+ component
45
+ } = require(`./__testfixtures__/${fixture}/input`);
46
+
47
+ issuesStories.add(fixture, () => /*#__PURE__*/React.createElement(ArgsStory, {
48
+ component: component
49
+ }), {
50
+ chromatic: {
51
+ disable: true
52
+ }
53
+ });
54
+ });
@@ -0,0 +1,28 @@
1
+ import { createDefaultValue, createDefaultValueFromRawDefaultProp } from '../lib/defaultValues';
2
+ export function enhanceTypeScriptProp(extractedProp, rawDefaultProp) {
3
+ const {
4
+ propDef
5
+ } = extractedProp;
6
+ const {
7
+ defaultValue
8
+ } = extractedProp.docgenInfo;
9
+
10
+ if (defaultValue != null && defaultValue.value != null) {
11
+ const newDefaultValue = createDefaultValue(defaultValue.value);
12
+
13
+ if (newDefaultValue != null) {
14
+ propDef.defaultValue = newDefaultValue;
15
+ }
16
+ } else if (rawDefaultProp != null) {
17
+ const newDefaultValue = createDefaultValueFromRawDefaultProp(rawDefaultProp, propDef);
18
+
19
+ if (newDefaultValue != null) {
20
+ propDef.defaultValue = newDefaultValue;
21
+ }
22
+ }
23
+
24
+ return propDef;
25
+ }
26
+ export function enhanceTypeScriptProps(extractedProps) {
27
+ return extractedProps.map(prop => enhanceTypeScriptProp(prop));
28
+ }