diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.test.ts index 9a61d6c72..6dab77e15 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.test.ts @@ -15,6 +15,15 @@ ruleTester.run("pageSection-update-variant-values", rule, { { code: `import { PageSection } from '@patternfly/react-core'; `, }, + { + code: `import { PageSection } from '@patternfly/react-core'; `, + }, + { + code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; `, + }, + { + code: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; `, + }, ], invalid: [ { @@ -27,5 +36,95 @@ ruleTester.run("pageSection-update-variant-values", rule, { }, ], }, + { + code: `import { PageSection as CustomPageSection } from '@patternfly/react-core'; `, + output: `import { PageSection as CustomPageSection } from '@patternfly/react-core'; `, + 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'; `, + output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core'; `, + 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'; `, + output: `import { PageSection, PageSectionVariants as CustomVariant } from '@patternfly/react-core'; `, + 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'; `, + output: `import { PageSection } from '@patternfly/react-core/dist/esm/components/Page/index.js'; `, + 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'; `, + output: `import { PageSection } from '@patternfly/react-core/dist/js/components/Page/index.js'; `, + 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'; `, + output: `import { PageSection } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; `, + 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'; `, + output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/esm/components/Page/index.js'; `, + 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'; `, + output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/js/components/Page/index.js'; `, + 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'; `, + output: `import { PageSection, PageSectionVariants } from '@patternfly/react-core/dist/dynamic/components/Page/index.js'; `, + 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", + }, + ], + }, ], }); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.ts index 6a395a44d..eadbb61e0 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSection-update-variant-values.ts @@ -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 @@ -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 ? {} @@ -22,20 +25,30 @@ 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: @@ -43,7 +56,7 @@ module.exports = { fix(fixer: { replaceText: (arg0: any, arg1: string) => any; }) { - return fixer.replaceText(attribute, ""); + return fixer.replaceText(variantProp, ""); }, }); } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesInput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesInput.tsx index e7bc04aca..e93343613 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesInput.tsx +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesInput.tsx @@ -1,5 +1,8 @@ -import { PageSection } from "@patternfly/react-core"; +import { PageSection, PageSectionVariants } from "@patternfly/react-core"; export const PageSectionUpdateVariantValuesInput = () => ( - + <> + + + ); diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesOutput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesOutput.tsx index 2997d73bd..735e20a26 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesOutput.tsx +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/pageSectionUpdateVariantValues/pageSectionUpdateVariantValuesOutput.tsx @@ -1,5 +1,8 @@ -import { PageSection } from "@patternfly/react-core"; +import { PageSection, PageSectionVariants } from "@patternfly/react-core"; export const PageSectionUpdateVariantValuesInput = () => ( - + <> + + + );