Skip to content

Commit

Permalink
fix(PageSection): updated logic to account for variant enum
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye committed Jun 6, 2024
1 parent 7a5a9f4 commit 8d87126
Show file tree
Hide file tree
Showing 4 changed files with 136 additions and 18 deletions.
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,95 @@ ruleTester.run("pageSection-update-variant-values", rule, {
},
],
},
{
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, JSXAttribute } 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 (!variantProp || !variantProp.value) {
return;
}
const variantValue = getAttributeValue(
context,
variantProp.value
);
const pageSectionVariantLocalName =
pageSectionVariantImport && pageSectionVariantImport.local.name;
const hasPatternFlyEnum =
variantValue.object &&
variantValue.object.name === pageSectionVariantLocalName;

if (!attribute || !attribute.value) {
if (variantProp.value.type !== "Literal" && !hasPatternFlyEnum) {
return;
}
const hasValidValue = variantValue.property
? validValues.includes(variantValue.property.name)
: validValues.includes(variantValue);
console.log(hasValidValue);

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, "");
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 />
</>
);

0 comments on commit 8d87126

Please sign in to comment.