@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.
- package/package.json +4 -4
- package/dist/cjs/index.js +0 -142
- package/dist/cjs/lib/utils.js +0 -40
- package/dist/cjs/transforms/add-component-parameters.js +0 -64
- package/dist/cjs/transforms/csf-2-to-3.js +0 -169
- 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/lib/utils.js +0 -25
- package/dist/esm/transforms/add-component-parameters.js +0 -57
- package/dist/esm/transforms/csf-2-to-3.js +0 -152
- 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/update-addon-info.js +0 -94
- package/dist/esm/transforms/update-organisation-name.js +0 -74
- package/dist/esm/transforms/upgrade-hierarchy-separators.js +0 -35
- 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 -152
- 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
- package/dist/ts3.9/lib/utils.d.ts +0 -2
- package/dist/ts3.9/transforms/csf-2-to-3.d.ts +0 -6
package/package.json
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
{
|
2
2
|
"name": "@storybook/codemod",
|
3
|
-
"version": "6.4.0-beta.
|
3
|
+
"version": "6.4.0-beta.24",
|
4
4
|
"description": "A collection of codemod scripts written with JSCodeshift",
|
5
5
|
"keywords": [
|
6
6
|
"storybook"
|
@@ -44,8 +44,8 @@
|
|
44
44
|
"@babel/types": "^7.12.11",
|
45
45
|
"@mdx-js/mdx": "^1.6.22",
|
46
46
|
"@storybook/csf": "0.0.2--canary.87bc651.0",
|
47
|
-
"@storybook/csf-tools": "6.4.0-beta.
|
48
|
-
"@storybook/node-logger": "6.4.0-beta.
|
47
|
+
"@storybook/csf-tools": "6.4.0-beta.24",
|
48
|
+
"@storybook/node-logger": "6.4.0-beta.24",
|
49
49
|
"core-js": "^3.8.2",
|
50
50
|
"cross-spawn": "^7.0.3",
|
51
51
|
"globby": "^11.0.2",
|
@@ -62,6 +62,6 @@
|
|
62
62
|
"publishConfig": {
|
63
63
|
"access": "public"
|
64
64
|
},
|
65
|
-
"gitHead": "
|
65
|
+
"gitHead": "f53ba4d7210b5c4809d57c74d7a42314172aa63e",
|
66
66
|
"sbmodern": "dist/modern/index.js"
|
67
67
|
}
|
package/dist/cjs/index.js
DELETED
@@ -1,142 +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.listCodemods = listCodemods;
|
9
|
-
exports.runCodemod = runCodemod;
|
10
|
-
Object.defineProperty(exports, "updateOrganisationName", {
|
11
|
-
enumerable: true,
|
12
|
-
get: function () {
|
13
|
-
return _updateOrganisationName.default;
|
14
|
-
}
|
15
|
-
});
|
16
|
-
Object.defineProperty(exports, "packageNames", {
|
17
|
-
enumerable: true,
|
18
|
-
get: function () {
|
19
|
-
return _updateOrganisationName.packageNames;
|
20
|
-
}
|
21
|
-
});
|
22
|
-
Object.defineProperty(exports, "updateAddonInfo", {
|
23
|
-
enumerable: true,
|
24
|
-
get: function () {
|
25
|
-
return _updateAddonInfo.default;
|
26
|
-
}
|
27
|
-
});
|
28
|
-
|
29
|
-
require("core-js/modules/es.promise.js");
|
30
|
-
|
31
|
-
var _fs = _interopRequireDefault(require("fs"));
|
32
|
-
|
33
|
-
var _path = _interopRequireDefault(require("path"));
|
34
|
-
|
35
|
-
var _util = require("util");
|
36
|
-
|
37
|
-
var _globby = _interopRequireDefault(require("globby"));
|
38
|
-
|
39
|
-
var _crossSpawn = require("cross-spawn");
|
40
|
-
|
41
|
-
var _utils = require("./lib/utils");
|
42
|
-
|
43
|
-
var _updateOrganisationName = _interopRequireWildcard(require("./transforms/update-organisation-name"));
|
44
|
-
|
45
|
-
var _updateAddonInfo = _interopRequireDefault(require("./transforms/update-addon-info"));
|
46
|
-
|
47
|
-
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
48
|
-
|
49
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
50
|
-
|
51
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
52
|
-
|
53
|
-
function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
|
54
|
-
|
55
|
-
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."); }
|
56
|
-
|
57
|
-
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); }
|
58
|
-
|
59
|
-
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; }
|
60
|
-
|
61
|
-
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; }
|
62
|
-
|
63
|
-
function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
|
64
|
-
|
65
|
-
var TRANSFORM_DIR = `${__dirname}/transforms`;
|
66
|
-
|
67
|
-
function listCodemods() {
|
68
|
-
return _fs.default.readdirSync(TRANSFORM_DIR).filter(function (fname) {
|
69
|
-
return fname.endsWith('.js');
|
70
|
-
}).map(function (fname) {
|
71
|
-
return fname.slice(0, -3);
|
72
|
-
});
|
73
|
-
}
|
74
|
-
|
75
|
-
var renameAsync = (0, _util.promisify)(_fs.default.rename);
|
76
|
-
|
77
|
-
async function renameFile(file, from, to, {
|
78
|
-
logger: logger
|
79
|
-
}) {
|
80
|
-
var newFile = file.replace(from, to);
|
81
|
-
logger.log(`Rename: ${file} ${newFile}`);
|
82
|
-
return renameAsync(file, newFile);
|
83
|
-
}
|
84
|
-
|
85
|
-
async function runCodemod(codemod, {
|
86
|
-
glob: glob,
|
87
|
-
logger: logger,
|
88
|
-
dryRun: dryRun,
|
89
|
-
rename: rename,
|
90
|
-
parser: parser
|
91
|
-
}) {
|
92
|
-
var codemods = listCodemods();
|
93
|
-
|
94
|
-
if (!codemods.includes(codemod)) {
|
95
|
-
throw new Error(`Unknown codemod ${codemod}. Run --list for options.`);
|
96
|
-
}
|
97
|
-
|
98
|
-
var renameParts = null;
|
99
|
-
|
100
|
-
if (rename) {
|
101
|
-
renameParts = rename.split(':');
|
102
|
-
|
103
|
-
if (renameParts.length !== 2) {
|
104
|
-
throw new Error(`Codemod rename: expected format "from:to", got "${rename}"`);
|
105
|
-
}
|
106
|
-
} // jscodeshift/prettier know how to handle .ts/.tsx extensions,
|
107
|
-
// so if the user uses one of those globs, we can auto-infer
|
108
|
-
|
109
|
-
|
110
|
-
var inferredParser = parser;
|
111
|
-
|
112
|
-
if (!parser) {
|
113
|
-
var extension = _path.default.extname(glob).slice(1);
|
114
|
-
|
115
|
-
var knownParser = (0, _utils.jscodeshiftToPrettierParser)(extension);
|
116
|
-
if (knownParser !== 'babel') inferredParser = extension;
|
117
|
-
}
|
118
|
-
|
119
|
-
var files = await (0, _globby.default)([glob, '!**/node_modules', '!**/dist']);
|
120
|
-
logger.log(`=> Applying ${codemod}: ${files.length} files`);
|
121
|
-
|
122
|
-
if (!dryRun) {
|
123
|
-
var parserArgs = inferredParser ? ['--parser', inferredParser] : [];
|
124
|
-
(0, _crossSpawn.sync)('npx', ['jscodeshift', '-t', `${TRANSFORM_DIR}/${codemod}.js`, ...parserArgs, ...files], {
|
125
|
-
stdio: 'inherit'
|
126
|
-
});
|
127
|
-
}
|
128
|
-
|
129
|
-
if (renameParts) {
|
130
|
-
var _renameParts = renameParts,
|
131
|
-
_renameParts2 = _slicedToArray(_renameParts, 2),
|
132
|
-
from = _renameParts2[0],
|
133
|
-
to = _renameParts2[1];
|
134
|
-
|
135
|
-
logger.log(`=> Renaming ${rename}: ${files.length} files`);
|
136
|
-
await Promise.all(files.map(function (file) {
|
137
|
-
return renameFile(file, new RegExp(`${from}$`), to, {
|
138
|
-
logger: logger
|
139
|
-
});
|
140
|
-
}));
|
141
|
-
}
|
142
|
-
}
|
package/dist/cjs/lib/utils.js
DELETED
@@ -1,40 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.jscodeshiftToPrettierParser = jscodeshiftToPrettierParser;
|
7
|
-
exports.sanitizeName = void 0;
|
8
|
-
|
9
|
-
var _camelCase = _interopRequireDefault(require("lodash/camelCase"));
|
10
|
-
|
11
|
-
var _upperFirst = _interopRequireDefault(require("lodash/upperFirst"));
|
12
|
-
|
13
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
14
|
-
|
15
|
-
var sanitizeName = function (name) {
|
16
|
-
var key = (0, _upperFirst.default)((0, _camelCase.default)(name)); // prepend _ if name starts with a digit
|
17
|
-
|
18
|
-
if (/^\d/.test(key)) {
|
19
|
-
key = `_${key}`;
|
20
|
-
} // prepend _ if name starts with a digit
|
21
|
-
|
22
|
-
|
23
|
-
if (/^\d/.test(key)) {
|
24
|
-
key = `_${key}`;
|
25
|
-
}
|
26
|
-
|
27
|
-
return key;
|
28
|
-
};
|
29
|
-
|
30
|
-
exports.sanitizeName = sanitizeName;
|
31
|
-
|
32
|
-
function jscodeshiftToPrettierParser(parser) {
|
33
|
-
var parserMap = {
|
34
|
-
babylon: 'babel',
|
35
|
-
flow: 'flow',
|
36
|
-
ts: 'typescript',
|
37
|
-
tsx: 'typescript'
|
38
|
-
};
|
39
|
-
return parserMap[parser] || 'babel';
|
40
|
-
}
|
@@ -1,64 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = transformer;
|
7
|
-
|
8
|
-
/**
|
9
|
-
* Adds a `component` parameter for each storiesOf(...) call.
|
10
|
-
*
|
11
|
-
* For example:
|
12
|
-
*
|
13
|
-
* input { Button } from './Button';
|
14
|
-
* storiesOf('Button', module).add('story', () => <Button label="The Button" />);
|
15
|
-
*
|
16
|
-
* Becomes:
|
17
|
-
*
|
18
|
-
* input { Button } from './Button';
|
19
|
-
* storiesOf('Button', module)
|
20
|
-
* .addParameters({ component: Button })
|
21
|
-
* .add('story', () => <Button label="The Button" />);
|
22
|
-
*
|
23
|
-
* Heuristics:
|
24
|
-
* - The storiesOf "kind" name must be Button
|
25
|
-
* - Button must be imported in the file
|
26
|
-
*/
|
27
|
-
function transformer(file, api) {
|
28
|
-
var j = api.jscodeshift;
|
29
|
-
var root = j(file.source); // Create a dictionary whose keys are all the named imports in the file.
|
30
|
-
// For instance:
|
31
|
-
//
|
32
|
-
// import foo from 'foo';
|
33
|
-
// import { bar, baz } from 'zoo';
|
34
|
-
//
|
35
|
-
// => { foo: true, bar: true, baz: true }
|
36
|
-
|
37
|
-
var importMap = {};
|
38
|
-
root.find(j.ImportDeclaration).forEach(function (imp) {
|
39
|
-
return imp.node.specifiers.forEach(function (spec) {
|
40
|
-
importMap[spec.local.name] = true;
|
41
|
-
});
|
42
|
-
});
|
43
|
-
|
44
|
-
function getLeafName(string) {
|
45
|
-
var parts = string.split(/\/|\.|\|/);
|
46
|
-
return parts[parts.length - 1];
|
47
|
-
}
|
48
|
-
|
49
|
-
function addComponentParameter(call) {
|
50
|
-
var node = call.node;
|
51
|
-
var leafName = getLeafName(node.arguments[0].value);
|
52
|
-
return j.callExpression(j.memberExpression(node, j.identifier('addParameters')), [j.objectExpression([j.property('init', j.identifier('component'), j.identifier(leafName))])]);
|
53
|
-
}
|
54
|
-
|
55
|
-
var storiesOfCalls = root.find(j.CallExpression).filter(function (call) {
|
56
|
-
return call.node.callee.name === 'storiesOf';
|
57
|
-
}).filter(function (call) {
|
58
|
-
return call.node.arguments.length > 0 && call.node.arguments[0].type === 'Literal';
|
59
|
-
}).filter(function (call) {
|
60
|
-
var leafName = getLeafName(call.node.arguments[0].value);
|
61
|
-
return importMap[leafName];
|
62
|
-
}).replaceWith(addComponentParameter);
|
63
|
-
return root.toSource();
|
64
|
-
}
|
@@ -1,169 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = void 0;
|
7
|
-
|
8
|
-
var _prettier = _interopRequireDefault(require("prettier"));
|
9
|
-
|
10
|
-
var t = _interopRequireWildcard(require("@babel/types"));
|
11
|
-
|
12
|
-
var _csfTools = require("@storybook/csf-tools");
|
13
|
-
|
14
|
-
var _utils = require("../lib/utils");
|
15
|
-
|
16
|
-
function _getRequireWildcardCache() { if (typeof WeakMap !== "function") return null; var cache = new WeakMap(); _getRequireWildcardCache = function () { return cache; }; return cache; }
|
17
|
-
|
18
|
-
function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } if (obj === null || typeof obj !== "object" && typeof obj !== "function") { return { default: obj }; } var cache = _getRequireWildcardCache(); if (cache && cache.has(obj)) { return cache.get(obj); } var newObj = {}; var hasPropertyDescriptor = Object.defineProperty && Object.getOwnPropertyDescriptor; for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { var desc = hasPropertyDescriptor ? Object.getOwnPropertyDescriptor(obj, key) : null; if (desc && (desc.get || desc.set)) { Object.defineProperty(newObj, key, desc); } else { newObj[key] = obj[key]; } } } newObj.default = obj; if (cache) { cache.set(obj, newObj); } return newObj; }
|
19
|
-
|
20
|
-
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
21
|
-
|
22
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }
|
23
|
-
|
24
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }
|
25
|
-
|
26
|
-
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; }
|
27
|
-
|
28
|
-
var logger = console;
|
29
|
-
|
30
|
-
var _rename = function (annotation) {
|
31
|
-
return annotation === 'storyName' ? 'name' : annotation;
|
32
|
-
};
|
33
|
-
|
34
|
-
var getTemplateBindVariable = function (init) {
|
35
|
-
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;
|
36
|
-
}; // export const A = ...
|
37
|
-
// A.parameters = { ... }; <===
|
38
|
-
|
39
|
-
|
40
|
-
var isStoryAnnotation = function (stmt, objectExports) {
|
41
|
-
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];
|
42
|
-
};
|
43
|
-
|
44
|
-
var isTemplateDeclaration = function (stmt, templates) {
|
45
|
-
return t.isVariableDeclaration(stmt) && stmt.declarations.length === 1 && t.isIdentifier(stmt.declarations[0].id) && templates[stmt.declarations[0].id.name];
|
46
|
-
};
|
47
|
-
|
48
|
-
var getNewExport = function (stmt, objectExports) {
|
49
|
-
if (t.isExportNamedDeclaration(stmt) && t.isVariableDeclaration(stmt.declaration) && stmt.declaration.declarations.length === 1) {
|
50
|
-
var decl = stmt.declaration.declarations[0];
|
51
|
-
|
52
|
-
if (t.isVariableDeclarator(decl) && t.isIdentifier(decl.id)) {
|
53
|
-
return objectExports[decl.id.name];
|
54
|
-
}
|
55
|
-
}
|
56
|
-
|
57
|
-
return null;
|
58
|
-
}; // Remove render function when it matches the global render function in react
|
59
|
-
// export default { component: Cat };
|
60
|
-
// export const A = (args) => <Cat {...args} />;
|
61
|
-
|
62
|
-
|
63
|
-
var isReactGlobalRenderFn = function (csf, storyFn) {
|
64
|
-
var _csf$_meta;
|
65
|
-
|
66
|
-
if ((_csf$_meta = csf._meta) !== null && _csf$_meta !== void 0 && _csf$_meta.component && t.isArrowFunctionExpression(storyFn) && storyFn.params.length === 1 && t.isJSXElement(storyFn.body)) {
|
67
|
-
var openingElement = storyFn.body.openingElement;
|
68
|
-
|
69
|
-
if (openingElement.selfClosing && t.isJSXIdentifier(openingElement.name) && openingElement.attributes.length === 1) {
|
70
|
-
var attr = openingElement.attributes[0];
|
71
|
-
var param = storyFn.params[0];
|
72
|
-
|
73
|
-
if (t.isJSXSpreadAttribute(attr) && t.isIdentifier(attr.argument) && t.isIdentifier(param) && param.name === attr.argument.name && csf._meta.component === openingElement.name.name) {
|
74
|
-
return true;
|
75
|
-
}
|
76
|
-
}
|
77
|
-
}
|
78
|
-
|
79
|
-
return false;
|
80
|
-
}; // A simple CSF story is a no-arg story without any extra annotations (params, args, etc.)
|
81
|
-
|
82
|
-
|
83
|
-
var isSimpleCSFStory = function (init, annotations) {
|
84
|
-
return annotations.length === 0 && t.isArrowFunctionExpression(init) && init.params.length === 0;
|
85
|
-
};
|
86
|
-
|
87
|
-
function transform({
|
88
|
-
source: source
|
89
|
-
}, api, options) {
|
90
|
-
var csf = (0, _csfTools.loadCsf)(source, {
|
91
|
-
defaultTitle: 'FIXME'
|
92
|
-
});
|
93
|
-
|
94
|
-
try {
|
95
|
-
csf.parse();
|
96
|
-
} catch (err) {
|
97
|
-
logger.log(`Error ${err}, skipping`);
|
98
|
-
return source;
|
99
|
-
}
|
100
|
-
|
101
|
-
var objectExports = {};
|
102
|
-
Object.entries(csf._storyExports).forEach(function ([key, decl]) {
|
103
|
-
var annotations = Object.entries(csf._storyAnnotations[key]).map(function ([annotation, val]) {
|
104
|
-
return t.objectProperty(t.identifier(_rename(annotation)), val);
|
105
|
-
});
|
106
|
-
var init = decl.init,
|
107
|
-
id = decl.id; // only replace arrow function expressions && template
|
108
|
-
// ignore no-arg stories without annotations
|
109
|
-
|
110
|
-
var template = getTemplateBindVariable(init);
|
111
|
-
|
112
|
-
if (!t.isArrowFunctionExpression(init) && !template || isSimpleCSFStory(init, annotations)) {
|
113
|
-
return;
|
114
|
-
} // Remove the render function when we can hoist the template
|
115
|
-
// const Template = (args) => <Cat {...args} />;
|
116
|
-
// export const A = Template.bind({});
|
117
|
-
|
118
|
-
|
119
|
-
var storyFn = template && csf._templates[template];
|
120
|
-
if (!storyFn) storyFn = init;
|
121
|
-
var keyId = t.identifier(key); // @ts-ignore
|
122
|
-
|
123
|
-
var typeAnnotation = id.typeAnnotation;
|
124
|
-
|
125
|
-
if (typeAnnotation) {
|
126
|
-
keyId.typeAnnotation = typeAnnotation;
|
127
|
-
}
|
128
|
-
|
129
|
-
var renderAnnotation = isReactGlobalRenderFn(csf, storyFn) ? [] : [t.objectProperty(t.identifier('render'), storyFn)];
|
130
|
-
objectExports[key] = t.exportNamedDeclaration(t.variableDeclaration('const', [t.variableDeclarator(keyId, t.objectExpression([...renderAnnotation, ...annotations]))]));
|
131
|
-
});
|
132
|
-
|
133
|
-
var updatedBody = csf._ast.program.body.reduce(function (acc, stmt) {
|
134
|
-
// remove story annotations & template declarations
|
135
|
-
if (isStoryAnnotation(stmt, objectExports) || isTemplateDeclaration(stmt, csf._templates)) {
|
136
|
-
return acc;
|
137
|
-
} // replace story exports with new object exports
|
138
|
-
|
139
|
-
|
140
|
-
var newExport = getNewExport(stmt, objectExports);
|
141
|
-
|
142
|
-
if (newExport) {
|
143
|
-
acc.push(newExport);
|
144
|
-
return acc;
|
145
|
-
} // include unknown statements
|
146
|
-
|
147
|
-
|
148
|
-
acc.push(stmt);
|
149
|
-
return acc;
|
150
|
-
}, []);
|
151
|
-
|
152
|
-
csf._ast.program.body = updatedBody;
|
153
|
-
var output = (0, _csfTools.formatCsf)(csf);
|
154
|
-
var prettierConfig = _prettier.default.resolveConfig.sync('.', {
|
155
|
-
editorconfig: true
|
156
|
-
}) || {
|
157
|
-
printWidth: 100,
|
158
|
-
tabWidth: 2,
|
159
|
-
bracketSpacing: true,
|
160
|
-
trailingComma: 'es5',
|
161
|
-
singleQuote: true
|
162
|
-
};
|
163
|
-
return _prettier.default.format(output, _objectSpread(_objectSpread({}, prettierConfig), {}, {
|
164
|
-
parser: (0, _utils.jscodeshiftToPrettierParser)(options === null || options === void 0 ? void 0 : options.parser)
|
165
|
-
}));
|
166
|
-
}
|
167
|
-
|
168
|
-
var _default = transform;
|
169
|
-
exports.default = _default;
|
@@ -1,93 +0,0 @@
|
|
1
|
-
"use strict";
|
2
|
-
|
3
|
-
Object.defineProperty(exports, "__esModule", {
|
4
|
-
value: true
|
5
|
-
});
|
6
|
-
exports.default = transformer;
|
7
|
-
|
8
|
-
var getContainingStatement = function (n) {
|
9
|
-
if (n.node.type.endsWith('Statement')) {
|
10
|
-
return n;
|
11
|
-
}
|
12
|
-
|
13
|
-
return getContainingStatement(n.parent);
|
14
|
-
};
|
15
|
-
/**
|
16
|
-
* Hoist CSF .story annotations
|
17
|
-
*
|
18
|
-
* For example:
|
19
|
-
*
|
20
|
-
* ```
|
21
|
-
* export const Basic = () => <Button />
|
22
|
-
* Basic.story = {
|
23
|
-
* name: 'foo',
|
24
|
-
* parameters: { ... },
|
25
|
-
* decorators = [ ... ],
|
26
|
-
* };
|
27
|
-
* ```
|
28
|
-
*
|
29
|
-
* Becomes:
|
30
|
-
*
|
31
|
-
* ```
|
32
|
-
* export const Basic = () => <Button />
|
33
|
-
* Basic.storyName = 'foo';
|
34
|
-
* Basic.parameters = { ... };
|
35
|
-
* Basic.decorators = [ ... ];
|
36
|
-
* ```
|
37
|
-
*/
|
38
|
-
|
39
|
-
|
40
|
-
function transformer(file, api) {
|
41
|
-
var j = api.jscodeshift;
|
42
|
-
var root = j(file.source);
|
43
|
-
|
44
|
-
var renameKey = function (exp) {
|
45
|
-
return exp.type === 'Identifier' && exp.name === 'name' ? j.identifier('storyName') : exp;
|
46
|
-
}; // 1. If the program does not have `export default { title: '....' }, skip it
|
47
|
-
|
48
|
-
|
49
|
-
var defaultExportWithTitle = root.find(j.ExportDefaultDeclaration).filter(function (def) {
|
50
|
-
return def.node.declaration.type === 'ObjectExpression' && def.node.declaration.properties.map(function (p) {
|
51
|
-
return p.key.name;
|
52
|
-
}).includes('title');
|
53
|
-
});
|
54
|
-
|
55
|
-
if (defaultExportWithTitle.size() === 0) {
|
56
|
-
return root.toSource();
|
57
|
-
} // 2. Replace each Foo.story = { x: xVal } with Foo.x = xVal;
|
58
|
-
|
59
|
-
|
60
|
-
var storyAssignments = root.find(j.AssignmentExpression).filter(function (exp) {
|
61
|
-
var _exp$node = exp.node,
|
62
|
-
left = _exp$node.left,
|
63
|
-
right = _exp$node.right;
|
64
|
-
return left.type === 'MemberExpression' && left.object.type === 'Identifier' && left.property.type === 'Identifier' && left.property.name === 'story' && right.type === 'ObjectExpression';
|
65
|
-
});
|
66
|
-
storyAssignments.forEach(function (exp) {
|
67
|
-
var _exp$node2 = exp.node,
|
68
|
-
left = _exp$node2.left,
|
69
|
-
right = _exp$node2.right;
|
70
|
-
right.properties.forEach(function (prop) {
|
71
|
-
var stmt = getContainingStatement(exp);
|
72
|
-
stmt.insertBefore(j.assignmentStatement('=', j.memberExpression(left.object, renameKey(prop.key)), prop.value));
|
73
|
-
});
|
74
|
-
}); // 3. Remove the .story annotations
|
75
|
-
|
76
|
-
storyAssignments.remove(); // 4. Replace each Foo.story.x with Foo.x;
|
77
|
-
|
78
|
-
var storyOverrides = root.find(j.AssignmentExpression).filter(function (exp) {
|
79
|
-
var left = exp.node.left;
|
80
|
-
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)
|
81
|
-
;
|
82
|
-
});
|
83
|
-
storyOverrides.replaceWith(function (exp) {
|
84
|
-
var _exp$node3 = exp.node,
|
85
|
-
left = _exp$node3.left,
|
86
|
-
right = _exp$node3.right;
|
87
|
-
return j.assignmentExpression('=', j.memberExpression(left.object.object, renameKey(left.property)), right);
|
88
|
-
}); // 4. Render the updated tree
|
89
|
-
|
90
|
-
return root.toSource({
|
91
|
-
quote: 'single'
|
92
|
-
});
|
93
|
-
}
|