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(Text): Replace missed text variant enums with content variant enum #750

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 @@ -141,6 +141,33 @@ 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],
},
// phase one of passing an enum directly as a prop
{
code: `import { TextListItem, TextListItemVariants } from '@patternfly/react-core'; <TextListItem component={TextListItemVariants.li}>Abc</TextListItem>`,
output: `import { Content, TextListItemVariants } from '@patternfly/react-core'; <Content component={ContentVariants.li}>Abc</Content>`,
errors: [
importDeclarationError,
jsxOpeningElementError,
identifierError,
jsxClosingElementError,
],
},
// phase two of passing an enum directly as a prop
{
code: `import { Content, TextListItemVariants } from '@patternfly/react-core'; <Content component={ContentVariants.li}>Abc</Content>`,
output: `import { Content, ContentVariants } from '@patternfly/react-core'; <Content component={ContentVariants.li}>Abc</Content>`,
errors: [importDeclarationError],
},
{
code: `import { TextProps } from '@patternfly/react-core'; interface Foo extends TextProps {}`,
output: `import { ContentProps } from '@patternfly/react-core'; interface Foo extends ContentProps {}`,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,20 @@ import {
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10643

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

const textComponents = ["Text", "TextContent", "TextList", "TextListItem"];

const nonComponentTextIdentifiers = ["TextProps", "TextVariants"];
const nonComponentTextIdentifiers = [
"TextProps",
"TextVariants",
"TextListVariants",
"TextListItemVariants",
];

const allTextIdentifiers = [
...textComponents,
Expand All @@ -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
? {}
: {
Expand All @@ -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,
Expand Down Expand Up @@ -173,7 +187,7 @@ module.exports = {
return;
}

const newText = node.name.replace("Text", "Content");
const newText = getNewText(node.name);

context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
<>
Expand Down
Loading