Skip to content

Commit

Permalink
feat(Modal): promoted Next implementation (#660)
Browse files Browse the repository at this point in the history
* feat(Modal): promoted Next implementation

* Created test helpers file
  • Loading branch information
thatblindgeye authored Jun 11, 2024
1 parent 258601d commit 9a08828
Show file tree
Hide file tree
Showing 12 changed files with 381 additions and 24 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { RuleTester } from "eslint";

export type ValidTests = Array<string | RuleTester.ValidTestCase>;
export type InvalidTests = RuleTester.InvalidTestCase[];
type TestErrors = {
message: string;
type: string;
}[];

export function createValidTest(code: string) {
return {
code,
};
}
export function createInvalidTest(
code: string,
output: string,
errors: TestErrors
) {
return {
code,
output,
errors,
};
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### modal-deprecated [(#10358)](https://github.com/patternfly/patternfly-react/pull/10358)

Our previous implementation of Modal has been deprecated. This rule will update import paths to our deprecated directory, but we recommend using our newly promoted implementation.

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
const ruleTester = require("../../ruletester");
import * as rule from "./modal-deprecated";
import {
ValidTests,
InvalidTests,
createValidTest,
createInvalidTest,
} from "../../helpers/testHelpers";

const specifiersToMove = [
"Modal",
"ModalVariant",
"ModalBox",
"ModalBoxBody",
"ModalBoxCloseButton",
"ModalBoxHeader",
"ModalBoxFooter",
"ModalContent",
];

const validTests: ValidTests = [];
const invalidTests: InvalidTests = [];

specifiersToMove.forEach((specifier) => {
if (!specifier.endsWith("Variant")) {
validTests.push(createValidTest(`<${specifier} />`));
}
validTests.push(
createValidTest(
`import { ${specifier} /* data-codemods */ } from '@patternfly/react-core';`
)
);
validTests.push(
createValidTest(
`import { ${specifier} } from '@patternfly/react-core/deprecated';`
)
);
validTests.push(
createValidTest(
`export { ${specifier} /* data-codemods */ } from '@patternfly/react-core';`
)
);
validTests.push(
createValidTest(
`export { ${specifier} } from '@patternfly/react-core/deprecated';`
)
);

const errorMessage = `${specifier} has been deprecated. This rule will update import paths to our deprecated directory, but we recommend using our newly promoted Modal implementation.`;
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core';`,
`import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} as CustomSpecifier } from '@patternfly/react-core';`,
`import {\n\t${specifier} as CustomSpecifier\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/esm/components/Modal/index.js';`,
`import {\n\t${specifier}\n} from '@patternfly/react-core/dist/esm/deprecated/components/Modal/index.js';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`,
`import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`,
`import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core';`,
`export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/esm/components/Modal/index.js';`,
`export {\n\t${specifier}\n} from '@patternfly/react-core/dist/esm/deprecated/components/Modal/index.js';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/js/components/Modal/index.js';`,
`export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/dynamic/components/Modal/index.js';`,
`export {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier}, Button } from '@patternfly/react-core';`,
`import {\n\tButton\n} from '@patternfly/react-core';
import {\n\t${specifier}\n} from '@patternfly/react-core/deprecated';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
});

ruleTester.run("modal-deprecated", rule, {
valid: validTests,
invalid: invalidTests,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { moveSpecifiers } from "../../helpers";

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

const specifiersToMove = [
"Modal",
"ModalVariant",
"ModalBox",
"ModalBoxBody",
"ModalBoxCloseButton",
"ModalBoxHeader",
"ModalBoxFooter",
"ModalContent",
];

const fromPackage = "@patternfly/react-core";
const toPackage = "@patternfly/react-core/deprecated";
const messageAfterImportNameChange =
"been deprecated. This rule will update import paths to our deprecated directory, but we recommend using our newly promoted Modal implementation.";

module.exports = {
meta: { fixable: "code" },
create: moveSpecifiers(
specifiersToMove,
fromPackage,
toPackage,
messageAfterImportNameChange
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Modal } from "@patternfly/react-core";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {
Modal
} from '@patternfly/react-core/deprecated';
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
### modalNext-promoted [(#10358)](https://github.com/patternfly/patternfly-react/pull/10358)

Our Next implementation of Modal has been promoted as our recommended implementation. This rule will update import paths.

#### Examples

In:

```jsx
%inputExample%
```

Out:

```jsx
%outputExample%
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
const ruleTester = require("../../ruletester");
import * as rule from "./modalNext-promoted";
import {
ValidTests,
InvalidTests,
createValidTest,
createInvalidTest,
} from "../../helpers/testHelpers";

const specifiersToMove = ["Modal", "ModalBody", "ModalHeader", "ModalFooter"];
const validTests: ValidTests = [];
const invalidTests: InvalidTests = [];

specifiersToMove.forEach((specifier) => {
if (!specifier.endsWith("Variant")) {
validTests.push(createValidTest(`<${specifier} />`));
}
validTests.push(
createValidTest(
`import { ${specifier} /* data-codemods */ } from '@patternfly/react-core';`
)
);
validTests.push(
createValidTest(`import { ${specifier} } from '@patternfly/react-core';`)
);
validTests.push(
createValidTest(
`export { ${specifier} /* data-codemods */ } from '@patternfly/react-core';`
)
);
validTests.push(
createValidTest(`export { ${specifier} } from '@patternfly/react-core';`)
);

const errorMessage = `${specifier} has been promoted. This rule will update import paths.`;
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/next';`,
`import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} as CustomSpecifier } from '@patternfly/react-core/next';`,
`import {\n\t${specifier} as CustomSpecifier /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/esm/next/components/Modal/index.js';`,
`import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/esm/components/Modal/index.js';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`,
`import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`,
`import {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/next';`,
`export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/esm/next/components/Modal/index.js';`,
`export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core/dist/esm/components/Modal/index.js';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/js/next/components/Modal/index.js';`,
`export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`export { ${specifier} } from '@patternfly/react-core/dist/dynamic/next/components/Modal/index.js';`,
`export {\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ExportNamedDeclaration" }]
)
);
invalidTests.push(
createInvalidTest(
`import { ${specifier} } from '@patternfly/react-core/next';\nimport { Button } from '@patternfly/react-core';`,
`\nimport {\n\tButton,\n\t${specifier} /* data-codemods */\n} from '@patternfly/react-core';`,
[{ message: errorMessage, type: "ImportDeclaration" }]
)
);
});

ruleTester.run("modalNext-promoted", rule, {
valid: validTests,
invalid: invalidTests,
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import { moveSpecifiers } from "../../helpers";

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

const specifiersToMove = ["Modal", "ModalBody", "ModalHeader", "ModalFooter"];

const fromPackage = "@patternfly/react-core/next";
const toPackage = "@patternfly/react-core";
const messageAfterImportNameChange =
"been promoted. This rule will update import paths.";

module.exports = {
meta: { fixable: "code" },
create: moveSpecifiers(
specifiersToMove,
fromPackage,
toPackage,
messageAfterImportNameChange
),
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
import { Modal } from "@patternfly/react-core/next";
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import {
Modal /* data-codemods */
} from '@patternfly/react-core';
Loading

0 comments on commit 9a08828

Please sign in to comment.