diff --git a/.changeset/thick-cycles-add.md b/.changeset/thick-cycles-add.md new file mode 100644 index 000000000..3b13972df --- /dev/null +++ b/.changeset/thick-cycles-add.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Remove type assertions for Property and ObjectProperty diff --git a/src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts b/src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts index 1e4e222bd..941f2ae61 100644 --- a/src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts +++ b/src/transforms/v2-to-v3/apis/getArgsWithoutWaiterConfig.ts @@ -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; } diff --git a/src/transforms/v2-to-v3/apis/getWaiterConfig.ts b/src/transforms/v2-to-v3/apis/getWaiterConfig.ts index da500fb95..0ee9886ee 100644 --- a/src/transforms/v2-to-v3/apis/getWaiterConfig.ts +++ b/src/transforms/v2-to-v3/apis/getWaiterConfig.ts @@ -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; } } }; diff --git a/src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts b/src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts index 14eef76d2..c2fe05d52 100644 --- a/src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts +++ b/src/transforms/v2-to-v3/apis/getWaiterConfigValue.ts @@ -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(); diff --git a/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts b/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts index 5c1d06000..2533fc8e8 100644 --- a/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts +++ b/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts @@ -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"; @@ -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"; }); } } diff --git a/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts b/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts index c80df6d9d..8d467e839 100644 --- a/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts +++ b/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts @@ -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 = ( @@ -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; } @@ -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), ]) ) ); diff --git a/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts index 6b1381d36..6d50af0dd 100644 --- a/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts +++ b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts @@ -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, @@ -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; } } } @@ -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) { diff --git a/src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts b/src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts index 75e241056..334627e58 100644 --- a/src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts +++ b/src/transforms/v2-to-v3/client-instances/getObjectWithUpdatedAwsConfigKeys.ts @@ -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}.`; @@ -31,30 +25,25 @@ 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); } @@ -62,11 +51,11 @@ export const getObjectWithUpdatedAwsConfigKeys = ( 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; } diff --git a/src/transforms/v2-to-v3/config/constants.ts b/src/transforms/v2-to-v3/config/constants.ts index b6a29aee7..0fd926db0 100644 --- a/src/transforms/v2-to-v3/config/constants.ts +++ b/src/transforms/v2-to-v3/config/constants.ts @@ -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", diff --git a/src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.ts b/src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.ts index 80e7eb867..577d22b0f 100644 --- a/src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.ts +++ b/src/transforms/v2-to-v3/modules/getRequireDeclaratorsWithObjectPattern.ts @@ -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 { @@ -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; }); }); diff --git a/src/transforms/v2-to-v3/modules/objectPatternPropertyCompareFn.ts b/src/transforms/v2-to-v3/modules/objectPatternPropertyCompareFn.ts index 4332f1386..72fa4e821 100644 --- a/src/transforms/v2-to-v3/modules/objectPatternPropertyCompareFn.ts +++ b/src/transforms/v2-to-v3/modules/objectPatternPropertyCompareFn.ts @@ -7,8 +7,6 @@ import type { SpreadPropertyPattern, } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST } from "../config"; - export type ObjectPatternProperty = | Property | PropertyPattern @@ -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; }; diff --git a/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts index c29e03363..d39bc227f 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts @@ -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"; @@ -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; }) ) ) { diff --git a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts index 5148ca87f..2b4076e16 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/getImportSpecifiers.ts @@ -1,29 +1,7 @@ -import type { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; +import type { Collection, JSCodeshift } from "jscodeshift"; import type { ImportSpecifierType } from "../types"; import { getRequireDeclarators } from "./getRequireDeclarators"; -const getImportSpecifiersFromObjectPattern = (properties: (Property | ObjectProperty)[]) => { - const importSpecifiers = new Set(); - - for (const property of properties) { - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) { - continue; - } - const objectProperty = property as Property | ObjectProperty; - const key = objectProperty.key; - const value = objectProperty.value; - if (key.type === "Identifier" && value.type === "Identifier") { - importSpecifiers.add({ - importedName: key.name, - localName: value.name, - }); - } - } - - return Array.from(importSpecifiers); -}; - export const getImportSpecifiers = ( j: JSCodeshift, source: Collection, @@ -54,9 +32,16 @@ export const getImportSpecifiers = ( if (declaratorInit?.type !== "CallExpression") { continue; } - const properties = declaratorId.properties as (Property | ObjectProperty)[]; - for (const importSpecifier of getImportSpecifiersFromObjectPattern(properties)) { - importSpecifiers.add(importSpecifier); + for (const property of declaratorId.properties) { + if (property.type !== "Property" && property.type !== "ObjectProperty") { + continue; + } + if (property.key.type === "Identifier" && property.value.type === "Identifier") { + importSpecifiers.add({ + importedName: property.key.name, + localName: property.value.name, + }); + } } } } diff --git a/src/transforms/v2-to-v3/modules/requireModule/removeRequire.ts b/src/transforms/v2-to-v3/modules/requireModule/removeRequire.ts index f811f89a1..d50254822 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/removeRequire.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/removeRequire.ts @@ -4,11 +4,8 @@ import type { Identifier, JSCodeshift, ObjectPattern, - ObjectProperty, - Property, VariableDeclarator, } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; import { removeDeclaration } from "../removeDeclaration"; import type { ImportSpecifierType } from "../types"; import { getRequireDeclarators } from "./getRequireDeclarators"; @@ -24,9 +21,10 @@ const isAnotherSpecifier = (j: JSCodeshift, source: Collection, localNa const id = varDeclarator.value.id as ObjectPattern; if ( id.properties.some((property) => { - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false; - const value = (property as Property | ObjectProperty).value; - return value.type === "Identifier" && value.name === localName; + if (property.type !== "Property" && property.type !== "ObjectProperty") { + return false; + } + return property.value.type === "Identifier" && property.value.name === localName; }) ) return true; @@ -90,15 +88,16 @@ export const removeRequire = (j: JSCodeshift, source: Collection) => } case "ObjectPattern": { id.properties = id.properties.filter((property) => { - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return true; + if (property.type !== "Property" && property.type !== "ObjectProperty") { + return true; + } - const propertyKey = (property as Property | ObjectProperty).key; - const propertyValue = (property as Property | ObjectProperty).value; - if (propertyKey.type !== "Identifier" || propertyValue.type !== "Identifier") + if (property.key.type !== "Identifier" || property.value.type !== "Identifier") { return true; + } - const importedName = propertyKey.name; - const localName = propertyValue.name; + const importedName = property.key.name; + const localName = property.value.name; return !isIdentifierRemovable(j, source, { importedName, localName }); }); if (id.properties.length === 0) {