Skip to content

Commit

Permalink
refactor(callout): reworked import portion of codemod
Browse files Browse the repository at this point in the history
  • Loading branch information
tay1orjones committed Sep 20, 2024
1 parent dd8af3e commit 78829a5
Show file tree
Hide file tree
Showing 21 changed files with 259 additions and 192 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ describe('StaticNotification', () => {
expect(spy).toHaveBeenCalledWith(
'Warning: `StaticNotification` has been renamed to `Callout`.' +
'Run the following codemod to automatically update usages in your' +
'project: `npx @carbon/upgrade migrate rename-staticnotification-to-callout --write`'
'project: `npx @carbon/upgrade migrate refactor-to-callout --write`'
);
spy.mockRestore();
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1408,7 +1408,7 @@ export const StaticNotification: React.FC<StaticNotificationProps> = (
didWarnAboutDeprecation,
'`StaticNotification` has been renamed to `Callout`.' +
'Run the following codemod to automatically update usages in your' +
'project: `npx @carbon/upgrade migrate rename-staticnotification-to-callout --write`'
'project: `npx @carbon/upgrade migrate refactor-to-callout --write`'
);
didWarnAboutDeprecation = true;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@
Run the following codemod to automatically update usages in your project:

```
npx @carbon/upgrade migrate rename-staticnotification-to-callout --write
npx @carbon/upgrade migrate refactor-to-callout --write
```
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ export const Default = () => (
project:
</p>
<CodeSnippet type="single" feedback="Copied to clipboard">
npx @carbon/upgrade migrate rename-staticnotification-to-callout --write
npx @carbon/upgrade migrate refactor-to-callout --write
</CodeSnippet>
</div>
</>
Expand Down
7 changes: 2 additions & 5 deletions packages/upgrade/src/upgrades.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,14 +292,11 @@ export const upgrades = [
},
},
{
name: 'rename-staticnotification-to-callout',
name: 'refactor-to-callout',
description:
'Rewrites imports and usages of StaticNotification to Callout',
migrate: async (options) => {
const transform = path.join(
TRANSFORM_DIR,
'rename-staticnotification-to-callout.js'
);
const transform = path.join(TRANSFORM_DIR, 'refactor-to-callout.js');
const paths =
Array.isArray(options.paths) && options.paths.length > 0
? options.paths
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Typical imports
import { unstable__StaticNotification as StaticNotification } from '@carbon/react';
import { unstable__StaticNotification } from '@carbon/react';

// If they used a custom name
import { unstable__StaticNotification as SomeOtherName } from '@carbon/react';

// If they already renamed it Callout
import { unstable__StaticNotification as Callout } from '@carbon/react';

// Local renames like this are unlikely but technically possible
const LocallyRenamedStaticNotification = unstable__StaticNotification;

// Component usages
const App = () => {
return (
<>
<StaticNotification title="Test" />
<SomeOtherName title="Test" />
<Callout title="Test" />
<LocallyRenamedStaticNotification title="Test" />
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
// Typical imports
import { unstable__Callout as Callout } from '@carbon/react';
import { unstable__Callout } from '@carbon/react';

// If they used a custom name
import { unstable__Callout as SomeOtherName } from '@carbon/react';

// If they already renamed it Callout
import { unstable__Callout as Callout } from '@carbon/react';

// Local renames like this are unlikely but technically possible
const LocallyRenamedStaticNotification = unstable__Callout;

// Component usages
const App = () => {
return (
<>
<Callout title="Test" />
<SomeOtherName title="Test" />
<Callout title="Test" />
<LocallyRenamedStaticNotification title="Test" />
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Existing usages should not be transformed
import { unstable__Callout } from '@carbon/react';
import { unstable__Callout as Callout } from '@carbon/react';
import { unstable__Callout as SomeOtherOtherName } from '@carbon/react';
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Existing usages should not be transformed
import { unstable__Callout } from '@carbon/react';
import { unstable__Callout as Callout } from '@carbon/react';
import { unstable__Callout as SomeOtherOtherName } from '@carbon/react';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Do not transform potential naming collisions from local paths
import { unstable__Callout } from './my/local/project';
import { unstable__Callout as Callout } from './my/local/project';
import { unstable_StaticNotification } from './my/local/project';
import { unstable_StaticNotification as StaticNotification } from './my/local/project';
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// Do not transform potential naming collisions from local paths
import { unstable__Callout } from './my/local/project';
import { unstable__Callout as Callout } from './my/local/project';
import { unstable_StaticNotification } from './my/local/project';
import { unstable_StaticNotification as StaticNotification } from './my/local/project';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Do not transform potential naming collisions from local paths
import { Callout } from '../my/local/project';
import { StaticNotification } from './my/local/project';
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
// Do not transform potential naming collisions from local paths
import { Callout } from '../my/local/project';
import { StaticNotification } from './my/local/project';
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
// Fully qualified import paths from within es
import {
type StaticNotificationProps,
StaticNotification,
} from '@carbon/react/es/components/Notification';

// If the transform results in duplicate imports, they should be deduped
import { unstable__StaticNotification } from '@carbon/react';
import { unstable__Callout } from '@carbon/react';

// Redefine the component with props to ensure type interface is handled
const Notification: React.FC<StaticNotificationProps> = (props) => {
return <StaticNotification {...props} />;
};

// Local renames like this are unlikely but technically possible
const LocallyRenamedStaticNotification = unstable__StaticNotification;
const LocallyRenamedCallout = unstable__Callout;

// Component usages
const App = () => {
return (
<>
<StaticNotification title="Test" />
<LocallyRenamedCallout title="Test" />
<LocallyRenamedStaticNotification title="Test" />
<Notification title="test" />
</>
);
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Fully qualified import paths from within es
import {
type CalloutProps,
Callout,
} from '@carbon/react/es/components/Notification';

// If the transform results in duplicate imports, they should be deduped// note the de
import { unstable__Callout } from '@carbon/react';

// Redefine the component with props to ensure type interface is handled
const Notification: React.FC<CalloutProps> = (props) => {
return <Callout {...props} />;
};

// Local renames like this are unlikely but technically possible
const LocallyRenamedStaticNotification = unstable__Callout;
const LocallyRenamedCallout = unstable__Callout;

// Component usages
const App = () => {
return (
<>
<Callout title="Test" />
<LocallyRenamedCallout title="Test" />
<LocallyRenamedStaticNotification title="Test" />
<Notification title="test" />
</>
);
};

This file was deleted.

This file was deleted.

16 changes: 16 additions & 0 deletions packages/upgrade/transforms/__tests__/refactor-to-callout.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
/**
* Copyright IBM Corp. 2016, 2023
*
* This source code is licensed under the Apache-2.0 license found in the
* LICENSE file in the root directory of this source tree.
*/

'use strict';

const { defineTest } = require('jscodeshift/dist/testUtils');

defineTest(__dirname, 'refactor-to-callout');
defineTest(__dirname, 'refactor-to-callout', null, 'refactor-to-callout2');
defineTest(__dirname, 'refactor-to-callout', null, 'refactor-to-callout3');
defineTest(__dirname, 'refactor-to-callout', null, 'refactor-to-callout4');
defineTest(__dirname, 'refactor-to-callout', null, 'refactor-to-callout5');

This file was deleted.

Loading

0 comments on commit 78829a5

Please sign in to comment.