Skip to content

Commit ccfc9bd

Browse files
committed
fix(misc): get rid of type casting / explain type casting
1 parent e9f8593 commit ccfc9bd

File tree

7 files changed

+67
-59
lines changed

7 files changed

+67
-59
lines changed

packages/eslint-plugin-pf-codemods/src/rules/helpers/JSXAttributes.ts

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -95,19 +95,29 @@ export function getVariableDeclaration(
9595
return undefined;
9696
}
9797

98+
export function getVariableInit(
99+
variableDeclaration: Scope.Variable | undefined
100+
) {
101+
if (!variableDeclaration || !variableDeclaration.defs.length) {
102+
return;
103+
}
104+
105+
const variableDefinition = variableDeclaration.defs[0];
106+
107+
if (variableDefinition.type !== "Variable") {
108+
return;
109+
}
110+
111+
return variableDefinition.node.init;
112+
}
113+
98114
export function getVariableValue(
99115
name: string,
100116
scope: Scope.Scope | null,
101117
context: Rule.RuleContext
102118
) {
103119
const variableDeclaration = getVariableDeclaration(name, scope);
104-
if (!variableDeclaration) {
105-
return;
106-
}
107-
108-
const variableInit = variableDeclaration.defs.length
109-
? variableDeclaration.defs[0].node.init
110-
: undefined;
120+
const variableInit = getVariableInit(variableDeclaration);
111121

112122
if (!variableInit) {
113123
return;
@@ -120,12 +130,12 @@ export function getVariableValue(
120130
);
121131
}
122132
if (variableInit.type === "Literal") {
123-
return variableInit.value as string;
133+
return variableInit.value;
124134
}
125135
if (variableInit.type === "MemberExpression") {
126-
return variableInit as MemberExpression;
136+
return variableInit;
127137
}
128138
if (variableInit.type === "ObjectExpression") {
129-
return variableInit.properties as Property[];
139+
return variableInit.properties;
130140
}
131141
}
Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,28 +1,26 @@
11
import { Rule } from "eslint";
2-
import { Identifier, MemberExpression } from "estree-jsx";
3-
import { getVariableDeclaration } from ".";
2+
import { MemberExpression } from "estree-jsx";
3+
import { getVariableValue } from ".";
44

55
/** Used to get a property name on an enum (MemberExpression). */
66
export function getEnumPropertyName(
77
context: Rule.RuleContext,
88
enumNode: MemberExpression
99
) {
10-
const isIdentifier = enumNode.property.type === "Identifier";
11-
const computed = enumNode.computed;
10+
if (enumNode.property.type === "Identifier") {
11+
// E.g. const key = "key"; someEnum[key]
12+
if (enumNode.computed) {
13+
const scope = context.getSourceCode().getScope(enumNode);
14+
const propertyName = enumNode.property.name;
1215

13-
// E.g. const key = "key"; someEnum[key]
14-
if (isIdentifier && computed) {
15-
const scope = context.getSourceCode().getScope(enumNode);
16-
const propertyName = (enumNode.property as Identifier).name;
17-
const propertyVariable = getVariableDeclaration(propertyName, scope);
18-
return propertyVariable?.defs[0].node.init.value as string;
19-
}
20-
// E.g. someEnum.key
21-
if (isIdentifier && !computed) {
22-
return (enumNode.property as Identifier).name;
16+
return getVariableValue(propertyName, scope, context)?.toString();
17+
}
18+
// E.g. someEnum.key
19+
return enumNode.property.name;
2320
}
21+
2422
// E.g. someEnum["key"]
2523
if (enumNode.property.type === "Literal") {
26-
return enumNode.property.value as string;
24+
return enumNode.property.value?.toString();
2725
}
2826
}

packages/eslint-plugin-pf-codemods/src/rules/helpers/getNodeForAttributeFixer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Rule } from "eslint";
22
import { JSXAttribute } from "estree-jsx";
3-
import { getVariableDeclaration } from "./JSXAttributes";
3+
import { getVariableDeclaration, getVariableInit } from "./JSXAttributes";
44

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

44-
return variableDeclaration && variableDeclaration.defs[0].node.init;
44+
return getVariableInit(variableDeclaration);
4545
}
4646
}

packages/eslint-plugin-pf-codemods/src/rules/helpers/propertyNameMatches.ts

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { Rule } from "eslint";
2-
import { Expression, Identifier, PrivateIdentifier } from "estree-jsx";
3-
import { getVariableDeclaration } from "./JSXAttributes";
2+
import { Expression, PrivateIdentifier } from "estree-jsx";
3+
import { getVariableValue } from "./JSXAttributes";
44

55
/** Check whether a property name is of a given value.
66
* Property can either be of an ObjectExpression - {propName: "value"} or MemberExpression - someObject.propName */
@@ -10,19 +10,23 @@ export function propertyNameMatches(
1010
computed: boolean,
1111
name: string
1212
) {
13-
const isIdentifier = key.type === "Identifier";
13+
if (key.type === "Identifier") {
14+
// E.g. const key = "key"; {[key]: value}; someObject[key]
15+
if (computed) {
16+
const scope = context.getSourceCode().getScope(key);
17+
const propertyName = key.name;
18+
const propertyVariableValue = getVariableValue(
19+
propertyName,
20+
scope,
21+
context
22+
);
1423

15-
// E.g. const key = "key"; {[key]: value}; someObject[key]
16-
if (isIdentifier && computed) {
17-
const scope = context.getSourceCode().getScope(key);
18-
const propertyName = (key as Identifier).name;
19-
const propertyVariable = getVariableDeclaration(propertyName, scope);
20-
return propertyVariable?.defs[0].node.init.value === name;
21-
}
22-
// E.g. {key: value}; someObject.key
23-
if (isIdentifier && !computed) {
24-
return (key as Identifier).name === name;
24+
return propertyVariableValue === name;
25+
}
26+
// E.g. {key: value}; someObject.key
27+
return key.name === name;
2528
}
29+
2630
// E.g. {"key": value} or {["key"]: value}; someObject["key"]
2731
if (key.type === "Literal") {
2832
return key.value === name;

packages/eslint-plugin-pf-codemods/src/rules/v6/cardUpdatedClickableMarkup/card-updated-clickable-markup.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,14 +53,14 @@ module.exports = {
5353
const selectableActionsValue = getAttributeValue(
5454
context,
5555
selectableActionsProp.value
56-
) as ObjectExpression["properties"];
56+
) as ObjectExpression["properties"]; // selectableActions prop on CardHeader accepts an object
5757
if (!selectableActionsValue) {
5858
return;
5959
}
6060

6161
const selectableActionsProperties = selectableActionsValue.filter(
6262
(val) => val.type === "Property"
63-
) as Property[];
63+
);
6464

6565
const nameProperty = getObjectProperty(
6666
context,
@@ -99,7 +99,7 @@ module.exports = {
9999
validPropertiesToRemove
100100
);
101101
const replacementProperties = propertiesToKeep
102-
.map((property: Property) =>
102+
.map((property) =>
103103
context.getSourceCode().getText(property)
104104
)
105105
.join(", ");
@@ -108,6 +108,11 @@ module.exports = {
108108
context,
109109
selectableActionsProp
110110
);
111+
112+
if (!nodeToUpdate) {
113+
return [];
114+
}
115+
111116
return fixer.replaceText(
112117
nodeToUpdate,
113118
propertiesToKeep.length

packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarReplacedSpacerSpaceItems/toolbar-replaced-spacer-spaceItems.ts

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Rule } from "eslint";
22
import { JSXOpeningElement, ObjectExpression } from "estree-jsx";
3-
import { Property } from "estree-jsx";
43
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
54

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

4443
context.report({
4544
node,

packages/eslint-plugin-pf-codemods/src/rules/v6/toolbarUpdateAlignValues/toolbar-update-align-values.ts

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,5 @@
11
import { Rule } from "eslint";
2-
import {
3-
Identifier,
4-
JSXOpeningElement,
5-
ObjectExpression,
6-
Property,
7-
} from "estree-jsx";
2+
import { JSXOpeningElement, ObjectExpression } from "estree-jsx";
83
import { getFromPackage, getAttribute, getAttributeValue } from "../../helpers";
94

105
const componentsPropMap: { [key: string]: string } = {
@@ -57,7 +52,8 @@ module.exports = {
5752
context,
5853
attribute.value
5954
) as ObjectExpression["properties"]
60-
).filter((prop) => prop.type === "Property") as Property[];
55+
) // align prop on Toolbar[Component] accepts an object
56+
.filter((prop) => prop.type === "Property");
6157
if (
6258
attributeValueProperties.every(
6359
(property) =>
@@ -82,17 +78,13 @@ module.exports = {
8278
continue;
8379
}
8480

85-
const propertyValueString = property.value.value as string;
81+
const propertyValueString = property.value.value as string; // value is expected to be "alignLeft" or "alignRight"
8682

8783
if (oldPropValues.includes(propertyValueString)) {
88-
const propertyKeyValue =
89-
property.key.type === "Literal"
90-
? `"${property.key.value}"`
91-
: (property.key as Identifier).name;
9284
fixes.push(
9385
fixer.replaceText(
94-
property,
95-
`${propertyKeyValue}: "${newPropValueMap[propertyValueString]}"`
86+
property.value,
87+
`"${newPropValueMap[propertyValueString]}"`
9688
)
9789
);
9890
}

0 commit comments

Comments
 (0)