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

feat: data-codemods cleanup rule #745

Merged
merged 6 commits into from
Aug 28, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 @@ -69,6 +69,7 @@ export const setupRules = [

// rules that will run after other rules (cleanup imports?)
export const cleanupRules = [
"data-codemods-cleanup",
adamviktora marked this conversation as resolved.
Show resolved Hide resolved
"no-unused-imports-v5",
"no-unused-imports-v6",
"no-duplicate-import-specifiers",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
### data-codemods-cleanup

This rule will remove `data-codemods` attributes and comments, which were introduced by our codemods in order to work correctly.

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
const ruleTester = require("../../ruletester");
import * as rule from "./data-codemods-cleanup";

const message = `This rule will remove data-codemods attributes and comments, which were introduced by our codemods in order to work correctly.`;

ruleTester.run("data-codemods-cleanup", rule, {
valid: [
{
code: `import { DualListSelector /* data-codemods */ } from 'somewhereElse';`,
},
{
code: `import { LoginMainFooterLinksItem } from 'somewhereElse'; <LoginMainFooterLinksItem data-codemods="true" />`,
},
],
invalid: [
{
code: `import { DualListSelector /* data-codemods */ } from '@patternfly/react-core';`,
output: `import { DualListSelector } from '@patternfly/react-core';`,
adamviktora marked this conversation as resolved.
Show resolved Hide resolved
errors: [
{
message,
type: "ImportSpecifier",
},
],
},
// aliased import
{
code: `import { DualListSelector as DLS /* data-codemods */ } from '@patternfly/react-core';`,
output: `import { DualListSelector as DLS } from '@patternfly/react-core';`,
errors: [
{
message,
type: "ImportSpecifier",
},
],
},
{
code: `import { LoginMainFooterLinksItem } from '@patternfly/react-core'; <LoginMainFooterLinksItem data-codemods="true" />`,
output: `import { LoginMainFooterLinksItem } from '@patternfly/react-core'; <LoginMainFooterLinksItem />`,
errors: [
{
message,
type: "JSXOpeningElement",
},
],
},
{
code: `import { MastheadLogo } from '@patternfly/react-core'; <MastheadLogo data-codemods />`,
output: `import { MastheadLogo } from '@patternfly/react-core'; <MastheadLogo />`,
errors: [
{
message,
type: "JSXOpeningElement",
},
],
},
// dist imports
{
code: `import { MastheadLogo } from '@patternfly/react-core/dist/esm/components/MastheadLogo'; <MastheadLogo data-codemods />`,
output: `import { MastheadLogo } from '@patternfly/react-core/dist/esm/components/MastheadLogo'; <MastheadLogo />`,
errors: [
{
message,
type: "JSXOpeningElement",
},
],
},
{
code: `import { MastheadLogo } from '@patternfly/react-core/dist/js/components/MastheadLogo'; <MastheadLogo data-codemods />`,
output: `import { MastheadLogo } from '@patternfly/react-core/dist/js/components/MastheadLogo'; <MastheadLogo />`,
errors: [
{
message,
type: "JSXOpeningElement",
},
],
},
// with alias
{
code: `import { MastheadLogo as ML } from '@patternfly/react-core'; <ML data-codemods />`,
output: `import { MastheadLogo as ML } from '@patternfly/react-core'; <ML />`,
errors: [
{
message,
type: "JSXOpeningElement",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import { Rule } from "eslint";
import { ImportSpecifier, JSXOpeningElement } from "estree-jsx";
import {
checkMatchingJSXOpeningElement,
getAttribute,
getFromPackage,
} from "../../helpers";

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

const message =
"This rule will remove data-codemods attributes and comments, which were introduced by our codemods in order to work correctly.";

return {
JSXOpeningElement(node: JSXOpeningElement) {
if (checkMatchingJSXOpeningElement(node, imports)) {
const dataCodemodsAttribute = getAttribute(node, "data-codemods");
if (dataCodemodsAttribute) {
context.report({
node,
message,
fix(fixer) {
return fixer.remove(dataCodemodsAttribute);
},
});
}
}
},
ImportSpecifier(node: ImportSpecifier) {
if (imports.some((specifier) => specifier === node)) {
const comments = context.getSourceCode().getCommentsAfter(node);
const dataCodemodsComment = comments.find((comment) =>
comment.value.includes("data-codemods")
);
if (dataCodemodsComment) {
context.report({
node,
message,
fix(fixer) {
return fixer.removeRange(dataCodemodsComment.range!);
},
});
}
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
DualListSelector /* data-codemods */,
LoginMainFooterLinksItem,
MastheadLogo,
} from "@patternfly/react-core";

export const DataCodemodsCleanupInput = () => (
<>
<DualListSelector />
<LoginMainFooterLinksItem data-codemods="true" />
<MastheadLogo data-codemods />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import {
DualListSelector,
LoginMainFooterLinksItem,
MastheadLogo,
} from "@patternfly/react-core";

export const DataCodemodsCleanupInput = () => (
<>
<DualListSelector />
<LoginMainFooterLinksItem />
<MastheadLogo />
</>
);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the output file had the formatter ran on it after execution. Doesn't really matter now, but will if we ever implement auto checking of these tsx files in CI.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, I thought we should run prettier on these output files, I do it every time 🤦

Loading