From c50fa191b50a6004ffbfe92351e24f60b386811f Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Mon, 26 Aug 2024 17:19:43 -0400 Subject: [PATCH 1/2] fix(Text): Replace missed text variant enums with content variant enum --- .../text-replace-with-content.test.ts | 10 +++++++ .../text-replace-with-content.ts | 28 ++++++++++++++----- .../textReplaceWithContentInput.tsx | 5 ++++ .../textReplaceWithContentOutput.tsx | 5 ++++ 4 files changed, 41 insertions(+), 7 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts index 2887c87b2..8afb380c8 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts @@ -141,6 +141,16 @@ ruleTester.run("text-replace-with-content", rule, { output: `import { ContentVariants } from '@patternfly/react-core'; const foo = ContentVariants.h1`, errors: [importDeclarationError, identifierError], }, + { + code: `import { TextListVariants } from '@patternfly/react-core'; const foo = TextListVariants.ul`, + output: `import { ContentVariants } from '@patternfly/react-core'; const foo = ContentVariants.ul`, + errors: [importDeclarationError, identifierError], + }, + { + code: `import { TextListItemVariants } from '@patternfly/react-core'; const foo = TextListItemVariants.li`, + output: `import { ContentVariants } from '@patternfly/react-core'; const foo = ContentVariants.li`, + errors: [importDeclarationError, identifierError], + }, { code: `import { TextProps } from '@patternfly/react-core'; interface Foo extends TextProps {}`, output: `import { ContentProps } from '@patternfly/react-core'; interface Foo extends ContentProps {}`, diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.ts index 743697160..d97ade698 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.ts @@ -14,6 +14,7 @@ import { } from "../../helpers"; // https://github.com/patternfly/patternfly-react/pull/10643 + module.exports = { meta: { fixable: "code" }, create: function (context: Rule.RuleContext) { @@ -21,7 +22,12 @@ module.exports = { const textComponents = ["Text", "TextContent", "TextList", "TextListItem"]; - const nonComponentTextIdentifiers = ["TextProps", "TextVariants"]; + const nonComponentTextIdentifiers = [ + "TextProps", + "TextVariants", + "TextListVariants", + "TextListItemVariants", + ]; const allTextIdentifiers = [ ...textComponents, @@ -35,6 +41,18 @@ module.exports = { const errorMessage = "We have replaced Text, TextContent, TextList and TextListItem with one Content component. Running this fix will change all of those components names to Content and add a `component` prop where necessary."; + function getNewText(specifier: string) { + if (textComponents.includes(specifier)) { + return "Content"; + } + + if (specifier === "TextProps") { + return "ContentProps"; + } + + return "ContentVariants"; + } + return !textImports.length ? {} : { @@ -51,11 +69,7 @@ module.exports = { } const specifierName = specifierToReplace.imported.name; - const newText = nonComponentTextIdentifiers.includes( - specifierName - ) - ? specifierName.replace("Text", "Content") - : "Content"; + const newText = getNewText(specifierName); context.report({ node, @@ -173,7 +187,7 @@ module.exports = { return; } - const newText = node.name.replace("Text", "Content"); + const newText = getNewText(node.name); context.report({ node, diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentInput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentInput.tsx index faffca5d3..b1e9abdcc 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentInput.tsx +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentInput.tsx @@ -2,13 +2,18 @@ import { Text, TextContent, TextList, + TextListVariants, TextListItem, + TextListItemVariants, TextProps, TextVariants, } from "@patternfly/react-core"; export const TextReplaceWithContentInput = () => { interface Foo extends TextProps {} + const foo = TextVariants.h1; + const bar = TextListVariants.ul; + const baz = TextListItemVariants.li; return ( <> diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentOutput.tsx b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentOutput.tsx index e6c336c87..cc6b6652b 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentOutput.tsx +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/textReplaceWithContentOutput.tsx @@ -2,13 +2,18 @@ import { Content, Content, Content, + ContentVariants, Content, + ContentVariants, ContentProps, ContentVariants, } from "@patternfly/react-core"; export const TextReplaceWithContentInput = () => { interface Foo extends ContentProps {} + const foo = ContentVariants.h1; + const bar = ContentVariants.ul; + const baz = ContentVariants.li; return ( <> From c06eec351eb54bc238c73cee00c5181c6c947c95 Mon Sep 17 00:00:00 2001 From: Austin Sullivan Date: Tue, 27 Aug 2024 15:36:20 -0400 Subject: [PATCH 2/2] chore(Text): Add test for passing enum directly as prop value --- .../text-replace-with-content.test.ts | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts index 8afb380c8..6316b72db 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/textReplaceWithContent/text-replace-with-content.test.ts @@ -151,6 +151,23 @@ ruleTester.run("text-replace-with-content", rule, { output: `import { ContentVariants } from '@patternfly/react-core'; const foo = ContentVariants.li`, errors: [importDeclarationError, identifierError], }, + // phase one of passing an enum directly as a prop + { + code: `import { TextListItem, TextListItemVariants } from '@patternfly/react-core'; Abc`, + output: `import { Content, TextListItemVariants } from '@patternfly/react-core'; Abc`, + errors: [ + importDeclarationError, + jsxOpeningElementError, + identifierError, + jsxClosingElementError, + ], + }, + // phase two of passing an enum directly as a prop + { + code: `import { Content, TextListItemVariants } from '@patternfly/react-core'; Abc`, + output: `import { Content, ContentVariants } from '@patternfly/react-core'; Abc`, + errors: [importDeclarationError], + }, { code: `import { TextProps } from '@patternfly/react-core'; interface Foo extends TextProps {}`, output: `import { ContentProps } from '@patternfly/react-core'; interface Foo extends ContentProps {}`,