Skip to content

Commit

Permalink
feat(misc): don't use "any" type
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Oct 30, 2024
1 parent 1d5507c commit e9f8593
Show file tree
Hide file tree
Showing 12 changed files with 209 additions and 86 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ export function isEnumValue(
propertyName: string | string[]
) {
if (
enumExpression.object?.type === "Identifier" &&
enumExpression.object.name === enumName
enumExpression?.object?.type === "Identifier" &&
enumExpression?.object?.name === enumName
) {
const nameMatches = (name: string) =>
propertyNameMatches(
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Rule } from "eslint";
import { JSXElement, JSXFragment } from "estree-jsx";
import { JSXElement, JSXFragment, MemberExpression } from "estree-jsx";
import {
childrenIsEmpty,
getFromPackage,
Expand All @@ -10,6 +10,8 @@ import {
getChildJSXElementByName,
isReactIcon,
makeJSXElementSelfClosing,
propertyNameMatches,
isEnumValue,
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10663
Expand Down Expand Up @@ -47,11 +49,16 @@ module.exports = {
variantProp?.value
);

const variantValueAsEnum = variantValue as MemberExpression;

const isEnumValuePlain =
buttonVariantEnumImport &&
variantValue?.object?.name ===
buttonVariantEnumImport.local.name &&
variantValue?.property.name === "plain";
!!buttonVariantEnumImport &&
isEnumValue(
context,
variantValueAsEnum,
buttonVariantEnumImport.local.name,
"plain"
);

const isPlain = variantValue === "plain" || isEnumValuePlain;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { Rule } from "eslint";
import { JSXElement, Property, Literal } from "estree-jsx";
import { JSXElement, ObjectExpression, Property } from "estree-jsx";
import {
getAllImportsFromPackage,
getFromPackage,
checkMatchingJSXOpeningElement,
getAttribute,
Expand Down Expand Up @@ -54,19 +53,23 @@ module.exports = {
const selectableActionsValue = getAttributeValue(
context,
selectableActionsProp.value
);
) as ObjectExpression["properties"];
if (!selectableActionsValue) {
return;
}

const selectableActionsProperties = selectableActionsValue.filter(
(val) => val.type === "Property"
) as Property[];

const nameProperty = getObjectProperty(
context,
selectableActionsValue,
selectableActionsProperties,
"name"
);
const idProperty = getObjectProperty(
context,
selectableActionsValue,
selectableActionsProperties,
"selectableActionId"
);

Expand All @@ -92,7 +95,7 @@ module.exports = {
return [];
}
const propertiesToKeep = removePropertiesFromObjectExpression(
selectableActionsValue,
selectableActionsProperties,
validPropertiesToRemove
);
const replacementProperties = propertiesToKeep
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,10 @@ module.exports = {
return;
}

const colorValue = getAttributeValue(context, colorProp.value);
const colorValue = getAttributeValue(
context,
colorProp.value
) as string;
if (Object.keys(replacementMap).includes(colorValue)) {
const newColorValue = replacementMap[colorValue];
const message = `The \`color\` prop on ${node.name.name} has been updated to replace "${colorValue}" with "${newColorValue}".`;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,9 +28,11 @@ module.exports = {
node,
"statusPageLinkText"
);
const statusPageLinkTextString: string =
getAttributeValue(context, statusPageLinkTextProp?.value) ??
"status page";
const statusPageLinkTextString =
(getAttributeValue(
context,
statusPageLinkTextProp?.value
) as string) ?? "status page";

if (statusPageLinkTextProp && statusPageLinkTextString.length) {
const firstChar = statusPageLinkTextString.charAt(0);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
import { JSXOpeningElement, MemberExpression } from "estree-jsx";
import {
getFromPackage,
getAttribute,
getAttributeValue,
isEnumValue,
getEnumPropertyName,
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10211
module.exports = {
Expand Down Expand Up @@ -38,18 +44,30 @@ module.exports = {
context,
colorVariantProp.value
);
const drawerColorVariantLocalName =
drawerColorVariantEnumImport &&
drawerColorVariantEnumImport.local.name;

const colorVariantValueAsEnum =
colorVariantValue as MemberExpression;

const hasPatternFlyEnum =
colorVariantValue &&
colorVariantValue.object &&
colorVariantValue.object.name === drawerColorVariantLocalName;
drawerColorVariantEnumImport &&
colorVariantValueAsEnum &&
colorVariantValueAsEnum.object &&
context
.getSourceCode()
.getText(colorVariantValueAsEnum.object) ===
drawerColorVariantEnumImport.local.name;

const isNoBackgroundEnum =
!!drawerColorVariantEnumImport &&
isEnumValue(
context,
colorVariantValueAsEnum,
drawerColorVariantEnumImport.local.name,
"noBackground"
);

const hasNoBackgroundValue =
colorVariantValue && colorVariantValue.property
? hasPatternFlyEnum &&
colorVariantValue.property.name === "noBackground"
: colorVariantValue === "no-background";
colorVariantValue === "no-background" || isNoBackgroundEnum;

if (!hasPatternFlyEnum && !hasNoBackgroundValue) {
return;
Expand All @@ -68,15 +86,19 @@ module.exports = {
}

if (!hasNoBackgroundValue && hasPatternFlyEnum) {
const enumPropertyName = colorVariantValue.property.name;
fixes.push(
fixer.replaceText(
colorVariantProp,
validDrawerContentValues.includes(enumPropertyName)
? `colorVariant="${colorVariantValue.property.name}"`
: ""
)
const enumPropertyName = getEnumPropertyName(
context,
colorVariantValueAsEnum
);
enumPropertyName &&
fixes.push(
fixer.replaceText(
colorVariantProp,
validDrawerContentValues.includes(enumPropertyName)
? `colorVariant="${enumPropertyName}"`
: ""
)
);
}
return fixes;
},
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
import { JSXOpeningElement, MemberExpression } from "estree-jsx";
import {
getFromPackage,
getAttribute,
getAttributeValue,
isEnumValue,
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10650
module.exports = {
Expand Down Expand Up @@ -29,10 +34,17 @@ module.exports = {
}

const typeValue = getAttributeValue(context, typeProp.value);
const typeValueAsEnum = typeValue as MemberExpression;

const isEnumValueNav =
pageSectionTypeEnum &&
typeValue.object?.name === pageSectionTypeEnum.local.name &&
typeValue.property.name === "nav";
isEnumValue(
context,
typeValueAsEnum,
pageSectionTypeEnum.local.name,
"nav"
);

if (typeValue !== "nav" && !isEnumValueNav) {
return;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
import {
getFromPackage,
getAttribute,
getAttributeValue,
isEnumValue,
attributeValueIsString,
} from "../../helpers";
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { JSXOpeningElement, MemberExpression } from "estree-jsx";

// https://github.com/patternfly/patternfly-react/pull/9774
// https://github.com/patternfly/patternfly-react/pull/9848
Expand Down Expand Up @@ -34,21 +40,33 @@ module.exports = {
context,
variantProp.value
);
const pageSectionVariantLocalName =
pageSectionVariantImport && pageSectionVariantImport.local.name;
const variantValueAsEnum = variantValue as MemberExpression;

const hasPatternFlyEnum =
variantValue?.object &&
variantValue.object.name === pageSectionVariantLocalName;
const variantValueIsLiteral =
variantProp.value.type === "Literal" ||
(variantProp.value.type === "JSXExpressionContainer" &&
variantProp.value.expression.type === "Literal");
if (!variantValueIsLiteral && !hasPatternFlyEnum) {
pageSectionVariantImport &&
variantValueAsEnum?.object &&
context.getSourceCode().getText(variantValueAsEnum.object) ===
pageSectionVariantImport.local.name;

if (
!attributeValueIsString(variantProp.value) &&
!hasPatternFlyEnum
) {
return;
}
const hasValidValue = variantValue?.property
? validValues.includes(variantValue.property.name)
: validValues.includes(variantValue);

const isValidEnumValue =
pageSectionVariantImport &&
isEnumValue(
context,
variantValueAsEnum,
pageSectionVariantImport.local.name,
validValues
);

const hasValidValue =
isValidEnumValue ||
validValues.includes(variantValue as string);

if (!hasValidValue) {
context.report({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
import { Rule } from "eslint";
import { JSXOpeningElement } from "estree-jsx";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
import { JSXOpeningElement, MemberExpression } from "estree-jsx";
import {
getFromPackage,
getAttribute,
getAttributeValue,
getEnumPropertyName,
isEnumValue,
} from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10674
module.exports = {
Expand All @@ -20,6 +26,7 @@ module.exports = {
"icon-button-group": "action-group-plain",
};
const oldVariantNames = Object.keys(renames);
type OldVariantType = "button-group" | "icon-button-group";

return !componentImports.length
? {}
Expand All @@ -38,24 +45,37 @@ module.exports = {
}

const variantValue = getAttributeValue(context, variant.value);
const variantValueAsEnum = variantValue as MemberExpression;

const isEnumToRename =
variantEnumImport &&
variantValue.object?.name === variantEnumImport.local.name &&
oldVariantNames.includes(variantValue.property.value);
isEnumValue(
context,
variantValueAsEnum,
variantEnumImport.local.name,
oldVariantNames
);

if (!oldVariantNames.includes(variantValue) && !isEnumToRename) {
if (
!oldVariantNames.includes(variantValue as string) &&
!isEnumToRename
) {
return;
}

const variantToRename: "button-group" | "icon-button-group" =
variantValue.property?.value ?? variantValue;
const variantToRename = isEnumToRename
? (getEnumPropertyName(
context,
variantValueAsEnum
) as OldVariantType)
: (variantValue as OldVariantType);

context.report({
node,
message: `The \`${variantToRename}\` variant of ${applicableComponent.imported.name} has been renamed to \`${renames[variantToRename]}\`.`,
fix(fixer) {
return fixer.replaceText(
isEnumToRename ? variantValue.property : variant,
isEnumToRename ? variantValueAsEnum.property : variant,
isEnumToRename
? `"${renames[variantToRename]}"`
: `variant="${renames[variantToRename]}"`
Expand Down
Loading

0 comments on commit e9f8593

Please sign in to comment.