Skip to content

Commit de55f71

Browse files
authored
Remove type assertions for ObjectExpression (#919)
1 parent 538670b commit de55f71

File tree

7 files changed

+52
-43
lines changed

7 files changed

+52
-43
lines changed

.changeset/modern-dryers-sing.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Remove type assertions for ObjectExpression

src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,8 @@ import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
22

33
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
44

5-
export const getArgsWithoutWaiterConfig = <T>(callArgument: T): T => {
6-
if ((callArgument as ObjectExpression).type !== "ObjectExpression") {
7-
return callArgument;
8-
}
9-
10-
const objectExpression = callArgument as ObjectExpression;
11-
objectExpression.properties = objectExpression.properties.filter((property) => {
5+
export const getArgsWithoutWaiterConfig = (options: ObjectExpression): ObjectExpression => {
6+
options.properties = options.properties.filter((property) => {
127
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
138
return true;
149
}
@@ -22,6 +17,5 @@ export const getArgsWithoutWaiterConfig = <T>(callArgument: T): T => {
2217
return true;
2318
});
2419

25-
// @ts-expect-error Type 'ObjectExpression' is not assignable to type 'T'.
26-
return objectExpression;
20+
return options;
2721
};

src/transforms/v2-to-v3/apis/getWaiterConfig.ts

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
22

33
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
44

5-
export const getWaiterConfig = (callArgument: unknown): ObjectExpression | undefined => {
6-
if ((callArgument as ObjectExpression).type !== "ObjectExpression") {
7-
return;
8-
}
9-
for (const property of (callArgument as ObjectExpression).properties) {
5+
export const getWaiterConfig = (originalConfig: ObjectExpression): ObjectExpression | undefined => {
6+
for (const property of originalConfig.properties) {
107
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
118
continue;
129
}

src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,7 @@ import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
22

33
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
44

5-
export const getWaiterConfigValue = (
6-
waiterConfiguration: ObjectExpression | undefined,
7-
key: string
8-
): string | undefined => {
9-
if (!waiterConfiguration) {
10-
return;
11-
}
5+
export const getWaiterConfigValue = (waiterConfiguration: ObjectExpression, key: string) => {
126
for (const property of waiterConfiguration.properties) {
137
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
148
continue;
@@ -19,8 +13,15 @@ export const getWaiterConfigValue = (
1913
continue;
2014
}
2115
if (propertyKey.name === key) {
22-
// @ts-expect-error value is Literal/StringLiteral
23-
return propertyValue.value;
16+
if (propertyValue.type === "Literal" || propertyValue.type === "StringLiteral") {
17+
if (typeof propertyValue.value === "number") {
18+
return propertyValue.value.toString();
19+
}
20+
if (typeof propertyValue.value === "string") {
21+
return propertyValue.value;
22+
}
23+
}
24+
return;
2425
}
2526
}
2627
};

src/transforms/v2-to-v3/apis/replaceWaiterApi.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,17 @@ export const replaceWaiterApi = (
2121
source
2222
.find(j.CallExpression, getClientWaiterCallExpression(clientId, waiterState))
2323
.replaceWith((callExpression) => {
24-
const waiterConfig = getWaiterConfig(callExpression.node.arguments[1]);
25-
const delay = getWaiterConfigValue(waiterConfig, "delay");
26-
const maxAttempts = getWaiterConfigValue(waiterConfig, "maxAttempts");
24+
let delay: string | undefined;
25+
let maxAttempts: string | undefined;
26+
27+
const callArguments = callExpression.node.arguments;
28+
if (callArguments.length > 1 && callArguments[1].type === "ObjectExpression") {
29+
const waiterConfig = getWaiterConfig(callArguments[1]);
30+
if (waiterConfig) {
31+
delay = getWaiterConfigValue(waiterConfig, "delay");
32+
maxAttempts = getWaiterConfigValue(waiterConfig, "maxAttempts");
33+
}
34+
}
2735

2836
const properties = [];
2937
properties.push(
@@ -54,9 +62,12 @@ export const replaceWaiterApi = (
5462
})
5563
);
5664

65+
const options = callExpression.node.arguments[1];
66+
const updatedOptions =
67+
options.type === "ObjectExpression" ? getArgsWithoutWaiterConfig(options) : options;
5768
return j.callExpression(j.identifier(getV3ClientWaiterApiName(waiterState)), [
5869
j.objectExpression(properties),
59-
getArgsWithoutWaiterConfig(callExpression.node.arguments[1]),
70+
updatedOptions,
6071
]);
6172
});
6273
}

src/transforms/v2-to-v3/client-instances/getAwsGlobalConfig.ts

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ export const getAwsGlobalConfig = (
2828
property: { type: "Identifier", name: "update" },
2929
},
3030
})
31-
.filter(
32-
({ node }) => node.arguments.length === 1 && node.arguments[0].type === "ObjectExpression"
33-
)
34-
.forEach(({ node }) => {
35-
const objectExpressionProperties = (node.arguments[0] as ObjectExpression).properties;
31+
.forEach((callExpression) => {
32+
const node = callExpression.node;
33+
if (node.arguments.length !== 1 || node.arguments[0].type !== "ObjectExpression") {
34+
return;
35+
}
3636

37+
const objectExpressionProperties = node.arguments[0].properties;
3738
objectExpressionProperties.forEach((property) => {
3839
objectExpression.properties.push(property);
3940
});

src/transforms/v2-to-v3/client-instances/replaceAwsConfig.ts

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@ export const replaceAwsConfig = (
2121
property: { type: "Identifier", name: "Config" },
2222
},
2323
})
24-
.filter(
25-
({ node }) =>
26-
node.arguments.length === 0 ||
27-
(node.arguments.length === 1 && node.arguments[0].type === "ObjectExpression")
28-
)
29-
.replaceWith(({ node }) => {
30-
const objectExpression =
31-
node.arguments.length === 0
32-
? j.objectExpression([])
33-
: (node.arguments[0] as ObjectExpression);
34-
return getObjectWithUpdatedAwsConfigKeys(j, objectExpression, awsGlobalConfig);
24+
.replaceWith((newExpression) => {
25+
const nodeArguments = newExpression.node.arguments;
26+
27+
if (nodeArguments.length === 0) {
28+
return getObjectWithUpdatedAwsConfigKeys(j, j.objectExpression([]), awsGlobalConfig);
29+
}
30+
if (nodeArguments.length === 1 && nodeArguments[0].type === "ObjectExpression") {
31+
return getObjectWithUpdatedAwsConfigKeys(j, nodeArguments[0], awsGlobalConfig);
32+
}
33+
34+
return newExpression;
3535
});
3636
};

0 commit comments

Comments
 (0)