From 12d3dc7fefad5a0411b94e30fa7fbd4c945df2cc Mon Sep 17 00:00:00 2001 From: Kasper Peulen Date: Fri, 13 Jan 2023 18:04:22 +0100 Subject: [PATCH] Upgrade deprecated type migration --- code/lib/codemod/package.json | 2 + .../transforms/__tests__/csf-2-to-3.test.ts | 17 +- .../upgrade-deprecated-types.test.ts | 170 ++++++++++++++++++ code/lib/codemod/src/transforms/csf-2-to-3.ts | 24 +-- .../transforms/upgrade-deprecated-types.ts | 142 +++++++++++++++ 5 files changed, 342 insertions(+), 13 deletions(-) create mode 100644 code/lib/codemod/src/transforms/__tests__/upgrade-deprecated-types.test.ts create mode 100644 code/lib/codemod/src/transforms/upgrade-deprecated-types.ts diff --git a/code/lib/codemod/package.json b/code/lib/codemod/package.json index 7a480fc98a8c..ea3d17f6db33 100644 --- a/code/lib/codemod/package.json +++ b/code/lib/codemod/package.json @@ -34,6 +34,7 @@ "./dist/transforms/storiesof-to-csf.js": "./dist/transforms/storiesof-to-csf.js", "./dist/transforms/update-addon-info.js": "./dist/transforms/update-addon-info.js", "./dist/transforms/update-organisation-name.js": "./dist/transforms/update-organisation-name.js", + "./dist/transforms/upgrade-deprecated-types.js": "./dist/transforms/upgrade-deprecated-types.js", "./dist/transforms/upgrade-hierarchy-separators.js": "./dist/transforms/upgrade-hierarchy-separators.js", "./package.json": "./package.json" }, @@ -81,6 +82,7 @@ "./src/transforms/storiesof-to-csf.js", "./src/transforms/update-addon-info.js", "./src/transforms/update-organisation-name.js", + "./src/transforms/upgrade-deprecated-types.ts", "./src/transforms/upgrade-hierarchy-separators.js" ] }, diff --git a/code/lib/codemod/src/transforms/__tests__/csf-2-to-3.test.ts b/code/lib/codemod/src/transforms/__tests__/csf-2-to-3.test.ts index f7dcdf684a35..e20799ded383 100644 --- a/code/lib/codemod/src/transforms/__tests__/csf-2-to-3.test.ts +++ b/code/lib/codemod/src/transforms/__tests__/csf-2-to-3.test.ts @@ -234,7 +234,7 @@ describe('csf-2-to-3', () => { `) ).toThrowErrorMatchingInlineSnapshot(` This codemod does not support namespace imports for a @storybook/react package. - Replace the namespace import with named imports and try again. + Replace the namespace import with named imports and try again. > 1 | import * as SB from '@storybook/react'; | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ 2 | import { CatProps } from './Cat'; @@ -286,7 +286,7 @@ describe('csf-2-to-3', () => { it('should replace function exports with objects and update type', () => { expect( tsTransform(dedent` - import { Story, StoryFn, ComponentStory } from '@storybook/react'; + import { Story, StoryFn, ComponentStory, ComponentStoryObj } from '@storybook/react'; // some extra whitespace to test @@ -315,9 +315,14 @@ describe('csf-2-to-3', () => { name: "Fluffy" }; + export const G: ComponentStoryObj = { + args: { + name: 'Fluffy', + }, + }; `) ).toMatchInlineSnapshot(` - import { StoryObj, StoryFn, ComponentStory } from '@storybook/react'; + import { StoryObj, StoryFn } from '@storybook/react'; // some extra whitespace to test @@ -351,6 +356,12 @@ describe('csf-2-to-3', () => { name: 'Fluffy', }, }; + + export const G: StoryObj = { + args: { + name: 'Fluffy', + }, + }; `); }); }); diff --git a/code/lib/codemod/src/transforms/__tests__/upgrade-deprecated-types.test.ts b/code/lib/codemod/src/transforms/__tests__/upgrade-deprecated-types.test.ts new file mode 100644 index 000000000000..51a7640072c5 --- /dev/null +++ b/code/lib/codemod/src/transforms/__tests__/upgrade-deprecated-types.test.ts @@ -0,0 +1,170 @@ +import { describe, expect, it } from '@jest/globals'; +import { dedent } from 'ts-dedent'; +import type { API } from 'jscodeshift'; +import ansiRegex from 'ansi-regex'; +import _transform from '../upgrade-deprecated-types'; + +expect.addSnapshotSerializer({ + print: (val: any) => val, + test: () => true, +}); + +const tsTransform = (source: string) => + _transform({ source, path: 'Component.stories.ts' }, {} as API, { parser: 'tsx' }).trim(); + +describe('upgrade-deprecated-types', () => { + describe('typescript', () => { + it('upgrade regular imports', () => { + expect( + tsTransform(dedent` + import { Story, ComponentMeta, Meta, ComponentStory, ComponentStoryObj, ComponentStoryFn } from '@storybook/react'; + import { Cat, CatProps } from './Cat'; + + const meta = { title: 'Cat', component: Cat } satisfies ComponentMeta + const meta2: Meta = { title: 'Cat', component: Cat }; + export default meta; + + export const A: ComponentStory = (args) => ; + export const B: any = (args) =>