Skip to content

Commit

Permalink
feat(codemod): rename NotAuthorized to UnauthorizedAccess
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Sep 23, 2024
1 parent f182182 commit dbfc10b
Show file tree
Hide file tree
Showing 7 changed files with 196 additions and 79 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,88 +6,99 @@ const renameMap = {
title: "titleText",
};

const errors = Object.entries(renameMap).map(([oldName, newName]) => ({
message: `The ${oldName} prop for NotAuthorized has been renamed to ${newName}.`,
type: "JSXOpeningElement",
}));
const getErrors = (component: string) =>
Object.entries(renameMap).map(([oldName, newName]) => ({
message: `The ${oldName} prop for ${component} has been renamed to ${newName}.`,
type: "JSXOpeningElement",
}));

ruleTester.run("component-groups-notAuthorized-rename-props", rule, {
valid: [
const components = ["NotAuthorized", "UnauthorizedAccess"];

const valid = components
.map((component) => [
{
code: `<NotAuthorized description="" />`,
code: `<${component} description="" />`,
},
{
code: `<NotAuthorized title="" />`,
code: `<${component} title="" />`,
},
{
code: `import { NotAuthorized } from '@patternfly/react-component-groups'; <NotAuthorized someOtherProp />`,
code: `import { ${component} } from '@patternfly/react-component-groups'; <${component} someOtherProp />`,
},
],
invalid: [
])
.flat();

const invalid = components
.map((component) => [
{
code: `import { NotAuthorized } from '@patternfly/react-component-groups';
<NotAuthorized
description="Description text"
title="Title text"
/>`,
output: `import { NotAuthorized } from '@patternfly/react-component-groups';
<NotAuthorized
bodyText="Description text"
titleText="Title text"
/>`,
errors,
code: `import { ${component} } from '@patternfly/react-component-groups';
<${component}
description="Description text"
title="Title text"
/>`,
output: `import { ${component} } from '@patternfly/react-component-groups';
<${component}
bodyText="Description text"
titleText="Title text"
/>`,
errors: getErrors(component),
},
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/cjs/NotAuthorized/index';
<NotAuthorized
description="Description text"
title="Title text"
/>`,
output: `import NotAuthorized from '@patternfly/react-component-groups/dist/cjs/NotAuthorized/index';
<NotAuthorized
bodyText="Description text"
titleText="Title text"
/>`,
errors,
code: `import ${component} from '@patternfly/react-component-groups/dist/cjs/${component}/index';
<${component}
description="Description text"
title="Title text"
/>`,
output: `import ${component} from '@patternfly/react-component-groups/dist/cjs/${component}/index';
<${component}
bodyText="Description text"
titleText="Title text"
/>`,
errors: getErrors(component),
},
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/esm/NotAuthorized/index';
<NotAuthorized
description="Description text"
title="Title text"
/>`,
output: `import NotAuthorized from '@patternfly/react-component-groups/dist/esm/NotAuthorized/index';
<NotAuthorized
bodyText="Description text"
titleText="Title text"
/>`,
errors,
code: `import ${component} from '@patternfly/react-component-groups/dist/esm/${component}/index';
<${component}
description="Description text"
title="Title text"
/>`,
output: `import ${component} from '@patternfly/react-component-groups/dist/esm/${component}/index';
<${component}
bodyText="Description text"
titleText="Title text"
/>`,
errors: getErrors(component),
},
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized';
<NotAuthorized
description="Description text"
title="Title text"
/>`,
output: `import NotAuthorized from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized';
<NotAuthorized
bodyText="Description text"
titleText="Title text"
/>`,
errors,
code: `import ${component} from '@patternfly/react-component-groups/dist/dynamic/${component}';
<${component}
description="Description text"
title="Title text"
/>`,
output: `import ${component} from '@patternfly/react-component-groups/dist/dynamic/${component}';
<${component}
bodyText="Description text"
titleText="Title text"
/>`,
errors: getErrors(component),
},
{
code: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized';
<NotAuth
description="Description text"
title="Title text"
/>`,
output: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized';
<NotAuth
bodyText="Description text"
titleText="Title text"
/>`,
errors,
code: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/${component}';
<NotAuth
description="Description text"
title="Title text"
/>`,
output: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/${component}';
<NotAuth
bodyText="Description text"
titleText="Title text"
/>`,
errors: getErrors(component),
},
],
])
.flat();

ruleTester.run("component-groups-notAuthorized-rename-props", rule, {
valid,
invalid,
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,27 @@ import { Renames } from "../../helpers/renameSinglePropOnNode";

// https://github.com/patternfly/react-component-groups/pull/145

const formatMessage = (oldPropName: string, newPropName: string) =>
`The ${oldPropName} prop for NotAuthorized has been renamed to ${newPropName}.`;
const formatMessage = (
component: string,
oldPropName: string,
newPropName: string
) =>
`The ${oldPropName} prop for ${component} has been renamed to ${newPropName}.`;

const renames: Renames = {
NotAuthorized: {
description: {
newName: "bodyText",
message: formatMessage("description", "bodyText"),
},
title: {
newName: "titleText",
message: formatMessage("title", "titleText"),
},
const getPropsRenames = (component: string) => ({
description: {
newName: "bodyText",
message: formatMessage(component, "description", "bodyText"),
},
title: {
newName: "titleText",
message: formatMessage(component, "title", "titleText"),
},
});

const renames: Renames = {
NotAuthorized: getPropsRenames("NotAuthorized"),
UnauthorizedAccess: getPropsRenames("UnauthorizedAccess"),
};

module.exports = {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### component-groups-notAuthorized-rename-to-unauthorizedAccess [(react-component-groups/#313)](https://github.com/patternfly/react-component-groups/pull/313)

In react-component-groups, we've renamed NotAuthorized component to UnauthorizedAccess

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const ruleTester = require("../../ruletester");
import * as rule from "./component-groups-notAuthorized-rename-to-unauthorizedAccess";

const errors = [
{
message: `NotAuthorized has been renamed to UnauthorizedAccess.`,
type: "JSXOpeningElement",
},
];

ruleTester.run(
"component-groups-notAuthorized-rename-to-unauthorizedAccess",
rule,
{
valid: [
// missing import
{
code: `<NotAuthorized />`,
},
// import from wrong package
{
code: `import { NotAuthorized } from '@patternfly/react-core'; <NotAuthorized />`,
},
],
invalid: [
{
code: `import { NotAuthorized } from '@patternfly/react-component-groups'; <NotAuthorized />`,
output: `import { UnauthorizedAccess } from '@patternfly/react-component-groups'; <UnauthorizedAccess data-codemods />`,
errors,
},
// named import with alias
{
code: `import { NotAuthorized as NotAuth } from '@patternfly/react-component-groups'; <NotAuth />`,
output: `import { UnauthorizedAccess as NotAuth } from '@patternfly/react-component-groups'; <NotAuth />`,
errors,
},
// default imports
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/cjs/NotAuthorized/index'; <NotAuthorized />`,
output: `import UnauthorizedAccess from '@patternfly/react-component-groups/dist/cjs/UnauthorizedAccess/index'; <UnauthorizedAccess data-codemods />`,
errors,
},
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/esm/NotAuthorized/index'; <NotAuthorized />`,
output: `import UnauthorizedAccess from '@patternfly/react-component-groups/dist/esm/UnauthorizedAccess/index'; <UnauthorizedAccess data-codemods />`,
errors,
},
{
code: `import NotAuthorized from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized'; <NotAuthorized />`,
output: `import UnauthorizedAccess from '@patternfly/react-component-groups/dist/dynamic/UnauthorizedAccess'; <UnauthorizedAccess data-codemods />`,
errors,
},
// default import with name not matching the component name
{
code: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/NotAuthorized'; <NotAuth />`,
output: `import NotAuth from '@patternfly/react-component-groups/dist/dynamic/UnauthorizedAccess'; <NotAuth />`,
errors,
},
],
}
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { renameComponent } from "../../helpers/renameComponent";

// https://github.com/patternfly/react-component-groups/pull/313
module.exports = {
meta: { fixable: "code" },
create: renameComponent(
{
NotAuthorized: "UnauthorizedAccess",
},
"@patternfly/react-component-groups"
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { NotAuthorized } from "@patternfly/react-component-groups";

export const ComponentGroupsNotAuthorizedRenameToUnauthorizedAccessInput =
() => <NotAuthorized />;
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { UnauthorizedAccess } from "@patternfly/react-component-groups";

export const ComponentGroupsNotAuthorizedRenameToUnauthorizedAccessInput =
() => <UnauthorizedAccess data-codemods />;

0 comments on commit dbfc10b

Please sign in to comment.