Skip to content

Commit dbc0fae

Browse files
authored
Remove type assertions for Property and ObjectProperty (#921)
1 parent bd95b2b commit dbc0fae

14 files changed

+105
-155
lines changed

.changeset/thick-cycles-add.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 Property and ObjectProperty

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

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,11 @@
1-
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
2-
3-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
1+
import type { ObjectExpression } from "jscodeshift";
42

53
export const getArgsWithoutWaiterConfig = (options: ObjectExpression): ObjectExpression => {
64
options.properties = options.properties.filter((property) => {
7-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
5+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
86
return true;
97
}
10-
const propertyKey = (property as Property | ObjectProperty).key;
8+
const propertyKey = property.key;
119
if (propertyKey.type !== "Identifier") {
1210
return true;
1311
}
Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,15 @@
1-
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
2-
3-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
1+
import type { ObjectExpression } from "jscodeshift";
42

53
export const getWaiterConfig = (originalConfig: ObjectExpression): ObjectExpression | undefined => {
64
for (const property of originalConfig.properties) {
7-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
5+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
86
continue;
97
}
10-
const propertyKey = (property as Property | ObjectProperty).key;
11-
const propertyValue = (property as Property | ObjectProperty).value;
12-
if (propertyKey.type !== "Identifier" || propertyValue.type !== "ObjectExpression") {
8+
if (property.key.type !== "Identifier" || property.value.type !== "ObjectExpression") {
139
continue;
1410
}
15-
if (propertyKey.name === "$waiter") {
16-
return propertyValue;
11+
if (property.key.name === "$waiter") {
12+
return property.value;
1713
}
1814
}
1915
};

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

Lines changed: 5 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,15 @@
1-
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";
2-
3-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
1+
import type { ObjectExpression } from "jscodeshift";
42

53
export const getWaiterConfigValue = (waiterConfiguration: ObjectExpression, key: string) => {
64
for (const property of waiterConfiguration.properties) {
7-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
5+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
86
continue;
97
}
10-
const propertyKey = (property as Property | ObjectProperty).key;
11-
const propertyValue = (property as Property | ObjectProperty).value;
12-
if (propertyKey.type !== "Identifier") {
8+
if (property.key.type !== "Identifier") {
139
continue;
1410
}
15-
if (propertyKey.name === key) {
11+
if (property.key.name === key) {
12+
const propertyValue = property.value;
1613
if (propertyValue.type === "Literal" || propertyValue.type === "StringLiteral") {
1714
if (typeof propertyValue.value === "number") {
1815
return propertyValue.value.toString();

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

Lines changed: 12 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,5 @@
1-
import type {
2-
Collection,
3-
JSCodeshift,
4-
NewExpression,
5-
ObjectExpression,
6-
ObjectProperty,
7-
Property,
8-
} from "jscodeshift";
1+
import type { Collection, JSCodeshift, NewExpression, ObjectExpression } from "jscodeshift";
92

10-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
113
import type { ClientIdentifier } from "../types";
124
import { getClientApiCallExpression } from "./getClientApiCallExpression";
135
import { getCommandName } from "./getCommandName";
@@ -41,26 +33,26 @@ export const replaceS3GetSignedUrlApi = (
4133
if (params.type === "ObjectExpression") {
4234
// Check if params has property 'Expires' and add it to options.
4335
for (const property of params.properties) {
44-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) continue;
45-
const propertyKey = (property as Property | ObjectProperty).key;
46-
const propertyValue = (property as Property | ObjectProperty).value;
47-
if (propertyKey.type === "Identifier") {
48-
const propertyKeyName = propertyKey.name;
49-
if (propertyKeyName === "Expires") {
36+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
37+
continue;
38+
}
39+
if (property.key.type === "Identifier") {
40+
if (property.key.name === "Expires") {
5041
// Add 'expiresIn' property to options.
5142
options.properties.push(
5243
j.objectProperty.from({
5344
key: j.identifier("expiresIn"),
54-
value: propertyValue,
45+
value: property.value,
5546
shorthand: true,
5647
})
5748
);
5849
// Remove 'Expires' property from params.
5950
params.properties = params.properties.filter((property) => {
60-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return true;
61-
const propertyKey = (property as Property | ObjectProperty).key;
62-
if (propertyKey.type !== "Identifier") return true;
63-
return propertyKey.name !== "Expires";
51+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
52+
return true;
53+
}
54+
if (property.key.type !== "Identifier") return true;
55+
return property.key.name !== "Expires";
6456
});
6557
}
6658
}

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

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
import type { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
2-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
1+
import type { ASTPath, JSCodeshift, NewExpression } from "jscodeshift";
32
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";
43

54
export const getDynamoDBDocClientArgs = (
@@ -16,11 +15,11 @@ export const getDynamoDBDocClientArgs = (
1615
const params = v2DocClientArgs[0];
1716
if (params.type === "ObjectExpression") {
1817
for (const property of params.properties) {
19-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
18+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
2019
continue;
2120
}
2221

23-
const propertyKey = (property as Property | ObjectProperty).key;
22+
const propertyKey = property.key;
2423
if (propertyKey.type !== "Identifier") {
2524
continue;
2625
}
@@ -36,11 +35,7 @@ export const getDynamoDBDocClientArgs = (
3635
"init",
3736
j.identifier(docClientOptionsHash[propertyKey.name]),
3837
j.objectExpression([
39-
j.property(
40-
"init",
41-
j.identifier(propertyKey.name),
42-
(property as Property | ObjectProperty).value
43-
),
38+
j.property("init", j.identifier(propertyKey.name), property.value),
4439
])
4540
)
4641
);

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

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
import type { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
1+
import type { ASTPath, JSCodeshift, NewExpression } from "jscodeshift";
22

3-
import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
3+
import { DYNAMODB } from "../config";
44

55
export const getDynamoDBForDocClient = (
66
j: JSCodeshift,
@@ -13,23 +13,21 @@ export const getDynamoDBForDocClient = (
1313
if (v2DocClientArgs.length > 0) {
1414
const params = v2DocClientArgs[0];
1515
if (params.type === "ObjectExpression") {
16-
const serviceProperty = params.properties.find((property) => {
17-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
18-
return false;
16+
for (const property of params.properties) {
17+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
18+
continue;
1919
}
20-
const propertyKey = (property as Property | ObjectProperty).key;
20+
21+
const propertyKey = property.key;
2122
if (propertyKey.type !== "Identifier") {
22-
return false;
23+
continue;
2324
}
25+
2426
if (propertyKey.name === "service") {
25-
return true;
27+
// The value here will work in most Document Client creations.
28+
// Adding typecast to skip TypeScript errors.
29+
return property.value as NewExpression;
2630
}
27-
}) as Property | ObjectProperty | undefined;
28-
29-
if (serviceProperty) {
30-
// The value here will work in most Document Client creations.
31-
// Adding typecast to skip TypeScript errors.
32-
return serviceProperty.value as NewExpression;
3331
}
3432
}
3533
}
@@ -41,14 +39,13 @@ export const getDynamoDBForDocClient = (
4139
if (v3DocClientArgs) {
4240
if (v3DocClientArgs.type === "ObjectExpression") {
4341
v3DocClientArgs.properties = v3DocClientArgs.properties.filter((property) => {
44-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
42+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
4543
return true;
4644
}
47-
const propertyKey = (property as Property | ObjectProperty).key;
48-
if (propertyKey.type !== "Identifier") {
45+
if (property.key.type !== "Identifier") {
4946
return true;
5047
}
51-
return !["convertEmptyValues", "wrapNumbers"].includes(propertyKey.name);
48+
return !["convertEmptyValues", "wrapNumbers"].includes(property.key.name);
5249
});
5350

5451
if (v3DocClientArgs.properties.length > 0) {

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

Lines changed: 14 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,5 @@
1-
import type {
2-
Identifier,
3-
JSCodeshift,
4-
ObjectExpression,
5-
ObjectProperty,
6-
Property,
7-
} from "jscodeshift";
8-
import { AWS_CONFIG_KEY_MAP, OBJECT_PROPERTY_TYPE_LIST } from "../config";
1+
import type { JSCodeshift, ObjectExpression } from "jscodeshift";
2+
import { AWS_CONFIG_KEY_MAP } from "../config";
93

104
const getRenameComment = (keyName: string, newKeyName: string) =>
115
` The key ${keyName} is renamed to ${newKeyName}.`;
@@ -31,42 +25,37 @@ export const getObjectWithUpdatedAwsConfigKeys = (
3125

3226
// Add properties from awsGlobalConfig
3327
for (const property of awsGlobalConfig.properties) {
34-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
28+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
3529
propertiesToUpdate.push(property);
3630
continue;
3731
}
3832

39-
const propertyKey = (property as Property | ObjectProperty).key;
40-
if (propertyKey.type !== "Identifier") {
33+
if (property.key.type !== "Identifier") {
4134
propertiesToUpdate.push(property);
4235
continue;
4336
}
4437

45-
const propertyKeyName = propertyKey.name;
38+
const propertyKeyName = property.key.name;
4639
if (
47-
!propertiesToUpdate
48-
.filter((propertyToUpdate) => OBJECT_PROPERTY_TYPE_LIST.includes(propertyToUpdate.type))
49-
.filter(
50-
(propertyToUpdate) =>
51-
(propertyToUpdate as Property | ObjectProperty).key.type === "Identifier"
52-
)
53-
.some(
54-
(propertyToUpdate) =>
55-
((propertyToUpdate as Property | ObjectProperty).key as Identifier).name ===
56-
propertyKeyName
57-
)
40+
!propertiesToUpdate.some((propertyToUpdate) => {
41+
if (propertyToUpdate.type === "Property" || propertyToUpdate.type === "ObjectProperty") {
42+
if (propertyToUpdate.key.type === "Identifier") {
43+
return propertyToUpdate.key.name === propertyKeyName;
44+
}
45+
}
46+
})
5847
) {
5948
propertiesToUpdate.push(property);
6049
}
6150
}
6251

6352
const updatedProperties = propertiesToUpdate
6453
.map((property) => {
65-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
54+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
6655
return property;
6756
}
6857

69-
const propertyKey = (property as Property | ObjectProperty).key;
58+
const propertyKey = property.key;
7059
if (propertyKey.type !== "Identifier") {
7160
return property;
7261
}

src/transforms/v2-to-v3/config/constants.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ export const DOCUMENT_CLIENT = "DocumentClient";
66
export const DYNAMODB_DOCUMENT = "DynamoDBDocument";
77
export const DYNAMODB_DOCUMENT_CLIENT = [DYNAMODB, DOCUMENT_CLIENT].join(".");
88

9-
export const OBJECT_PROPERTY_TYPE_LIST = ["Property", "ObjectProperty"];
109
export const FUNCTION_TYPE_LIST = [
1110
"FunctionDeclaration",
1211
"FunctionExpression",

src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.ts

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
1+
import type { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
43
import { getRequireDeclarators } from "./requireModule";
54

65
export interface GetRequireDeclaratorsWithObjectPattern {
@@ -19,8 +18,9 @@ export const getRequireDeclaratorsWithObjectPattern = (
1918
}
2019
const { properties } = declarator.value.id;
2120
return properties.some((property) => {
22-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false;
23-
const propertyValue = (property as Property | ObjectProperty).value;
24-
return propertyValue.type === "Identifier" && propertyValue.name === identifierName;
21+
if (property.type !== "Property" && property.type !== "ObjectProperty") {
22+
return false;
23+
}
24+
return property.value.type === "Identifier" && property.value.name === identifierName;
2525
});
2626
});

0 commit comments

Comments
 (0)