From 72f488f4712c3ad9963a012fd009bfb453a88db3 Mon Sep 17 00:00:00 2001 From: adamviktora Date: Tue, 12 Nov 2024 13:40:01 +0100 Subject: [PATCH] fix(misc): get rid of type casting / explain type casting --- .../src/rules/helpers/JSXAttributes.ts | 32 ++++++++++++------- .../src/rules/helpers/getEnumPropertyName.ts | 28 ++++++++-------- .../rules/helpers/getNodeForAttributeFixer.ts | 4 +-- .../src/rules/helpers/propertyNameMatches.ts | 30 +++++++++-------- .../card-updated-clickable-markup.ts | 9 ++++-- .../toolbar-replaced-spacer-spaceItems.ts | 3 +- .../toolbar-update-align-values.ts | 20 ++++-------- 7 files changed, 66 insertions(+), 60 deletions(-) diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.ts index 22ca349d..03c9cba7 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.ts @@ -6,8 +6,6 @@ import { JSXEmptyExpression, JSXFragment, JSXOpeningElement, - MemberExpression, - Property, } from "estree-jsx"; export function getAttribute( @@ -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; @@ -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; } } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getEnumPropertyName.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getEnumPropertyName.ts index c4b2e0e8..3970f5d0 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getEnumPropertyName.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getEnumPropertyName.ts @@ -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(); } } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getNodeForAttributeFixer.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getNodeForAttributeFixer.ts index 2c72749d..0113d143 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/getNodeForAttributeFixer.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/getNodeForAttributeFixer.ts @@ -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. ``, or @@ -41,6 +41,6 @@ function getJSXExpressionContainerValue( scope ); - return variableDeclaration && variableDeclaration.defs[0].node.init; + return getVariableInit(variableDeclaration); } } diff --git a/packages/eslint-plugin-pf-codemods/src/rules/helpers/propertyNameMatches.ts b/packages/eslint-plugin-pf-codemods/src/rules/helpers/propertyNameMatches.ts index abc549df..22353a39 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/helpers/propertyNameMatches.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/helpers/propertyNameMatches.ts @@ -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 */ @@ -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; diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/cardUpdatedClickableMarkup/card-updated-clickable-markup.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/cardUpdatedClickableMarkup/card-updated-clickable-markup.ts index 29972658..8c326b96 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/cardUpdatedClickableMarkup/card-updated-clickable-markup.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/cardUpdatedClickableMarkup/card-updated-clickable-markup.ts @@ -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; } @@ -99,7 +99,7 @@ module.exports = { validPropertiesToRemove ); const replacementProperties = propertiesToKeep - .map((property: Property) => + .map((property) => context.getSourceCode().getText(property) ) .join(", "); @@ -108,6 +108,11 @@ module.exports = { context, selectableActionsProp ); + + if (!nodeToUpdate) { + return []; + } + return fixer.replaceText( nodeToUpdate, propertiesToKeep.length diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarReplacedSpacerSpaceItems/toolbar-replaced-spacer-spaceItems.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarReplacedSpacerSpaceItems/toolbar-replaced-spacer-spaceItems.ts index 960f017b..9cb2896a 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarReplacedSpacerSpaceItems/toolbar-replaced-spacer-spaceItems.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarReplacedSpacerSpaceItems/toolbar-replaced-spacer-spaceItems.ts @@ -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 @@ -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, diff --git a/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarUpdateAlignValues/toolbar-update-align-values.ts b/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarUpdateAlignValues/toolbar-update-align-values.ts index 8d39365c..3caa5dbe 100644 --- a/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarUpdateAlignValues/toolbar-update-align-values.ts +++ b/packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarUpdateAlignValues/toolbar-update-align-values.ts @@ -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 } = { @@ -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) => @@ -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]}"` ) ); }