Skip to content

Commit

Permalink
fix(misc): get rid of type casting / explain type casting
Browse files Browse the repository at this point in the history
  • Loading branch information
adamviktora committed Nov 12, 2024
1 parent e9f8593 commit 72f488f
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 60 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,6 @@ import {
JSXEmptyExpression,
JSXFragment,
JSXOpeningElement,
MemberExpression,
Property,
} from "estree-jsx";

export function getAttribute(
Expand Down Expand Up @@ -95,19 +93,29 @@ export function getVariableDeclaration(
return undefined;
}

export function getVariableInit(
variableDeclaration: Scope.Variable | undefined
) {
if (!variableDeclaration || !variableDeclaration.defs.length) {
return;
}

const variableDefinition = variableDeclaration.defs[0];

if (variableDefinition.type !== "Variable") {
return;
}

return variableDefinition.node.init;
}

export function getVariableValue(
name: string,
scope: Scope.Scope | null,
context: Rule.RuleContext
) {
const variableDeclaration = getVariableDeclaration(name, scope);
if (!variableDeclaration) {
return;
}

const variableInit = variableDeclaration.defs.length
? variableDeclaration.defs[0].node.init
: undefined;
const variableInit = getVariableInit(variableDeclaration);

if (!variableInit) {
return;
Expand All @@ -120,12 +128,12 @@ export function getVariableValue(
);
}
if (variableInit.type === "Literal") {
return variableInit.value as string;
return variableInit.value;
}
if (variableInit.type === "MemberExpression") {
return variableInit as MemberExpression;
return variableInit;
}
if (variableInit.type === "ObjectExpression") {
return variableInit.properties as Property[];
return variableInit.properties;
}
}
Original file line number Diff line number Diff line change
@@ -1,28 +1,26 @@
import { Rule } from "eslint";
import { Identifier, MemberExpression } from "estree-jsx";
import { getVariableDeclaration } from ".";
import { MemberExpression } from "estree-jsx";
import { getVariableValue } from ".";

/** Used to get a property name on an enum (MemberExpression). */
export function getEnumPropertyName(
context: Rule.RuleContext,
enumNode: MemberExpression
) {
const isIdentifier = enumNode.property.type === "Identifier";
const computed = enumNode.computed;
if (enumNode.property.type === "Identifier") {
// E.g. const key = "key"; someEnum[key]
if (enumNode.computed) {
const scope = context.getSourceCode().getScope(enumNode);
const propertyName = enumNode.property.name;

// E.g. const key = "key"; someEnum[key]
if (isIdentifier && computed) {
const scope = context.getSourceCode().getScope(enumNode);
const propertyName = (enumNode.property as Identifier).name;
const propertyVariable = getVariableDeclaration(propertyName, scope);
return propertyVariable?.defs[0].node.init.value as string;
}
// E.g. someEnum.key
if (isIdentifier && !computed) {
return (enumNode.property as Identifier).name;
return getVariableValue(propertyName, scope, context)?.toString();
}
// E.g. someEnum.key
return enumNode.property.name;
}

// E.g. someEnum["key"]
if (enumNode.property.type === "Literal") {
return enumNode.property.value as string;
return enumNode.property.value?.toString();
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from "eslint";
import { JSXAttribute } from "estree-jsx";
import { getVariableDeclaration } from "./JSXAttributes";
import { getVariableDeclaration, getVariableInit } from "./JSXAttributes";

/** Used to find the node where a prop value is initially assigned, to then be passed
* as a fixer function's nodeOrToken argument. Useful for when a prop may have an inline value, e.g. `<Comp prop="value" />`, or
Expand Down Expand Up @@ -41,6 +41,6 @@ function getJSXExpressionContainerValue(
scope
);

return variableDeclaration && variableDeclaration.defs[0].node.init;
return getVariableInit(variableDeclaration);
}
}
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Rule } from "eslint";
import { Expression, Identifier, PrivateIdentifier } from "estree-jsx";
import { getVariableDeclaration } from "./JSXAttributes";
import { Expression, PrivateIdentifier } from "estree-jsx";
import { getVariableValue } from "./JSXAttributes";

/** Check whether a property name is of a given value.
* Property can either be of an ObjectExpression - {propName: "value"} or MemberExpression - someObject.propName */
Expand All @@ -10,19 +10,23 @@ export function propertyNameMatches(
computed: boolean,
name: string
) {
const isIdentifier = key.type === "Identifier";
if (key.type === "Identifier") {
// E.g. const key = "key"; {[key]: value}; someObject[key]
if (computed) {
const scope = context.getSourceCode().getScope(key);
const propertyName = key.name;
const propertyVariableValue = getVariableValue(
propertyName,
scope,
context
);

// E.g. const key = "key"; {[key]: value}; someObject[key]
if (isIdentifier && computed) {
const scope = context.getSourceCode().getScope(key);
const propertyName = (key as Identifier).name;
const propertyVariable = getVariableDeclaration(propertyName, scope);
return propertyVariable?.defs[0].node.init.value === name;
}
// E.g. {key: value}; someObject.key
if (isIdentifier && !computed) {
return (key as Identifier).name === name;
return propertyVariableValue === name;
}
// E.g. {key: value}; someObject.key
return key.name === name;
}

// E.g. {"key": value} or {["key"]: value}; someObject["key"]
if (key.type === "Literal") {
return key.value === name;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ module.exports = {
const selectableActionsValue = getAttributeValue(
context,
selectableActionsProp.value
) as ObjectExpression["properties"];
) as ObjectExpression["properties"]; // selectableActions prop on CardHeader accepts an object
if (!selectableActionsValue) {
return;
}
Expand Down Expand Up @@ -99,7 +99,7 @@ module.exports = {
validPropertiesToRemove
);
const replacementProperties = propertiesToKeep
.map((property: Property) =>
.map((property) =>
context.getSourceCode().getText(property)
)
.join(", ");
Expand All @@ -108,6 +108,11 @@ module.exports = {
context,
selectableActionsProp
);

if (!nodeToUpdate) {
return [];
}

return fixer.replaceText(
nodeToUpdate,
propertiesToKeep.length
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Rule } from "eslint";
import { JSXOpeningElement, ObjectExpression } from "estree-jsx";
import { Property } from "estree-jsx";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";

// https://github.com/patternfly/patternfly-react/pull/10418
Expand Down Expand Up @@ -39,7 +38,7 @@ module.exports = {
(getAttributeValue(
context,
spacerProp.value
) as ObjectExpression["properties"]);
) as ObjectExpression["properties"]); // spacer prop on Toolbar[Component] accepts an object

context.report({
node,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,5 @@
import { Rule } from "eslint";
import {
Identifier,
JSXOpeningElement,
ObjectExpression,
Property,
} from "estree-jsx";
import { JSXOpeningElement, ObjectExpression, Property } from "estree-jsx";
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";

const componentsPropMap: { [key: string]: string } = {
Expand Down Expand Up @@ -57,7 +52,8 @@ module.exports = {
context,
attribute.value
) as ObjectExpression["properties"]
).filter((prop) => prop.type === "Property") as Property[];
) // align prop on Toolbar[Component] accepts an object
.filter((prop) => prop.type === "Property") as Property[];
if (
attributeValueProperties.every(
(property) =>
Expand All @@ -82,17 +78,13 @@ module.exports = {
continue;
}

const propertyValueString = property.value.value as string;
const propertyValueString = property.value.value as string; // value is expected to be "alignLeft" or "alignRight"

if (oldPropValues.includes(propertyValueString)) {
const propertyKeyValue =
property.key.type === "Literal"
? `"${property.key.value}"`
: (property.key as Identifier).name;
fixes.push(
fixer.replaceText(
property,
`${propertyKeyValue}: "${newPropValueMap[propertyValueString]}"`
property.value,
`"${newPropValueMap[propertyValueString]}"`
)
);
}
Expand Down

0 comments on commit 72f488f

Please sign in to comment.