Skip to content

Commit

Permalink
chore(rules): type existing rules (#596)
Browse files Browse the repository at this point in the history
  • Loading branch information
thatblindgeye authored Feb 28, 2024
1 parent 23968dd commit 05bdaf3
Show file tree
Hide file tree
Showing 8 changed files with 106 additions and 134 deletions.
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9876
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(
const accordionItemImport = imports.find(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "AccordionItem"
);

return !componentImports.length
return !accordionItemImport
? {}
: {
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" &&
accordionItemImport.local.name === node.name.name
) {
context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,47 +1,37 @@
import { getFromPackage, findAncestor } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9876
module.exports = {
meta: { fixable: "code" },
create: function (context: {
getSourceCode: () => {
getText(node: any): string;
};
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(
const accordionToggleImport = imports.find(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "AccordionToggle"
);

return !componentImports.length
return !accordionToggleImport
? {}
: {
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" &&
accordionToggleImport.local.name === node.name.name
) {
const attribute = node.attributes.find(
(attr: { name: { name: string } }) =>
attr.name?.name === "isExpanded"
(attr) =>
attr.type === "JSXAttribute" &&
attr.name.name === "isExpanded"
);
if (attribute) {
context.report({
node,
message:
"The `isExpanded` prop for AccordionToggle has been moved to AccordionItem.",
fix(fixer: {
replaceText: (arg0: any, arg1: string) => any;
insertTextAfter: (arg0: string, arg1: string) => any;
}) {
fix(fixer) {
const accordionItemAncestor = findAncestor(
node,
(current) =>
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,25 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/10036
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(
const drawerHeadImport = imports.find(
(specifier: { imported: { name: string } }) =>
specifier.imported.name === "DrawerHead"
);

return !componentImports.length
return !drawerHeadImport
? {}
: {
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" &&
drawerHeadImport.local.name === node.name.name
) {
context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,18 @@
import { getFromPackage, findVariableDeclaration } from "../../helpers";
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import {
MemberExpression,
Identifier,
JSXOpeningElement,
JSXAttribute,
} from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/10017
// https://github.com/patternfly/patternfly-react/pull/10036
// Possible TODOs: check for variable references passed in as values
module.exports = {
meta: { fixable: "code" },
create: function (context: {
getScope: () => {
upper: any;
variables: { defs: { node: { init: { value: string } } }[] }[];
};
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(
Expand All @@ -32,16 +29,13 @@ module.exports = {
return !componentImports.length && !colorVariantEnumImport.length
? {}
: {
MemberExpression(node: {
object: { name: string };
property: { name: string };
}) {
MemberExpression(node: MemberExpression) {
if (
colorVariantEnumImport
.map((imp: { local: { name: any } }) => imp.local.name)
.includes(node.object.name)
.map((imp) => imp.local.name)
.includes((node.object as Identifier).name)
) {
if (node.property.name === "light200") {
if ((node.property as Identifier).name === "light200") {
context.report({
node,
message:
Expand All @@ -55,25 +49,35 @@ module.exports = {
}
}
},
JSXOpeningElement(node: { name: { name: any }; attributes: any[] }) {
JSXOpeningElement(node: JSXOpeningElement) {
if (
node.name.type === "JSXIdentifier" &&
componentImports
.map((imp: { local: { name: any } }) => imp.local.name)
.map((imp) => imp.local.name)
.includes(node.name.name)
) {
const attribute = node.attributes.find(
(attr: { name: { name: string } }) =>
attr.name?.name === "colorVariant"
);
(attr) =>
attr.type === "JSXAttribute" &&
attr.name.name === "colorVariant"
) as JSXAttribute | undefined;

if (attribute && attribute.value.value === "light-200") {
if (!attribute || !attribute.value) {
return;
}
if (
attribute.value.type === "Literal" &&
typeof attribute.value.value === "string" &&
attribute.value.value === "light-200"
) {
context.report({
node,
message: `The "light-200" value for the \`colorVariant\` prop has been replaced with the "secondary" value for ${node.name.name}.`,
fix(fixer: {
replaceText: (arg0: any, arg1: string) => any;
}) {
return fixer.replaceText(attribute.value, '"secondary"');
fix(fixer) {
return fixer.replaceText(
attribute,
'colorVariant="secondary"'
);
},
});
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,25 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";

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

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

return !componentImports.length
return !helperTextItemImport
? {}
: {
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" &&
helperTextItemImport.local.name === node.name.name
) {
context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,11 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { ImportDeclaration } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/10026
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 simpleFileUploadImport = imports.find(
Expand All @@ -20,14 +16,13 @@ module.exports = {
return !simpleFileUploadImport
? {}
: {
ImportDeclaration(node: {
specifiers: { imported: { name: string } }[];
}) {
ImportDeclaration(node: ImportDeclaration) {
if (
node.specifiers.find(
(specifier: { imported: { name: string } }) =>
(specifier) =>
specifier.type === "ImportSpecifier" &&
specifier.imported.name ===
simpleFileUploadImport.imported.name
simpleFileUploadImport.imported.name
)
) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
import { getFromPackage } from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement, JSXAttribute } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9930
// https://github.com/patternfly/patternfly-react/pull/10044
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, exports } = getFromPackage(
context,
"@patternfly/react-core"
Expand All @@ -24,21 +20,31 @@ module.exports = {
return !tabsImport
? {}
: {
JSXOpeningElement(node: { name: { name: any }; attributes: any[] }) {
if (node.name.name === tabsImport.local.name) {
JSXOpeningElement(node: JSXOpeningElement) {
if (
node.name.type === "JSXIdentifier" &&
tabsImport.local.name === node.name.name
) {
const attribute = node.attributes.find(
(attr: { name: { name: string }; value: { value: string } }) =>
attr.name?.name === "variant"
);
if (attribute && attribute.value.value === "light300") {
(attr) =>
attr.type === "JSXAttribute" && attr.name.name === "variant"
) as JSXAttribute | undefined;

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

if (
attribute.value.type === "Literal" &&
typeof attribute.value.value === "string" &&
attribute.value.value === "light300"
) {
context.report({
node,
message:
'The "light300" value for the `variant` prop on Tabs has been replaced with the "secondary" value.',
fix(fixer: {
replaceText: (arg0: any, arg1: string) => any;
}) {
return fixer.replaceText(attribute.value, '"secondary"');
fix(fixer) {
return fixer.replaceText(attribute, 'variant="secondary"');
},
});
}
Expand Down
Loading

0 comments on commit 05bdaf3

Please sign in to comment.