-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Feat(codemods): Add Tabs props rename codemod #DS-1096
Rename TabItem prop `forTab` to `forTabPane`. Rename TabPane prop `tabId` to `id`.
- Loading branch information
Showing
4 changed files
with
103 additions
and
0 deletions.
There are no files selected for viewing
10 changes: 10 additions & 0 deletions
10
...odemods/src/transforms/v2/web-react/__testfixtures__/tabs-tabitem-tabpane-props.input.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { TabItem, TabPane } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<TabItem forTab="tab1" data-test="test" /> | ||
<TabPane tabId="tab1" /> | ||
</> | ||
); |
10 changes: 10 additions & 0 deletions
10
...demods/src/transforms/v2/web-react/__testfixtures__/tabs-tabitem-tabpane-props.output.tsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import React from 'react'; | ||
// @ts-ignore: No declaration -- The library is not installed; we don't need to install it for fixtures. | ||
import { TabItem, TabPane } from '@lmc-eu/spirit-web-react'; | ||
|
||
export const MyComponent = () => ( | ||
<> | ||
<TabItem forTabPane="tab1" data-test="test" /> | ||
<TabPane id="tab1" /> | ||
</> | ||
); |
3 changes: 3 additions & 0 deletions
3
packages/codemods/src/transforms/v2/web-react/__tests__/tabs-tabitem-tabpane-props.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
import { testTransform } from '../../../../../tests/testUtils'; | ||
|
||
testTransform(__dirname, 'tabs-tabitem-tabpane-props'); |
80 changes: 80 additions & 0 deletions
80
packages/codemods/src/transforms/v2/web-react/tabs-tabitem-tabpane-props.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
import { API, FileInfo } from 'jscodeshift'; | ||
|
||
const transform = (fileInfo: FileInfo, api: API) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
|
||
// Find import statements for the specific module and TabItem or TabPane specifier | ||
const importStatements = root.find(j.ImportDeclaration, { | ||
source: { | ||
value: (value: string) => /^@lmc-eu\/spirit-web-react(\/.*)?$/.test(value), | ||
}, | ||
}); | ||
|
||
// Check if the module is imported | ||
if (importStatements.length > 0) { | ||
const tabItemComponentSpecifier = importStatements.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
name: 'TabItem', | ||
}, | ||
}); | ||
|
||
const tabPaneComponentSpecifier = importStatements.find(j.ImportSpecifier, { | ||
imported: { | ||
type: 'Identifier', | ||
name: 'TabPane', | ||
}, | ||
}); | ||
|
||
// Check if TabItem specifier is present | ||
if (tabItemComponentSpecifier.length > 0) { | ||
// Find TabItem components in the module | ||
const components = root.find(j.JSXOpeningElement, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'TabItem', | ||
}, | ||
}); | ||
|
||
// Rename 'forTab' attribute to 'forTabPane' | ||
components | ||
.find(j.JSXAttribute, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'forTab', | ||
}, | ||
}) | ||
.forEach((attributePath) => { | ||
attributePath.node.name.name = 'forTabPane'; | ||
}); | ||
} | ||
|
||
// Check if TabPane specifier is present | ||
if (tabPaneComponentSpecifier.length > 0) { | ||
// Find TabPane components in the module | ||
const components = root.find(j.JSXOpeningElement, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'TabPane', | ||
}, | ||
}); | ||
|
||
// Rename 'tabId' attribute to 'id' | ||
components | ||
.find(j.JSXAttribute, { | ||
name: { | ||
type: 'JSXIdentifier', | ||
name: 'tabId', | ||
}, | ||
}) | ||
.forEach((attributePath) => { | ||
attributePath.node.name.name = 'id'; | ||
}); | ||
} | ||
} | ||
|
||
return root.toSource(); | ||
}; | ||
|
||
export default transform; |