@storybook/codemod 6.4.0-beta.23 → 6.4.0-beta.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (41) hide show
  1. package/package.json +4 -4
  2. package/dist/cjs/index.js +0 -142
  3. package/dist/cjs/lib/utils.js +0 -40
  4. package/dist/cjs/transforms/add-component-parameters.js +0 -64
  5. package/dist/cjs/transforms/csf-2-to-3.js +0 -169
  6. package/dist/cjs/transforms/csf-hoist-story-annotations.js +0 -93
  7. package/dist/cjs/transforms/csf-to-mdx.js +0 -196
  8. package/dist/cjs/transforms/mdx-to-csf.js +0 -153
  9. package/dist/cjs/transforms/move-builtin-addons.js +0 -57
  10. package/dist/cjs/transforms/storiesof-to-csf.js +0 -300
  11. package/dist/cjs/transforms/update-addon-info.js +0 -101
  12. package/dist/cjs/transforms/update-organisation-name.js +0 -83
  13. package/dist/cjs/transforms/upgrade-hierarchy-separators.js +0 -42
  14. package/dist/esm/index.js +0 -99
  15. package/dist/esm/lib/utils.js +0 -25
  16. package/dist/esm/transforms/add-component-parameters.js +0 -57
  17. package/dist/esm/transforms/csf-2-to-3.js +0 -152
  18. package/dist/esm/transforms/csf-hoist-story-annotations.js +0 -86
  19. package/dist/esm/transforms/csf-to-mdx.js +0 -188
  20. package/dist/esm/transforms/mdx-to-csf.js +0 -139
  21. package/dist/esm/transforms/move-builtin-addons.js +0 -50
  22. package/dist/esm/transforms/storiesof-to-csf.js +0 -287
  23. package/dist/esm/transforms/update-addon-info.js +0 -94
  24. package/dist/esm/transforms/update-organisation-name.js +0 -74
  25. package/dist/esm/transforms/upgrade-hierarchy-separators.js +0 -35
  26. package/dist/modern/index.js +0 -99
  27. package/dist/modern/lib/utils.js +0 -25
  28. package/dist/modern/transforms/add-component-parameters.js +0 -57
  29. package/dist/modern/transforms/csf-2-to-3.js +0 -152
  30. package/dist/modern/transforms/csf-hoist-story-annotations.js +0 -86
  31. package/dist/modern/transforms/csf-to-mdx.js +0 -188
  32. package/dist/modern/transforms/mdx-to-csf.js +0 -139
  33. package/dist/modern/transforms/move-builtin-addons.js +0 -50
  34. package/dist/modern/transforms/storiesof-to-csf.js +0 -287
  35. package/dist/modern/transforms/update-addon-info.js +0 -94
  36. package/dist/modern/transforms/update-organisation-name.js +0 -74
  37. package/dist/modern/transforms/upgrade-hierarchy-separators.js +0 -35
  38. package/dist/ts3.4/lib/utils.d.ts +0 -2
  39. package/dist/ts3.4/transforms/csf-2-to-3.d.ts +0 -6
  40. package/dist/ts3.9/lib/utils.d.ts +0 -2
  41. package/dist/ts3.9/transforms/csf-2-to-3.d.ts +0 -6
@@ -1,196 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
-
8
- var _recast = require("recast");
9
-
10
- var _csf = require("@storybook/csf");
11
-
12
- function exportMdx(root, options) {
13
- // eslint-disable-next-line no-underscore-dangle
14
- var path = root.__paths[0]; // FIXME: insert the title as markdown after all of the imports
15
-
16
- return path.node.program.body.map(function (n) {
17
- var _prettyPrint = (0, _recast.prettyPrint)(n, options),
18
- code = _prettyPrint.code;
19
-
20
- if (n.type === 'JSXElement') {
21
- return `${code}\n`;
22
- }
23
-
24
- return code;
25
- }).join('\n');
26
- }
27
-
28
- function parseIncludeExclude(prop) {
29
- var _prettyPrint2 = (0, _recast.prettyPrint)(prop, {}),
30
- code = _prettyPrint2.code; // eslint-disable-next-line no-eval
31
-
32
-
33
- return eval(code);
34
- }
35
- /**
36
- * Convert a component's module story file into an MDX file
37
- *
38
- * For example:
39
- *
40
- * ```
41
- * input { Button } from './Button';
42
- * export default {
43
- * title: 'Button'
44
- * }
45
- * export const story = () => <Button label="The Button" />;
46
- * ```
47
- *
48
- * Becomes:
49
- *
50
- * ```
51
- * import { Meta, Story } from '@storybook/addon-docs';
52
- * input { Button } from './Button';
53
- *
54
- * <Meta title='Button' />
55
- *
56
- * <Story name='story'>
57
- * <Button label="The Button" />
58
- * </Story>
59
- * ```
60
- */
61
-
62
-
63
- function transformer(file, api) {
64
- var j = api.jscodeshift;
65
- var root = j(file.source); // FIXME: save out all the storyFn.story = { ... }
66
-
67
- var storyKeyToStory = {}; // save out includeStories / excludeStories
68
-
69
- var meta = {};
70
-
71
- function makeAttr(key, val) {
72
- return j.jsxAttribute(j.jsxIdentifier(key), val.type === 'Literal' ? val : j.jsxExpressionContainer(val));
73
- }
74
-
75
- function getStoryContents(node) {
76
- return node.type === 'ArrowFunctionExpression' && node.body.type === 'JSXElement' ? node.body : j.jsxExpressionContainer(node);
77
- }
78
-
79
- function getName(storyKey) {
80
- var story = storyKeyToStory[storyKey];
81
-
82
- if (story) {
83
- var name = story.properties.find(function (prop) {
84
- return prop.key.name === 'name';
85
- });
86
-
87
- if (name && name.value.type === 'Literal') {
88
- return name.value.value;
89
- }
90
- }
91
-
92
- return storyKey;
93
- }
94
-
95
- function getStoryAttrs(storyKey) {
96
- var attrs = [];
97
- var story = storyKeyToStory[storyKey];
98
-
99
- if (story) {
100
- story.properties.forEach(function (prop) {
101
- var key = prop.key,
102
- value = prop.value;
103
-
104
- if (key.name !== 'name') {
105
- attrs.push(makeAttr(key.name, value));
106
- }
107
- });
108
- }
109
-
110
- return attrs;
111
- } // 1. If the program does not have `export default { title: '....' }, skip it
112
-
113
-
114
- var defaultExportWithTitle = root.find(j.ExportDefaultDeclaration).filter(function (def) {
115
- return def.node.declaration.properties.map(function (p) {
116
- return p.key.name;
117
- }).includes('title');
118
- });
119
-
120
- if (defaultExportWithTitle.size() === 0) {
121
- return root.toSource();
122
- } // 2a. Add imports from '@storybook/addon-docs'
123
-
124
-
125
- 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
126
-
127
- root.find(j.ImportDeclaration).filter(function (decl) {
128
- return decl.node.source.value === 'react';
129
- }).remove(); // 3. Save out all the excluded stories
130
-
131
- defaultExportWithTitle.forEach(function (exp) {
132
- exp.node.declaration.properties.forEach(function (p) {
133
- if (['includeStories', 'excludeStories'].includes(p.key.name)) {
134
- meta[p.key.name] = parseIncludeExclude(p.value);
135
- }
136
- });
137
- }); // 4. Collect all the story exports in storyKeyToStory[key] = null;
138
-
139
- var namedExports = root.find(j.ExportNamedDeclaration);
140
- namedExports.forEach(function (exp) {
141
- var storyKey = exp.node.declaration.declarations[0].id.name;
142
-
143
- if ((0, _csf.isExportStory)(storyKey, meta)) {
144
- storyKeyToStory[storyKey] = null;
145
- }
146
- }); // 5. Collect all the storyKey.story in storyKeyToStory and also remove them
147
-
148
- var storyAssignments = root.find(j.AssignmentExpression).filter(function (exp) {
149
- var left = exp.node.left;
150
- return left.type === 'MemberExpression' && left.object.type === 'Identifier' && left.object.name in storyKeyToStory && left.property.type === 'Identifier' && left.property.name === 'story';
151
- });
152
- storyAssignments.forEach(function (exp) {
153
- var _exp$node = exp.node,
154
- left = _exp$node.left,
155
- right = _exp$node.right;
156
- storyKeyToStory[left.object.name] = right;
157
- });
158
- storyAssignments.remove(); // 6. Convert the default export to <Meta />
159
-
160
- defaultExportWithTitle.replaceWith(function (exp) {
161
- var jsxId = j.jsxIdentifier('Meta');
162
- var attrs = [];
163
- exp.node.declaration.properties.forEach(function (prop) {
164
- var key = prop.key,
165
- value = prop.value;
166
-
167
- if (!['includeStories', 'excludeStories'].includes(key.name)) {
168
- attrs.push(makeAttr(key.name, value));
169
- }
170
- });
171
- var opening = j.jsxOpeningElement(jsxId, attrs);
172
- opening.selfClosing = true;
173
- return j.jsxElement(opening);
174
- }); // 7. Convert all the named exports to <Story>...</Story>
175
-
176
- namedExports.replaceWith(function (exp) {
177
- var storyKey = exp.node.declaration.declarations[0].id.name;
178
-
179
- if (!(0, _csf.isExportStory)(storyKey, meta)) {
180
- return exp.node;
181
- }
182
-
183
- var jsxId = j.jsxIdentifier('Story');
184
- var name = getName(storyKey);
185
- var attributes = [makeAttr('name', j.literal(name)), ...getStoryAttrs(storyKey)];
186
- var opening = j.jsxOpeningElement(jsxId, attributes);
187
- var closing = j.jsxClosingElement(jsxId);
188
- var children = [getStoryContents(exp.node.declaration.declarations[0].init)];
189
- return j.jsxElement(opening, closing, children);
190
- });
191
- return exportMdx(root, {
192
- quote: 'single',
193
- trailingComma: 'true',
194
- tabWidth: 2
195
- });
196
- }
@@ -1,153 +0,0 @@
1
- "use strict";
2
-
3
- Object.defineProperty(exports, "__esModule", {
4
- value: true
5
- });
6
- exports.default = transformer;
7
-
8
- var _mdx = _interopRequireDefault(require("@mdx-js/mdx"));
9
-
10
- var _prettier = _interopRequireDefault(require("prettier"));
11
-
12
- var _utils = require("../lib/utils");
13
-
14
- function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
15
-
16
- // import recast from 'recast';
17
-
18
- /**
19
- * Convert a component's MDX file into module story format
20
- */
21
- function transformer(file, api) {
22
- var j = api.jscodeshift;
23
-
24
- var code = _mdx.default.sync(file.source, {});
25
-
26
- var root = j(code);
27
-
28
- function parseJsxAttributes(attributes) {
29
- var result = {};
30
- attributes.forEach(function (attr) {
31
- var key = attr.name.name;
32
- var val = attr.value.type === 'JSXExpressionContainer' ? attr.value.expression : attr.value;
33
- result[key] = val;
34
- });
35
- return result;
36
- }
37
-
38
- function genObjectExpression(attrs) {
39
- return j.objectExpression(Object.entries(attrs).map(function ([key, val]) {
40
- return j.property('init', j.identifier(key), val);
41
- }));
42
- }
43
-
44
- function convertToStories(path) {
45
- var base = j(path);
46
- var meta = {};
47
- var includeStories = [];
48
- var storyStatements = []; // get rid of all mdxType junk
49
-
50
- base.find(j.JSXAttribute).filter(function (attr) {
51
- return attr.node.name.name === 'mdxType';
52
- }).remove(); // parse <Meta title="..." />
53
-
54
- base.find(j.JSXElement).filter(function (elt) {
55
- return elt.node.openingElement.name.name === 'Meta';
56
- }).forEach(function (elt) {
57
- var attrs = parseJsxAttributes(elt.node.openingElement.attributes);
58
- Object.assign(meta, attrs);
59
- }); // parse <Story name="..." />
60
-
61
- base.find(j.JSXElement).filter(function (elt) {
62
- return elt.node.openingElement.name.name === 'Story';
63
- }).forEach(function (elt) {
64
- var attrs = parseJsxAttributes(elt.node.openingElement.attributes);
65
-
66
- if (attrs.name) {
67
- var storyKey = (0, _utils.sanitizeName)(attrs.name.value);
68
- includeStories.push(storyKey);
69
-
70
- if (storyKey === attrs.name.value) {
71
- delete attrs.name;
72
- }
73
-
74
- var body = elt.node.children.find(function (n) {
75
- return n.type !== 'JSXText';
76
- }) || j.literal(elt.node.children[0].value);
77
-
78
- if (body.type === 'JSXExpressionContainer') {
79
- body = body.expression;
80
- }
81
-
82
- storyStatements.push(j.exportDeclaration(false, j.variableDeclaration('const', [j.variableDeclarator(j.identifier(storyKey), body.type === 'ArrowFunctionExpression' ? body : j.arrowFunctionExpression([], body))])));
83
-
84
- if (Object.keys(attrs).length > 0) {
85
- storyStatements.push(j.assignmentStatement('=', j.memberExpression(j.identifier(storyKey), j.identifier('story')), genObjectExpression(attrs)));
86
- }
87
-
88
- storyStatements.push(j.emptyStatement());
89
- }
90
- });
91
-
92
- if (root.find(j.ExportNamedDeclaration).size() > 0) {
93
- meta.includeStories = j.arrayExpression(includeStories.map(function (key) {
94
- return j.literal(key);
95
- }));
96
- }
97
-
98
- var statements = [j.exportDefaultDeclaration(genObjectExpression(meta)), j.emptyStatement(), ...storyStatements];
99
- var lastStatement = root.find(j.Statement).at(-1);
100
- statements.reverse().forEach(function (stmt) {
101
- lastStatement.insertAfter(stmt);
102
- });
103
- base.remove();
104
- }
105
-
106
- root.find(j.ExportDefaultDeclaration).forEach(convertToStories); // strip out Story/Meta import and MDX junk
107
- // /* @jsx mdx */
108
-
109
- root.find(j.ImportDeclaration).at(0).replaceWith(function (exp) {
110
- return j.importDeclaration(exp.node.specifiers, exp.node.source);
111
- }); // import { Story, Meta } from '@storybook/addon-docs';
112
-
113
- root.find(j.ImportDeclaration).filter(function (exp) {
114
- return exp.node.source.value === '@storybook/addon-docs';
115
- }).remove(); // const makeShortcode = ...
116
- // const layoutProps = {};
117
- // const MDXLayout = 'wrapper';
118
-
119
- var MDX_DECLS = ['makeShortcode', 'layoutProps', 'MDXLayout'];
120
- root.find(j.VariableDeclaration).filter(function (decl) {
121
- return decl.node.declarations.length === 1 && MDX_DECLS.includes(decl.node.declarations[0].id.name);
122
- }).remove(); // const Source = makeShortcode('Source');
123
-
124
- root.find(j.VariableDeclarator).filter(function (expr) {
125
- return expr.node.init.type === 'CallExpression' && expr.node.init.callee.type === 'Identifier' && expr.node.init.callee.name === 'makeShortcode';
126
- }).remove(); // MDXContent.isMDXComponent = true;
127
-
128
- root.find(j.AssignmentExpression).filter(function (expr) {
129
- return expr.node.left.type === 'MemberExpression' && expr.node.left.object.type === 'Identifier' && expr.node.left.object.name === 'MDXContent';
130
- }).remove(); // Add back `import React from 'react';` which is implicit in MDX
131
-
132
- var react = root.find(j.ImportDeclaration).filter(function (decl) {
133
- return decl.node.source.value === 'react';
134
- });
135
-
136
- if (react.size() === 0) {
137
- root.find(j.Statement).at(0).insertBefore(j.importDeclaration([j.importDefaultSpecifier(j.identifier('React'))], j.literal('react')));
138
- }
139
-
140
- var source = root.toSource({
141
- trailingComma: true,
142
- quote: 'single',
143
- tabWidth: 2
144
- });
145
- return _prettier.default.format(source, {
146
- parser: 'babel',
147
- printWidth: 100,
148
- tabWidth: 2,
149
- bracketSpacing: true,
150
- trailingComma: 'es5',
151
- singleQuote: true
152
- });
153
- }
@@ -1,57 +0,0 @@
1
- "use strict";
2
-
3
- require("core-js/modules/es.symbol.description.js");
4
-
5
- Object.defineProperty(exports, "__esModule", {
6
- value: true
7
- });
8
- exports.default = transformer;
9
-
10
- function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
11
-
12
- 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."); }
13
-
14
- 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); }
15
-
16
- 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; }
17
-
18
- function _iterableToArrayLimit(arr, i) { if (typeof Symbol === "undefined" || !(Symbol.iterator in Object(arr))) return; var _arr = []; var _n = true; var _d = false; var _e = undefined; try { for (var _i = arr[Symbol.iterator](), _s; !(_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; }
19
-
20
- function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
21
-
22
- function transformer(file, api) {
23
- var j = api.jscodeshift;
24
-
25
- var createImportDeclaration = function (specifiers, source) {
26
- return j.importDeclaration(specifiers.map(function (s) {
27
- return j.importSpecifier(j.identifier(s));
28
- }), j.literal(source));
29
- };
30
-
31
- var deprecates = {
32
- action: [['action'], '@storybook/addon-actions'],
33
- linkTo: [['linkTo'], '@storybook/addon-links']
34
- };
35
- var transform = j(file.source).find(j.ImportDeclaration).filter(function (i) {
36
- return i.value.source.value === '@storybook/react';
37
- }).forEach(function (i) {
38
- var importStatement = i.value;
39
- importStatement.specifiers = importStatement.specifiers.filter(function (specifier) {
40
- var item = deprecates[specifier.local.name];
41
-
42
- if (item) {
43
- var _item = _slicedToArray(item, 2),
44
- specifiers = _item[0],
45
- moduleName = _item[1];
46
-
47
- i.insertAfter(createImportDeclaration(specifiers, moduleName));
48
- return false;
49
- }
50
-
51
- return specifier;
52
- });
53
- });
54
- return transform.toSource({
55
- quote: 'single'
56
- });
57
- }