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

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,439 +0,0 @@
1
- import { describe, it, expect } from '@jest/globals';
2
- import { dedent } from 'ts-dedent';
3
- import type { API } from 'jscodeshift';
4
- import ansiRegex from 'ansi-regex';
5
- import _transform from '../csf-2-to-3';
6
-
7
- expect.addSnapshotSerializer({
8
- print: (val: any) => val,
9
- test: () => true,
10
- });
11
-
12
- const jsTransform = (source: string) =>
13
- _transform({ source, path: 'Component.stories.js' }, {} as API, {}).trim();
14
- const tsTransform = (source: string) =>
15
- _transform({ source, path: 'Component.stories.ts' }, {} as API, { parser: 'tsx' }).trim();
16
-
17
- describe('csf-2-to-3', () => {
18
- describe('javascript', () => {
19
- it('should replace non-simple function exports with objects', () => {
20
- expect(
21
- jsTransform(dedent`
22
- export default { title: 'Cat' };
23
- export const A = () => <Cat />;
24
- export const B = (args) => <Button {...args} />;
25
- `)
26
- ).toMatchInlineSnapshot(`
27
- export default { title: 'Cat' };
28
- export const A = () => <Cat />;
29
- export const B = {
30
- render: (args) => <Button {...args} />,
31
- };
32
- `);
33
- });
34
-
35
- it('should move annotations into story objects', () => {
36
- expect(
37
- jsTransform(dedent`
38
- export default { title: 'Cat' };
39
-
40
- export const A = () => <Cat />;
41
- A.storyName = 'foo';
42
- A.parameters = { bar: 2 };
43
- A.play = () => {};
44
- `)
45
- ).toMatchInlineSnapshot(`
46
- export default { title: 'Cat' };
47
-
48
- export const A = {
49
- render: () => <Cat />,
50
- name: 'foo',
51
- parameters: { bar: 2 },
52
- play: () => {},
53
- };
54
- `);
55
- });
56
-
57
- it('should ignore non-story exports, statements', () => {
58
- expect(
59
- jsTransform(dedent`
60
- export default { title: 'components/Fruit', includeStories: ['A'] };
61
-
62
- export const A = (args) => <Apple {...args} />;
63
-
64
- export const B = (args) => <Banana {...args} />;
65
-
66
- const C = (args) => <Cherry {...args} />;
67
- `)
68
- ).toMatchInlineSnapshot(`
69
- export default { title: 'components/Fruit', includeStories: ['A'] };
70
-
71
- export const A = {
72
- render: (args) => <Apple {...args} />,
73
- };
74
-
75
- export const B = (args) => <Banana {...args} />;
76
-
77
- const C = (args) => <Cherry {...args} />;
78
- `);
79
- });
80
-
81
- it('should do nothing when there is no meta', () => {
82
- expect(
83
- jsTransform(dedent`
84
- export const A = () => <Apple />;
85
-
86
- export const B = (args) => <Banana {...args} />;
87
- `)
88
- ).toMatchInlineSnapshot(`
89
- export const A = () => <Apple />;
90
-
91
- export const B = (args) => <Banana {...args} />;
92
- `);
93
- });
94
-
95
- it('should remove implicit global render function (react)', () => {
96
- expect(
97
- jsTransform(dedent`
98
- export default { title: 'Cat', component: Cat };
99
- export const A = (args) => <Cat {...args} />;
100
- export const B = (args) => <Banana {...args} />;
101
- `)
102
- ).toMatchInlineSnapshot(`
103
- export default { title: 'Cat', component: Cat };
104
- export const A = {};
105
- export const B = {
106
- render: (args) => <Banana {...args} />,
107
- };
108
- `);
109
- });
110
-
111
- it('should ignore object exports', () => {
112
- expect(
113
- jsTransform(dedent`
114
- export default { title: 'Cat', component: Cat };
115
-
116
- export const A = {
117
- render: (args) => <Cat {...args} />
118
- };
119
- `)
120
- ).toMatchInlineSnapshot(`
121
- export default { title: 'Cat', component: Cat };
122
-
123
- export const A = {
124
- render: (args) => <Cat {...args} />,
125
- };
126
- `);
127
- });
128
-
129
- it('should hoist template.bind (if there is only one)', () => {
130
- expect(
131
- jsTransform(dedent`
132
- export default { title: 'Cat' };
133
- const Template = (args) => <Cat {...args} />;
134
- export const A = Template.bind({});
135
- A.args = { isPrimary: false };
136
- `)
137
- ).toMatchInlineSnapshot(`
138
- export default { title: 'Cat' };
139
- const Template = (args) => <Cat {...args} />;
140
-
141
- export const A = {
142
- render: Template,
143
- args: { isPrimary: false },
144
- };
145
- `);
146
- });
147
-
148
- it('should reuse the template when there are multiple Template.bind references but no component defined', () => {
149
- expect(
150
- jsTransform(dedent`
151
- export default { title: 'Cat' };
152
- const Template = (args) => <Cat {...args} />;
153
-
154
- export const A = Template.bind({});
155
- A.args = { isPrimary: false };
156
-
157
- export const B = Template.bind({});
158
- B.args = { isPrimary: true };
159
-
160
-
161
- export const C = Template.bind({});
162
- C.args = { bla: true };
163
-
164
- export const D = Template.bind({});
165
- D.args = { bla: false };
166
- `)
167
- ).toMatchInlineSnapshot(`
168
- export default { title: 'Cat' };
169
- const Template = (args) => <Cat {...args} />;
170
-
171
- export const A = {
172
- render: Template,
173
- args: { isPrimary: false },
174
- };
175
-
176
- export const B = {
177
- render: Template,
178
- args: { isPrimary: true },
179
- };
180
-
181
- export const C = {
182
- render: Template,
183
- args: { bla: true },
184
- };
185
-
186
- export const D = {
187
- render: Template,
188
- args: { bla: false },
189
- };
190
- `);
191
- });
192
-
193
- it('should remove implicit global render for template.bind', () => {
194
- expect(
195
- jsTransform(dedent`
196
- export default { title: 'Cat', component: Cat };
197
-
198
- const Template = (args) => <Cat {...args} />;
199
-
200
- export const A = Template.bind({});
201
- A.args = { isPrimary: false };
202
-
203
- const Template2 = (args) => <Banana {...args} />;
204
-
205
- export const B = Template2.bind({});
206
- B.args = { isPrimary: true };
207
- `)
208
- ).toMatchInlineSnapshot(`
209
- export default { title: 'Cat', component: Cat };
210
-
211
- export const A = {
212
- args: { isPrimary: false },
213
- };
214
-
215
- const Template2 = (args) => <Banana {...args} />;
216
-
217
- export const B = {
218
- render: Template2,
219
- args: { isPrimary: true },
220
- };
221
- `);
222
- });
223
-
224
- it('should ignore no-arg stories without annotations', () => {
225
- expect(
226
- jsTransform(dedent`
227
- export default { title: 'Cat', component: Cat };
228
-
229
- export const A = (args) => <Cat {...args} />;
230
- export const B = () => <Cat name="frisky" />;
231
- export const C = () => <Cat name="fluffy" />;
232
- C.parameters = { foo: 2 };
233
- `)
234
- ).toMatchInlineSnapshot(`
235
- export default { title: 'Cat', component: Cat };
236
-
237
- export const A = {};
238
- export const B = () => <Cat name="frisky" />;
239
-
240
- export const C = {
241
- render: () => <Cat name="fluffy" />,
242
- parameters: { foo: 2 },
243
- };
244
- `);
245
- });
246
-
247
- it('should work for v1-style annotations', () => {
248
- expect(
249
- jsTransform(dedent`
250
- export default { title: 'Cat' };
251
- export const A = (args) => <Cat {...args} />;
252
- A.story = {
253
- parameters: { foo: 2 }
254
- };
255
- `)
256
- ).toMatchInlineSnapshot(`
257
- export default { title: 'Cat' };
258
-
259
- export const A = {
260
- render: (args) => <Cat {...args} />,
261
- parameters: { foo: 2 },
262
- };
263
- `);
264
- });
265
- });
266
-
267
- describe('typescript', () => {
268
- it('should error with namespace imports', () => {
269
- expect.addSnapshotSerializer({
270
- serialize: (value) => value.replace(ansiRegex(), ''),
271
- test: () => true,
272
- });
273
- expect(() =>
274
- tsTransform(dedent`
275
- import * as SB from '@storybook/react';
276
- import { CatProps } from './Cat';
277
-
278
- const meta = { title: 'Cat', component: Cat } as Meta<CatProps>
279
- export default meta;
280
-
281
- export const A: SB.StoryFn<CatProps> = () => <Cat />;
282
- `)
283
- ).toThrowErrorMatchingInlineSnapshot(`
284
- This codemod does not support namespace imports for a @storybook/react package.
285
- Replace the namespace import with named imports and try again.
286
- `);
287
- });
288
- it('should keep local names', () => {
289
- expect(
290
- tsTransform(dedent`
291
- import { Meta, StoryObj as CSF3, StoryFn as CSF2 } from '@storybook/react';
292
- import { CatProps } from './Cat';
293
-
294
- const meta = { title: 'Cat', component: Cat } satisfies Meta<CatProps>
295
- export default meta;
296
-
297
- export const A: CSF2<CatProps> = () => <Cat />;
298
-
299
- export const B: CSF3<CatProps> = {
300
- args: { name: "already csf3" }
301
- };
302
-
303
- export const C: CSF2<CatProps> = (args) => <Cat {...args} />;
304
- C.args = {
305
- name: "Fluffy"
306
- };
307
- `)
308
- ).toMatchInlineSnapshot(`
309
- import { Meta, StoryObj as CSF3, StoryFn as CSF2 } from '@storybook/react';
310
- import { CatProps } from './Cat';
311
-
312
- const meta = { title: 'Cat', component: Cat } satisfies Meta<CatProps>;
313
- export default meta;
314
-
315
- export const A: CSF2<CatProps> = () => <Cat />;
316
-
317
- export const B: CSF3<CatProps> = {
318
- args: { name: 'already csf3' },
319
- };
320
-
321
- export const C: CSF3<CatProps> = {
322
- args: {
323
- name: 'Fluffy',
324
- },
325
- };
326
- `);
327
- });
328
-
329
- it('should replace function exports with objects and update type', () => {
330
- expect(
331
- tsTransform(dedent`
332
- import { Story, StoryFn, ComponentStory, ComponentStoryObj } from '@storybook/react';
333
-
334
- // some extra whitespace to test
335
-
336
- export default {
337
- title: 'Cat',
338
- component: Cat,
339
- } as Meta<CatProps>;
340
-
341
- export const A: Story<CatProps> = (args) => <Cat {...args} />;
342
- A.args = { name: "Fluffy" };
343
-
344
- export const B: any = (args) => <Button {...args} />;
345
-
346
- export const C: Story<CatProps> = () => <Cat />;
347
-
348
- export const D: StoryFn<CatProps> = (args) => <Cat {...args} />;
349
- D.args = {
350
- name: "Fluffy"
351
- };
352
-
353
- export const E: ComponentStory<Cat> = (args) => <Cat {...args} />;
354
- E.args = { name: "Fluffy" };
355
-
356
- export const F: Story = (args) => <Cat {...args} />;
357
- F.args = {
358
- name: "Fluffy"
359
- };
360
-
361
- export const G: ComponentStoryObj<typeof Cat> = {
362
- args: {
363
- name: 'Fluffy',
364
- },
365
- };
366
- `)
367
- ).toMatchInlineSnapshot(`
368
- import { StoryObj, StoryFn } from '@storybook/react';
369
-
370
- // some extra whitespace to test
371
-
372
- export default {
373
- title: 'Cat',
374
- component: Cat,
375
- } as Meta<CatProps>;
376
-
377
- export const A: StoryObj<CatProps> = {
378
- args: { name: 'Fluffy' },
379
- };
380
-
381
- export const B: any = {
382
- render: (args) => <Button {...args} />,
383
- };
384
-
385
- export const C: StoryFn<CatProps> = () => <Cat />;
386
-
387
- export const D: StoryObj<CatProps> = {
388
- args: {
389
- name: 'Fluffy',
390
- },
391
- };
392
-
393
- export const E: StoryObj<Cat> = {
394
- args: { name: 'Fluffy' },
395
- };
396
-
397
- export const F: StoryObj = {
398
- args: {
399
- name: 'Fluffy',
400
- },
401
- };
402
-
403
- export const G: StoryObj<typeof Cat> = {
404
- args: {
405
- name: 'Fluffy',
406
- },
407
- };
408
- `);
409
- });
410
-
411
- it('migrate Story type to StoryFn when used in an not exported Template function', () => {
412
- expect(
413
- tsTransform(dedent`
414
- import { Story, Meta } from '@storybook/react'
415
-
416
- export default {
417
- component: Cat,
418
- } satisfies Meta
419
-
420
- const Template: Story = () => <div>Hello World</div>;
421
-
422
- export const Default = Template.bind({})
423
- `)
424
- ).toMatchInlineSnapshot(`
425
- import { StoryFn, Meta } from '@storybook/react';
426
-
427
- export default {
428
- component: Cat,
429
- } satisfies Meta;
430
-
431
- const Template: StoryFn = () => <div>Hello World</div>;
432
-
433
- export const Default = {
434
- render: Template,
435
- };
436
- `);
437
- });
438
- });
439
- });