Skip to content

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
merged 1 commit into from
Aug 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export const warningRules = [
"aboutModalBoxHero-remove-subcomponent",
"accordionItem-warn-update-markup",
"applicationLauncher-warn-input",
"button-support-favorite-variant",
"card-deprecate-props",
"card-warn-component",
"charts-warn-tooltip",
Expand Down
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%
```
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: [
{
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",
},
],
},
],
});
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.`,
});
}
},
};
},
};
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} />
</>
);
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} />
</>
);
30 changes: 30 additions & 0 deletions packages/pf-codemods/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2235,6 +2235,36 @@ export const PageToggleButtonReplaceBarsIconWithIsHamburgerButtonOutput = () =>
);
```

### 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
import { Button } from "@patternfly/react-core";
import { StarIcon } from "@patternfly/react-icons";

export const ButtonSupportFavoriteVariantInput = () => (
<Button>
<StarIcon />
</Button>
);
```

Out:

```jsx
import { Button } from "@patternfly/react-core";
import { StarIcon } from "@patternfly/react-icons";

export const ButtonSupportFavoriteVariantOutput = () => (
<Button isFavorite={true} />
);
```

### pagination-warn-markup-changed [(#10662)](https://github.com/patternfly/patternfly-react/pull/10662)

The markup for Pagination has changed. There is now a wrapper element rendered around the PaginationOptionsMenu toggle. This rule does not provide a fixer, but will throw a warning.
Expand Down
Loading