-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(ToolbarItem): variant prop options removed (#757)
- Loading branch information
1 parent
760e8ba
commit 50be98f
Showing
11 changed files
with
253 additions
and
95 deletions.
There are no files selected for viewing
12 changes: 12 additions & 0 deletions
12
packages/eslint-plugin-pf-codemods/src/rules/helpers/attributeValueIsString.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,12 @@ | ||
import { JSXAttribute } from "estree-jsx"; | ||
|
||
/** Checks if an attribute value is a string, either as a Literal: <Comp attr="value"> | ||
* or a Literal in an expression container: <Comp attr={"value"}> */ | ||
export function attributeValueIsString(value: JSXAttribute["value"]) { | ||
return ( | ||
(value?.type === "Literal" && typeof value.value === "string") || | ||
(value?.type === "JSXExpressionContainer" && | ||
value.expression.type === "Literal" && | ||
typeof value.expression.value === "string") | ||
); | ||
} |
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
17 changes: 0 additions & 17 deletions
17
.../v6/toolbarItemReplaceChipGroupVariant/toolbarItem-replace-chipGroup-variant.md
This file was deleted.
Oops, something went wrong.
67 changes: 0 additions & 67 deletions
67
.../src/rules/v6/toolbarItemReplaceChipGroupVariant/toolbarItem-replace-chipGroup-variant.ts
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
...c/rules/v6/toolbarItemReplaceChipGroupVariant/toolbarItemReplaceChipGroupVariantInput.tsx
This file was deleted.
Oops, something went wrong.
5 changes: 0 additions & 5 deletions
5
.../rules/v6/toolbarItemReplaceChipGroupVariant/toolbarItemReplaceChipGroupVariantOutput.tsx
This file was deleted.
Oops, something went wrong.
17 changes: 17 additions & 0 deletions
17
.../src/rules/v6/toolbarItemVariantPropUpdates/toolbarItem-variant-prop-updates.md
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,17 @@ | ||
### toolbarItem-variant-prop-updates [(#10649)](https://github.com/patternfly/patternfly-react/pull/10649) | ||
|
||
The variant prop for ToolbarItem has been updated: "bulk-select", "overflow-menu" and "search-filter" were removed and "chip-group" was renamed to "label-group". | ||
|
||
#### Examples | ||
|
||
In: | ||
|
||
```jsx | ||
%inputExample% | ||
``` | ||
|
||
Out: | ||
|
||
```jsx | ||
%outputExample% | ||
``` |
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
111 changes: 111 additions & 0 deletions
111
...f-codemods/src/rules/v6/toolbarItemVariantPropUpdates/toolbarItem-variant-prop-updates.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,111 @@ | ||
import { Rule } from "eslint"; | ||
import { | ||
JSXOpeningElement, | ||
MemberExpression, | ||
Identifier, | ||
Literal, | ||
} from "estree-jsx"; | ||
import { | ||
getFromPackage, | ||
getAttribute, | ||
getAttributeValue, | ||
attributeValueIsString, | ||
} from "../../helpers"; | ||
|
||
// https://github.com/patternfly/patternfly-react/pull/10649 | ||
module.exports = { | ||
meta: { fixable: "code" }, | ||
create: function (context: Rule.RuleContext) { | ||
const { imports } = getFromPackage(context, "@patternfly/react-core"); | ||
|
||
const componentImport = imports.find( | ||
(specifier) => specifier.imported.name === "ToolbarItem" | ||
); | ||
const enumImport = imports.find( | ||
(specifier) => specifier.imported.name === "ToolbarItemVariant" | ||
); | ||
|
||
const variantsToRemove = ["bulk-select", "overflow-menu", "search-filter"]; | ||
|
||
const nodeIsEnum = (node: MemberExpression) => | ||
enumImport && | ||
node.object && | ||
node.property && | ||
(node.object as Identifier).name === enumImport.local.name && | ||
node.property.type === "Literal"; | ||
|
||
return !componentImport | ||
? {} | ||
: { | ||
MemberExpression(node: MemberExpression) { | ||
if (nodeIsEnum(node)) { | ||
const variantValue = (node.property as Literal).value as string; | ||
|
||
if (variantValue === "chip-group") { | ||
context.report({ | ||
node, | ||
message: | ||
'The "chip-group" variant for ToolbarItem has been replaced with "label-group".', | ||
fix(fixer) { | ||
return fixer.replaceText(node.property, '"label-group"'); | ||
}, | ||
}); | ||
} | ||
|
||
if (variantsToRemove.includes(variantValue)) { | ||
context.report({ | ||
node, | ||
message: `The "${variantValue}" variant for ToolbarItem has been removed.`, | ||
}); | ||
} | ||
} | ||
}, | ||
JSXOpeningElement(node: JSXOpeningElement) { | ||
if ( | ||
node.name.type === "JSXIdentifier" && | ||
componentImport.local.name === node.name.name | ||
) { | ||
const variant = getAttribute(node, "variant"); | ||
if (!variant || !variant.value) { | ||
return; | ||
} | ||
|
||
const variantValue = getAttributeValue(context, variant.value); | ||
|
||
const variantValueIsLiteral = attributeValueIsString( | ||
variant.value | ||
); | ||
|
||
if ( | ||
(variantValueIsLiteral && | ||
variantsToRemove.includes(variantValue)) || | ||
(nodeIsEnum(variantValue) && | ||
variantsToRemove.includes(variantValue.property.value)) | ||
) { | ||
const variantToRemove = | ||
variantValue.property?.value ?? variantValue; | ||
|
||
context.report({ | ||
node, | ||
message: `The "${variantToRemove}" variant for ToolbarItem has been removed.`, | ||
fix(fixer) { | ||
return fixer.remove(variant); | ||
}, | ||
}); | ||
} | ||
|
||
if (variantValueIsLiteral && variantValue === "chip-group") { | ||
context.report({ | ||
node, | ||
message: | ||
'The "chip-group" variant for ToolbarItem has been replaced with "label-group".', | ||
fix(fixer) { | ||
return fixer.replaceText(variant, `variant="label-group"`); | ||
}, | ||
}); | ||
} | ||
} | ||
}, | ||
}; | ||
}, | ||
}; |
10 changes: 10 additions & 0 deletions
10
...odemods/src/rules/v6/toolbarItemVariantPropUpdates/toolbarItemVariantPropUpdatesInput.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,10 @@ | ||
import { ToolbarItem } from "@patternfly/react-core"; | ||
|
||
export const ToolbarItemVariantPropUpdatesInput = () => ( | ||
<> | ||
<ToolbarItem variant="chip-group" /> | ||
<ToolbarItem variant="bulk-select" /> | ||
<ToolbarItem variant="overflow-menu" /> | ||
<ToolbarItem variant="search-filter" /> | ||
</> | ||
); |
10 changes: 10 additions & 0 deletions
10
...demods/src/rules/v6/toolbarItemVariantPropUpdates/toolbarItemVariantPropUpdatesOutput.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,10 @@ | ||
import { ToolbarItem } from "@patternfly/react-core"; | ||
|
||
export const ToolbarItemVariantPropUpdatesInput = () => ( | ||
<> | ||
<ToolbarItem variant="label-group" /> | ||
<ToolbarItem /> | ||
<ToolbarItem /> | ||
<ToolbarItem /> | ||
</> | ||
); |