-
Notifications
You must be signed in to change notification settings - Fork 1.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: implement codemod ibm-products-update-userprofileimage #18058
Open
anamikaanu96
wants to merge
12
commits into
carbon-design-system:main
Choose a base branch
from
anamikaanu96:userProfileImage-to-userAvatar
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+313
−0
Open
Changes from all commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
c5504a0
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 a53331c
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 42d38ef
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 7f0047a
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 dd869bf
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 a54c4dd
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 3a35b11
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 5dd8b82
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 62aa551
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 e64c767
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 0360501
feat: implement codemod ibm-products-update-userprofileimage
anamikaanu96 d8309e5
Merge branch 'main' into userProfileImage-to-userAvatar
2nikhiltom File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
57 changes: 57 additions & 0 deletions
57
packages/upgrade/fixtures/ibm-products-update-userprofileimage-sample.jsx
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,57 @@ | ||
import React from 'react'; | ||
import { UserProfileImage } from '@carbon/ibm-products'; | ||
import { Add } from '@carbon/react/icons'; | ||
|
||
const ImageUrl = | ||
'https://assets.ibm.com/is/image/ibm/oday?wid1278&hei=718&fit=constrain,0&qlt=85,0'; | ||
|
||
export const Example = () => ( | ||
<> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
initials="thomas j. watson" | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
kind="group" | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
icon={() => <Add size={20} />} | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
image={ImageUrl} | ||
imageDescription="Avatar of Thomas Watson" | ||
/> | ||
<MyComponent | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
initials="thomas j. watson" | ||
/> | ||
</> | ||
); |
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
48 changes: 48 additions & 0 deletions
48
packages/upgrade/transforms/__testfixtures__/ibm-products-update-userprofileimage.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,48 @@ | ||
import React from 'react'; | ||
import { UserProfileImage } from '@carbon/ibm-products'; | ||
import { Add } from '@carbon/react/icons'; | ||
|
||
const ImageUrl = | ||
'https://img.freepik.com/free-photo/portrait-man-cartoon-style_23-2151133939.jpg?semt=ais_hybrid'; | ||
|
||
export const Example = () => ( | ||
<> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
initials="thomas j. watson" | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
kind="user" | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
icon={() => <Add size={20} />} | ||
/> | ||
<UserProfileImage | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="light-purple" | ||
theme="light" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
image={ImageUrl} | ||
imageDescription="Avatar of Thomas Watson" | ||
/> | ||
</> | ||
); |
41 changes: 41 additions & 0 deletions
41
packages/upgrade/transforms/__testfixtures__/ibm-products-update-userprofileimage.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 { User } from "@carbon/react/icons"; | ||
import React from 'react'; | ||
import { UserAvatar } from '@carbon/ibm-products'; | ||
import { Add } from '@carbon/react/icons'; | ||
|
||
const ImageUrl = | ||
'https://img.freepik.com/free-photo/portrait-man-cartoon-style_23-2151133939.jpg?semt=ais_hybrid'; | ||
|
||
export const Example = () => ( | ||
<> | ||
<UserAvatar | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="order-5-purple" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
name="thomas j. watson" /> | ||
<UserAvatar | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="order-5-purple" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
renderIcon={User} /> | ||
<UserAvatar | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="order-5-purple" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
renderIcon={() => <Add size={20} />} /> | ||
<UserAvatar | ||
size="md" | ||
tooltipAlignment="bottom" | ||
backgroundColor="order-5-purple" | ||
tooltipText="Thomas Watson" | ||
className="myClass" | ||
image={ImageUrl} | ||
imageDescription="Avatar of Thomas Watson" /> | ||
</> | ||
); |
12 changes: 12 additions & 0 deletions
12
packages/upgrade/transforms/__tests__/ibm-products-update-userprofileimage-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. 2021, 2024 | ||
* | ||
* 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, 'ibm-products-update-userprofileimage'); |
125 changes: 125 additions & 0 deletions
125
packages/upgrade/transforms/ibm-products-update-userprofileimage.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,125 @@ | ||
/** | ||
* Copyright IBM Corp. 2021, 2024 | ||
* | ||
* This source code is licensed under the Apache-2.0 license found in the | ||
* LICENSE file in the root directory of this source tree. | ||
* | ||
* Rewrites UserProfileImage to UserAvatar | ||
* | ||
* Transforms: | ||
* | ||
* <UserProfileImage /> | ||
* | ||
* Into: | ||
* | ||
* <UserAvatar /> | ||
*/ | ||
|
||
'use strict'; | ||
|
||
const transform = (fileInfo, api) => { | ||
const j = api.jscodeshift; | ||
const root = j(fileInfo.source); | ||
let shouldImportUser = false; | ||
let shouldImportGroup = false; | ||
|
||
const ensureImport = (identifierName) => { | ||
const importDeclaration = j.importDeclaration( | ||
[j.importSpecifier(j.identifier(identifierName))], | ||
j.literal('@carbon/react/icons') | ||
); | ||
|
||
const existingImport = root | ||
.find(j.ImportDeclaration, { | ||
source: { value: '@carbon/react/icons' }, | ||
}) | ||
.filter((path) => { | ||
return path.node.specifiers.some( | ||
(specifier) => specifier.imported.name === identifierName | ||
); | ||
}); | ||
|
||
if (existingImport.size() === 0) { | ||
root.find(j.Program).get('body', 0).insertBefore(importDeclaration); | ||
} | ||
}; | ||
// Transform UserProfileImage to UserAvatar | ||
root | ||
.find(j.JSXElement, { | ||
openingElement: { name: { name: 'UserProfileImage' } }, | ||
}) | ||
.forEach((path) => { | ||
// Change the component name | ||
path.node.openingElement.name.name = 'UserAvatar'; | ||
if (path.node.closingElement) { | ||
path.node.closingElement.name.name = 'UserAvatar'; | ||
} | ||
|
||
const colorMapping = { | ||
'light-cyan': 'order-1-cyan', | ||
'dark-cyan': 'order-7-cyan', | ||
'light-gray': 'order-2-gray', | ||
'dark-gray': 'order-8-gray', | ||
'light-green': 'order-3-green', | ||
'dark-green': 'order-9-green', | ||
'light-magenta': 'order-10-magenta', | ||
'dark-magenta': 'order-10-magenta', | ||
'light-purple': 'order-5-purple', | ||
'dark-purple': 'order-11-purple', | ||
'light-teal': 'order-6-teal', | ||
'dark-teal': 'order-12-teal', | ||
}; | ||
const updatedAttributes = []; | ||
// Update attributes | ||
path.node.openingElement.attributes.forEach((attr) => { | ||
if (attr.name.name === 'backgroundColor') { | ||
if (colorMapping[attr.value.value]) { | ||
attr.value.value = colorMapping[attr.value.value]; | ||
} | ||
} | ||
if (attr.name.name === 'theme') { | ||
return; | ||
} | ||
if (attr.name.name === 'initials') { | ||
attr.name.name = 'name'; | ||
} | ||
if (attr.name.name === 'kind' || attr.name.name === 'icon') { | ||
const originalName = attr.name.name; | ||
attr.name.name = 'renderIcon'; | ||
if (originalName === 'kind') { | ||
if (attr.value.value === 'user') { | ||
attr.value = j.jsxExpressionContainer(j.identifier('User')); | ||
shouldImportUser = true; | ||
} else if (attr.value.value === 'group') { | ||
attr.value = j.jsxExpressionContainer(j.identifier('Group')); | ||
shouldImportGroup = true; | ||
} | ||
} | ||
} | ||
updatedAttributes.push(attr); | ||
}); | ||
path.node.openingElement.attributes = updatedAttributes; | ||
}); | ||
// Update import statement | ||
root | ||
.find(j.ImportDeclaration) | ||
.filter((path) => path.node.source.value === '@carbon/ibm-products') | ||
.forEach((path) => { | ||
path.node.specifiers.forEach((specifier) => { | ||
if (specifier.imported.name === 'UserProfileImage') { | ||
specifier.imported.name = 'UserAvatar'; | ||
} | ||
}); | ||
}); | ||
|
||
if (shouldImportUser) { | ||
ensureImport('User'); | ||
} | ||
|
||
if (shouldImportGroup) { | ||
ensureImport('Group'); | ||
} | ||
return root.toSource(); | ||
}; | ||
|
||
module.exports = transform; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this file necessary? How is it used?