-
Notifications
You must be signed in to change notification settings - Fork 20
feat(853): favorite button codemod #854
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
Merged
gitdallas
merged 1 commit into
patternfly:main
from
gitdallas:feat/favorite-button-codemod
Aug 15, 2025
+343
−0
Merged
Changes from all commits
Commits
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
This file contains hidden or 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
17 changes: 17 additions & 0 deletions
17
...ds/src/rules/v6/buttonSupportFavoriteVariant/button-support-favorite-variant.md
This file contains hidden or 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,17 @@ | ||
### button-support-favorite-variant [(#11853)](https://github.com/patternfly/patternfly-react/pull/11853) | ||
|
||
This rule detects when you're using `StarIcon` with Button components and suggests using the new `isFavorite` and `isFavorited` props instead. This supports the new favorite variant that includes animation styling. | ||
|
||
#### Examples | ||
|
||
In: | ||
|
||
```jsx | ||
%inputExample% | ||
``` | ||
|
||
Out: | ||
|
||
```jsx | ||
%outputExample% | ||
``` |
127 changes: 127 additions & 0 deletions
127
...odemods/src/rules/v6/buttonSupportFavoriteVariant/button-support-favorite-variant.test.ts
This file contains hidden or 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,127 @@ | ||
const ruleTester = require("../../ruletester"); | ||
import * as rule from "./button-support-favorite-variant"; | ||
|
||
ruleTester.run("button-support-favorite-variant", rule, { | ||
valid: [ | ||
{ | ||
code: `<Button isFavorite={true} />`, | ||
}, | ||
{ | ||
code: `<Button isFavorited={true} />`, | ||
}, | ||
{ | ||
code: `<Button isFavorite={true} isFavorited={false} />`, | ||
}, | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; <Button>Some text</Button>`, | ||
}, | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { SomeIcon } from '@patternfly/react-icons'; <Button><SomeIcon /></Button>`, | ||
}, | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; <Button icon={<SomeIcon />} />`, | ||
}, | ||
// Button from PF, StarIcon not from PF - should not trigger | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@some-other-package'; <Button><StarIcon /></Button>`, | ||
}, | ||
// Button not from PF, StarIcon from PF - should not trigger | ||
{ | ||
code: `import { Button } from '@some-other-package'; import { StarIcon } from '@patternfly/react-icons'; <Button><StarIcon /></Button>`, | ||
}, | ||
// Both Button and StarIcon not from PF - should not trigger | ||
{ | ||
code: `import { Button } from '@some-other-package'; import { StarIcon } from '@another-package'; <Button><StarIcon /></Button>`, | ||
}, | ||
// Button from PF, StarIcon not from PF (in icon prop) - should not trigger | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@some-other-package'; <Button icon={<StarIcon />} />`, | ||
}, | ||
// Button not from PF, StarIcon from PF (in icon prop) - should not trigger | ||
{ | ||
code: `import { Button } from '@some-other-package'; import { StarIcon } from '@patternfly/react-icons'; <Button icon={<StarIcon />} />`, | ||
}, | ||
], | ||
invalid: [ | ||
// Simple case - StarIcon as child | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <Button><StarIcon /></Button>`, | ||
errors: [ | ||
gitdallas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon in icon prop | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <Button icon={<StarIcon />} />`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon with aliased import | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon as Star } from '@patternfly/react-icons'; <Button><Star /></Button>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// Button with aliased import | ||
{ | ||
code: `import { Button as PFButton } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <PFButton><StarIcon /></PFButton>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon with text | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <Button>Favorite <StarIcon /></Button>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon with other icon | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon, SomeIcon } from '@patternfly/react-icons'; <Button><SomeIcon /><StarIcon /></Button>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon in fragment with text | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <Button><>Favorite <StarIcon /></></Button>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
// StarIcon with existing props | ||
{ | ||
code: `import { Button } from '@patternfly/react-core'; import { StarIcon } from '@patternfly/react-icons'; <Button variant="primary"><StarIcon /></Button>`, | ||
errors: [ | ||
{ | ||
message: "It looks like you are trying to create a custom favorites button. Use `isFavorite` and `isFavorited` button properties instead to apply the correct styling.", | ||
type: "JSXElement", | ||
}, | ||
], | ||
}, | ||
], | ||
}); |
90 changes: 90 additions & 0 deletions
90
...-pf-codemods/src/rules/v6/buttonSupportFavoriteVariant/button-support-favorite-variant.ts
This file contains hidden or 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,90 @@ | ||
import { Rule } from "eslint"; | ||
import { JSXElement, JSXIdentifier, JSXFragment } from "estree-jsx"; | ||
import { | ||
getFromPackage, | ||
getAttribute, | ||
getAttributeValue, | ||
isReactIcon, | ||
} from "../../helpers"; | ||
|
||
// https://www.patternfly.org/components/button/#favorite | ||
module.exports = { | ||
meta: {}, | ||
create: function (context: Rule.RuleContext) { | ||
const { imports: buttonImports } = getFromPackage(context, "@patternfly/react-core"); | ||
const { imports: iconImports } = getFromPackage(context, "@patternfly/react-icons"); | ||
|
||
const buttonImport = buttonImports.find( | ||
(specifier) => specifier.imported.name === "Button" | ||
); | ||
|
||
if (!buttonImport) { | ||
return {}; | ||
} | ||
|
||
// Helper function to check if a JSX element is a StarIcon | ||
const isStarIcon = (element: JSXElement) => { | ||
return isReactIcon(context, element) && | ||
iconImports.some( | ||
(specifier) => specifier.imported.name === "StarIcon" && | ||
specifier.local.name === (element.openingElement.name as JSXIdentifier).name | ||
); | ||
}; | ||
|
||
// Helper function to recursively check for StarIcon in children | ||
const hasStarIconInChildren = (children: any[]): boolean => { | ||
return children.some((child) => { | ||
if (child.type === "JSXElement") { | ||
return isStarIcon(child); | ||
} | ||
if (child.type === "JSXFragment") { | ||
return hasStarIconInChildren(child.children); | ||
} | ||
return false; | ||
}); | ||
}; | ||
|
||
return { | ||
JSXElement(node: JSXElement) { | ||
if ( | ||
node.openingElement.name.type === "JSXIdentifier" && | ||
buttonImport.local.name === node.openingElement.name.name | ||
) { | ||
// Check if Button already has isFavorite or isFavorited props | ||
const isFavoriteProp = getAttribute(node.openingElement, "isFavorite"); | ||
const isFavoritedProp = getAttribute(node.openingElement, "isFavorited"); | ||
|
||
if (isFavoriteProp || isFavoritedProp) { | ||
return; // Already using the new props | ||
} | ||
|
||
// Check for StarIcon in the icon prop | ||
const iconProp = getAttribute(node.openingElement, "icon"); | ||
if (iconProp && iconProp.value?.type === "JSXExpressionContainer") { | ||
const iconExpression = iconProp.value.expression; | ||
if (iconExpression.type === "JSXElement" && isStarIcon(iconExpression)) { | ||
context.report({ | ||
node, | ||
message: `It looks like you are trying to create a custom favorites button. Use \`isFavorite\` and \`isFavorited\` button properties instead to apply the correct styling.`, | ||
}); | ||
return; | ||
} | ||
} | ||
|
||
// Check for StarIcon in children (including fragments) | ||
const hasStarIconChild = hasStarIconInChildren(node.children); | ||
|
||
if (!hasStarIconChild) { | ||
return; // No StarIcon found | ||
} | ||
|
||
// Report warning for all cases | ||
context.report({ | ||
node, | ||
message: `It looks like you are trying to create a custom favorites button. Use \`isFavorite\` and \`isFavorited\` button properties instead to apply the correct styling.`, | ||
}); | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
39 changes: 39 additions & 0 deletions
39
...-codemods/src/rules/v6/buttonSupportFavoriteVariant/buttonSupportFavoriteVariantInput.tsx
This file contains hidden or 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,39 @@ | ||
import { Button } from "@patternfly/react-core"; | ||
import { StarIcon, SomeIcon } from "@patternfly/react-icons"; | ||
|
||
export const ButtonSupportFavoriteVariantInput = () => ( | ||
<> | ||
{/* Simple case - can be auto-fixed */} | ||
<Button> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon in icon prop - can be auto-fixed */} | ||
<Button icon={<StarIcon />} /> | ||
|
||
{/* StarIcon with existing props - can be auto-fixed */} | ||
<Button variant="primary"> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon with text - warning only */} | ||
<Button> | ||
Favorite <StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon with other icon - warning only */} | ||
<Button> | ||
<SomeIcon /> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon in fragment - warning only */} | ||
<Button> | ||
<>Favorite <StarIcon /></> | ||
</Button> | ||
|
||
{/* Already using new props - should be ignored */} | ||
<Button isFavorite={true} /> | ||
<Button isFavorited={true} /> | ||
</> | ||
); |
39 changes: 39 additions & 0 deletions
39
...codemods/src/rules/v6/buttonSupportFavoriteVariant/buttonSupportFavoriteVariantOutput.tsx
This file contains hidden or 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,39 @@ | ||
import { Button } from "@patternfly/react-core"; | ||
import { StarIcon, SomeIcon } from "@patternfly/react-icons"; | ||
|
||
export const ButtonSupportFavoriteVariantInput = () => ( | ||
<> | ||
{/* Simple case - warning only */} | ||
<Button> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon in icon prop - warning only */} | ||
<Button icon={<StarIcon />} /> | ||
|
||
{/* StarIcon with existing props - warning only */} | ||
<Button variant="primary"> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon with text - warning only */} | ||
<Button> | ||
Favorite <StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon with other icon - warning only */} | ||
<Button> | ||
<SomeIcon /> | ||
<StarIcon /> | ||
</Button> | ||
|
||
{/* StarIcon in fragment - warning only */} | ||
<Button> | ||
<>Favorite <StarIcon /></> | ||
</Button> | ||
|
||
{/* Already using new props - no warning */} | ||
<Button isFavorite={true} /> | ||
<Button isFavorited={true} /> | ||
</> | ||
); |
This file contains hidden or 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
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.
Uh oh!
There was an error while loading. Please reload this page.