Skip to content

Commit

Permalink
Added typing to PageSection rules
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye committed Feb 26, 2024
1 parent c5e7364 commit 49ef5b6
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 36 deletions.
Original file line number Diff line number Diff line change
@@ -1,39 +1,39 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement, JSXAttribute, Literal } from "estree-jsx";
import { isJsxAttribute } from "typescript";

// https://github.com/patternfly/patternfly-react/pull/9774
// https://github.com/patternfly/patternfly-react/pull/9848
module.exports = {
meta: { fixable: "code" },
create: function (context: {
report: (arg0: {
node: any;
message: string;
fix(fixer: any): any;
}) => void;
}) {
create: function (context: Rule.RuleContext) {
const { imports } = getFromPackage(context, "@patternfly/react-core");

const componentImports = imports.filter(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "PageSection"
const pageSectionImport = imports.find(
(specifier) => specifier.imported.name === "PageSection"
);

return !componentImports.length
return !pageSectionImport
? {}
: {
JSXOpeningElement(node: { name: { name: any }; attributes: any[] }) {
JSXOpeningElement(node: JSXOpeningElement) {
if (
componentImports
.map((imp: { local: { name: any } }) => imp.local.name)
.includes(node.name.name)
node.name.type === "JSXIdentifier" &&
pageSectionImport.local.name === node.name.name
) {
const attribute = node.attributes.find(
(attr: { name: { name: string } }) =>
attr.name?.name === "variant"
);
(attr) =>
attr.type === "JSXAttribute" && attr.name.name === "variant"
) as JSXAttribute | undefined;

if (!attribute || !attribute.value) {
return;
}

if (
attribute &&
attribute.value.type === "Literal" &&
typeof attribute.value.value === "string" &&
!["default", "secondary"].includes(attribute.value.value)
) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,34 +1,28 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9848
module.exports = {
meta: {},
create: function (context: {
report: (arg0: {
node: any;
message: string;
fix?(fixer: any): any;
}) => void;
}) {
create: function (context: Rule.RuleContext) {
const { imports } = getFromPackage(context, "@patternfly/react-core");

const componentImports = imports.filter(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "PageSection"
const pageSectionImport = imports.find(
(specifier) => specifier.imported.name === "PageSection"
);

return !componentImports.length
return !pageSectionImport
? {}
: {
JSXOpeningElement(node: { name: { name: any }; attributes: any[] }) {
JSXOpeningElement(node: JSXOpeningElement) {
if (
componentImports
.map((imp: { local: { name: any } }) => imp.local.name)
.includes(node.name.name)
node.name.type === "JSXIdentifier" &&
pageSectionImport.local.name === node.name.name
) {
const attribute = node.attributes.find(
(attr: { name: { name: string } }) =>
attr.name?.name === "variant"
(attr) =>
attr.type === "JSXAttribute" && attr.name.name === "variant"
);
if (attribute) {
context.report({
Expand Down

0 comments on commit 49ef5b6

Please sign in to comment.