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

fix(PageSection): updated logic to account for variant enum #657

Merged
merged 2 commits into from
Jun 13, 2024
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 @@ -15,6 +15,15 @@ ruleTester.run("pageSection-update-variant-values", rule, {
{
code: `import { PageSection } from '@patternfly/react-core'; <PageSection variant="secondary" />`,
},
{
code: `import { PageSection } from '@patternfly/react-core'; <PageSection variant={PageSectionVariants.dark} />`,
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; <PageSection variant={PageSectionVariants.default} />`,
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; <PageSection variant={PageSectionVariants.secondary} />`,
},
],
invalid: [
{
Expand All @@ -27,5 +36,105 @@ ruleTester.run("pageSection-update-variant-values", rule, {
},
],
},
Copy link
Contributor

Choose a reason for hiding this comment

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

Can we add test for the variant={"dark"}?

Suggested change
},
},
{
code: `import { PageSection } from '@patternfly/react-core'; <PageSection variant={"dark"} />`,
output: `import { PageSection } from '@patternfly/react-core'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},

{
code: `import { PageSection } from '@patternfly/react-core'; <PageSection variant={"dark"} />`,
output: `import { PageSection } from '@patternfly/react-core'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection as CustomPageSection } from '@patternfly/react-core'; <CustomPageSection variant="dark" />`,
output: `import { PageSection as CustomPageSection } from '@patternfly/react-core'; <CustomPageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; <PageSection variant={PageSectionVariants.dark} />`,
output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection, PageSectionVariants as CustomVariant } from '@patternfly/react-core'; <PageSection variant={CustomVariant.dark} />`,
output: `import { PageSection, PageSectionVariants as CustomVariant } from '@patternfly/react-core'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection } from '@patternfly/react-core/dist/esm/components/Page/index.js'; <PageSection variant="dark" />`,
output: `import { PageSection } from '@patternfly/react-core/dist/esm/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection } from '@patternfly/react-core/dist/js/components/Page/index.js'; <PageSection variant="dark" />`,
output: `import { PageSection } from '@patternfly/react-core/dist/js/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; <PageSection variant="dark" />`,
output: `import { PageSection } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/esm/components/Page/index.js'; <PageSection variant={PageSectionVariants.dark} />`,
output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/esm/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/js/components/Page/index.js'; <PageSection variant={PageSectionVariants.dark} />`,
output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/js/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
{
code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; <PageSection variant={PageSectionVariants.dark} />`,
output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; <PageSection />`,
errors: [
{
message: `The \`variant\` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".`,
type: "JSXOpeningElement",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { getFromPackage } from "../../helpers";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement, JSXAttribute, Literal } from "estree-jsx";
import { isJsxAttribute } from "typescript";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9774
// https://github.com/patternfly/patternfly-react/pull/9848
Expand All @@ -13,6 +12,10 @@ module.exports = {
const pageSectionImport = imports.find(
(specifier) => specifier.imported.name === "PageSection"
);
const pageSectionVariantImport = imports.find(
(specifier) => specifier.imported.name === "PageSectionVariants"
);
const validValues = ["default", "secondary"];

return !pageSectionImport
? {}
Expand All @@ -22,28 +25,38 @@ module.exports = {
node.name.type === "JSXIdentifier" &&
pageSectionImport.local.name === node.name.name
) {
const attribute = node.attributes.find(
(attr) =>
attr.type === "JSXAttribute" && attr.name.name === "variant"
) as JSXAttribute | undefined;
const variantProp = getAttribute(node, "variant");

if (!attribute || !attribute.value) {
if (!variantProp || !variantProp.value) {
return;
}
const variantValue = getAttributeValue(
context,
variantProp.value
);
const pageSectionVariantLocalName =
pageSectionVariantImport && pageSectionVariantImport.local.name;
const hasPatternFlyEnum =
variantValue.object &&
variantValue.object.name === pageSectionVariantLocalName;
const variantValueIsLiteral =
variantProp.value.type === "Literal" ||
(variantProp.value.type === "JSXExpressionContainer" &&
variantProp.value.expression.type === "Literal");
if (!variantValueIsLiteral && !hasPatternFlyEnum) {
return;
}
const hasValidValue = variantValue.property
? validValues.includes(variantValue.property.name)
: validValues.includes(variantValue);

if (
attribute.value.type === "Literal" &&
typeof attribute.value.value === "string" &&
!["default", "secondary"].includes(attribute.value.value)
) {
if (!hasValidValue) {
context.report({
node,
message:
'The `variant` prop for PageSection now only accepts a value of "default" or "secondary". Running the fix for this rule will remove the prop so it uses the default value of "default".',
fix(fixer: {
replaceText: (arg0: any, arg1: string) => any;
}) {
return fixer.replaceText(attribute, "");
fix(fixer) {
return fixer.replaceText(variantProp, "");
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { PageSection } from "@patternfly/react-core";
import { PageSection, PageSectionVariants } from "@patternfly/react-core";

export const PageSectionUpdateVariantValuesInput = () => (
<PageSection variant='dark' />
<>
<PageSection variant='dark' />
<PageSection variant={PageSectionVariants.dark} />
</>
);
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import { PageSection } from "@patternfly/react-core";
import { PageSection, PageSectionVariants } from "@patternfly/react-core";

export const PageSectionUpdateVariantValuesInput = () => (
<PageSection />
<>
<PageSection />
<PageSection />
</>
);
Loading