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(LoginPage): updated markup for LoginMainHeader #752

Merged
merged 5 commits into from
Sep 2, 2024
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 @@ -27,6 +27,7 @@ export const warningRules = [
"horizontalSubnav-warn-ariaLabel",
"jumpLinksItem-warn-markup-change",
"label-warn-truncated-default",
"loginMainHeader-warn-updated-markup",
"logViewer-moved-styles",
"menuItemAction-warn-update-markup",
"menuToggle-warn-iconOnly-toggle",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ export * from "./interfaces";
export * from "./isReactIcon";
export * from "./JSXAttributes";
export * from "./makeJSXElementSelfClosing";
export * from "./nodeMatches";
export * from "./nodeMatches/checkMatchingImportDeclaration";
export * from "./nodeMatches/checkMatchingJSXOpeningElement";
export * from "./pfPackageMatches";
export * from "./removeElement";
export * from "./removeEmptyLineAfter";
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { ImportDeclaration, ImportSpecifier } from "estree-jsx";
import { pfPackageMatches } from "../pfPackageMatches";

function checkSpecifierExists(
node: ImportDeclaration,
importSpecifier: ImportSpecifier
) {
return node.specifiers.some(
(specifier) =>
specifier.type === "ImportSpecifier" &&
specifier.imported.name === importSpecifier.imported.name
);
}

/** Used to check whether the current ImportDeclaration node matches at least 1 of the import specifiers. */
export function checkMatchingImportDeclaration(
Copy link
Contributor

Choose a reason for hiding this comment

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

Can you refactor this helper to return only boolean? Or if there is a purpose of returning the specifier to return it as ImportSpecifier with the type casting. Depends on whether we need the specifier in a return value, but in the array case it would just return the first specifier, so maybe using .some everywhere (instead of .find) would be better.

node: ImportDeclaration,
imports: ImportSpecifier | ImportSpecifier[],
packageName: string = "@patternfly/react-core"
) {
if (!pfPackageMatches(packageName, node.source.value)) {
return false;
}

if (Array.isArray(imports)) {
return imports.some((imp) => checkSpecifierExists(node, imp));
}

return checkSpecifierExists(node, imports);
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import {
ImportDefaultSpecifier,
} from "estree-jsx";

/** Used to check whether the current JSXOpeningElement node matches at least 1 of the import specifiers. */
export function checkMatchingJSXOpeningElement(
node: JSXOpeningElement,
imports:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ImportDeclaration } from "estree-jsx";

// TODO: Swap the params so that packageName has a default value of pf/react-core and can be optional
export function pfPackageMatches(
packageName: string,
nodeSrc: ImportDeclaration["source"]["value"]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
### loginMainHeader-warn-updated-markup [(#10880)](https://github.com/patternfly/patternfly-react/pull/10880)

The markup for LoginMainHeader - which is used internally within LoginPage - has been updated, now using a `div` wrapper instead of a `header` element wrapper.
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const ruleTester = require("../../ruletester");
import * as rule from "./loginMainHeader-warn-updated-markup";

ruleTester.run("loginMainHeader-warn-updated-markup", rule, {
valid: [
{
code: `<LoginMainHeader />`,
},
{
code: `import { LoginMainHeader } from 'somewhere-else'`,
},
{
code: `<LoginPage />`,
},
{
code: `import { LoginPage } from 'somewhere-else'`,
},
],
invalid: [
{
code: `import { LoginMainHeader } from '@patternfly/react-core';`,
output: `import { LoginMainHeader } from '@patternfly/react-core';`,
errors: [
{
message: `The markup for LoginMainHeader has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginPage } from '@patternfly/react-core';`,
output: `import { LoginPage } from '@patternfly/react-core';`,
errors: [
{
message: `The markup for LoginMainHeader (which is used internally within LoginPage) has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginPage, LoginMainHeader, Button } from '@patternfly/react-core';`,
output: `import { LoginPage, LoginMainHeader, Button } from '@patternfly/react-core';`,
errors: [
{
message: `The markup for LoginMainHeader (which is used internally within LoginPage) has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginMainHeader as CustomThing } from '@patternfly/react-core';`,
output: `import { LoginMainHeader as CustomThing } from '@patternfly/react-core';`,
errors: [
{
message: `The markup for LoginMainHeader has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginMainHeader } from '@patternfly/react-core/dist/esm/components/LoginPage/index.js';`,
output: `import { LoginMainHeader } from '@patternfly/react-core/dist/esm/components/LoginPage/index.js';`,
errors: [
{
message: `The markup for LoginMainHeader has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginMainHeader } from '@patternfly/react-core/dist/js/components/LoginPage/index.js';`,
output: `import { LoginMainHeader } from '@patternfly/react-core/dist/js/components/LoginPage/index.js';`,
errors: [
{
message: `The markup for LoginMainHeader has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
{
code: `import { LoginMainHeader } from '@patternfly/react-core/dist/dynamic/components/LoginPage/index.js';`,
output: `import { LoginMainHeader } from '@patternfly/react-core/dist/dynamic/components/LoginPage/index.js';`,
errors: [
{
message: `The markup for LoginMainHeader has been updated, now using a div wrapper instead of a header element wrapper.`,
type: "ImportDeclaration",
},
],
},
],
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import { Rule } from "eslint";
import { ImportDeclaration } from "estree-jsx";
import { getFromPackage, checkMatchingImportDeclaration } from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10880
module.exports = {
meta: {},
create: function (context: Rule.RuleContext) {
const basePackage = "@patternfly/react-core";
const { imports: loginImports } = getFromPackage(context, basePackage, [
"LoginPage",
"LoginMainHeader",
]);

return !loginImports.length
? {}
: {
ImportDeclaration(node: ImportDeclaration) {
if (checkMatchingImportDeclaration(node, loginImports)) {
const hasLoginPageImport = loginImports.find(
(imp) => imp.imported.name === "LoginPage"
);
context.report({
node,
message: `The markup for LoginMainHeader${
hasLoginPageImport
? " (which is used internally within LoginPage)"
: ""
} has been updated, now using a div wrapper instead of a header element wrapper.`,
});
}
},
};
},
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LoginMainHeader, LoginPage } from "@patternfly/react-core";

export const LoginMainHeaderWarnUpdatedMarkupInput = () => (
<>
<LoginPage />
<LoginMainHeader />
</>
);
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { LoginMainHeader, LoginPage } from "@patternfly/react-core";

export const LoginMainHeaderWarnUpdatedMarkupInput = () => (
<>
<LoginPage />
<LoginMainHeader />
</>
);
Loading