@storybook/react 7.0.0-alpha.3 → 7.0.0-alpha.30
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/chunk-X53CQCCB.mjs +1 -0
- package/dist/config.d.ts +24 -0
- package/dist/config.js +7 -0
- package/dist/config.mjs +7 -0
- package/dist/index.d.ts +173 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/dist/types-9f8d440a.d.ts +17 -0
- package/package.json +43 -19
- package/preview.js +1 -1
- package/LICENSE +0 -21
- package/dist/cjs/config.js +0 -43
- package/dist/cjs/docs/config.js +0 -25
- package/dist/cjs/docs/extractArgTypes.js +0 -47
- package/dist/cjs/docs/extractProps.js +0 -57
- package/dist/cjs/docs/jsxDecorator.js +0 -210
- package/dist/cjs/docs/lib/captions.js +0 -18
- package/dist/cjs/docs/lib/componentTypes.js +0 -14
- package/dist/cjs/docs/lib/defaultValues/createDefaultValue.js +0 -86
- package/dist/cjs/docs/lib/defaultValues/createFromRawDefaultProp.js +0 -205
- package/dist/cjs/docs/lib/defaultValues/generateArray.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/generateObject.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/index.js +0 -31
- package/dist/cjs/docs/lib/defaultValues/prettyIdentifier.js +0 -36
- package/dist/cjs/docs/lib/generateCode.js +0 -74
- package/dist/cjs/docs/lib/index.js +0 -57
- package/dist/cjs/docs/lib/inspection/acornParser.js +0 -230
- package/dist/cjs/docs/lib/inspection/index.js +0 -31
- package/dist/cjs/docs/lib/inspection/inspectValue.js +0 -24
- package/dist/cjs/docs/lib/inspection/types.js +0 -19
- package/dist/cjs/docs/lib/isHtmlTag.js +0 -14
- package/dist/cjs/docs/propTypes/createType.js +0 -456
- package/dist/cjs/docs/propTypes/generateFuncSignature.js +0 -68
- package/dist/cjs/docs/propTypes/handleProp.js +0 -52
- package/dist/cjs/docs/propTypes/rawDefaultPropResolvers.js +0 -43
- package/dist/cjs/docs/propTypes/sortProps.js +0 -21
- package/dist/cjs/docs/react-argtypes.stories.js +0 -67
- package/dist/cjs/docs/typeScript/handleProp.js +0 -38
- package/dist/cjs/index.js +0 -98
- package/dist/cjs/preview/config.js +0 -19
- package/dist/cjs/preview/globals.js +0 -13
- package/dist/cjs/preview/index.js +0 -44
- package/dist/cjs/preview/render.js +0 -165
- package/dist/cjs/preview/types-6-0.js +0 -5
- package/dist/cjs/preview/types-6-3.js +0 -18
- package/dist/cjs/preview/types-7-0.js +0 -5
- package/dist/cjs/preview/types.js +0 -5
- package/dist/cjs/testing/index.js +0 -112
- package/dist/esm/config.js +0 -6
- package/dist/esm/docs/config.js +0 -12
- package/dist/esm/docs/extractArgTypes.js +0 -37
- package/dist/esm/docs/extractProps.js +0 -42
- package/dist/esm/docs/jsxDecorator.js +0 -170
- package/dist/esm/docs/lib/captions.js +0 -6
- package/dist/esm/docs/lib/componentTypes.js +0 -2
- package/dist/esm/docs/lib/defaultValues/createDefaultValue.js +0 -72
- package/dist/esm/docs/lib/defaultValues/createFromRawDefaultProp.js +0 -182
- package/dist/esm/docs/lib/defaultValues/generateArray.js +0 -21
- package/dist/esm/docs/lib/defaultValues/generateObject.js +0 -21
- package/dist/esm/docs/lib/defaultValues/index.js +0 -2
- package/dist/esm/docs/lib/defaultValues/prettyIdentifier.js +0 -24
- package/dist/esm/docs/lib/generateCode.js +0 -59
- package/dist/esm/docs/lib/index.js +0 -4
- package/dist/esm/docs/lib/inspection/acornParser.js +0 -211
- package/dist/esm/docs/lib/inspection/index.js +0 -2
- package/dist/esm/docs/lib/inspection/inspectValue.js +0 -15
- package/dist/esm/docs/lib/inspection/types.js +0 -12
- package/dist/esm/docs/lib/isHtmlTag.js +0 -4
- package/dist/esm/docs/propTypes/createType.js +0 -445
- package/dist/esm/docs/propTypes/generateFuncSignature.js +0 -57
- package/dist/esm/docs/propTypes/handleProp.js +0 -39
- package/dist/esm/docs/propTypes/rawDefaultPropResolvers.js +0 -31
- package/dist/esm/docs/propTypes/sortProps.js +0 -14
- package/dist/esm/docs/react-argtypes.stories.js +0 -54
- package/dist/esm/docs/typeScript/handleProp.js +0 -28
- package/dist/esm/index.js +0 -5
- package/dist/esm/preview/config.js +0 -1
- package/dist/esm/preview/globals.js +0 -8
- package/dist/esm/preview/index.js +0 -21
- package/dist/esm/preview/render.js +0 -141
- package/dist/esm/preview/types-6-0.js +0 -1
- package/dist/esm/preview/types-6-3.js +0 -10
- package/dist/esm/preview/types-7-0.js +0 -1
- package/dist/esm/preview/types.js +0 -1
- package/dist/esm/testing/index.js +0 -96
- package/dist/types/config.d.ts +0 -10
- package/dist/types/docs/config.d.ts +0 -10
- package/dist/types/docs/extractArgTypes.d.ts +0 -2
- package/dist/types/docs/extractProps.d.ts +0 -5
- package/dist/types/docs/jsxDecorator.d.ts +0 -23
- package/dist/types/docs/lib/captions.d.ts +0 -6
- package/dist/types/docs/lib/componentTypes.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/createDefaultValue.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/createFromRawDefaultProp.d.ts +0 -11
- package/dist/types/docs/lib/defaultValues/generateArray.d.ts +0 -3
- package/dist/types/docs/lib/defaultValues/generateObject.d.ts +0 -3
- package/dist/types/docs/lib/defaultValues/index.d.ts +0 -2
- package/dist/types/docs/lib/defaultValues/prettyIdentifier.d.ts +0 -4
- package/dist/types/docs/lib/generateCode.d.ts +0 -3
- package/dist/types/docs/lib/index.d.ts +0 -4
- package/dist/types/docs/lib/inspection/acornParser.d.ts +0 -7
- package/dist/types/docs/lib/inspection/index.d.ts +0 -2
- package/dist/types/docs/lib/inspection/inspectValue.d.ts +0 -2
- package/dist/types/docs/lib/inspection/types.d.ts +0 -50
- package/dist/types/docs/lib/isHtmlTag.d.ts +0 -1
- package/dist/types/docs/propTypes/createType.d.ts +0 -2
- package/dist/types/docs/propTypes/generateFuncSignature.d.ts +0 -4
- package/dist/types/docs/propTypes/handleProp.d.ts +0 -5
- package/dist/types/docs/propTypes/rawDefaultPropResolvers.d.ts +0 -1
- package/dist/types/docs/propTypes/sortProps.d.ts +0 -4
- package/dist/types/docs/react-argtypes.stories.d.ts +0 -1
- package/dist/types/docs/typeScript/handleProp.d.ts +0 -3
- package/dist/types/index.d.ts +0 -4
- package/dist/types/preview/config.d.ts +0 -1
- package/dist/types/preview/globals.d.ts +0 -1
- package/dist/types/preview/index.d.ts +0 -25
- package/dist/types/preview/render.d.ts +0 -5
- package/dist/types/preview/types-6-0.d.ts +0 -35
- package/dist/types/preview/types-6-3.d.ts +0 -40
- package/dist/types/preview/types-7-0.d.ts +0 -21
- package/dist/types/preview/types.d.ts +0 -16
- package/dist/types/testing/index.d.ts +0 -84
|
@@ -1,170 +0,0 @@
|
|
|
1
|
-
const _excluded = ["mdxType", "originalType", "children"];
|
|
2
|
-
|
|
3
|
-
function _objectWithoutPropertiesLoose(source, excluded) { if (source == null) return {}; var target = {}; var sourceKeys = Object.keys(source); var key, i; for (i = 0; i < sourceKeys.length; i++) { key = sourceKeys[i]; if (excluded.indexOf(key) >= 0) continue; target[key] = source[key]; } return target; }
|
|
4
|
-
|
|
5
|
-
/* eslint-disable no-underscore-dangle */
|
|
6
|
-
import React, { createElement } from 'react';
|
|
7
|
-
import reactElementToJSXString from 'react-element-to-jsx-string';
|
|
8
|
-
import dedent from 'ts-dedent';
|
|
9
|
-
import deprecate from 'util-deprecate';
|
|
10
|
-
import { addons, useEffect } from '@storybook/addons';
|
|
11
|
-
import { SourceType, SNIPPET_RENDERED, getDocgenSection } from '@storybook/docs-tools';
|
|
12
|
-
import { logger } from '@storybook/client-logger';
|
|
13
|
-
import { isMemo, isForwardRef } from './lib';
|
|
14
|
-
|
|
15
|
-
/** Run the user supplied onBeforeRender function if it exists */
|
|
16
|
-
const applyBeforeRender = (domString, options) => {
|
|
17
|
-
if (typeof options.onBeforeRender !== 'function') {
|
|
18
|
-
return domString;
|
|
19
|
-
}
|
|
20
|
-
|
|
21
|
-
const deprecatedOnBeforeRender = deprecate(options.onBeforeRender, dedent`
|
|
22
|
-
StoryFn.parameters.jsx.onBeforeRender was deprecated.
|
|
23
|
-
Prefer StoryFn.parameters.jsx.transformSource instead.
|
|
24
|
-
See https://github.com/storybookjs/storybook/blob/next/MIGRATION.md#deprecated-onbeforerender for details.
|
|
25
|
-
`);
|
|
26
|
-
return deprecatedOnBeforeRender(domString);
|
|
27
|
-
};
|
|
28
|
-
/** Run the user supplied transformSource function if it exists */
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
const applyTransformSource = (domString, options, context) => {
|
|
32
|
-
if (typeof options.transformSource !== 'function') {
|
|
33
|
-
return domString;
|
|
34
|
-
}
|
|
35
|
-
|
|
36
|
-
return options.transformSource(domString, context);
|
|
37
|
-
};
|
|
38
|
-
/** Apply the users parameters and render the jsx for a story */
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
export const renderJsx = (code, options) => {
|
|
42
|
-
if (typeof code === 'undefined') {
|
|
43
|
-
logger.warn('Too many skip or undefined component');
|
|
44
|
-
return null;
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
let renderedJSX = code;
|
|
48
|
-
const Type = renderedJSX.type;
|
|
49
|
-
|
|
50
|
-
for (let i = 0; i < options.skip; i += 1) {
|
|
51
|
-
if (typeof renderedJSX === 'undefined') {
|
|
52
|
-
logger.warn('Cannot skip undefined element');
|
|
53
|
-
return null;
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (React.Children.count(renderedJSX) > 1) {
|
|
57
|
-
logger.warn('Trying to skip an array of elements');
|
|
58
|
-
return null;
|
|
59
|
-
}
|
|
60
|
-
|
|
61
|
-
if (typeof renderedJSX.props.children === 'undefined') {
|
|
62
|
-
logger.warn('Not enough children to skip elements.');
|
|
63
|
-
|
|
64
|
-
if (typeof renderedJSX.type === 'function' && renderedJSX.type.name === '') {
|
|
65
|
-
renderedJSX = /*#__PURE__*/React.createElement(Type, renderedJSX.props);
|
|
66
|
-
}
|
|
67
|
-
} else if (typeof renderedJSX.props.children === 'function') {
|
|
68
|
-
renderedJSX = renderedJSX.props.children();
|
|
69
|
-
} else {
|
|
70
|
-
renderedJSX = renderedJSX.props.children;
|
|
71
|
-
}
|
|
72
|
-
}
|
|
73
|
-
|
|
74
|
-
const displayNameDefaults = typeof options.displayName === 'string' ? {
|
|
75
|
-
showFunctions: true,
|
|
76
|
-
displayName: () => options.displayName
|
|
77
|
-
} : {
|
|
78
|
-
// To get exotic component names resolving properly
|
|
79
|
-
displayName: el => el.type.displayName || getDocgenSection(el.type, 'displayName') || (el.type.name !== '_default' ? el.type.name : null) || (typeof el.type === 'function' ? 'No Display Name' : null) || (isForwardRef(el.type) ? el.type.render.name : null) || (isMemo(el.type) ? el.type.type.name : null) || el.type
|
|
80
|
-
};
|
|
81
|
-
const filterDefaults = {
|
|
82
|
-
filterProps: (value, key) => value !== undefined
|
|
83
|
-
};
|
|
84
|
-
const opts = Object.assign({}, displayNameDefaults, filterDefaults, options);
|
|
85
|
-
const result = React.Children.map(code, c => {
|
|
86
|
-
// @ts-ignore FIXME: workaround react-element-to-jsx-string
|
|
87
|
-
const child = typeof c === 'number' ? c.toString() : c;
|
|
88
|
-
let string = applyBeforeRender(reactElementToJSXString(child, opts), options);
|
|
89
|
-
|
|
90
|
-
if (string.indexOf('"') > -1) {
|
|
91
|
-
const matches = string.match(/\S+=\\"([^"]*)\\"/g);
|
|
92
|
-
|
|
93
|
-
if (matches) {
|
|
94
|
-
matches.forEach(match => {
|
|
95
|
-
string = string.replace(match, match.replace(/"/g, "'"));
|
|
96
|
-
});
|
|
97
|
-
}
|
|
98
|
-
}
|
|
99
|
-
|
|
100
|
-
return string;
|
|
101
|
-
}).join('\n');
|
|
102
|
-
return result.replace(/function\s+noRefCheck\(\)\s+\{\}/, '() => {}');
|
|
103
|
-
};
|
|
104
|
-
const defaultOpts = {
|
|
105
|
-
skip: 0,
|
|
106
|
-
showFunctions: false,
|
|
107
|
-
enableBeautify: true,
|
|
108
|
-
showDefaultProps: false
|
|
109
|
-
};
|
|
110
|
-
export const skipJsxRender = context => {
|
|
111
|
-
const sourceParams = context?.parameters.docs?.source;
|
|
112
|
-
const isArgsStory = context?.parameters.__isArgsStory; // always render if the user forces it
|
|
113
|
-
|
|
114
|
-
if (sourceParams?.type === SourceType.DYNAMIC) {
|
|
115
|
-
return false;
|
|
116
|
-
} // never render if the user is forcing the block to render code, or
|
|
117
|
-
// if the user provides code, or if it's not an args story.
|
|
118
|
-
|
|
119
|
-
|
|
120
|
-
return !isArgsStory || sourceParams?.code || sourceParams?.type === SourceType.CODE;
|
|
121
|
-
};
|
|
122
|
-
|
|
123
|
-
const isMdx = node => node.type?.displayName === 'MDXCreateElement' && !!node.props?.mdxType;
|
|
124
|
-
|
|
125
|
-
const mdxToJsx = node => {
|
|
126
|
-
if (!isMdx(node)) return node;
|
|
127
|
-
|
|
128
|
-
const _node$props = node.props,
|
|
129
|
-
{
|
|
130
|
-
originalType,
|
|
131
|
-
children
|
|
132
|
-
} = _node$props,
|
|
133
|
-
rest = _objectWithoutPropertiesLoose(_node$props, _excluded);
|
|
134
|
-
|
|
135
|
-
let jsxChildren = [];
|
|
136
|
-
|
|
137
|
-
if (children) {
|
|
138
|
-
const array = Array.isArray(children) ? children : [children];
|
|
139
|
-
jsxChildren = array.map(mdxToJsx);
|
|
140
|
-
}
|
|
141
|
-
|
|
142
|
-
return /*#__PURE__*/createElement(originalType, rest, ...jsxChildren);
|
|
143
|
-
};
|
|
144
|
-
|
|
145
|
-
export const jsxDecorator = (storyFn, context) => {
|
|
146
|
-
const channel = addons.getChannel();
|
|
147
|
-
const skip = skipJsxRender(context);
|
|
148
|
-
const story = storyFn();
|
|
149
|
-
let jsx = '';
|
|
150
|
-
useEffect(() => {
|
|
151
|
-
if (!skip) channel.emit(SNIPPET_RENDERED, (context || {}).id, jsx);
|
|
152
|
-
}); // We only need to render JSX if the source block is actually going to
|
|
153
|
-
// consume it. Otherwise it's just slowing us down.
|
|
154
|
-
|
|
155
|
-
if (skip) {
|
|
156
|
-
return story;
|
|
157
|
-
}
|
|
158
|
-
|
|
159
|
-
const options = Object.assign({}, defaultOpts, context?.parameters.jsx || {}); // Exclude decorators from source code snippet by default
|
|
160
|
-
|
|
161
|
-
const storyJsx = context?.parameters.docs?.source?.excludeDecorators ? context.originalStoryFn(context.args, context) : story;
|
|
162
|
-
const sourceJsx = mdxToJsx(storyJsx);
|
|
163
|
-
const rendered = renderJsx(sourceJsx, options);
|
|
164
|
-
|
|
165
|
-
if (rendered) {
|
|
166
|
-
jsx = applyTransformSource(rendered, options, context);
|
|
167
|
-
}
|
|
168
|
-
|
|
169
|
-
return story;
|
|
170
|
-
};
|
|
@@ -1,72 +0,0 @@
|
|
|
1
|
-
import { createSummaryValue, isTooLongForDefaultValueSummary } from '@storybook/docs-tools';
|
|
2
|
-
import { FUNCTION_CAPTION, ELEMENT_CAPTION } from '../captions';
|
|
3
|
-
import { InspectionType, inspectValue } from '../inspection';
|
|
4
|
-
import { isHtmlTag } from '../isHtmlTag';
|
|
5
|
-
import { generateCode } from '../generateCode';
|
|
6
|
-
import { generateObject } from './generateObject';
|
|
7
|
-
import { generateArray } from './generateArray';
|
|
8
|
-
import { getPrettyIdentifier } from './prettyIdentifier';
|
|
9
|
-
|
|
10
|
-
function generateFunc({
|
|
11
|
-
inferredType,
|
|
12
|
-
ast
|
|
13
|
-
}) {
|
|
14
|
-
const {
|
|
15
|
-
identifier
|
|
16
|
-
} = inferredType;
|
|
17
|
-
|
|
18
|
-
if (identifier != null) {
|
|
19
|
-
return createSummaryValue(getPrettyIdentifier(inferredType), generateCode(ast));
|
|
20
|
-
}
|
|
21
|
-
|
|
22
|
-
const prettyCaption = generateCode(ast, true);
|
|
23
|
-
return !isTooLongForDefaultValueSummary(prettyCaption) ? createSummaryValue(prettyCaption) : createSummaryValue(FUNCTION_CAPTION, generateCode(ast));
|
|
24
|
-
} // All elements are JSX elements.
|
|
25
|
-
// JSX elements are not supported by escodegen.
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
function generateElement(defaultValue, inspectionResult) {
|
|
29
|
-
const {
|
|
30
|
-
inferredType
|
|
31
|
-
} = inspectionResult;
|
|
32
|
-
const {
|
|
33
|
-
identifier
|
|
34
|
-
} = inferredType;
|
|
35
|
-
|
|
36
|
-
if (identifier != null) {
|
|
37
|
-
if (!isHtmlTag(identifier)) {
|
|
38
|
-
const prettyIdentifier = getPrettyIdentifier(inferredType);
|
|
39
|
-
return createSummaryValue(prettyIdentifier, defaultValue);
|
|
40
|
-
}
|
|
41
|
-
}
|
|
42
|
-
|
|
43
|
-
return !isTooLongForDefaultValueSummary(defaultValue) ? createSummaryValue(defaultValue) : createSummaryValue(ELEMENT_CAPTION, defaultValue);
|
|
44
|
-
}
|
|
45
|
-
|
|
46
|
-
export function createDefaultValue(defaultValue) {
|
|
47
|
-
try {
|
|
48
|
-
const inspectionResult = inspectValue(defaultValue);
|
|
49
|
-
|
|
50
|
-
switch (inspectionResult.inferredType.type) {
|
|
51
|
-
case InspectionType.OBJECT:
|
|
52
|
-
return generateObject(inspectionResult);
|
|
53
|
-
|
|
54
|
-
case InspectionType.FUNCTION:
|
|
55
|
-
return generateFunc(inspectionResult);
|
|
56
|
-
|
|
57
|
-
case InspectionType.ELEMENT:
|
|
58
|
-
return generateElement(defaultValue, inspectionResult);
|
|
59
|
-
|
|
60
|
-
case InspectionType.ARRAY:
|
|
61
|
-
return generateArray(inspectionResult);
|
|
62
|
-
|
|
63
|
-
default:
|
|
64
|
-
return null;
|
|
65
|
-
}
|
|
66
|
-
} catch (e) {
|
|
67
|
-
// eslint-disable-next-line no-console
|
|
68
|
-
console.error(e);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
return null;
|
|
72
|
-
}
|
|
@@ -1,182 +0,0 @@
|
|
|
1
|
-
/* eslint-disable @typescript-eslint/ban-types */
|
|
2
|
-
import isPlainObject from 'lodash/isPlainObject';
|
|
3
|
-
import isFunction from 'lodash/isFunction';
|
|
4
|
-
import isString from 'lodash/isString'; // @ts-ignore
|
|
5
|
-
|
|
6
|
-
import reactElementToJSXString from 'react-element-to-jsx-string';
|
|
7
|
-
import { createSummaryValue, isTooLongForDefaultValueSummary } from '@storybook/docs-tools';
|
|
8
|
-
import { inspectValue } from '../inspection';
|
|
9
|
-
import { generateObject } from './generateObject';
|
|
10
|
-
import { generateArray } from './generateArray';
|
|
11
|
-
import { getPrettyElementIdentifier, getPrettyFuncIdentifier } from './prettyIdentifier';
|
|
12
|
-
import { OBJECT_CAPTION, FUNCTION_CAPTION, ELEMENT_CAPTION } from '../captions';
|
|
13
|
-
import { isHtmlTag } from '../isHtmlTag';
|
|
14
|
-
|
|
15
|
-
function isReactElement(element) {
|
|
16
|
-
return element.$$typeof != null;
|
|
17
|
-
}
|
|
18
|
-
|
|
19
|
-
export function extractFunctionName(func, propName) {
|
|
20
|
-
const {
|
|
21
|
-
name
|
|
22
|
-
} = func; // Comparison with the prop name is to discard inferred function names.
|
|
23
|
-
|
|
24
|
-
if (name !== '' && name !== 'anonymous' && name !== propName) {
|
|
25
|
-
return name;
|
|
26
|
-
}
|
|
27
|
-
|
|
28
|
-
return null;
|
|
29
|
-
}
|
|
30
|
-
|
|
31
|
-
const stringResolver = rawDefaultProp => {
|
|
32
|
-
return createSummaryValue(JSON.stringify(rawDefaultProp));
|
|
33
|
-
};
|
|
34
|
-
|
|
35
|
-
function generateReactObject(rawDefaultProp) {
|
|
36
|
-
const {
|
|
37
|
-
type
|
|
38
|
-
} = rawDefaultProp;
|
|
39
|
-
const {
|
|
40
|
-
displayName
|
|
41
|
-
} = type;
|
|
42
|
-
const jsx = reactElementToJSXString(rawDefaultProp, {});
|
|
43
|
-
|
|
44
|
-
if (displayName != null) {
|
|
45
|
-
const prettyIdentifier = getPrettyElementIdentifier(displayName);
|
|
46
|
-
return createSummaryValue(prettyIdentifier, jsx);
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
if (isString(type)) {
|
|
50
|
-
// This is an HTML element.
|
|
51
|
-
if (isHtmlTag(type)) {
|
|
52
|
-
const jsxCompact = reactElementToJSXString(rawDefaultProp, {
|
|
53
|
-
tabStop: 0
|
|
54
|
-
});
|
|
55
|
-
const jsxSummary = jsxCompact.replace(/\r?\n|\r/g, '');
|
|
56
|
-
|
|
57
|
-
if (!isTooLongForDefaultValueSummary(jsxSummary)) {
|
|
58
|
-
return createSummaryValue(jsxSummary);
|
|
59
|
-
}
|
|
60
|
-
}
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
return createSummaryValue(ELEMENT_CAPTION, jsx);
|
|
64
|
-
}
|
|
65
|
-
|
|
66
|
-
const objectResolver = rawDefaultProp => {
|
|
67
|
-
if (isReactElement(rawDefaultProp) && rawDefaultProp.type != null) {
|
|
68
|
-
return generateReactObject(rawDefaultProp);
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
if (isPlainObject(rawDefaultProp)) {
|
|
72
|
-
const inspectionResult = inspectValue(JSON.stringify(rawDefaultProp));
|
|
73
|
-
return generateObject(inspectionResult);
|
|
74
|
-
}
|
|
75
|
-
|
|
76
|
-
if (Array.isArray(rawDefaultProp)) {
|
|
77
|
-
const inspectionResult = inspectValue(JSON.stringify(rawDefaultProp));
|
|
78
|
-
return generateArray(inspectionResult);
|
|
79
|
-
}
|
|
80
|
-
|
|
81
|
-
return createSummaryValue(OBJECT_CAPTION);
|
|
82
|
-
};
|
|
83
|
-
|
|
84
|
-
const functionResolver = (rawDefaultProp, propDef) => {
|
|
85
|
-
let isElement = false;
|
|
86
|
-
let inspectionResult; // Try to display the name of the component. The body of the component is omitted since the code has been transpiled.
|
|
87
|
-
|
|
88
|
-
if (isFunction(rawDefaultProp.render)) {
|
|
89
|
-
isElement = true;
|
|
90
|
-
} else if (rawDefaultProp.prototype != null && isFunction(rawDefaultProp.prototype.render)) {
|
|
91
|
-
isElement = true;
|
|
92
|
-
} else {
|
|
93
|
-
let innerElement;
|
|
94
|
-
|
|
95
|
-
try {
|
|
96
|
-
inspectionResult = inspectValue(rawDefaultProp.toString());
|
|
97
|
-
const {
|
|
98
|
-
hasParams,
|
|
99
|
-
params
|
|
100
|
-
} = inspectionResult.inferredType;
|
|
101
|
-
|
|
102
|
-
if (hasParams) {
|
|
103
|
-
// It might be a functional component accepting props.
|
|
104
|
-
if (params.length === 1 && params[0].type === 'ObjectPattern') {
|
|
105
|
-
innerElement = rawDefaultProp({});
|
|
106
|
-
}
|
|
107
|
-
} else {
|
|
108
|
-
innerElement = rawDefaultProp();
|
|
109
|
-
}
|
|
110
|
-
|
|
111
|
-
if (innerElement != null) {
|
|
112
|
-
if (isReactElement(innerElement)) {
|
|
113
|
-
isElement = true;
|
|
114
|
-
}
|
|
115
|
-
}
|
|
116
|
-
} catch (e) {// do nothing.
|
|
117
|
-
}
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
const funcName = extractFunctionName(rawDefaultProp, propDef.name);
|
|
121
|
-
|
|
122
|
-
if (funcName != null) {
|
|
123
|
-
if (isElement) {
|
|
124
|
-
return createSummaryValue(getPrettyElementIdentifier(funcName));
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
if (inspectionResult != null) {
|
|
128
|
-
inspectionResult = inspectValue(rawDefaultProp.toString());
|
|
129
|
-
}
|
|
130
|
-
|
|
131
|
-
const {
|
|
132
|
-
hasParams
|
|
133
|
-
} = inspectionResult.inferredType;
|
|
134
|
-
return createSummaryValue(getPrettyFuncIdentifier(funcName, hasParams));
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
return createSummaryValue(isElement ? ELEMENT_CAPTION : FUNCTION_CAPTION);
|
|
138
|
-
};
|
|
139
|
-
|
|
140
|
-
const defaultResolver = rawDefaultProp => {
|
|
141
|
-
return createSummaryValue(rawDefaultProp.toString());
|
|
142
|
-
};
|
|
143
|
-
|
|
144
|
-
const DEFAULT_TYPE_RESOLVERS = {
|
|
145
|
-
string: stringResolver,
|
|
146
|
-
object: objectResolver,
|
|
147
|
-
function: functionResolver,
|
|
148
|
-
default: defaultResolver
|
|
149
|
-
};
|
|
150
|
-
export function createTypeResolvers(customResolvers = {}) {
|
|
151
|
-
return Object.assign({}, DEFAULT_TYPE_RESOLVERS, customResolvers);
|
|
152
|
-
} // When react-docgen cannot provide a defaultValue we take it from the raw defaultProp.
|
|
153
|
-
// It works fine for types that are not transpiled. For the types that are transpiled, we can only provide partial support.
|
|
154
|
-
// This means that:
|
|
155
|
-
// - The detail might not be available.
|
|
156
|
-
// - Identifiers might not be "prettified" for all the types.
|
|
157
|
-
|
|
158
|
-
export function createDefaultValueFromRawDefaultProp(rawDefaultProp, propDef, typeResolvers = DEFAULT_TYPE_RESOLVERS) {
|
|
159
|
-
try {
|
|
160
|
-
// Keep the extra () otherwise it will fail for functions.
|
|
161
|
-
switch (typeof rawDefaultProp) {
|
|
162
|
-
case 'string':
|
|
163
|
-
return typeResolvers.string(rawDefaultProp, propDef);
|
|
164
|
-
|
|
165
|
-
case 'object':
|
|
166
|
-
return typeResolvers.object(rawDefaultProp, propDef);
|
|
167
|
-
|
|
168
|
-
case 'function':
|
|
169
|
-
{
|
|
170
|
-
return typeResolvers.function(rawDefaultProp, propDef);
|
|
171
|
-
}
|
|
172
|
-
|
|
173
|
-
default:
|
|
174
|
-
return typeResolvers.default(rawDefaultProp, propDef);
|
|
175
|
-
}
|
|
176
|
-
} catch (e) {
|
|
177
|
-
// eslint-disable-next-line no-console
|
|
178
|
-
console.error(e);
|
|
179
|
-
}
|
|
180
|
-
|
|
181
|
-
return null;
|
|
182
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { createSummaryValue, isTooLongForDefaultValueSummary } from '@storybook/docs-tools';
|
|
2
|
-
import { ARRAY_CAPTION } from '../captions';
|
|
3
|
-
import { generateArrayCode } from '../generateCode';
|
|
4
|
-
export function generateArray({
|
|
5
|
-
inferredType,
|
|
6
|
-
ast
|
|
7
|
-
}) {
|
|
8
|
-
const {
|
|
9
|
-
depth
|
|
10
|
-
} = inferredType;
|
|
11
|
-
|
|
12
|
-
if (depth <= 2) {
|
|
13
|
-
const compactArray = generateArrayCode(ast, true);
|
|
14
|
-
|
|
15
|
-
if (!isTooLongForDefaultValueSummary(compactArray)) {
|
|
16
|
-
return createSummaryValue(compactArray);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return createSummaryValue(ARRAY_CAPTION, generateArrayCode(ast));
|
|
21
|
-
}
|
|
@@ -1,21 +0,0 @@
|
|
|
1
|
-
import { createSummaryValue, isTooLongForDefaultValueSummary } from '@storybook/docs-tools';
|
|
2
|
-
import { OBJECT_CAPTION } from '../captions';
|
|
3
|
-
import { generateObjectCode } from '../generateCode';
|
|
4
|
-
export function generateObject({
|
|
5
|
-
inferredType,
|
|
6
|
-
ast
|
|
7
|
-
}) {
|
|
8
|
-
const {
|
|
9
|
-
depth
|
|
10
|
-
} = inferredType;
|
|
11
|
-
|
|
12
|
-
if (depth === 1) {
|
|
13
|
-
const compactObject = generateObjectCode(ast, true);
|
|
14
|
-
|
|
15
|
-
if (!isTooLongForDefaultValueSummary(compactObject)) {
|
|
16
|
-
return createSummaryValue(compactObject);
|
|
17
|
-
}
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
return createSummaryValue(OBJECT_CAPTION, generateObjectCode(ast));
|
|
21
|
-
}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import { InspectionType } from '../inspection';
|
|
2
|
-
export function getPrettyFuncIdentifier(identifier, hasArguments) {
|
|
3
|
-
return hasArguments ? `${identifier}( ... )` : `${identifier}()`;
|
|
4
|
-
}
|
|
5
|
-
export function getPrettyElementIdentifier(identifier) {
|
|
6
|
-
return `<${identifier} />`;
|
|
7
|
-
}
|
|
8
|
-
export function getPrettyIdentifier(inferredType) {
|
|
9
|
-
const {
|
|
10
|
-
type,
|
|
11
|
-
identifier
|
|
12
|
-
} = inferredType;
|
|
13
|
-
|
|
14
|
-
switch (type) {
|
|
15
|
-
case InspectionType.FUNCTION:
|
|
16
|
-
return getPrettyFuncIdentifier(identifier, inferredType.hasParams);
|
|
17
|
-
|
|
18
|
-
case InspectionType.ELEMENT:
|
|
19
|
-
return getPrettyElementIdentifier(identifier);
|
|
20
|
-
|
|
21
|
-
default:
|
|
22
|
-
return identifier;
|
|
23
|
-
}
|
|
24
|
-
}
|
|
@@ -1,59 +0,0 @@
|
|
|
1
|
-
import { generate } from 'escodegen';
|
|
2
|
-
import dedent from 'ts-dedent';
|
|
3
|
-
const BASIC_OPTIONS = {
|
|
4
|
-
format: {
|
|
5
|
-
indent: {
|
|
6
|
-
style: ' '
|
|
7
|
-
},
|
|
8
|
-
semicolons: false
|
|
9
|
-
}
|
|
10
|
-
};
|
|
11
|
-
const COMPACT_OPTIONS = Object.assign({}, BASIC_OPTIONS, {
|
|
12
|
-
format: {
|
|
13
|
-
newline: ''
|
|
14
|
-
}
|
|
15
|
-
});
|
|
16
|
-
const PRETTY_OPTIONS = Object.assign({}, BASIC_OPTIONS);
|
|
17
|
-
export function generateCode(ast, compact = false) {
|
|
18
|
-
return generate(ast, compact ? COMPACT_OPTIONS : PRETTY_OPTIONS);
|
|
19
|
-
}
|
|
20
|
-
export function generateObjectCode(ast, compact = false) {
|
|
21
|
-
return !compact ? generateCode(ast) : generateCompactObjectCode(ast);
|
|
22
|
-
}
|
|
23
|
-
|
|
24
|
-
function generateCompactObjectCode(ast) {
|
|
25
|
-
let result = generateCode(ast, true); // Cannot get escodegen to add a space before the last } with the compact mode settings.
|
|
26
|
-
// Fix it until a better solution is found.
|
|
27
|
-
|
|
28
|
-
if (!result.endsWith(' }')) {
|
|
29
|
-
result = `${result.slice(0, -1)} }`;
|
|
30
|
-
}
|
|
31
|
-
|
|
32
|
-
return result;
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export function generateArrayCode(ast, compact = false) {
|
|
36
|
-
return !compact ? generateMultilineArrayCode(ast) : generateCompactArrayCode(ast);
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function generateMultilineArrayCode(ast) {
|
|
40
|
-
let result = generateCode(ast); // escodegen add extra spacing before the closing bracket of a multiple line array with a nested object.
|
|
41
|
-
// Fix it until a better solution is found.
|
|
42
|
-
|
|
43
|
-
if (result.endsWith(' }]')) {
|
|
44
|
-
result = dedent(result);
|
|
45
|
-
}
|
|
46
|
-
|
|
47
|
-
return result;
|
|
48
|
-
}
|
|
49
|
-
|
|
50
|
-
function generateCompactArrayCode(ast) {
|
|
51
|
-
let result = generateCode(ast, true); // escodegen add extra an extra before the opening bracket of a compact array that contains primitive values.
|
|
52
|
-
// Fix it until a better solution is found.
|
|
53
|
-
|
|
54
|
-
if (result.startsWith('[ ')) {
|
|
55
|
-
result = result.replace('[ ', '[');
|
|
56
|
-
}
|
|
57
|
-
|
|
58
|
-
return result;
|
|
59
|
-
}
|