@storybook/codemod 6.5.9 → 7.0.0-alpha.10
Sign up to get free protection for your applications and to get access to all the features.
- package/dist/cjs/lib/utils.js +5 -0
- package/dist/cjs/transforms/csf-2-to-3.js +5 -1
- package/dist/esm/lib/utils.js +5 -0
- package/dist/esm/transforms/csf-2-to-3.js +5 -1
- package/dist/{ts3.9 → types}/lib/utils.d.ts +0 -0
- package/dist/{ts3.9 → types}/transforms/csf-2-to-3.d.ts +0 -0
- package/package.json +5 -7
- package/dist/modern/index.js +0 -99
- package/dist/modern/lib/utils.js +0 -25
- package/dist/modern/transforms/add-component-parameters.js +0 -57
- package/dist/modern/transforms/csf-2-to-3.js +0 -159
- package/dist/modern/transforms/csf-hoist-story-annotations.js +0 -86
- package/dist/modern/transforms/csf-to-mdx.js +0 -188
- package/dist/modern/transforms/mdx-to-csf.js +0 -139
- package/dist/modern/transforms/move-builtin-addons.js +0 -50
- package/dist/modern/transforms/storiesof-to-csf.js +0 -287
- package/dist/modern/transforms/update-addon-info.js +0 -94
- package/dist/modern/transforms/update-organisation-name.js +0 -74
- package/dist/modern/transforms/upgrade-hierarchy-separators.js +0 -35
- package/dist/ts3.4/lib/utils.d.ts +0 -2
- package/dist/ts3.4/transforms/csf-2-to-3.d.ts +0 -6
@@ -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
|
-
}
|
@@ -1,287 +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
|
-
import prettier from 'prettier';
|
8
|
-
import { logger } from '@storybook/node-logger';
|
9
|
-
import { storyNameFromExport } from '@storybook/csf';
|
10
|
-
import { sanitizeName, jscodeshiftToPrettierParser } from '../lib/utils';
|
11
|
-
/**
|
12
|
-
* Convert a legacy story API to component story format
|
13
|
-
*
|
14
|
-
* For example:
|
15
|
-
*
|
16
|
-
* ```
|
17
|
-
* input { Button } from './Button';
|
18
|
-
* storiesOf('Button', module).add('story', () => <Button label="The Button" />);
|
19
|
-
* ```
|
20
|
-
*
|
21
|
-
* Becomes:
|
22
|
-
*
|
23
|
-
* ```
|
24
|
-
* input { Button } from './Button';
|
25
|
-
* export default {
|
26
|
-
* title: 'Button'
|
27
|
-
* }
|
28
|
-
* export const story = () => <Button label="The Button" />;
|
29
|
-
*
|
30
|
-
* NOTES: only support chained storiesOf() calls
|
31
|
-
*/
|
32
|
-
|
33
|
-
export default function transformer(file, api, options) {
|
34
|
-
var LITERAL = ['ts', 'tsx'].includes(options.parser) ? 'StringLiteral' : 'Literal';
|
35
|
-
var j = api.jscodeshift;
|
36
|
-
var root = j(file.source);
|
37
|
-
|
38
|
-
function extractDecorators(parameters) {
|
39
|
-
if (!parameters) {
|
40
|
-
return {};
|
41
|
-
}
|
42
|
-
|
43
|
-
if (!parameters.properties) {
|
44
|
-
return {
|
45
|
-
storyParams: parameters
|
46
|
-
};
|
47
|
-
}
|
48
|
-
|
49
|
-
var storyDecorators = parameters.properties.find(function (p) {
|
50
|
-
return p.key.name === 'decorators';
|
51
|
-
});
|
52
|
-
|
53
|
-
if (!storyDecorators) {
|
54
|
-
return {
|
55
|
-
storyParams: parameters
|
56
|
-
};
|
57
|
-
}
|
58
|
-
|
59
|
-
storyDecorators = storyDecorators.value;
|
60
|
-
|
61
|
-
var storyParams = _objectSpread({}, parameters);
|
62
|
-
|
63
|
-
storyParams.properties = storyParams.properties.filter(function (p) {
|
64
|
-
return p.key.name !== 'decorators';
|
65
|
-
});
|
66
|
-
|
67
|
-
if (storyParams.properties.length === 0) {
|
68
|
-
return {
|
69
|
-
storyDecorators: storyDecorators
|
70
|
-
};
|
71
|
-
}
|
72
|
-
|
73
|
-
return {
|
74
|
-
storyParams: storyParams,
|
75
|
-
storyDecorators: storyDecorators
|
76
|
-
};
|
77
|
-
}
|
78
|
-
|
79
|
-
function convertToModuleExports(path, originalExports) {
|
80
|
-
var base = j(path);
|
81
|
-
var statements = [];
|
82
|
-
var extraExports = []; // .addDecorator
|
83
|
-
|
84
|
-
var decorators = [];
|
85
|
-
base.find(j.CallExpression).filter(function (call) {
|
86
|
-
return call.node.callee.property && call.node.callee.property.name === 'addDecorator';
|
87
|
-
}).forEach(function (add) {
|
88
|
-
var decorator = add.node.arguments[0];
|
89
|
-
decorators.push(decorator);
|
90
|
-
});
|
91
|
-
|
92
|
-
if (decorators.length > 0) {
|
93
|
-
decorators.reverse();
|
94
|
-
extraExports.push(j.property('init', j.identifier('decorators'), j.arrayExpression(decorators)));
|
95
|
-
} // .addParameters
|
96
|
-
|
97
|
-
|
98
|
-
var parameters = [];
|
99
|
-
base.find(j.CallExpression).filter(function (call) {
|
100
|
-
return call.node.callee.property && call.node.callee.property.name === 'addParameters';
|
101
|
-
}).forEach(function (add) {
|
102
|
-
// jscodeshift gives us the find results in reverse, but these args come in
|
103
|
-
// order, so we double reverse here. ugh.
|
104
|
-
var params = [...add.node.arguments[0].properties];
|
105
|
-
params.reverse();
|
106
|
-
params.forEach(function (prop) {
|
107
|
-
return parameters.push(prop);
|
108
|
-
});
|
109
|
-
});
|
110
|
-
|
111
|
-
if (parameters.length > 0) {
|
112
|
-
parameters.reverse();
|
113
|
-
extraExports.push(j.property('init', j.identifier('parameters'), j.objectExpression(parameters)));
|
114
|
-
}
|
115
|
-
|
116
|
-
if (originalExports.length > 0) {
|
117
|
-
extraExports.push(j.property('init', j.identifier('excludeStories'), j.arrayExpression(originalExports.map(function (exp) {
|
118
|
-
return j.literal(exp);
|
119
|
-
}))));
|
120
|
-
} // storiesOf(...)
|
121
|
-
|
122
|
-
|
123
|
-
base.find(j.CallExpression).filter(function (call) {
|
124
|
-
return call.node.callee.name === 'storiesOf';
|
125
|
-
}).filter(function (call) {
|
126
|
-
return call.node.arguments.length > 0 && call.node.arguments[0].type === LITERAL;
|
127
|
-
}).forEach(function (storiesOf) {
|
128
|
-
var title = storiesOf.node.arguments[0].value;
|
129
|
-
statements.push(j.exportDefaultDeclaration(j.objectExpression([j.property('init', j.identifier('title'), j.literal(title)), ...extraExports])));
|
130
|
-
}); // .add(...)
|
131
|
-
|
132
|
-
var adds = [];
|
133
|
-
base.find(j.CallExpression).filter(function (add) {
|
134
|
-
return add.node.callee.property && add.node.callee.property.name === 'add';
|
135
|
-
}).filter(function (add) {
|
136
|
-
return add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL;
|
137
|
-
}).forEach(function (add) {
|
138
|
-
return adds.push(add);
|
139
|
-
});
|
140
|
-
adds.reverse();
|
141
|
-
adds.push(path);
|
142
|
-
var identifiers = new Set();
|
143
|
-
root.find(j.Identifier).forEach(function ({
|
144
|
-
value: value
|
145
|
-
}) {
|
146
|
-
return identifiers.add(value.name);
|
147
|
-
});
|
148
|
-
adds.forEach(function (add) {
|
149
|
-
var name = add.node.arguments[0].value;
|
150
|
-
var key = sanitizeName(name);
|
151
|
-
|
152
|
-
while (identifiers.has(key)) {
|
153
|
-
key = `_${key}`;
|
154
|
-
}
|
155
|
-
|
156
|
-
identifiers.add(key);
|
157
|
-
|
158
|
-
if (storyNameFromExport(key) === name) {
|
159
|
-
name = null;
|
160
|
-
}
|
161
|
-
|
162
|
-
var val = add.node.arguments[1];
|
163
|
-
statements.push(j.exportDeclaration(false, j.variableDeclaration('const', [j.variableDeclarator(j.identifier(key), val)])));
|
164
|
-
var storyAnnotations = [];
|
165
|
-
|
166
|
-
if (name) {
|
167
|
-
storyAnnotations.push(j.property('init', j.identifier('name'), j.literal(name)));
|
168
|
-
}
|
169
|
-
|
170
|
-
if (add.node.arguments.length > 2) {
|
171
|
-
var originalStoryParams = add.node.arguments[2];
|
172
|
-
|
173
|
-
var _extractDecorators = extractDecorators(originalStoryParams),
|
174
|
-
storyParams = _extractDecorators.storyParams,
|
175
|
-
storyDecorators = _extractDecorators.storyDecorators;
|
176
|
-
|
177
|
-
if (storyParams) {
|
178
|
-
storyAnnotations.push(j.property('init', j.identifier('parameters'), storyParams));
|
179
|
-
}
|
180
|
-
|
181
|
-
if (storyDecorators) {
|
182
|
-
storyAnnotations.push(j.property('init', j.identifier('decorators'), storyDecorators));
|
183
|
-
}
|
184
|
-
}
|
185
|
-
|
186
|
-
if (storyAnnotations.length > 0) {
|
187
|
-
statements.push(j.assignmentStatement('=', j.memberExpression(j.identifier(key), j.identifier('story')), j.objectExpression(storyAnnotations)));
|
188
|
-
}
|
189
|
-
});
|
190
|
-
var stmt = path.parent.node.type === 'VariableDeclarator' ? path.parent.parent : path.parent;
|
191
|
-
statements.reverse();
|
192
|
-
statements.forEach(function (s) {
|
193
|
-
return stmt.insertAfter(s);
|
194
|
-
});
|
195
|
-
j(stmt).remove();
|
196
|
-
} // Save the original storiesOf
|
197
|
-
|
198
|
-
|
199
|
-
var initialStoriesOf = root.find(j.CallExpression).filter(function (call) {
|
200
|
-
return call.node.callee.name === 'storiesOf';
|
201
|
-
});
|
202
|
-
var defaultExports = root.find(j.ExportDefaultDeclaration); // If there's already a default export
|
203
|
-
|
204
|
-
if (defaultExports.size() > 0) {
|
205
|
-
if (initialStoriesOf.size() > 0) {
|
206
|
-
logger.warn(`Found ${initialStoriesOf.size()} 'storiesOf' calls but existing default export, SKIPPING: '${file.path}'`);
|
207
|
-
}
|
208
|
-
|
209
|
-
return root.toSource();
|
210
|
-
} // Exclude all the original named exports
|
211
|
-
|
212
|
-
|
213
|
-
var originalExports = [];
|
214
|
-
root.find(j.ExportNamedDeclaration).forEach(function (exp) {
|
215
|
-
var _exp$node = exp.node,
|
216
|
-
declaration = _exp$node.declaration,
|
217
|
-
specifiers = _exp$node.specifiers;
|
218
|
-
|
219
|
-
if (declaration) {
|
220
|
-
var id = declaration.id,
|
221
|
-
declarations = declaration.declarations;
|
222
|
-
|
223
|
-
if (declarations) {
|
224
|
-
declarations.forEach(function (decl) {
|
225
|
-
var _decl$id = decl.id,
|
226
|
-
name = _decl$id.name,
|
227
|
-
properties = _decl$id.properties;
|
228
|
-
|
229
|
-
if (name) {
|
230
|
-
originalExports.push(name);
|
231
|
-
} else if (properties) {
|
232
|
-
properties.forEach(function (prop) {
|
233
|
-
return originalExports.push(prop.key.name);
|
234
|
-
});
|
235
|
-
}
|
236
|
-
});
|
237
|
-
} else if (id) {
|
238
|
-
originalExports.push(id.name);
|
239
|
-
}
|
240
|
-
} else if (specifiers) {
|
241
|
-
specifiers.forEach(function (spec) {
|
242
|
-
return originalExports.push(spec.exported.name);
|
243
|
-
});
|
244
|
-
}
|
245
|
-
}); // each top-level add expression corresponds to the last "add" of the chain.
|
246
|
-
// replace it with the entire export statements
|
247
|
-
|
248
|
-
root.find(j.CallExpression).filter(function (add) {
|
249
|
-
return add.node.callee.property && add.node.callee.property.name === 'add';
|
250
|
-
}).filter(function (add) {
|
251
|
-
return add.node.arguments.length >= 2 && add.node.arguments[0].type === LITERAL;
|
252
|
-
}).filter(function (add) {
|
253
|
-
return ['ExpressionStatement', 'VariableDeclarator'].includes(add.parentPath.node.type);
|
254
|
-
}).forEach(function (path) {
|
255
|
-
return convertToModuleExports(path, originalExports);
|
256
|
-
}); // remove storiesOf import
|
257
|
-
|
258
|
-
root.find(j.ImportSpecifier).filter(function (spec) {
|
259
|
-
return spec.node.imported.name === 'storiesOf' && spec.parent.node.source.value.startsWith('@storybook/');
|
260
|
-
}).forEach(function (spec) {
|
261
|
-
var toRemove = spec.parent.node.specifiers.length > 1 ? spec : spec.parent;
|
262
|
-
j(toRemove).remove();
|
263
|
-
});
|
264
|
-
var source = root.toSource({
|
265
|
-
trailingComma: true,
|
266
|
-
quote: 'single',
|
267
|
-
tabWidth: 2
|
268
|
-
});
|
269
|
-
|
270
|
-
if (initialStoriesOf.size() > 1) {
|
271
|
-
logger.warn(`Found ${initialStoriesOf.size()} 'storiesOf' calls, PLEASE FIX BY HAND: '${file.path}'`);
|
272
|
-
return source;
|
273
|
-
}
|
274
|
-
|
275
|
-
var prettierConfig = prettier.resolveConfig.sync('.', {
|
276
|
-
editorconfig: true
|
277
|
-
}) || {
|
278
|
-
printWidth: 100,
|
279
|
-
tabWidth: 2,
|
280
|
-
bracketSpacing: true,
|
281
|
-
trailingComma: 'es5',
|
282
|
-
singleQuote: true
|
283
|
-
};
|
284
|
-
return prettier.format(source, _objectSpread(_objectSpread({}, prettierConfig), {}, {
|
285
|
-
parser: jscodeshiftToPrettierParser(options.parser) || 'babel'
|
286
|
-
}));
|
287
|
-
}
|