-
Notifications
You must be signed in to change notification settings - Fork 1.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: codemod to replace slug prop to decorator (#18535)
- Loading branch information
1 parent
6a145c0
commit 32f0e85
Showing
7 changed files
with
270 additions
and
0 deletions.
There are no files selected for viewing
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
41 changes: 41 additions & 0 deletions
41
packages/upgrade/transforms/__testfixtures__/slug-prop-to-decorator-prop.input.js
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,41 @@ | ||
import React from 'react'; | ||
import { Dropdown, Checkbox, Tag } from '@carbon/react'; | ||
|
||
function TestComponent() { | ||
return ( | ||
//prettier-ignore | ||
<div> | ||
{/* Basic Dropdown usage */} | ||
<Dropdown | ||
label="Select an option" | ||
slug="dropdown-1" | ||
items={['Option 1', 'Option 2']} | ||
id="dropdown-1" | ||
titleText="Dropdown" | ||
/> | ||
{/* Checkbox with expression */} | ||
<Checkbox | ||
labelText="Check me" | ||
slug={'checkbox-1'} | ||
id="checkbox-1" | ||
/> | ||
{/* Tag with string literal */} | ||
<Tag slug={'static-tag'} type="red"> | ||
Important | ||
</Tag> | ||
{/* Nested structure */} | ||
<div> | ||
<Tag slug="tag-1" type="blue"> | ||
Active | ||
</Tag> | ||
<Checkbox | ||
slug="checkbox-2" | ||
labelText="Enable feature" | ||
id="checkbox-2" | ||
/> | ||
</div> | ||
</div> | ||
); | ||
} | ||
|
||
export default TestComponent; |
41 changes: 41 additions & 0 deletions
41
packages/upgrade/transforms/__testfixtures__/slug-prop-to-decorator-prop.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,41 @@ | ||
import React from 'react'; | ||
import { Dropdown, Checkbox, Tag } from '@carbon/react'; | ||
|
||
const TestComponent: React.FC = () => { | ||
return ( | ||
//prettier-ignore | ||
<div> | ||
{/* Basic Dropdown usage */} | ||
<Dropdown | ||
label="Select an option" | ||
slug="dropdown-1" | ||
items={['Option 1', 'Option 2']} | ||
id="dropdown-1" | ||
titleText="Dropdown" | ||
/> | ||
{/* Checkbox with expression */} | ||
<Checkbox | ||
labelText="Check me" | ||
slug={'checkbox-1'} | ||
id="checkbox-1" | ||
/> | ||
{/* Tag with string literal */} | ||
<Tag slug={'static-tag'} type="red"> | ||
Important | ||
</Tag> | ||
{/* Nested structure */} | ||
<div> | ||
<Tag slug="tag-1" type="blue"> | ||
Active | ||
</Tag> | ||
<Checkbox | ||
slug="checkbox-2" | ||
labelText="Enable feature" | ||
id="checkbox-2" | ||
/> | ||
</div> | ||
</div> | ||
); | ||
}; | ||
|
||
export default TestComponent; |
41 changes: 41 additions & 0 deletions
41
packages/upgrade/transforms/__testfixtures__/slug-prop-to-decorator-prop.output.js
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,41 @@ | ||
import React from 'react'; | ||
import { Dropdown, Checkbox, Tag } from '@carbon/react'; | ||
|
||
function TestComponent() { | ||
return ( | ||
//prettier-ignore | ||
(<div> | ||
{/* Basic Dropdown usage */} | ||
<Dropdown | ||
label="Select an option" | ||
decorator="dropdown-1" | ||
items={['Option 1', 'Option 2']} | ||
id="dropdown-1" | ||
titleText="Dropdown" | ||
/> | ||
{/* Checkbox with expression */} | ||
<Checkbox | ||
labelText="Check me" | ||
decorator={'checkbox-1'} | ||
id="checkbox-1" | ||
/> | ||
{/* Tag with string literal */} | ||
<Tag decorator={'static-tag'} type="red"> | ||
Important | ||
</Tag> | ||
{/* Nested structure */} | ||
<div> | ||
<Tag decorator="tag-1" type="blue"> | ||
Active | ||
</Tag> | ||
<Checkbox | ||
decorator="checkbox-2" | ||
labelText="Enable feature" | ||
id="checkbox-2" | ||
/> | ||
</div> | ||
</div>) | ||
); | ||
} | ||
|
||
export default TestComponent; |
41 changes: 41 additions & 0 deletions
41
packages/upgrade/transforms/__testfixtures__/slug-prop-to-decorator-prop.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,41 @@ | ||
import React from 'react'; | ||
import { Dropdown, Checkbox, Tag } from '@carbon/react'; | ||
|
||
const TestComponent: React.FC = () => { | ||
return ( | ||
//prettier-ignore | ||
(<div> | ||
{/* Basic Dropdown usage */} | ||
<Dropdown | ||
label="Select an option" | ||
decorator="dropdown-1" | ||
items={['Option 1', 'Option 2']} | ||
id="dropdown-1" | ||
titleText="Dropdown" | ||
/> | ||
{/* Checkbox with expression */} | ||
<Checkbox | ||
labelText="Check me" | ||
decorator={'checkbox-1'} | ||
id="checkbox-1" | ||
/> | ||
{/* Tag with string literal */} | ||
<Tag decorator={'static-tag'} type="red"> | ||
Important | ||
</Tag> | ||
{/* Nested structure */} | ||
<div> | ||
<Tag decorator="tag-1" type="blue"> | ||
Active | ||
</Tag> | ||
<Checkbox | ||
decorator="checkbox-2" | ||
labelText="Enable feature" | ||
id="checkbox-2" | ||
/> | ||
</div> | ||
</div>) | ||
); | ||
}; | ||
|
||
export default TestComponent; |
12 changes: 12 additions & 0 deletions
12
packages/upgrade/transforms/__tests__/slug-prop-to-decorator-prop-test.js
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,12 @@ | ||
/** | ||
* Copyright IBM Corp. 2025 | ||
* | ||
* 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, 'slug-prop-to-decorator-prop'); |
47 changes: 47 additions & 0 deletions
47
packages/upgrade/transforms/slug-prop-to-decorator-prop.js
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,47 @@ | ||
/** | ||
* Copyright IBM Corp. 2025 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* Replace slug prop with decorator | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const defaultOptions = { | ||
quote: 'single', | ||
trailingComma: true, | ||
}; | ||
|
||
function transform(fileInfo, api, options) { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
const printOptions = options.printOptions || defaultOptions; | ||
|
||
// Early return if no JSX elements with slug prop found | ||
if (!root.find(j.JSXAttribute, { name: { name: 'slug' } }).size()) { | ||
return null; | ||
} | ||
|
||
// Replace slug with decorator | ||
root | ||
.find(j.JSXAttribute, { | ||
name: { name: 'slug' }, | ||
}) | ||
.forEach((path) => { | ||
// Create new decorator attribute with same value as slug | ||
const newAttribute = j.jsxAttribute( | ||
j.jsxIdentifier('decorator'), | ||
path.node.value | ||
); | ||
|
||
// Replace the slug attribute with decorator | ||
j(path).replaceWith(newAttribute); | ||
}); | ||
|
||
return root.toSource(printOptions); | ||
} | ||
|
||
module.exports = transform; | ||
module.exports.parser = 'tsx'; |