Skip to content
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

feat(Label): replace isOverflowLabel with variant="overflow" #580

Merged
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
@@ -0,0 +1,17 @@
### label-remove-isOverflowLabel [(#10037)](https://github.com/patternfly/patternfly-react/pull/10037)

The `isOverflowLabel` prop for Label has been replaced with `variant="overflow"`. Running the fix for this rule will replace an existing `variant` prop (which had no effect if `isOverflowLabel` was used).

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const ruleTester = require("../../ruletester");
import * as rule from "./label-remove-isOverflowLabel";

ruleTester.run("label-remove-isOverflowLabel", rule, {
valid: [
{
code: `<Label isOverflowLabel />`,
},
{
code: `import { Label } from '@patternfly/react-core'; <Label someOtherProp />`,
},
],
invalid: [
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will require an update to be made either to the renameProps helper or this rule to be more custom, but we should add an invalid test that checks whether an existing variant prop is replaced or not:

{
  code:   `import { Label } from '@patternfly/react-core'; <Label isOverflowLabel variant="outline" />`,
  output: `import { Label } from '@patternfly/react-core'; <Label variant="overflow" />`,
  errors: [{
      message: `isOverflowLabel prop for Label has been replaced with variant="overflow"`,
      type: "JSXOpeningElement",
      }]
    },

I'd be fine having this be a followup, though, especially since we can't be sure if consumers would be passing both isOverflowLabel and a variant (since the variant shouldn't have any effect for an overflow label I believe).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point! I think we can do the change right now, I will make this rule more custom

{
code: `import { Label } from '@patternfly/react-core'; <Label isOverflowLabel />`,
output: `import { Label } from '@patternfly/react-core'; <Label variant="overflow" />`,
errors: [
{
message:
'The `isOverflowLabel` prop for Label has been replaced with `variant="overflow"`.',
type: "JSXOpeningElement",
},
],
},
{
code: `import { Label } from '@patternfly/react-core'; <Label isOverflowLabel variant="outline" />`,
output: `import { Label } from '@patternfly/react-core'; <Label variant="overflow" />`,
errors: [
{
message:
'The `isOverflowLabel` prop for Label has been replaced with `variant="overflow"`. Running the fix for this rule will replace an existing `variant` prop (which had no effect if `isOverflowLabel` was used).',
type: "JSXOpeningElement",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/10037
module.exports = {
meta: { fixable: "code" },
create: function (context: Rule.RuleContext) {
const { imports } = getFromPackage(context, "@patternfly/react-core");

const labelImport = imports.find(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "Label"
);

return !labelImport
? {}
: {
JSXOpeningElement(node: JSXOpeningElement) {
if (
node.name.type === "JSXIdentifier" &&
labelImport.local.name === node.name.name
) {
const isOverflowLabelAttribute = node.attributes.find(
(attr) =>
attr.type === "JSXAttribute" &&
attr.name?.name === "isOverflowLabel"
);

if (isOverflowLabelAttribute === undefined) {
return;
}

const variantAttribute = node.attributes.find(
(attr) =>
attr.type === "JSXAttribute" && attr.name?.name === "variant"
);

const baseMessage =
'The `isOverflowLabel` prop for Label has been replaced with `variant="overflow"`.';

context.report({
node,
message: variantAttribute
? `${baseMessage} Running the fix for this rule will replace an existing \`variant\` prop (which had no effect if \`isOverflowLabel\` was used).`
: baseMessage,
fix: <Rule.ReportFixer>function (fixer) {
const fixes = [
fixer.replaceText(
isOverflowLabelAttribute,
'variant="overflow"'
),
];
if (variantAttribute) {
fixes.push(fixer.remove(variantAttribute));
}

return fixes;
},
});
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
import { Label } from "@patternfly/react-core";

export const LabelRemoveIsOverflowLabelInput = () => <Label isOverflowLabel />;
export const LabelRemoveIsOverflowLabelInput2 = () => (
<Label isOverflowLabel variant="outline" />
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { Label } from "@patternfly/react-core";

export const LabelRemoveIsOverflowLabelInput = () => (
<Label variant="overflow" />
);
export const LabelRemoveIsOverflowLabelInput2 = () => (
<Label variant="overflow" />
);
Loading