@storybook/codemod 7.4.0-alpha.0 → 7.4.0-alpha.2

Sign up to get free protection for your applications and to get access to all the features.
Files changed (95) hide show
  1. package/package.json +11 -4
  2. package/project.json +0 -6
  3. package/src/index.ts +0 -103
  4. package/src/lib/utils.test.js +0 -9
  5. package/src/lib/utils.ts +0 -29
  6. package/src/transforms/__testfixtures__/add-component-parameters/add-component-parameters.input.js +0 -44
  7. package/src/transforms/__testfixtures__/add-component-parameters/add-component-parameters.output.snapshot +0 -68
  8. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/basic.input.js +0 -25
  9. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/basic.output.snapshot +0 -27
  10. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/overrides.input.js +0 -25
  11. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/overrides.output.snapshot +0 -28
  12. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/variable.input.js +0 -13
  13. package/src/transforms/__testfixtures__/csf-hoist-story-annotations/variable.output.snapshot +0 -17
  14. package/src/transforms/__testfixtures__/mdx-to-csf/basic.input.mdx +0 -18
  15. package/src/transforms/__testfixtures__/mdx-to-csf/basic.output.snapshot +0 -40
  16. package/src/transforms/__testfixtures__/mdx-to-csf/component-id.input.mdx +0 -6
  17. package/src/transforms/__testfixtures__/mdx-to-csf/component-id.output.snapshot +0 -17
  18. package/src/transforms/__testfixtures__/mdx-to-csf/decorators.input.mdx +0 -8
  19. package/src/transforms/__testfixtures__/mdx-to-csf/decorators.output.snapshot +0 -18
  20. package/src/transforms/__testfixtures__/mdx-to-csf/exclude-stories.input.mdx +0 -19
  21. package/src/transforms/__testfixtures__/mdx-to-csf/exclude-stories.output.snapshot +0 -39
  22. package/src/transforms/__testfixtures__/mdx-to-csf/parameters.input.mdx +0 -14
  23. package/src/transforms/__testfixtures__/mdx-to-csf/parameters.output.snapshot +0 -23
  24. package/src/transforms/__testfixtures__/mdx-to-csf/plaintext.input.mdx +0 -3
  25. package/src/transforms/__testfixtures__/mdx-to-csf/plaintext.output.snapshot +0 -11
  26. package/src/transforms/__testfixtures__/mdx-to-csf/story-function.input.mdx +0 -10
  27. package/src/transforms/__testfixtures__/mdx-to-csf/story-function.output.snapshot +0 -18
  28. package/src/transforms/__testfixtures__/mdx-to-csf/story-parameters.input.mdx +0 -18
  29. package/src/transforms/__testfixtures__/mdx-to-csf/story-parameters.output.snapshot +0 -32
  30. package/src/transforms/__testfixtures__/mdx-to-csf/story-refs.input.mdx +0 -22
  31. package/src/transforms/__testfixtures__/mdx-to-csf/story-refs.output.snapshot +0 -34
  32. package/src/transforms/__testfixtures__/move-builtin-addons/default.input.js +0 -2
  33. package/src/transforms/__testfixtures__/move-builtin-addons/default.output.snapshot +0 -8
  34. package/src/transforms/__testfixtures__/move-builtin-addons/with-no-change.input.js +0 -3
  35. package/src/transforms/__testfixtures__/move-builtin-addons/with-no-change.output.snapshot +0 -7
  36. package/src/transforms/__testfixtures__/storiesof-to-csf/basic.input.js +0 -18
  37. package/src/transforms/__testfixtures__/storiesof-to-csf/basic.output.snapshot +0 -45
  38. package/src/transforms/__testfixtures__/storiesof-to-csf/collision.input.js +0 -11
  39. package/src/transforms/__testfixtures__/storiesof-to-csf/collision.output.snapshot +0 -38
  40. package/src/transforms/__testfixtures__/storiesof-to-csf/const.input.js +0 -1
  41. package/src/transforms/__testfixtures__/storiesof-to-csf/const.output.snapshot +0 -13
  42. package/src/transforms/__testfixtures__/storiesof-to-csf/decorators.input.js +0 -9
  43. package/src/transforms/__testfixtures__/storiesof-to-csf/decorators.output.snapshot +0 -18
  44. package/src/transforms/__testfixtures__/storiesof-to-csf/default.input.js +0 -7
  45. package/src/transforms/__testfixtures__/storiesof-to-csf/default.output.snapshot +0 -17
  46. package/src/transforms/__testfixtures__/storiesof-to-csf/digit.input.js +0 -1
  47. package/src/transforms/__testfixtures__/storiesof-to-csf/digit.output.js +0 -5
  48. package/src/transforms/__testfixtures__/storiesof-to-csf/digit.output.snapshot +0 -9
  49. package/src/transforms/__testfixtures__/storiesof-to-csf/export-destructuring.input.js +0 -11
  50. package/src/transforms/__testfixtures__/storiesof-to-csf/export-destructuring.output.snapshot +0 -23
  51. package/src/transforms/__testfixtures__/storiesof-to-csf/export-function.input.js +0 -12
  52. package/src/transforms/__testfixtures__/storiesof-to-csf/export-function.output.snapshot +0 -23
  53. package/src/transforms/__testfixtures__/storiesof-to-csf/export-names.input.js +0 -14
  54. package/src/transforms/__testfixtures__/storiesof-to-csf/export-names.output.snapshot +0 -26
  55. package/src/transforms/__testfixtures__/storiesof-to-csf/exports.input.js +0 -2
  56. package/src/transforms/__testfixtures__/storiesof-to-csf/exports.output.snapshot +0 -16
  57. package/src/transforms/__testfixtures__/storiesof-to-csf/module.input.js +0 -12
  58. package/src/transforms/__testfixtures__/storiesof-to-csf/module.output.snapshot +0 -16
  59. package/src/transforms/__testfixtures__/storiesof-to-csf/multi.input.js +0 -14
  60. package/src/transforms/__testfixtures__/storiesof-to-csf/multi.output.snapshot +0 -39
  61. package/src/transforms/__testfixtures__/storiesof-to-csf/parameters-as-var.input.js +0 -8
  62. package/src/transforms/__testfixtures__/storiesof-to-csf/parameters-as-var.output.snapshot +0 -20
  63. package/src/transforms/__testfixtures__/storiesof-to-csf/parameters.input.js +0 -10
  64. package/src/transforms/__testfixtures__/storiesof-to-csf/parameters.output.snapshot +0 -23
  65. package/src/transforms/__testfixtures__/storiesof-to-csf/storiesof-var.input.js +0 -11
  66. package/src/transforms/__testfixtures__/storiesof-to-csf/storiesof-var.output.snapshot +0 -23
  67. package/src/transforms/__testfixtures__/storiesof-to-csf/story-decorators.input.js +0 -11
  68. package/src/transforms/__testfixtures__/storiesof-to-csf/story-decorators.output.snapshot +0 -29
  69. package/src/transforms/__testfixtures__/storiesof-to-csf/story-parameters.input.js +0 -14
  70. package/src/transforms/__testfixtures__/storiesof-to-csf/story-parameters.output.snapshot +0 -32
  71. package/src/transforms/__testfixtures__/update-addon-info/update-addon-info.input.js +0 -184
  72. package/src/transforms/__testfixtures__/update-addon-info/update-addon-info.output.snapshot +0 -184
  73. package/src/transforms/__testfixtures__/update-organisation-name/update-organisation-name.input.js +0 -19
  74. package/src/transforms/__testfixtures__/update-organisation-name/update-organisation-name.output.snapshot +0 -23
  75. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/csf.input.js +0 -3
  76. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/csf.output.snapshot +0 -7
  77. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/dynamic-storiesof.input.js +0 -5
  78. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/dynamic-storiesof.output.snapshot +0 -9
  79. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/storiesof.input.js +0 -8
  80. package/src/transforms/__testfixtures__/upgrade-hierarchy-separators/storiesof.output.snapshot +0 -12
  81. package/src/transforms/__tests__/csf-2-to-3.test.ts +0 -439
  82. package/src/transforms/__tests__/mdx-to-csf.test.ts +0 -628
  83. package/src/transforms/__tests__/transforms.tests.js +0 -32
  84. package/src/transforms/__tests__/upgrade-deprecated-types.test.ts +0 -170
  85. package/src/transforms/add-component-parameters.js +0 -62
  86. package/src/transforms/csf-2-to-3.ts +0 -335
  87. package/src/transforms/csf-hoist-story-annotations.js +0 -97
  88. package/src/transforms/mdx-to-csf.ts +0 -340
  89. package/src/transforms/move-builtin-addons.js +0 -32
  90. package/src/transforms/storiesof-to-csf.js +0 -277
  91. package/src/transforms/update-addon-info.js +0 -114
  92. package/src/transforms/update-organisation-name.js +0 -71
  93. package/src/transforms/upgrade-deprecated-types.ts +0 -142
  94. package/src/transforms/upgrade-hierarchy-separators.js +0 -39
  95. package/tsconfig.json +0 -10
@@ -1,114 +0,0 @@
1
- /**
2
- * Takes the deprecated addon-info API, addWithInfo, and
3
- * converts to the new withInfo API.
4
- *
5
- * Example of deprecated addWithInfo API:
6
- *
7
- * storiesOf('Button')
8
- * .addWithInfo(
9
- * 'story name',
10
- * 'Story description.',
11
- * () => (
12
- * <Button label="The Button" />
13
- * )
14
- * )
15
- *
16
- * Converts to the new withInfo API:
17
- *
18
- * storiesOf('Button')
19
- * .add('story name', withInfo(
20
- * 'Story description.'
21
- * )(() => (
22
- * <Button label="The Button" />
23
- * )))
24
- */
25
- export default function transformer(file, api) {
26
- const j = api.jscodeshift;
27
- const root = j(file.source);
28
-
29
- /**
30
- * Returns a list of parameters for the withInfo function. The contents
31
- * of this list is either the second argument from the original
32
- * addWithInfo function, if no additional options were used, or a
33
- * combined object of all the options from the original function.
34
- * @param {list} args - original addWithInfo function parameters
35
- * @return {list} the modified list of parameters for the new function
36
- */
37
- const getOptions = (args) => {
38
- if (args[3] === undefined) {
39
- if (args[2] === undefined) {
40
- // when the optional description string is not supplied for addWithInfo, use story name
41
- return [args[0]];
42
- }
43
- return [args[1]];
44
- }
45
- return [
46
- j.objectExpression([
47
- j.property('init', j.identifier('text'), args[1]),
48
- ...args[3].properties,
49
- ]),
50
- ];
51
- };
52
-
53
- /**
54
- * Constructs the new withInfo function from the parameters of the
55
- * original addWithInfo function.
56
- * @param {CallExpression} addWithInfoExpression - original function
57
- * @returns {CallExpression} the new withInfo function
58
- */
59
- const withInfo = (addWithInfoExpression) => {
60
- const { node } = addWithInfoExpression;
61
- const args = node.arguments;
62
-
63
- // if optional description string is not supplied, the story component becomes second arg
64
- const storyComponent = args[2] ? args[2] : args[1];
65
-
66
- node.callee.property.name = 'add';
67
- node.arguments = [
68
- args[0],
69
- j.callExpression(j.callExpression(j.identifier('withInfo'), getOptions(args)), [
70
- storyComponent,
71
- ]),
72
- ];
73
-
74
- return node;
75
- };
76
-
77
- /**
78
- * Checks for - import { withInfo } from "@storybook/addon-info";
79
- * Adds the import if necessary.
80
- */
81
- const checkWithInfoImport = () => {
82
- const importExists = root
83
- .find(j.ImportDeclaration)
84
- .filter((imp) => imp.node.source.value === '@storybook/addon-info')
85
- .size();
86
-
87
- if (importExists) return;
88
-
89
- root
90
- .find(j.ImportDeclaration)
91
- .at(-1)
92
- .insertAfter(
93
- j.importDeclaration(
94
- [j.importSpecifier(j.identifier('withInfo'))],
95
- j.literal('@storybook/addon-info')
96
- )
97
- );
98
- };
99
-
100
- const addWithInfoExpressions = root.find(j.CallExpression, {
101
- callee: {
102
- property: {
103
- name: 'addWithInfo',
104
- },
105
- },
106
- });
107
-
108
- if (addWithInfoExpressions.size()) {
109
- checkWithInfoImport();
110
- addWithInfoExpressions.replaceWith(withInfo);
111
- }
112
-
113
- return root.toSource();
114
- }
@@ -1,71 +0,0 @@
1
- export const packageNames = {
2
- '@kadira/react-storybook-decorator-centered': '@storybook/addon-centered',
3
- '@kadira/storybook-addons': '@storybook/preview-api',
4
- '@kadira/storybook-addon-actions': '@storybook/addon-actions',
5
- '@kadira/storybook-addon-comments': '@storybook/addon-comments',
6
- '@kadira/storybook-addon-graphql': '@storybook/addon-graphql',
7
- '@kadira/storybook-addon-info': '@storybook/addon-info',
8
- '@kadira/storybook-addon-knobs': '@storybook/addon-knobs',
9
- '@kadira/storybook-addon-links': '@storybook/addon-links',
10
- '@kadira/storybook-addon-notes': '@storybook/addon-notes',
11
- '@kadira/storybook-addon-options': '@storybook/addon-options',
12
- '@kadira/storybook-channels': '@storybook/channels',
13
- '@kadira/storybook-channel-postmsg': '@storybook/channel-postmessage',
14
- '@kadira/storybook-channel-websocket': '@storybook/channel-websocket',
15
- '@kadira/storybook-ui': '@storybook/manager',
16
- '@kadira/react-native-storybook': '@storybook/react-native',
17
- '@kadira/react-storybook': '@storybook/react',
18
- '@kadira/getstorybook': '@storybook/cli',
19
- '@kadira/storybook': '@storybook/react',
20
- storyshots: '@storybook/addon-storyshots',
21
- getstorybook: '@storybook/cli',
22
- };
23
-
24
- export default function transformer(file, api) {
25
- const j = api.jscodeshift;
26
-
27
- const packageNamesKeys = Object.keys(packageNames);
28
-
29
- /**
30
- * Checks whether the node value matches a Storybook package
31
- * @param {string} the import declaration node
32
- * @returns {string} whether the node value matches a Storybook package
33
- */
34
- const getMatch = (oldpart) => packageNamesKeys.find((newpart) => oldpart.match(newpart));
35
-
36
- /**
37
- * Returns the name of the Storybook packages with the organisation name,
38
- * replacing the old `@kadira/` prefix.
39
- * @param {string} oldPackageName the name of the old package
40
- * @return {string} the new package name
41
- * @example
42
- * // returns '@storybook/storybook'
43
- * getNewPackageName('@kadira/storybook')
44
- */
45
- const getNewPackageName = (oldPackageName) => {
46
- const match = getMatch(oldPackageName);
47
-
48
- if (match) {
49
- const replacement = packageNames[match];
50
- return oldPackageName.replace(match, replacement);
51
- }
52
- return oldPackageName;
53
- };
54
-
55
- /**
56
- * updatePackageName - updates the source name of the Storybook packages
57
- * @param {ImportDeclaration} declaration the import declaration
58
- * @returns {ImportDeclaration.Node} the import declaration node
59
- */
60
- const updatePackageName = (declaration) => {
61
- // eslint-disable-next-line no-param-reassign
62
- declaration.node.source.value = getNewPackageName(declaration.node.source.value);
63
-
64
- return declaration.node;
65
- };
66
-
67
- return j(file.source)
68
- .find(j.ImportDeclaration)
69
- .replaceWith(updatePackageName)
70
- .toSource({ quote: 'single' });
71
- }
@@ -1,142 +0,0 @@
1
- /* eslint-disable no-underscore-dangle */
2
- import prettier from 'prettier';
3
- import type { API, FileInfo } from 'jscodeshift';
4
- import type { BabelFile, NodePath } from '@babel/core';
5
- import * as babel from '@babel/core';
6
- import { loadCsf, printCsf } from '@storybook/csf-tools';
7
- import * as t from '@babel/types';
8
-
9
- const logger = console;
10
-
11
- const deprecatedTypes = [
12
- 'ComponentStory',
13
- 'ComponentStoryFn',
14
- 'ComponentStoryObj',
15
- 'ComponentMeta',
16
- 'Story',
17
- ];
18
-
19
- function migrateType(oldType: string) {
20
- if (oldType === 'Story' || oldType === 'ComponentStory') return 'StoryFn';
21
- return oldType.replace('Component', '');
22
- }
23
-
24
- export default function transform(info: FileInfo, api: API, options: { parser?: string }) {
25
- // TODO what do I need to with the title?
26
- const csf = loadCsf(info.source, { makeTitle: (title) => title });
27
- const fileNode = csf._ast;
28
- // @ts-expect-error File is not yet exposed, see https://github.com/babel/babel/issues/11350#issuecomment-644118606
29
- const file: BabelFile = new babel.File(
30
- { filename: info.path },
31
- { code: info.source, ast: fileNode }
32
- );
33
-
34
- upgradeDeprecatedTypes(file);
35
-
36
- let output = printCsf(csf).code;
37
-
38
- try {
39
- const prettierConfig = prettier.resolveConfig.sync('.', { editorconfig: true }) || {
40
- printWidth: 100,
41
- tabWidth: 2,
42
- bracketSpacing: true,
43
- trailingComma: 'es5',
44
- singleQuote: true,
45
- };
46
-
47
- output = prettier.format(output, { ...prettierConfig, filepath: info.path });
48
- } catch (e) {
49
- logger.log(`Failed applying prettier to ${info.path}.`);
50
- }
51
-
52
- return output;
53
- }
54
-
55
- export const parser = 'tsx';
56
-
57
- export function upgradeDeprecatedTypes(file: BabelFile) {
58
- const importedNamespaces: Set<string> = new Set();
59
- const typeReferencesToUpdate: Set<string> = new Set();
60
- const existingImports: { name: string; isAlias: boolean; path: NodePath }[] = [];
61
-
62
- file.path.traverse({
63
- ImportDeclaration: (path) => {
64
- existingImports.push(
65
- ...path.get('specifiers').map((specifier) => ({
66
- name: specifier.node.local.name,
67
- isAlias: !(
68
- specifier.isImportSpecifier() &&
69
- t.isIdentifier(specifier.node.imported) &&
70
- specifier.node.local.name === specifier.node.imported.name
71
- ),
72
- path: specifier,
73
- }))
74
- );
75
-
76
- const source = path.node.source.value;
77
- if (!source.startsWith('@storybook')) return;
78
-
79
- path.get('specifiers').forEach((specifier) => {
80
- if (specifier.isImportNamespaceSpecifier()) {
81
- importedNamespaces.add(specifier.node.local.name);
82
- }
83
- if (!specifier.isImportSpecifier()) return;
84
- const imported = specifier.get('imported');
85
- if (!imported.isIdentifier()) return;
86
-
87
- // if we find a deprecated import
88
- if (deprecatedTypes.includes(imported.node.name)) {
89
- // we don't have to rewrite type references for aliased imports
90
- if (imported.node.name === specifier.node.local.name) {
91
- typeReferencesToUpdate.add(specifier.node.local.name);
92
- }
93
-
94
- const newType = migrateType(imported.node.name);
95
-
96
- // replace the deprecated import type when the new type isn't yet imported
97
- // note that we don't replace the local name of the specifier
98
- if (!existingImports.some((it) => it.name === newType)) {
99
- imported.replaceWith(t.identifier(newType));
100
- existingImports.push({ name: newType, isAlias: false, path: specifier });
101
- } else {
102
- // if the existing import has the same local name but is an alias we throw
103
- // we could have imported the type with an alias, but seems to much effort
104
- const existingImport = existingImports.find((it) => it.name === newType && it.isAlias);
105
- if (existingImport) {
106
- throw existingImport.path.buildCodeFrameError(
107
- 'This codemod does not support local imports that are called the same as a storybook import.\n' +
108
- 'Rename this local import and try again.'
109
- );
110
- } else {
111
- // if the type already exists, without being aliased
112
- // we can safely remove the deprecated import now
113
- specifier.remove();
114
- }
115
- }
116
- }
117
- });
118
- },
119
- });
120
-
121
- file.path.traverse({
122
- TSTypeReference: (path) => {
123
- const typeName = path.get('typeName');
124
- if (typeName.isIdentifier()) {
125
- if (typeReferencesToUpdate.has(typeName.node.name)) {
126
- typeName.replaceWith(t.identifier(migrateType(typeName.node.name)));
127
- }
128
- } else if (typeName.isTSQualifiedName()) {
129
- // For example SB.StoryObj
130
- const namespace = typeName.get('left');
131
- if (namespace.isIdentifier()) {
132
- if (importedNamespaces.has(namespace.node.name)) {
133
- const right = typeName.get('right');
134
- if (deprecatedTypes.includes(right.node.name)) {
135
- right.replaceWith(t.identifier(migrateType(right.node.name)));
136
- }
137
- }
138
- }
139
- }
140
- },
141
- });
142
- }
@@ -1,39 +0,0 @@
1
- function upgradeSeparator(path) {
2
- return path.replace(/[|.]/g, '/');
3
- }
4
-
5
- export default function transformer(file, api, options) {
6
- const j = api.jscodeshift;
7
- const root = j(file.source);
8
-
9
- // storiesOf(...)
10
- root
11
- .find(j.CallExpression)
12
- .filter((call) => call.node.callee.name === 'storiesOf')
13
- .filter(
14
- (call) =>
15
- call.node.arguments.length > 0 &&
16
- ['Literal', 'StringLiteral'].includes(call.node.arguments[0].type)
17
- )
18
- .forEach((call) => {
19
- const arg0 = call.node.arguments[0];
20
- arg0.value = upgradeSeparator(arg0.value);
21
- });
22
-
23
- // export default { title: ... }
24
- root
25
- .find(j.ExportDefaultDeclaration)
26
- .filter((def) => def.node.declaration.properties.map((p) => p.key.name).includes('title'))
27
- .forEach((def) => {
28
- if (def.node.declaration && def.node.declaration.properties) {
29
- def.node.declaration.properties.forEach((p) => {
30
- if (p.key.name === 'title') {
31
- // eslint-disable-next-line no-param-reassign
32
- p.value.value = upgradeSeparator(p.value.value);
33
- }
34
- });
35
- }
36
- });
37
-
38
- return root.toSource({ quote: 'single' });
39
- }
package/tsconfig.json DELETED
@@ -1,10 +0,0 @@
1
- {
2
- "extends": "../../tsconfig.json",
3
- "compilerOptions": {
4
- "skipLibCheck": true,
5
- "allowJs": true,
6
- "strict": false
7
- },
8
- "include": ["src/**/*"],
9
- "exclude": ["node_modules", "__testfixtures__", "__tests__"]
10
- }