-
Notifications
You must be signed in to change notification settings - Fork 18
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(Button): Extend icon moving to support Icon and react-icon child…
…ren (#735) * feat(Button): Extend icon moving to support Icon and react-icon children * fix(Button): fix bugs found during review * chore(Button): Update single test files * chore(helpers): Update helper import after merge
- Loading branch information
1 parent
7f21c53
commit d5633b0
Showing
6 changed files
with
147 additions
and
17 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
31 changes: 31 additions & 0 deletions
31
packages/eslint-plugin-pf-codemods/src/rules/helpers/isReactIcon.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,31 @@ | ||
import { Rule } from "eslint"; | ||
import { JSXElement } from "estree-jsx"; | ||
import { getFromPackage, getDefaultImportsFromPackage } from "./index"; | ||
|
||
/** Returns true if an element is a patternfly/react-icons import, false if it isn't, and undefined | ||
* if no element is passed (for type safety) */ | ||
export function isReactIcon(context: Rule.RuleContext, element?: JSXElement) { | ||
if (!element) { | ||
return; | ||
} | ||
|
||
const openingElementIdentifier = element.openingElement.name; | ||
|
||
// TODO: update this to use the appropriate getNodeName helper once it lands | ||
if (openingElementIdentifier.type !== "JSXIdentifier") { | ||
return; | ||
} | ||
const elementName = openingElementIdentifier.name; | ||
|
||
const pfIconsPackage = "@patternfly/react-icons"; | ||
const { imports: iconImports } = getFromPackage(context, pfIconsPackage); | ||
const iconDefaultImports = getDefaultImportsFromPackage( | ||
context, | ||
pfIconsPackage | ||
); | ||
const allIconImports = [...iconImports, ...iconDefaultImports]; | ||
|
||
return allIconImports.some( | ||
(iconImport) => iconImport.local.name === elementName | ||
); | ||
} |
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
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
19 changes: 15 additions & 4 deletions
19
...-plugin-pf-codemods/src/rules/v6/buttonMoveIconsIconProp/buttonMoveIconsIconPropInput.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 |
---|---|---|
@@ -1,7 +1,18 @@ | ||
import { Button } from "@patternfly/react-core"; | ||
import { Button, Icon } from "@patternfly/react-core"; | ||
import { SomeIcon } from "@patternfly/react-icons"; | ||
|
||
export const ButtonMoveIconsIconPropInput = () => ( | ||
<Button variant='plain'> | ||
<span>Icon</span> | ||
</Button> | ||
<> | ||
<Button variant="plain"> | ||
<span>Icon</span> | ||
</Button> | ||
<Button> | ||
<Icon> | ||
<SomeIcon /> | ||
</Icon> | ||
</Button> | ||
<Button> | ||
<SomeIcon /> | ||
</Button> | ||
</> | ||
); |
15 changes: 13 additions & 2 deletions
15
...plugin-pf-codemods/src/rules/v6/buttonMoveIconsIconProp/buttonMoveIconsIconPropOutput.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 |
---|---|---|
@@ -1,5 +1,16 @@ | ||
import { Button } from "@patternfly/react-core"; | ||
import { Button, Icon } from "@patternfly/react-core"; | ||
import { SomeIcon } from "@patternfly/react-icons"; | ||
|
||
export const ButtonMoveIconsIconPropInput = () => ( | ||
<Button icon={<span>Icon</span>} variant='plain'></Button> | ||
<> | ||
<Button icon={<span>Icon</span>} variant="plain"></Button> | ||
<Button icon={<Icon> | ||
<SomeIcon /> | ||
</Icon>}> | ||
|
||
</Button> | ||
<Button icon={<SomeIcon />}> | ||
|
||
</Button> | ||
</> | ||
); |