Skip to content

Commit

Permalink
Remove type assertions for Property and ObjectProperty (#921)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Jul 29, 2024
1 parent bd95b2b commit dbc0fae
Show file tree
Hide file tree
Showing 14 changed files with 105 additions and 155 deletions.
5 changes: 5 additions & 0 deletions .changeset/thick-cycles-add.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Remove type assertions for Property and ObjectProperty
8 changes: 3 additions & 5 deletions src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { ObjectExpression } from "jscodeshift";

export const getArgsWithoutWaiterConfig = (options: ObjectExpression): ObjectExpression => {
options.properties = options.properties.filter((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return true;
}
const propertyKey = (property as Property | ObjectProperty).key;
const propertyKey = property.key;
if (propertyKey.type !== "Identifier") {
return true;
}
Expand Down
14 changes: 5 additions & 9 deletions src/transforms/v2-to-v3/apis/getWaiterConfig.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,15 @@
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { ObjectExpression } from "jscodeshift";

export const getWaiterConfig = (originalConfig: ObjectExpression): ObjectExpression | undefined => {
for (const property of originalConfig.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
continue;
}
const propertyKey = (property as Property | ObjectProperty).key;
const propertyValue = (property as Property | ObjectProperty).value;
if (propertyKey.type !== "Identifier" || propertyValue.type !== "ObjectExpression") {
if (property.key.type !== "Identifier" || property.value.type !== "ObjectExpression") {
continue;
}
if (propertyKey.name === "$waiter") {
return propertyValue;
if (property.key.name === "$waiter") {
return property.value;
}
}
};
13 changes: 5 additions & 8 deletions src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,15 @@
import type { ObjectExpression, ObjectProperty, Property } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { ObjectExpression } from "jscodeshift";

export const getWaiterConfigValue = (waiterConfiguration: ObjectExpression, key: string) => {
for (const property of waiterConfiguration.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
continue;
}
const propertyKey = (property as Property | ObjectProperty).key;
const propertyValue = (property as Property | ObjectProperty).value;
if (propertyKey.type !== "Identifier") {
if (property.key.type !== "Identifier") {
continue;
}
if (propertyKey.name === key) {
if (property.key.name === key) {
const propertyValue = property.value;
if (propertyValue.type === "Literal" || propertyValue.type === "StringLiteral") {
if (typeof propertyValue.value === "number") {
return propertyValue.value.toString();
Expand Down
32 changes: 12 additions & 20 deletions src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,5 @@
import type {
Collection,
JSCodeshift,
NewExpression,
ObjectExpression,
ObjectProperty,
Property,
} from "jscodeshift";
import type { Collection, JSCodeshift, NewExpression, ObjectExpression } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { ClientIdentifier } from "../types";
import { getClientApiCallExpression } from "./getClientApiCallExpression";
import { getCommandName } from "./getCommandName";
Expand Down Expand Up @@ -41,26 +33,26 @@ export const replaceS3GetSignedUrlApi = (
if (params.type === "ObjectExpression") {
// Check if params has property 'Expires' and add it to options.
for (const property of params.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) continue;
const propertyKey = (property as Property | ObjectProperty).key;
const propertyValue = (property as Property | ObjectProperty).value;
if (propertyKey.type === "Identifier") {
const propertyKeyName = propertyKey.name;
if (propertyKeyName === "Expires") {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
continue;
}
if (property.key.type === "Identifier") {
if (property.key.name === "Expires") {
// Add 'expiresIn' property to options.
options.properties.push(
j.objectProperty.from({
key: j.identifier("expiresIn"),
value: propertyValue,
value: property.value,
shorthand: true,
})
);
// Remove 'Expires' property from params.
params.properties = params.properties.filter((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return true;
const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") return true;
return propertyKey.name !== "Expires";
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return true;
}
if (property.key.type !== "Identifier") return true;
return property.key.name !== "Expires";
});
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import type { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { ASTPath, JSCodeshift, NewExpression } from "jscodeshift";
import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient";

export const getDynamoDBDocClientArgs = (
Expand All @@ -16,11 +15,11 @@ export const getDynamoDBDocClientArgs = (
const params = v2DocClientArgs[0];
if (params.type === "ObjectExpression") {
for (const property of params.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
continue;
}

const propertyKey = (property as Property | ObjectProperty).key;
const propertyKey = property.key;
if (propertyKey.type !== "Identifier") {
continue;
}
Expand All @@ -36,11 +35,7 @@ export const getDynamoDBDocClientArgs = (
"init",
j.identifier(docClientOptionsHash[propertyKey.name]),
j.objectExpression([
j.property(
"init",
j.identifier(propertyKey.name),
(property as Property | ObjectProperty).value
),
j.property("init", j.identifier(propertyKey.name), property.value),
])
)
);
Expand Down
33 changes: 15 additions & 18 deletions src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift";
import type { ASTPath, JSCodeshift, NewExpression } from "jscodeshift";

import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { DYNAMODB } from "../config";

export const getDynamoDBForDocClient = (
j: JSCodeshift,
Expand All @@ -13,23 +13,21 @@ export const getDynamoDBForDocClient = (
if (v2DocClientArgs.length > 0) {
const params = v2DocClientArgs[0];
if (params.type === "ObjectExpression") {
const serviceProperty = params.properties.find((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
return false;
for (const property of params.properties) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
continue;
}
const propertyKey = (property as Property | ObjectProperty).key;

const propertyKey = property.key;
if (propertyKey.type !== "Identifier") {
return false;
continue;
}

if (propertyKey.name === "service") {
return true;
// The value here will work in most Document Client creations.
// Adding typecast to skip TypeScript errors.
return property.value as NewExpression;
}
}) as Property | ObjectProperty | undefined;

if (serviceProperty) {
// The value here will work in most Document Client creations.
// Adding typecast to skip TypeScript errors.
return serviceProperty.value as NewExpression;
}
}
}
Expand All @@ -41,14 +39,13 @@ export const getDynamoDBForDocClient = (
if (v3DocClientArgs) {
if (v3DocClientArgs.type === "ObjectExpression") {
v3DocClientArgs.properties = v3DocClientArgs.properties.filter((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return true;
}
const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
if (property.key.type !== "Identifier") {
return true;
}
return !["convertEmptyValues", "wrapNumbers"].includes(propertyKey.name);
return !["convertEmptyValues", "wrapNumbers"].includes(property.key.name);
});

if (v3DocClientArgs.properties.length > 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,5 @@
import type {
Identifier,
JSCodeshift,
ObjectExpression,
ObjectProperty,
Property,
} from "jscodeshift";
import { AWS_CONFIG_KEY_MAP, OBJECT_PROPERTY_TYPE_LIST } from "../config";
import type { JSCodeshift, ObjectExpression } from "jscodeshift";
import { AWS_CONFIG_KEY_MAP } from "../config";

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

// Add properties from awsGlobalConfig
for (const property of awsGlobalConfig.properties) {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
propertiesToUpdate.push(property);
continue;
}

const propertyKey = (property as Property | ObjectProperty).key;
if (propertyKey.type !== "Identifier") {
if (property.key.type !== "Identifier") {
propertiesToUpdate.push(property);
continue;
}

const propertyKeyName = propertyKey.name;
const propertyKeyName = property.key.name;
if (
!propertiesToUpdate
.filter((propertyToUpdate) => OBJECT_PROPERTY_TYPE_LIST.includes(propertyToUpdate.type))
.filter(
(propertyToUpdate) =>
(propertyToUpdate as Property | ObjectProperty).key.type === "Identifier"
)
.some(
(propertyToUpdate) =>
((propertyToUpdate as Property | ObjectProperty).key as Identifier).name ===
propertyKeyName
)
!propertiesToUpdate.some((propertyToUpdate) => {
if (propertyToUpdate.type === "Property" || propertyToUpdate.type === "ObjectProperty") {
if (propertyToUpdate.key.type === "Identifier") {
return propertyToUpdate.key.name === propertyKeyName;
}
}
})
) {
propertiesToUpdate.push(property);
}
}

const updatedProperties = propertiesToUpdate
.map((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return property;
}

const propertyKey = (property as Property | ObjectProperty).key;
const propertyKey = property.key;
if (propertyKey.type !== "Identifier") {
return property;
}
Expand Down
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/config/constants.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ export const DOCUMENT_CLIENT = "DocumentClient";
export const DYNAMODB_DOCUMENT = "DynamoDBDocument";
export const DYNAMODB_DOCUMENT_CLIENT = [DYNAMODB, DOCUMENT_CLIENT].join(".");

export const OBJECT_PROPERTY_TYPE_LIST = ["Property", "ObjectProperty"];
export const FUNCTION_TYPE_LIST = [
"FunctionDeclaration",
"FunctionExpression",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
import type { Collection, JSCodeshift } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
import { getRequireDeclarators } from "./requireModule";

export interface GetRequireDeclaratorsWithObjectPattern {
Expand All @@ -19,8 +18,9 @@ export const getRequireDeclaratorsWithObjectPattern = (
}
const { properties } = declarator.value.id;
return properties.some((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false;
const propertyValue = (property as Property | ObjectProperty).value;
return propertyValue.type === "Identifier" && propertyValue.name === identifierName;
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return false;
}
return property.value.type === "Identifier" && property.value.name === identifierName;
});
});
18 changes: 8 additions & 10 deletions src/transforms/v2-to-v3/modules/objectPatternPropertyCompareFn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@ import type {
SpreadPropertyPattern,
} from "jscodeshift";

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

export type ObjectPatternProperty =
| Property
| PropertyPattern
Expand All @@ -21,14 +19,14 @@ export const objectPatternPropertyCompareFn = (
property1: ObjectPatternProperty,
property2: ObjectPatternProperty
) => {
if (
OBJECT_PROPERTY_TYPE_LIST.includes(property1.type) &&
OBJECT_PROPERTY_TYPE_LIST.includes(property2.type)
) {
const property1Key = (property1 as Property | ObjectProperty).key;
const property2Key = (property2 as Property | ObjectProperty).key;
if (property1Key.type === "Identifier" && property2Key.type === "Identifier")
return property1Key.name.localeCompare(property2Key.name);
if (property1.type !== "Property" && property1.type !== "ObjectProperty") {
return 0;
}
if (property2.type !== "Property" && property2.type !== "ObjectProperty") {
return 0;
}
if (property1.key.type === "Identifier" && property2.key.type === "Identifier") {
return property1.key.name.localeCompare(property2.key.name);
}
return 0;
};
14 changes: 7 additions & 7 deletions src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift";
import type { Collection, JSCodeshift, ObjectPattern } from "jscodeshift";

import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config";
import { PACKAGE_NAME } from "../../config";
import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn";
import { getRequireDeclarators } from "../requireModule";
import type { ModulesOptions } from "../types";
Expand Down Expand Up @@ -30,13 +30,13 @@ export const addNamedModule = (
(variableDeclarator) =>
variableDeclarator.id.type === "ObjectPattern" &&
variableDeclarator.id.properties.find((property) => {
if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false;
const key = (property as Property | ObjectProperty).key;
const value = (property as Property | ObjectProperty).value;
if (key.type !== "Identifier" || value.type !== "Identifier") {
if (property.type !== "Property" && property.type !== "ObjectProperty") {
return false;
}
return key.name === importedName && value.name === localName;
if (property.key.type !== "Identifier" || property.value.type !== "Identifier") {
return false;
}
return property.key.name === importedName && property.value.name === localName;
})
)
) {
Expand Down
Loading

0 comments on commit dbc0fae

Please sign in to comment.