@storybook/codemod 7.0.0-alpha.7 → 7.0.0-beta.0
Sign up to get free protection for your applications and to get access to all the features.
- package/README.md +0 -36
- package/dist/index.d.ts +261 -0
- package/dist/index.js +1 -0
- package/dist/index.mjs +1 -0
- package/jest.config.js +7 -0
- package/package.json +32 -29
- package/src/index.js +77 -0
- package/src/lib/utils.test.js +9 -0
- package/{dist/esm/lib/utils.js → src/lib/utils.ts} +10 -11
- package/src/transforms/__testfixtures__/add-component-parameters/add-component-parameters.input.js +44 -0
- package/src/transforms/__testfixtures__/add-component-parameters/add-component-parameters.output.snapshot +68 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/basic.input.js +25 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/basic.output.snapshot +27 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/overrides.input.js +25 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/overrides.output.snapshot +28 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/variable.input.js +13 -0
- package/src/transforms/__testfixtures__/csf-hoist-story-annotations/variable.output.snapshot +17 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/basic.input.js +20 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/basic.output.snapshot +18 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/component-id.input.js +9 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/component-id.output.snapshot +10 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/decorators.input.js +13 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/decorators.output.snapshot +12 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/exclude-stories.input.js +23 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/exclude-stories.output.snapshot +22 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/parameters.input.js +16 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/parameters.output.snapshot +17 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/story-function.input.js +19 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/story-function.output.snapshot +18 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/story-parameters.input.js +24 -0
- package/src/transforms/__testfixtures__/csf-to-mdx/story-parameters.output.snapshot +22 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/basic.input.js +18 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/basic.output.snapshot +40 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/component-id.input.js +6 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/component-id.output.snapshot +17 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/decorators.input.js +8 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/decorators.output.snapshot +18 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/exclude-stories.input.js +19 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/exclude-stories.output.snapshot +39 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/parameters.input.js +14 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/parameters.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/plaintext.input.js +3 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/plaintext.output.snapshot +11 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-function.input.js +10 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-function.output.snapshot +18 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-parameters.input.js +18 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-parameters.output.snapshot +32 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-refs.input.js +22 -0
- package/src/transforms/__testfixtures__/mdx-to-csf/story-refs.output.snapshot +34 -0
- package/src/transforms/__testfixtures__/move-builtin-addons/default.input.js +2 -0
- package/src/transforms/__testfixtures__/move-builtin-addons/default.output.snapshot +8 -0
- package/src/transforms/__testfixtures__/move-builtin-addons/with-no-change.input.js +3 -0
- package/src/transforms/__testfixtures__/move-builtin-addons/with-no-change.output.snapshot +7 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/basic.input.js +18 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/basic.output.snapshot +45 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/collision.input.js +11 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/collision.output.snapshot +38 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/const.input.js +1 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/const.output.snapshot +13 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/decorators.input.js +9 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/decorators.output.snapshot +18 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/default.input.js +7 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/default.output.snapshot +17 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/digit.input.js +1 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/digit.output.js +5 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/digit.output.snapshot +9 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-destructuring.input.js +11 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-destructuring.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-function.input.js +12 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-function.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-names.input.js +14 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/export-names.output.snapshot +26 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/exports.input.js +2 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/exports.output.snapshot +16 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/module.input.js +12 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/module.output.snapshot +16 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/multi.input.js +14 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/multi.output.snapshot +39 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/parameters-as-var.input.js +8 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/parameters-as-var.output.snapshot +20 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/parameters.input.js +10 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/parameters.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/storiesof-var.input.js +11 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/storiesof-var.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/story-decorators.input.js +11 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/story-decorators.output.snapshot +29 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/story-parameters.input.js +14 -0
- package/src/transforms/__testfixtures__/storiesof-to-csf/story-parameters.output.snapshot +32 -0
- package/src/transforms/__testfixtures__/update-addon-info/update-addon-info.input.js +198 -0
- package/src/transforms/__testfixtures__/update-addon-info/update-addon-info.output.snapshot +198 -0
- package/src/transforms/__testfixtures__/update-organisation-name/update-organisation-name.input.js +19 -0
- package/src/transforms/__testfixtures__/update-organisation-name/update-organisation-name.output.snapshot +23 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/csf.input.js +3 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/csf.output.snapshot +7 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/dynamic-storiesof.input.js +5 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/dynamic-storiesof.output.snapshot +9 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/storiesof.input.js +8 -0
- package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/storiesof.output.snapshot +12 -0
- package/src/transforms/__tests__/csf-2-to-3.test.ts +250 -0
- package/src/transforms/__tests__/transforms.tests.js +32 -0
- package/{dist/esm → src}/transforms/add-component-parameters.js +26 -21
- package/src/transforms/csf-2-to-3.ts +184 -0
- package/src/transforms/csf-hoist-story-annotations.js +97 -0
- package/src/transforms/csf-to-mdx.js +190 -0
- package/src/transforms/move-builtin-addons.js +32 -0
- package/src/transforms/storiesof-to-csf.js +277 -0
- package/{dist/esm → src}/transforms/update-addon-info.js +44 -24
- package/{dist/esm → src}/transforms/update-organisation-name.js +21 -24
- package/src/transforms/upgrade-hierarchy-separators.js +39 -0
- package/tsconfig.json +7 -0
- package/LICENSE +0 -21
- package/dist/cjs/index.js +0 -142
- package/dist/cjs/lib/utils.js +0 -45
- package/dist/cjs/transforms/add-component-parameters.js +0 -64
- package/dist/cjs/transforms/csf-2-to-3.js +0 -180
- package/dist/cjs/transforms/csf-hoist-story-annotations.js +0 -93
- package/dist/cjs/transforms/csf-to-mdx.js +0 -196
- package/dist/cjs/transforms/mdx-to-csf.js +0 -153
- package/dist/cjs/transforms/move-builtin-addons.js +0 -57
- package/dist/cjs/transforms/storiesof-to-csf.js +0 -300
- package/dist/cjs/transforms/update-addon-info.js +0 -101
- package/dist/cjs/transforms/update-organisation-name.js +0 -83
- package/dist/cjs/transforms/upgrade-hierarchy-separators.js +0 -42
- package/dist/esm/index.js +0 -99
- package/dist/esm/transforms/csf-2-to-3.js +0 -163
- package/dist/esm/transforms/csf-hoist-story-annotations.js +0 -86
- package/dist/esm/transforms/csf-to-mdx.js +0 -188
- package/dist/esm/transforms/mdx-to-csf.js +0 -139
- package/dist/esm/transforms/move-builtin-addons.js +0 -50
- package/dist/esm/transforms/storiesof-to-csf.js +0 -287
- package/dist/esm/transforms/upgrade-hierarchy-separators.js +0 -35
- package/dist/types/lib/utils.d.ts +0 -2
- package/dist/types/transforms/csf-2-to-3.d.ts +0 -6
@@ -1,163 +0,0 @@
|
|
1
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
2
|
-
|
3
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
4
|
-
|
5
|
-
function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }
|
6
|
-
|
7
|
-
/* eslint-disable no-underscore-dangle */
|
8
|
-
import prettier from 'prettier';
|
9
|
-
import * as t from '@babel/types';
|
10
|
-
import { formatCsf, loadCsf } from '@storybook/csf-tools';
|
11
|
-
import { jscodeshiftToPrettierParser } from '../lib/utils';
|
12
|
-
var logger = console;
|
13
|
-
|
14
|
-
var _rename = function (annotation) {
|
15
|
-
return annotation === 'storyName' ? 'name' : annotation;
|
16
|
-
};
|
17
|
-
|
18
|
-
var getTemplateBindVariable = function (init) {
|
19
|
-
return t.isCallExpression(init) && t.isMemberExpression(init.callee) && t.isIdentifier(init.callee.object) && t.isIdentifier(init.callee.property) && init.callee.property.name === 'bind' && (init.arguments.length === 0 || init.arguments.length === 1 && t.isObjectExpression(init.arguments[0]) && init.arguments[0].properties.length === 0) ? init.callee.object.name : null;
|
20
|
-
}; // export const A = ...
|
21
|
-
// A.parameters = { ... }; <===
|
22
|
-
|
23
|
-
|
24
|
-
var isStoryAnnotation = function (stmt, objectExports) {
|
25
|
-
return t.isExpressionStatement(stmt) && t.isAssignmentExpression(stmt.expression) && t.isMemberExpression(stmt.expression.left) && t.isIdentifier(stmt.expression.left.object) && objectExports[stmt.expression.left.object.name];
|
26
|
-
};
|
27
|
-
|
28
|
-
var isTemplateDeclaration = function (stmt, templates) {
|
29
|
-
return t.isVariableDeclaration(stmt) && stmt.declarations.length === 1 && t.isIdentifier(stmt.declarations[0].id) && templates[stmt.declarations[0].id.name];
|
30
|
-
};
|
31
|
-
|
32
|
-
var getNewExport = function (stmt, objectExports) {
|
33
|
-
if (t.isExportNamedDeclaration(stmt) && t.isVariableDeclaration(stmt.declaration) && stmt.declaration.declarations.length === 1) {
|
34
|
-
var decl = stmt.declaration.declarations[0];
|
35
|
-
|
36
|
-
if (t.isVariableDeclarator(decl) && t.isIdentifier(decl.id)) {
|
37
|
-
return objectExports[decl.id.name];
|
38
|
-
}
|
39
|
-
}
|
40
|
-
|
41
|
-
return null;
|
42
|
-
}; // Remove render function when it matches the global render function in react
|
43
|
-
// export default { component: Cat };
|
44
|
-
// export const A = (args) => <Cat {...args} />;
|
45
|
-
|
46
|
-
|
47
|
-
var isReactGlobalRenderFn = function (csf, storyFn) {
|
48
|
-
var _csf$_meta;
|
49
|
-
|
50
|
-
if ((_csf$_meta = csf._meta) !== null && _csf$_meta !== void 0 && _csf$_meta.component && t.isArrowFunctionExpression(storyFn) && storyFn.params.length === 1 && t.isJSXElement(storyFn.body)) {
|
51
|
-
var openingElement = storyFn.body.openingElement;
|
52
|
-
|
53
|
-
if (openingElement.selfClosing && t.isJSXIdentifier(openingElement.name) && openingElement.attributes.length === 1) {
|
54
|
-
var attr = openingElement.attributes[0];
|
55
|
-
var param = storyFn.params[0];
|
56
|
-
|
57
|
-
if (t.isJSXSpreadAttribute(attr) && t.isIdentifier(attr.argument) && t.isIdentifier(param) && param.name === attr.argument.name && csf._meta.component === openingElement.name.name) {
|
58
|
-
return true;
|
59
|
-
}
|
60
|
-
}
|
61
|
-
}
|
62
|
-
|
63
|
-
return false;
|
64
|
-
}; // A simple CSF story is a no-arg story without any extra annotations (params, args, etc.)
|
65
|
-
|
66
|
-
|
67
|
-
var isSimpleCSFStory = function (init, annotations) {
|
68
|
-
return annotations.length === 0 && t.isArrowFunctionExpression(init) && init.params.length === 0;
|
69
|
-
};
|
70
|
-
|
71
|
-
function transform({
|
72
|
-
source: source
|
73
|
-
}, api, options) {
|
74
|
-
var makeTitle = function (userTitle) {
|
75
|
-
return userTitle || 'FIXME';
|
76
|
-
};
|
77
|
-
|
78
|
-
var csf = loadCsf(source, {
|
79
|
-
makeTitle: makeTitle
|
80
|
-
});
|
81
|
-
|
82
|
-
try {
|
83
|
-
csf.parse();
|
84
|
-
} catch (err) {
|
85
|
-
logger.log(`Error ${err}, skipping`);
|
86
|
-
return source;
|
87
|
-
}
|
88
|
-
|
89
|
-
var objectExports = {};
|
90
|
-
Object.entries(csf._storyExports).forEach(function ([key, decl]) {
|
91
|
-
var annotations = Object.entries(csf._storyAnnotations[key]).map(function ([annotation, val]) {
|
92
|
-
return t.objectProperty(t.identifier(_rename(annotation)), val);
|
93
|
-
});
|
94
|
-
|
95
|
-
if (t.isVariableDeclarator(decl)) {
|
96
|
-
var init = decl.init,
|
97
|
-
id = decl.id; // only replace arrow function expressions && template
|
98
|
-
// ignore no-arg stories without annotations
|
99
|
-
|
100
|
-
var template = getTemplateBindVariable(init);
|
101
|
-
|
102
|
-
if (!t.isArrowFunctionExpression(init) && !template || isSimpleCSFStory(init, annotations)) {
|
103
|
-
return;
|
104
|
-
} // Remove the render function when we can hoist the template
|
105
|
-
// const Template = (args) => <Cat {...args} />;
|
106
|
-
// export const A = Template.bind({});
|
107
|
-
|
108
|
-
|
109
|
-
var storyFn = template && csf._templates[template];
|
110
|
-
|
111
|
-
if (!storyFn) {
|
112
|
-
storyFn = init;
|
113
|
-
}
|
114
|
-
|
115
|
-
var keyId = t.identifier(key); // @ts-ignore
|
116
|
-
|
117
|
-
var typeAnnotation = id.typeAnnotation;
|
118
|
-
|
119
|
-
if (typeAnnotation) {
|
120
|
-
keyId.typeAnnotation = typeAnnotation;
|
121
|
-
}
|
122
|
-
|
123
|
-
var renderAnnotation = isReactGlobalRenderFn(csf, storyFn) ? [] : [t.objectProperty(t.identifier('render'), storyFn)];
|
124
|
-
objectExports[key] = t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(keyId, t.objectExpression([...renderAnnotation, ...annotations]))]));
|
125
|
-
}
|
126
|
-
});
|
127
|
-
|
128
|
-
var updatedBody = csf._ast.program.body.reduce(function (acc, stmt) {
|
129
|
-
// remove story annotations & template declarations
|
130
|
-
if (isStoryAnnotation(stmt, objectExports) || isTemplateDeclaration(stmt, csf._templates)) {
|
131
|
-
return acc;
|
132
|
-
} // replace story exports with new object exports
|
133
|
-
|
134
|
-
|
135
|
-
var newExport = getNewExport(stmt, objectExports);
|
136
|
-
|
137
|
-
if (newExport) {
|
138
|
-
acc.push(newExport);
|
139
|
-
return acc;
|
140
|
-
} // include unknown statements
|
141
|
-
|
142
|
-
|
143
|
-
acc.push(stmt);
|
144
|
-
return acc;
|
145
|
-
}, []);
|
146
|
-
|
147
|
-
csf._ast.program.body = updatedBody;
|
148
|
-
var output = formatCsf(csf);
|
149
|
-
var prettierConfig = prettier.resolveConfig.sync('.', {
|
150
|
-
editorconfig: true
|
151
|
-
}) || {
|
152
|
-
printWidth: 100,
|
153
|
-
tabWidth: 2,
|
154
|
-
bracketSpacing: true,
|
155
|
-
trailingComma: 'es5',
|
156
|
-
singleQuote: true
|
157
|
-
};
|
158
|
-
return prettier.format(output, _objectSpread(_objectSpread({}, prettierConfig), {}, {
|
159
|
-
parser: jscodeshiftToPrettierParser(options === null || options === void 0 ? void 0 : options.parser)
|
160
|
-
}));
|
161
|
-
}
|
162
|
-
|
163
|
-
export default transform;
|
@@ -1,86 +0,0 @@
|
|
1
|
-
var getContainingStatement = function (n) {
|
2
|
-
if (n.node.type.endsWith('Statement')) {
|
3
|
-
return n;
|
4
|
-
}
|
5
|
-
|
6
|
-
return getContainingStatement(n.parent);
|
7
|
-
};
|
8
|
-
/**
|
9
|
-
* Hoist CSF .story annotations
|
10
|
-
*
|
11
|
-
* For example:
|
12
|
-
*
|
13
|
-
* ```
|
14
|
-
* export const Basic = () => <Button />
|
15
|
-
* Basic.story = {
|
16
|
-
* name: 'foo',
|
17
|
-
* parameters: { ... },
|
18
|
-
* decorators = [ ... ],
|
19
|
-
* };
|
20
|
-
* ```
|
21
|
-
*
|
22
|
-
* Becomes:
|
23
|
-
*
|
24
|
-
* ```
|
25
|
-
* export const Basic = () => <Button />
|
26
|
-
* Basic.storyName = 'foo';
|
27
|
-
* Basic.parameters = { ... };
|
28
|
-
* Basic.decorators = [ ... ];
|
29
|
-
* ```
|
30
|
-
*/
|
31
|
-
|
32
|
-
|
33
|
-
export default function transformer(file, api) {
|
34
|
-
var j = api.jscodeshift;
|
35
|
-
var root = j(file.source);
|
36
|
-
|
37
|
-
var renameKey = function (exp) {
|
38
|
-
return exp.type === 'Identifier' && exp.name === 'name' ? j.identifier('storyName') : exp;
|
39
|
-
}; // 1. If the program does not have `export default { title: '....' }, skip it
|
40
|
-
|
41
|
-
|
42
|
-
var defaultExportWithTitle = root.find(j.ExportDefaultDeclaration).filter(function (def) {
|
43
|
-
return def.node.declaration.type === 'ObjectExpression' && def.node.declaration.properties.map(function (p) {
|
44
|
-
return p.key.name;
|
45
|
-
}).includes('title');
|
46
|
-
});
|
47
|
-
|
48
|
-
if (defaultExportWithTitle.size() === 0) {
|
49
|
-
return root.toSource();
|
50
|
-
} // 2. Replace each Foo.story = { x: xVal } with Foo.x = xVal;
|
51
|
-
|
52
|
-
|
53
|
-
var storyAssignments = root.find(j.AssignmentExpression).filter(function (exp) {
|
54
|
-
var _exp$node = exp.node,
|
55
|
-
left = _exp$node.left,
|
56
|
-
right = _exp$node.right;
|
57
|
-
return left.type === 'MemberExpression' && left.object.type === 'Identifier' && left.property.type === 'Identifier' && left.property.name === 'story' && right.type === 'ObjectExpression';
|
58
|
-
});
|
59
|
-
storyAssignments.forEach(function (exp) {
|
60
|
-
var _exp$node2 = exp.node,
|
61
|
-
left = _exp$node2.left,
|
62
|
-
right = _exp$node2.right;
|
63
|
-
right.properties.forEach(function (prop) {
|
64
|
-
var stmt = getContainingStatement(exp);
|
65
|
-
stmt.insertBefore(j.assignmentStatement('=', j.memberExpression(left.object, renameKey(prop.key)), prop.value));
|
66
|
-
});
|
67
|
-
}); // 3. Remove the .story annotations
|
68
|
-
|
69
|
-
storyAssignments.remove(); // 4. Replace each Foo.story.x with Foo.x;
|
70
|
-
|
71
|
-
var storyOverrides = root.find(j.AssignmentExpression).filter(function (exp) {
|
72
|
-
var left = exp.node.left;
|
73
|
-
return left.type === 'MemberExpression' && left.object.type === 'MemberExpression' && left.object.property.type === 'Identifier' && left.object.property.name === 'story' && left.property.type === 'Identifier' // ?? ANNOTATION_FIELDS.includes(right.property.name)
|
74
|
-
;
|
75
|
-
});
|
76
|
-
storyOverrides.replaceWith(function (exp) {
|
77
|
-
var _exp$node3 = exp.node,
|
78
|
-
left = _exp$node3.left,
|
79
|
-
right = _exp$node3.right;
|
80
|
-
return j.assignmentExpression('=', j.memberExpression(left.object.object, renameKey(left.property)), right);
|
81
|
-
}); // 4. Render the updated tree
|
82
|
-
|
83
|
-
return root.toSource({
|
84
|
-
quote: 'single'
|
85
|
-
});
|
86
|
-
}
|
@@ -1,188 +0,0 @@
|
|
1
|
-
import { prettyPrint } from 'recast';
|
2
|
-
import { isExportStory } from '@storybook/csf';
|
3
|
-
|
4
|
-
function exportMdx(root, options) {
|
5
|
-
// eslint-disable-next-line no-underscore-dangle
|
6
|
-
var path = root.__paths[0]; // FIXME: insert the title as markdown after all of the imports
|
7
|
-
|
8
|
-
return path.node.program.body.map(function (n) {
|
9
|
-
var _prettyPrint = prettyPrint(n, options),
|
10
|
-
code = _prettyPrint.code;
|
11
|
-
|
12
|
-
if (n.type === 'JSXElement') {
|
13
|
-
return `${code}\n`;
|
14
|
-
}
|
15
|
-
|
16
|
-
return code;
|
17
|
-
}).join('\n');
|
18
|
-
}
|
19
|
-
|
20
|
-
function parseIncludeExclude(prop) {
|
21
|
-
var _prettyPrint2 = prettyPrint(prop, {}),
|
22
|
-
code = _prettyPrint2.code; // eslint-disable-next-line no-eval
|
23
|
-
|
24
|
-
|
25
|
-
return eval(code);
|
26
|
-
}
|
27
|
-
/**
|
28
|
-
* Convert a component's module story file into an MDX file
|
29
|
-
*
|
30
|
-
* For example:
|
31
|
-
*
|
32
|
-
* ```
|
33
|
-
* input { Button } from './Button';
|
34
|
-
* export default {
|
35
|
-
* title: 'Button'
|
36
|
-
* }
|
37
|
-
* export const story = () => <Button label="The Button" />;
|
38
|
-
* ```
|
39
|
-
*
|
40
|
-
* Becomes:
|
41
|
-
*
|
42
|
-
* ```
|
43
|
-
* import { Meta, Story } from '@storybook/addon-docs';
|
44
|
-
* input { Button } from './Button';
|
45
|
-
*
|
46
|
-
* <Meta title='Button' />
|
47
|
-
*
|
48
|
-
* <Story name='story'>
|
49
|
-
* <Button label="The Button" />
|
50
|
-
* </Story>
|
51
|
-
* ```
|
52
|
-
*/
|
53
|
-
|
54
|
-
|
55
|
-
export default function transformer(file, api) {
|
56
|
-
var j = api.jscodeshift;
|
57
|
-
var root = j(file.source); // FIXME: save out all the storyFn.story = { ... }
|
58
|
-
|
59
|
-
var storyKeyToStory = {}; // save out includeStories / excludeStories
|
60
|
-
|
61
|
-
var meta = {};
|
62
|
-
|
63
|
-
function makeAttr(key, val) {
|
64
|
-
return j.jsxAttribute(j.jsxIdentifier(key), val.type === 'Literal' ? val : j.jsxExpressionContainer(val));
|
65
|
-
}
|
66
|
-
|
67
|
-
function getStoryContents(node) {
|
68
|
-
return node.type === 'ArrowFunctionExpression' && node.body.type === 'JSXElement' ? node.body : j.jsxExpressionContainer(node);
|
69
|
-
}
|
70
|
-
|
71
|
-
function getName(storyKey) {
|
72
|
-
var story = storyKeyToStory[storyKey];
|
73
|
-
|
74
|
-
if (story) {
|
75
|
-
var name = story.properties.find(function (prop) {
|
76
|
-
return prop.key.name === 'name';
|
77
|
-
});
|
78
|
-
|
79
|
-
if (name && name.value.type === 'Literal') {
|
80
|
-
return name.value.value;
|
81
|
-
}
|
82
|
-
}
|
83
|
-
|
84
|
-
return storyKey;
|
85
|
-
}
|
86
|
-
|
87
|
-
function getStoryAttrs(storyKey) {
|
88
|
-
var attrs = [];
|
89
|
-
var story = storyKeyToStory[storyKey];
|
90
|
-
|
91
|
-
if (story) {
|
92
|
-
story.properties.forEach(function (prop) {
|
93
|
-
var key = prop.key,
|
94
|
-
value = prop.value;
|
95
|
-
|
96
|
-
if (key.name !== 'name') {
|
97
|
-
attrs.push(makeAttr(key.name, value));
|
98
|
-
}
|
99
|
-
});
|
100
|
-
}
|
101
|
-
|
102
|
-
return attrs;
|
103
|
-
} // 1. If the program does not have `export default { title: '....' }, skip it
|
104
|
-
|
105
|
-
|
106
|
-
var defaultExportWithTitle = root.find(j.ExportDefaultDeclaration).filter(function (def) {
|
107
|
-
return def.node.declaration.properties.map(function (p) {
|
108
|
-
return p.key.name;
|
109
|
-
}).includes('title');
|
110
|
-
});
|
111
|
-
|
112
|
-
if (defaultExportWithTitle.size() === 0) {
|
113
|
-
return root.toSource();
|
114
|
-
} // 2a. Add imports from '@storybook/addon-docs'
|
115
|
-
|
116
|
-
|
117
|
-
root.find(j.ImportDeclaration).at(-1).insertAfter(j.emptyStatement()).insertAfter(j.importDeclaration([j.importSpecifier(j.identifier('Meta')), j.importSpecifier(j.identifier('Story'))], j.literal('@storybook/addon-docs'))); // 2b. Remove react import which is implicit
|
118
|
-
|
119
|
-
root.find(j.ImportDeclaration).filter(function (decl) {
|
120
|
-
return decl.node.source.value === 'react';
|
121
|
-
}).remove(); // 3. Save out all the excluded stories
|
122
|
-
|
123
|
-
defaultExportWithTitle.forEach(function (exp) {
|
124
|
-
exp.node.declaration.properties.forEach(function (p) {
|
125
|
-
if (['includeStories', 'excludeStories'].includes(p.key.name)) {
|
126
|
-
meta[p.key.name] = parseIncludeExclude(p.value);
|
127
|
-
}
|
128
|
-
});
|
129
|
-
}); // 4. Collect all the story exports in storyKeyToStory[key] = null;
|
130
|
-
|
131
|
-
var namedExports = root.find(j.ExportNamedDeclaration);
|
132
|
-
namedExports.forEach(function (exp) {
|
133
|
-
var storyKey = exp.node.declaration.declarations[0].id.name;
|
134
|
-
|
135
|
-
if (isExportStory(storyKey, meta)) {
|
136
|
-
storyKeyToStory[storyKey] = null;
|
137
|
-
}
|
138
|
-
}); // 5. Collect all the storyKey.story in storyKeyToStory and also remove them
|
139
|
-
|
140
|
-
var storyAssignments = root.find(j.AssignmentExpression).filter(function (exp) {
|
141
|
-
var left = exp.node.left;
|
142
|
-
return left.type === 'MemberExpression' && left.object.type === 'Identifier' && left.object.name in storyKeyToStory && left.property.type === 'Identifier' && left.property.name === 'story';
|
143
|
-
});
|
144
|
-
storyAssignments.forEach(function (exp) {
|
145
|
-
var _exp$node = exp.node,
|
146
|
-
left = _exp$node.left,
|
147
|
-
right = _exp$node.right;
|
148
|
-
storyKeyToStory[left.object.name] = right;
|
149
|
-
});
|
150
|
-
storyAssignments.remove(); // 6. Convert the default export to <Meta />
|
151
|
-
|
152
|
-
defaultExportWithTitle.replaceWith(function (exp) {
|
153
|
-
var jsxId = j.jsxIdentifier('Meta');
|
154
|
-
var attrs = [];
|
155
|
-
exp.node.declaration.properties.forEach(function (prop) {
|
156
|
-
var key = prop.key,
|
157
|
-
value = prop.value;
|
158
|
-
|
159
|
-
if (!['includeStories', 'excludeStories'].includes(key.name)) {
|
160
|
-
attrs.push(makeAttr(key.name, value));
|
161
|
-
}
|
162
|
-
});
|
163
|
-
var opening = j.jsxOpeningElement(jsxId, attrs);
|
164
|
-
opening.selfClosing = true;
|
165
|
-
return j.jsxElement(opening);
|
166
|
-
}); // 7. Convert all the named exports to <Story>...</Story>
|
167
|
-
|
168
|
-
namedExports.replaceWith(function (exp) {
|
169
|
-
var storyKey = exp.node.declaration.declarations[0].id.name;
|
170
|
-
|
171
|
-
if (!isExportStory(storyKey, meta)) {
|
172
|
-
return exp.node;
|
173
|
-
}
|
174
|
-
|
175
|
-
var jsxId = j.jsxIdentifier('Story');
|
176
|
-
var name = getName(storyKey);
|
177
|
-
var attributes = [makeAttr('name', j.literal(name)), ...getStoryAttrs(storyKey)];
|
178
|
-
var opening = j.jsxOpeningElement(jsxId, attributes);
|
179
|
-
var closing = j.jsxClosingElement(jsxId);
|
180
|
-
var children = [getStoryContents(exp.node.declaration.declarations[0].init)];
|
181
|
-
return j.jsxElement(opening, closing, children);
|
182
|
-
});
|
183
|
-
return exportMdx(root, {
|
184
|
-
quote: 'single',
|
185
|
-
trailingComma: 'true',
|
186
|
-
tabWidth: 2
|
187
|
-
});
|
188
|
-
}
|
@@ -1,139 +0,0 @@
|
|
1
|
-
// import recast from 'recast';
|
2
|
-
import mdx from '@mdx-js/mdx';
|
3
|
-
import prettier from 'prettier';
|
4
|
-
import { sanitizeName } from '../lib/utils';
|
5
|
-
/**
|
6
|
-
* Convert a component's MDX file into module story format
|
7
|
-
*/
|
8
|
-
|
9
|
-
export default function transformer(file, api) {
|
10
|
-
var j = api.jscodeshift;
|
11
|
-
var code = mdx.sync(file.source, {});
|
12
|
-
var root = j(code);
|
13
|
-
|
14
|
-
function parseJsxAttributes(attributes) {
|
15
|
-
var result = {};
|
16
|
-
attributes.forEach(function (attr) {
|
17
|
-
var key = attr.name.name;
|
18
|
-
var val = attr.value.type === 'JSXExpressionContainer' ? attr.value.expression : attr.value;
|
19
|
-
result[key] = val;
|
20
|
-
});
|
21
|
-
return result;
|
22
|
-
}
|
23
|
-
|
24
|
-
function genObjectExpression(attrs) {
|
25
|
-
return j.objectExpression(Object.entries(attrs).map(function ([key, val]) {
|
26
|
-
return j.property('init', j.identifier(key), val);
|
27
|
-
}));
|
28
|
-
}
|
29
|
-
|
30
|
-
function convertToStories(path) {
|
31
|
-
var base = j(path);
|
32
|
-
var meta = {};
|
33
|
-
var includeStories = [];
|
34
|
-
var storyStatements = []; // get rid of all mdxType junk
|
35
|
-
|
36
|
-
base.find(j.JSXAttribute).filter(function (attr) {
|
37
|
-
return attr.node.name.name === 'mdxType';
|
38
|
-
}).remove(); // parse <Meta title="..." />
|
39
|
-
|
40
|
-
base.find(j.JSXElement).filter(function (elt) {
|
41
|
-
return elt.node.openingElement.name.name === 'Meta';
|
42
|
-
}).forEach(function (elt) {
|
43
|
-
var attrs = parseJsxAttributes(elt.node.openingElement.attributes);
|
44
|
-
Object.assign(meta, attrs);
|
45
|
-
}); // parse <Story name="..." />
|
46
|
-
|
47
|
-
base.find(j.JSXElement).filter(function (elt) {
|
48
|
-
return elt.node.openingElement.name.name === 'Story';
|
49
|
-
}).forEach(function (elt) {
|
50
|
-
var attrs = parseJsxAttributes(elt.node.openingElement.attributes);
|
51
|
-
|
52
|
-
if (attrs.name) {
|
53
|
-
var storyKey = sanitizeName(attrs.name.value);
|
54
|
-
includeStories.push(storyKey);
|
55
|
-
|
56
|
-
if (storyKey === attrs.name.value) {
|
57
|
-
delete attrs.name;
|
58
|
-
}
|
59
|
-
|
60
|
-
var body = elt.node.children.find(function (n) {
|
61
|
-
return n.type !== 'JSXText';
|
62
|
-
}) || j.literal(elt.node.children[0].value);
|
63
|
-
|
64
|
-
if (body.type === 'JSXExpressionContainer') {
|
65
|
-
body = body.expression;
|
66
|
-
}
|
67
|
-
|
68
|
-
storyStatements.push(j.exportDeclaration(false, j.variableDeclaration('const', [j.variableDeclarator(j.identifier(storyKey), body.type === 'ArrowFunctionExpression' ? body : j.arrowFunctionExpression([], body))])));
|
69
|
-
|
70
|
-
if (Object.keys(attrs).length > 0) {
|
71
|
-
storyStatements.push(j.assignmentStatement('=', j.memberExpression(j.identifier(storyKey), j.identifier('story')), genObjectExpression(attrs)));
|
72
|
-
}
|
73
|
-
|
74
|
-
storyStatements.push(j.emptyStatement());
|
75
|
-
}
|
76
|
-
});
|
77
|
-
|
78
|
-
if (root.find(j.ExportNamedDeclaration).size() > 0) {
|
79
|
-
meta.includeStories = j.arrayExpression(includeStories.map(function (key) {
|
80
|
-
return j.literal(key);
|
81
|
-
}));
|
82
|
-
}
|
83
|
-
|
84
|
-
var statements = [j.exportDefaultDeclaration(genObjectExpression(meta)), j.emptyStatement(), ...storyStatements];
|
85
|
-
var lastStatement = root.find(j.Statement).at(-1);
|
86
|
-
statements.reverse().forEach(function (stmt) {
|
87
|
-
lastStatement.insertAfter(stmt);
|
88
|
-
});
|
89
|
-
base.remove();
|
90
|
-
}
|
91
|
-
|
92
|
-
root.find(j.ExportDefaultDeclaration).forEach(convertToStories); // strip out Story/Meta import and MDX junk
|
93
|
-
// /* @jsx mdx */
|
94
|
-
|
95
|
-
root.find(j.ImportDeclaration).at(0).replaceWith(function (exp) {
|
96
|
-
return j.importDeclaration(exp.node.specifiers, exp.node.source);
|
97
|
-
}); // import { Story, Meta } from '@storybook/addon-docs';
|
98
|
-
|
99
|
-
root.find(j.ImportDeclaration).filter(function (exp) {
|
100
|
-
return exp.node.source.value === '@storybook/addon-docs';
|
101
|
-
}).remove(); // const makeShortcode = ...
|
102
|
-
// const layoutProps = {};
|
103
|
-
// const MDXLayout = 'wrapper';
|
104
|
-
|
105
|
-
var MDX_DECLS = ['makeShortcode', 'layoutProps', 'MDXLayout'];
|
106
|
-
root.find(j.VariableDeclaration).filter(function (decl) {
|
107
|
-
return decl.node.declarations.length === 1 && MDX_DECLS.includes(decl.node.declarations[0].id.name);
|
108
|
-
}).remove(); // const Source = makeShortcode('Source');
|
109
|
-
|
110
|
-
root.find(j.VariableDeclarator).filter(function (expr) {
|
111
|
-
return expr.node.init.type === 'CallExpression' && expr.node.init.callee.type === 'Identifier' && expr.node.init.callee.name === 'makeShortcode';
|
112
|
-
}).remove(); // MDXContent.isMDXComponent = true;
|
113
|
-
|
114
|
-
root.find(j.AssignmentExpression).filter(function (expr) {
|
115
|
-
return expr.node.left.type === 'MemberExpression' && expr.node.left.object.type === 'Identifier' && expr.node.left.object.name === 'MDXContent';
|
116
|
-
}).remove(); // Add back `import React from 'react';` which is implicit in MDX
|
117
|
-
|
118
|
-
var react = root.find(j.ImportDeclaration).filter(function (decl) {
|
119
|
-
return decl.node.source.value === 'react';
|
120
|
-
});
|
121
|
-
|
122
|
-
if (react.size() === 0) {
|
123
|
-
root.find(j.Statement).at(0).insertBefore(j.importDeclaration([j.importDefaultSpecifier(j.identifier('React'))], j.literal('react')));
|
124
|
-
}
|
125
|
-
|
126
|
-
var source = root.toSource({
|
127
|
-
trailingComma: true,
|
128
|
-
quote: 'single',
|
129
|
-
tabWidth: 2
|
130
|
-
});
|
131
|
-
return prettier.format(source, {
|
132
|
-
parser: 'babel',
|
133
|
-
printWidth: 100,
|
134
|
-
tabWidth: 2,
|
135
|
-
bracketSpacing: true,
|
136
|
-
trailingComma: 'es5',
|
137
|
-
singleQuote: true
|
138
|
-
});
|
139
|
-
}
|
@@ -1,50 +0,0 @@
|
|
1
|
-
import "core-js/modules/es.symbol.description.js";
|
2
|
-
|
3
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
4
|
-
|
5
|
-
function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
|
6
|
-
|
7
|
-
function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
|
8
|
-
|
9
|
-
function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) { arr2[i] = arr[i]; } return arr2; }
|
10
|
-
|
11
|
-
function _iterableToArrayLimit(arr, i) { var _i = arr == null ? null : typeof Symbol !== "undefined" && arr[Symbol.iterator] || arr["@@iterator"]; if (_i == null) return; var _arr = []; var _n = true; var _d = false; var _s, _e; try { for (_i = _i.call(arr); !(_n = (_s = _i.next()).done); _n = true) { _arr.push(_s.value); if (i && _arr.length === i) break; } } catch (err) { _d = true; _e = err; } finally { try { if (!_n && _i["return"] != null) _i["return"](); } finally { if (_d) throw _e; } } return _arr; }
|
12
|
-
|
13
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
14
|
-
|
15
|
-
export default function transformer(file, api) {
|
16
|
-
var j = api.jscodeshift;
|
17
|
-
|
18
|
-
var createImportDeclaration = function (specifiers, source) {
|
19
|
-
return j.importDeclaration(specifiers.map(function (s) {
|
20
|
-
return j.importSpecifier(j.identifier(s));
|
21
|
-
}), j.literal(source));
|
22
|
-
};
|
23
|
-
|
24
|
-
var deprecates = {
|
25
|
-
action: [['action'], '@storybook/addon-actions'],
|
26
|
-
linkTo: [['linkTo'], '@storybook/addon-links']
|
27
|
-
};
|
28
|
-
var transform = j(file.source).find(j.ImportDeclaration).filter(function (i) {
|
29
|
-
return i.value.source.value === '@storybook/react';
|
30
|
-
}).forEach(function (i) {
|
31
|
-
var importStatement = i.value;
|
32
|
-
importStatement.specifiers = importStatement.specifiers.filter(function (specifier) {
|
33
|
-
var item = deprecates[specifier.local.name];
|
34
|
-
|
35
|
-
if (item) {
|
36
|
-
var _item = _slicedToArray(item, 2),
|
37
|
-
specifiers = _item[0],
|
38
|
-
moduleName = _item[1];
|
39
|
-
|
40
|
-
i.insertAfter(createImportDeclaration(specifiers, moduleName));
|
41
|
-
return false;
|
42
|
-
}
|
43
|
-
|
44
|
-
return specifier;
|
45
|
-
});
|
46
|
-
});
|
47
|
-
return transform.toSource({
|
48
|
-
quote: 'single'
|
49
|
-
});
|
50
|
-
}
|