Skip to content

Commit

Permalink
feat(ToolbarItem): variant prop options removed
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Sep 3, 2024
1 parent f950da7 commit 9bf5bc6
Show file tree
Hide file tree
Showing 11 changed files with 253 additions and 95 deletions.
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")
);
}
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
export * from "./attributeValueIsString";
export * from "./childrenIsEmpty";
export * from "./contextReports";
export * from "./findAncestor";
Expand Down

This file was deleted.

This file was deleted.

This file was deleted.

This file was deleted.

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%
```
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const ruleTester = require("../../ruletester");
import * as rule from "./toolbarItem-replace-chipGroup-variant";
import * as rule from "./toolbarItem-variant-prop-updates";

ruleTester.run("toolbarItem-replace-chipGroup-variant", rule, {
valid: [
Expand Down Expand Up @@ -30,6 +30,16 @@ ruleTester.run("toolbarItem-replace-chipGroup-variant", rule, {
},
],
},
{
code: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant={"chip-group"} />`,
output: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant="label-group" />`,
errors: [
{
message: `The "chip-group" variant for ToolbarItem has been replaced with "label-group".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { ToolbarItem as CustomItem } from '@patternfly/react-core'; <CustomItem variant="chip-group" />`,
output: `import { ToolbarItem as CustomItem } from '@patternfly/react-core'; <CustomItem variant="label-group" />`,
Expand Down Expand Up @@ -90,5 +100,86 @@ ruleTester.run("toolbarItem-replace-chipGroup-variant", rule, {
},
],
},
{
code: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant="bulk-select" />`,
output: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem />`,
errors: [
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant="overflow-menu" />`,
output: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem />`,
errors: [
{
message: `The "overflow-menu" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant="search-filter" />`,
output: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem />`,
errors: [
{
message: `The "search-filter" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem variant={"bulk-select"} />`,
output: `import { ToolbarItem } from '@patternfly/react-core'; <ToolbarItem />`,
errors: [
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core'; <ToolbarItem variant={ToolbarItemVariant["bulk-select"]} />`,
output: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core'; <ToolbarItem />`,
errors: [
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "MemberExpression",
},
],
},
{
code: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
const variant = ToolbarItemVariant["bulk-select"]; <ToolbarItem variant={variant} />`,
output: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core';
const variant = ToolbarItemVariant["bulk-select"]; <ToolbarItem />`,
errors: [
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "MemberExpression",
},
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "JSXOpeningElement",
},
],
},
// error without the option to fix
{
code: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core'; const variant = ToolbarItemVariant["bulk-select"];`,
output: `import { ToolbarItem, ToolbarItemVariant } from '@patternfly/react-core'; const variant = ToolbarItemVariant["bulk-select"];`,
errors: [
{
message: `The "bulk-select" variant for ToolbarItem has been removed.`,
type: "MemberExpression",
},
],
},
],
});
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"`);
},
});
}
}
},
};
},
};
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" />
</>
);
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 />
</>
);

0 comments on commit 9bf5bc6

Please sign in to comment.