diff --git a/.changeset/rotten-dragons-obey.md b/.changeset/rotten-dragons-obey.md new file mode 100644 index 000000000..109e130ca --- /dev/null +++ b/.changeset/rotten-dragons-obey.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Add import equals declaration for named imports diff --git a/scripts/generateNewClientTests/getGlobalImportEqualsOutput.ts b/scripts/generateNewClientTests/getGlobalImportEqualsOutput.ts index 266f07baf..8e5a1726b 100644 --- a/scripts/generateNewClientTests/getGlobalImportEqualsOutput.ts +++ b/scripts/generateNewClientTests/getGlobalImportEqualsOutput.ts @@ -1,5 +1,3 @@ -import { CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/config"; -import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils"; import { CLIENTS_TO_TEST } from "./config"; import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; @@ -10,11 +8,7 @@ export const getGlobalImportEqualsOutput = () => { content += getV3PackageImportEqualsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST)); content += "\n"; - content += getV3ClientsNewExpressionCode( - CLIENTS_TO_TEST.map((clientName) => - [getDefaultLocalName(clientName), CLIENT_NAMES_MAP[clientName]].join(".") - ) - ); + content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); return content; }; diff --git a/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts b/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts index d67475e30..4b2dc9442 100644 --- a/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts @@ -1,5 +1,3 @@ -import { CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/config"; -import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils"; import { CLIENTS_TO_TEST } from "./config"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode"; @@ -9,11 +7,7 @@ export const getServiceImportEqualsOutput = () => { content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST); content += "\n"; - content += getV3ClientsNewExpressionCode( - CLIENTS_TO_TEST.map((clientName) => - [getDefaultLocalName(clientName), CLIENT_NAMES_MAP[clientName]].join(".") - ) - ); + content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); return content; }; diff --git a/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts b/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts index 87340d499..ea7a7a85b 100644 --- a/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts @@ -1,3 +1,14 @@ -import { getServiceImportEqualsOutput } from "./getServiceImportEqualsOutput"; +import { CLIENTS_TO_TEST } from "./config"; +import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; +import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; +import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode"; -export const getServiceImportEqualsWithNameOutput = getServiceImportEqualsOutput; +export const getServiceImportEqualsWithNameOutput = () => { + let content = ``; + + content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST, { useLocalSuffix: true }); + content += "\n"; + content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix)); + + return content; +}; diff --git a/scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts b/scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts index daf7f6cc3..e4b5cbbc8 100644 --- a/scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts +++ b/scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts @@ -1,13 +1,30 @@ -import { CLIENT_NAMES, CLIENT_PACKAGE_NAMES_MAP } from "../../src/transforms/v2-to-v3/config"; +import { + CLIENT_NAMES, + CLIENT_NAMES_MAP, + CLIENT_PACKAGE_NAMES_MAP, +} from "../../src/transforms/v2-to-v3/config"; import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils"; +import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; -export const getV3PackageImportEqualsCode = (clientsToTest: typeof CLIENT_NAMES) => { +export interface V3PackageImportEqualsCodeOptions { + useLocalSuffix?: boolean; +} + +export const getV3PackageImportEqualsCode = ( + clientsToTest: typeof CLIENT_NAMES, + options?: V3PackageImportEqualsCodeOptions +) => { let content = ``; + const { useLocalSuffix = false } = options || {}; for (const v2ClientName of clientsToTest) { const v3ClientDefaultLocalName = getDefaultLocalName(v2ClientName); const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`; content += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n`; + + const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; + const importName = useLocalSuffix ? getClientNameWithLocalSuffix(v2ClientName) : v3ClientName; + content += `import ${importName} = ${[v3ClientDefaultLocalName, v3ClientName].join(".")};\n`; } return content; diff --git a/src/transforms/v2-to-v3/__fixtures__/api-basic-type/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-basic-type/global-import-equals.output.ts index 82f4f4c2f..58eeb484a 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-basic-type/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-basic-type/global-import-equals.output.ts @@ -1,3 +1,4 @@ import AWS_S3 = require("@aws-sdk/client-s3"); +import Tag = AWS_S3.Tag; -const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }]; \ No newline at end of file +const testTags: Tag[] = [{ Key: "key", Value: "value" }]; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-basic-type/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-basic-type/service-import-equals.output.ts index 82f4f4c2f..58eeb484a 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-basic-type/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-basic-type/service-import-equals.output.ts @@ -1,3 +1,4 @@ import AWS_S3 = require("@aws-sdk/client-s3"); +import Tag = AWS_S3.Tag; -const testTags: AWS_S3.Tag[] = [{ Key: "key", Value: "value" }]; \ No newline at end of file +const testTags: Tag[] = [{ Key: "key", Value: "value" }]; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/global-import-equals.output.ts index 381fd7d64..5c8ce0df9 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/global-import-equals.output.ts @@ -1,18 +1,27 @@ import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; +import ListTablesCommandOutput = AWS_DynamoDB.ListTablesCommandOutput; +import ListTablesCommandInput = AWS_DynamoDB.ListTablesCommandInput; import AWS_Lambda = require("@aws-sdk/client-lambda"); +import Lambda = AWS_Lambda.Lambda; +import InvokeCommandOutput = AWS_Lambda.InvokeCommandOutput; +import InvokeCommandInput = AWS_Lambda.InvokeCommandInput; import AWS_STS = require("@aws-sdk/client-sts"); +import STS = AWS_STS.STS; +import GetCallerIdentityCommandOutput = AWS_STS.GetCallerIdentityCommandOutput; +import GetCallerIdentityCommandInput = AWS_STS.GetCallerIdentityCommandInput; -const ddbClient = new AWS_DynamoDB.DynamoDB({ region: "us-west-2" }); -const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 }; -const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient +const ddbClient = new DynamoDB({ region: "us-west-2" }); +const listTablesInput: ListTablesCommandInput = { Limit: 10 }; +const listTablesOutput: ListTablesCommandOutput = await ddbClient .listTables(listTablesInput); -const stsClient = new AWS_STS.STS({ region: "us-west-2" }); -const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {}; -const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient +const stsClient = new STS({ region: "us-west-2" }); +const getCallerIdentityInput: GetCallerIdentityCommandInput = {}; +const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient .getCallerIdentity(getCallerIdentityInput); -const lambdaClient = new AWS_Lambda.Lambda({ region: "us-west-2" }); -const invokeInput: AWS_Lambda.InvokeCommandInput = { FunctionName: "my-function" }; -const invokeOutput: AWS_Lambda.InvokeCommandOutput = await lambdaClient +const lambdaClient = new Lambda({ region: "us-west-2" }); +const invokeInput: InvokeCommandInput = { FunctionName: "my-function" }; +const invokeOutput: InvokeCommandOutput = await lambdaClient .invoke(invokeInput); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/service-import-equals.output.ts index 381fd7d64..5c8ce0df9 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-input-output-type/service-import-equals.output.ts @@ -1,18 +1,27 @@ import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; +import ListTablesCommandOutput = AWS_DynamoDB.ListTablesCommandOutput; +import ListTablesCommandInput = AWS_DynamoDB.ListTablesCommandInput; import AWS_Lambda = require("@aws-sdk/client-lambda"); +import Lambda = AWS_Lambda.Lambda; +import InvokeCommandOutput = AWS_Lambda.InvokeCommandOutput; +import InvokeCommandInput = AWS_Lambda.InvokeCommandInput; import AWS_STS = require("@aws-sdk/client-sts"); +import STS = AWS_STS.STS; +import GetCallerIdentityCommandOutput = AWS_STS.GetCallerIdentityCommandOutput; +import GetCallerIdentityCommandInput = AWS_STS.GetCallerIdentityCommandInput; -const ddbClient = new AWS_DynamoDB.DynamoDB({ region: "us-west-2" }); -const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 }; -const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient +const ddbClient = new DynamoDB({ region: "us-west-2" }); +const listTablesInput: ListTablesCommandInput = { Limit: 10 }; +const listTablesOutput: ListTablesCommandOutput = await ddbClient .listTables(listTablesInput); -const stsClient = new AWS_STS.STS({ region: "us-west-2" }); -const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {}; -const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient +const stsClient = new STS({ region: "us-west-2" }); +const getCallerIdentityInput: GetCallerIdentityCommandInput = {}; +const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient .getCallerIdentity(getCallerIdentityInput); -const lambdaClient = new AWS_Lambda.Lambda({ region: "us-west-2" }); -const invokeInput: AWS_Lambda.InvokeCommandInput = { FunctionName: "my-function" }; -const invokeOutput: AWS_Lambda.InvokeCommandOutput = await lambdaClient +const lambdaClient = new Lambda({ region: "us-west-2" }); +const invokeInput: InvokeCommandInput = { FunctionName: "my-function" }; +const invokeOutput: InvokeCommandOutput = await lambdaClient .invoke(invokeInput); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-import-equals.output.ts index c16ab3123..f76ee98a1 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-promise/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/global-import-equals.output.ts @@ -1,4 +1,5 @@ import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; -const client = new AWS_DynamoDB.DynamoDB(); +const client = new DynamoDB(); const data = await client.listTables(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-promise/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-import-equals.output.ts index c16ab3123..f76ee98a1 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-promise/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-promise/service-import-equals.output.ts @@ -1,4 +1,5 @@ import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; -const client = new AWS_DynamoDB.DynamoDB(); +const client = new DynamoDB(); const data = await client.listTables(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-import-equals.output.ts index e8e2bc62c..db9289a63 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/global-import-equals.output.ts @@ -1,6 +1,10 @@ import AWS_APIGateway = require("@aws-sdk/client-api-gateway"); +import MethodSnapshot = AWS_APIGateway.MethodSnapshot; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import AttributeValue = AWS_DynamoDB.AttributeValue; import AWS_S3 = require("@aws-sdk/client-s3"); +import Bucket = AWS_S3.Bucket; +import ChecksumAlgorithm = AWS_S3.ChecksumAlgorithm; // Native types const stringType: string = "string"; @@ -18,14 +22,14 @@ const stringArray: Array = ["string1", "string2"]; const booleanArray: Array = [true, false]; const numberArray: Array = [123, 456]; const blobArray: Array = [new Uint8Array()]; -const enumArray: Array = ["CRC32"]; -const structureArray: Array = [{ Name: "bucketName" }]; +const enumArray: Array = ["CRC32"]; +const structureArray: Array = [{ Name: "bucketName" }]; // Maps const stringMap: Record = { key: "value" }; const booleanMap: Record = { key: true }; const numberMap: Record = { key: 123 }; -const structureMap: Record = { key: { apiKeyRequired: true } }; +const structureMap: Record = { key: { apiKeyRequired: true } }; // Nested arrays const arrayNestedTwice: Array> = [[1, 2], [3, 4]]; @@ -37,12 +41,12 @@ const arrayNestedFour: Array>>> = [ // Nested maps const mapNestedTwice: Record> = { key: stringMap }; -const mapNestedTwiceStruct: Record> = { key: structureMap }; +const mapNestedTwiceStruct: Record> = { key: structureMap }; // Nested arrays and maps const mapOfArrays: Record> = { key: ["value"] }; const mapOfMapOfArrays: Record>> = { key: mapOfArrays }; -const mapOfArrayOfMaps: Record>> = { key: [{ key: { S:"A" }}] }; +const mapOfArrayOfMaps: Record>> = { key: [{ key: { S:"A" }}] }; const mapOfArrayOfArrays: Record>> = { key: [[1], [2]] }; const arrayOfMaps: Array> = [stringMap]; const arrayOfMapOfArrays: Array>> = [mapOfArrays]; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-import-equals.output.ts index e8e2bc62c..db9289a63 100644 --- a/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/api-redundant-type/service-import-equals.output.ts @@ -1,6 +1,10 @@ import AWS_APIGateway = require("@aws-sdk/client-api-gateway"); +import MethodSnapshot = AWS_APIGateway.MethodSnapshot; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import AttributeValue = AWS_DynamoDB.AttributeValue; import AWS_S3 = require("@aws-sdk/client-s3"); +import Bucket = AWS_S3.Bucket; +import ChecksumAlgorithm = AWS_S3.ChecksumAlgorithm; // Native types const stringType: string = "string"; @@ -18,14 +22,14 @@ const stringArray: Array = ["string1", "string2"]; const booleanArray: Array = [true, false]; const numberArray: Array = [123, 456]; const blobArray: Array = [new Uint8Array()]; -const enumArray: Array = ["CRC32"]; -const structureArray: Array = [{ Name: "bucketName" }]; +const enumArray: Array = ["CRC32"]; +const structureArray: Array = [{ Name: "bucketName" }]; // Maps const stringMap: Record = { key: "value" }; const booleanMap: Record = { key: true }; const numberMap: Record = { key: 123 }; -const structureMap: Record = { key: { apiKeyRequired: true } }; +const structureMap: Record = { key: { apiKeyRequired: true } }; // Nested arrays const arrayNestedTwice: Array> = [[1, 2], [3, 4]]; @@ -37,12 +41,12 @@ const arrayNestedFour: Array>>> = [ // Nested maps const mapNestedTwice: Record> = { key: stringMap }; -const mapNestedTwiceStruct: Record> = { key: structureMap }; +const mapNestedTwiceStruct: Record> = { key: structureMap }; // Nested arrays and maps const mapOfArrays: Record> = { key: ["value"] }; const mapOfMapOfArrays: Record>> = { key: mapOfArrays }; -const mapOfArrayOfMaps: Record>> = { key: [{ key: { S:"A" }}] }; +const mapOfArrayOfMaps: Record>> = { key: [{ key: { S:"A" }}] }; const mapOfArrayOfArrays: Record>> = { key: [[1], [2]] }; const arrayOfMaps: Array> = [stringMap]; const arrayOfMapOfArrays: Array>> = [mapOfArrays]; \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/global-import-equals.output.ts index 0dfca9f56..6ec6c4531 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/global-import-equals.output.ts @@ -1,11 +1,15 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb"); +import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument; +import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput; +import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; -const docClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" })); +const docClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" })); -const docClientScanInput: AWS_lib_dynamodb.ScanCommandInput = { +const docClientScanInput: ScanCommandInput = { TableName: "TableName" }; -const docClientScanOutput: AWS_lib_dynamodb.ScanCommandOutput = await docClient +const docClientScanOutput: ScanCommandOutput = await docClient .scan(docClientScanInput); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/service-import-equals.output.ts index 0dfca9f56..9662a199c 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client-input-output-type/service-import-equals.output.ts @@ -1,11 +1,15 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb"); +import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument; +import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput; +import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynDB = AWS_DynamoDB.DynamoDB; -const docClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" })); +const docClient = DynamoDBDocument.from(new DynDB({ region: "us-west-2" })); -const docClientScanInput: AWS_lib_dynamodb.ScanCommandInput = { +const docClientScanInput: ScanCommandInput = { TableName: "TableName" }; -const docClientScanOutput: AWS_lib_dynamodb.ScanCommandOutput = await docClient +const docClientScanOutput: ScanCommandOutput = await docClient .scan(docClientScanInput); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-import-equals.output.ts index 25e04496c..451ab0c81 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-import-equals.output.ts @@ -1,5 +1,7 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb"); +import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; -const documentClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" })); +const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" })); const response = await documentClient.scan({ TableName: "TABLE_NAME" }); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals-with-name.output.ts b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals-with-name.output.ts index 25e04496c..b69920619 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals-with-name.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals-with-name.output.ts @@ -1,5 +1,7 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb"); +import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDBClient = AWS_DynamoDB.DynamoDB; -const documentClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" })); +const documentClient = DynamoDBDocument.from(new DynamoDBClient({ region: "us-west-2" })); const response = await documentClient.scan({ TableName: "TABLE_NAME" }); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals.output.ts index 25e04496c..451ab0c81 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-import-equals.output.ts @@ -1,5 +1,7 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb"); +import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument; import AWS_DynamoDB = require("@aws-sdk/client-dynamodb"); +import DynamoDB = AWS_DynamoDB.DynamoDB; -const documentClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" })); +const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" })); const response = await documentClient.scan({ TableName: "TABLE_NAME" }); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.output.ts index 611eab842..88df23f62 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.output.ts @@ -3,9 +3,12 @@ import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer"); +import AccessAnalyzer = AWS_AccessAnalyzer.AccessAnalyzer; import AWS_ACM = require("@aws-sdk/client-acm"); +import ACM = AWS_ACM.ACM; import AWS_Discovery = require("@aws-sdk/client-application-discovery-service"); +import ApplicationDiscoveryService = AWS_Discovery.ApplicationDiscoveryService; -new AWS_ACM.ACM(); -new AWS_AccessAnalyzer.AccessAnalyzer(); -new AWS_Discovery.ApplicationDiscoveryService(); +new ACM(); +new AccessAnalyzer(); +new ApplicationDiscoveryService(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.output.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.output.ts index b3cbb2bd9..e73ae2af5 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.output.ts @@ -3,9 +3,12 @@ import AWS_ACM = require("@aws-sdk/client-acm"); +import ACMClient = AWS_ACM.ACM; import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer"); +import AccessAnalyzerClient = AWS_AccessAnalyzer.AccessAnalyzer; import AWS_Discovery = require("@aws-sdk/client-application-discovery-service"); +import DiscoveryClient = AWS_Discovery.ApplicationDiscoveryService; -new AWS_ACM.ACM(); -new AWS_AccessAnalyzer.AccessAnalyzer(); -new AWS_Discovery.ApplicationDiscoveryService(); +new ACMClient(); +new AccessAnalyzerClient(); +new DiscoveryClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.output.ts index b3cbb2bd9..5b36073e1 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.output.ts @@ -3,9 +3,12 @@ import AWS_ACM = require("@aws-sdk/client-acm"); +import ACM = AWS_ACM.ACM; import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer"); +import AccessAnalyzer = AWS_AccessAnalyzer.AccessAnalyzer; import AWS_Discovery = require("@aws-sdk/client-application-discovery-service"); +import ApplicationDiscoveryService = AWS_Discovery.ApplicationDiscoveryService; -new AWS_ACM.ACM(); -new AWS_AccessAnalyzer.AccessAnalyzer(); -new AWS_Discovery.ApplicationDiscoveryService(); +new ACM(); +new AccessAnalyzer(); +new ApplicationDiscoveryService(); diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.output.ts index e187511ce..aca62379b 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.import-equals.output.ts @@ -1,10 +1,13 @@ import AWS_s3_request_presigner = require("@aws-sdk/s3-request-presigner"); +import getSignedUrl = AWS_s3_request_presigner.getSignedUrl; import AWS_S3 = require("@aws-sdk/client-s3"); +import GetObjectCommand = AWS_S3.GetObjectCommand; +import S3 = AWS_S3.S3; -const s3 = new AWS_S3.S3(); +const s3 = new S3(); -url = await AWS_s3_request_presigner.getSignedUrl(s3, new AWS_S3.GetObjectCommand({ Bucket: "bucket", Key: "key" })); -url = await AWS_s3_request_presigner.getSignedUrl(s3, new AWS_S3.GetObjectCommand({ +url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" })); +url = await getSignedUrl(s3, new GetObjectCommand({ Bucket: "bucket", Key: "key" }), { diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-import-equals.output.ts index 562ede6af..61ac036d7 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-import-equals.output.ts @@ -1,8 +1,10 @@ import AWS_lib_storage = require("@aws-sdk/lib-storage"); +import Upload = AWS_lib_storage.Upload; import AWS_S3 = require("@aws-sdk/client-s3"); +import S3 = AWS_S3.S3; -const client = new AWS_S3.S3({ region: "REGION" }); -await new AWS_lib_storage.Upload({ +const client = new S3({ region: "REGION" }); +await new Upload({ client, params: { diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-import-equals.output.ts index 562ede6af..61ac036d7 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-import-equals.output.ts @@ -1,8 +1,10 @@ import AWS_lib_storage = require("@aws-sdk/lib-storage"); +import Upload = AWS_lib_storage.Upload; import AWS_S3 = require("@aws-sdk/client-s3"); +import S3 = AWS_S3.S3; -const client = new AWS_S3.S3({ region: "REGION" }); -await new AWS_lib_storage.Upload({ +const client = new S3({ region: "REGION" }); +await new Upload({ client, params: { diff --git a/src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.output.ts index f87b2aae1..2d48254b9 100644 --- a/src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/waiters/global-import-equals.output.ts @@ -1,11 +1,13 @@ import AWS_S3 = require("@aws-sdk/client-s3"); +import waitUntilBucketExists = AWS_S3.waitUntilBucketExists; +import S3 = AWS_S3.S3; const Bucket = "BUCKET_NAME"; -const client = new AWS_S3.S3({ region: "REGION" }); +const client = new S3({ region: "REGION" }); await client.createBucket({ Bucket }); -await AWS_S3.waitUntilBucketExists({ +await waitUntilBucketExists({ client, maxWaitTime: 200 }, { Bucket }); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.output.ts b/src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.output.ts index f87b2aae1..2d48254b9 100644 --- a/src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.output.ts +++ b/src/transforms/v2-to-v3/__fixtures__/waiters/service-import-equals.output.ts @@ -1,11 +1,13 @@ import AWS_S3 = require("@aws-sdk/client-s3"); +import waitUntilBucketExists = AWS_S3.waitUntilBucketExists; +import S3 = AWS_S3.S3; const Bucket = "BUCKET_NAME"; -const client = new AWS_S3.S3({ region: "REGION" }); +const client = new S3({ region: "REGION" }); await client.createBucket({ Bucket }); -await AWS_S3.waitUntilBucketExists({ +await waitUntilBucketExists({ client, maxWaitTime: 200 }, { Bucket }); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts b/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts index c9523d3c8..db3043ba2 100644 --- a/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts +++ b/src/transforms/v2-to-v3/apis/replaceS3GetSignedUrlApi.ts @@ -8,23 +8,16 @@ import { ObjectProperty, } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST, S3 } from "../config"; -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; +import { OBJECT_PROPERTY_TYPE_LIST } from "../config"; import { ClientIdentifier } from "../types"; import { getClientApiCallExpression } from "./getClientApiCallExpression"; import { getCommandName } from "./getCommandName"; -export interface ReplaceS3GetSignedUrlApiOptions { - clientIdentifiers: ClientIdentifier[]; - importType: ImportType; -} - // Updates `s3.getSignedUrl()` API with `await getSignedUrl(s3, command)` API. export const replaceS3GetSignedUrlApi = ( j: JSCodeshift, source: Collection, - { clientIdentifiers, importType }: ReplaceS3GetSignedUrlApiOptions + clientIdentifiers: ClientIdentifier[] ): void => { for (const clientId of clientIdentifiers) { for (const getSignedUrlApiName of ["getSignedUrl", "getSignedUrlPromise"]) { @@ -78,23 +71,17 @@ export const replaceS3GetSignedUrlApi = ( ); } - const commandName = getV3ClientTypeName(getCommandName(apiName), S3, importType); const getSignedUrlArgs: (ClientIdentifier | NewExpression | ObjectExpression)[] = [ clientId, - j.newExpression(j.identifier(commandName), [params]), + j.newExpression(j.identifier(getCommandName(apiName)), [params]), ]; if (options.properties.length > 0) { getSignedUrlArgs.push(options); } - const getSignedUrlIdentifier = getV3ClientTypeName( - "getSignedUrl", - "s3_request_presigner", - importType - ); const outputCallExpression = j.callExpression.from({ - callee: j.identifier(getSignedUrlIdentifier), + callee: j.identifier("getSignedUrl"), arguments: getSignedUrlArgs, }); diff --git a/src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts b/src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts index b51935956..4748afc9d 100644 --- a/src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts +++ b/src/transforms/v2-to-v3/apis/replaceS3UploadApi.ts @@ -1,20 +1,13 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift"; -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; import { ClientIdentifier } from "../types"; import { getClientApiCallExpression } from "./getClientApiCallExpression"; -export interface ReplaceS3UploadApiOptions { - clientIdentifiers: ClientIdentifier[]; - importType: ImportType; -} - // Updates `s3.upload()` API with `new Upload()` API. export const replaceS3UploadApi = ( j: JSCodeshift, source: Collection, - { clientIdentifiers, importType }: ReplaceS3UploadApiOptions + clientIdentifiers: ClientIdentifier[] ): void => { for (const clientId of clientIdentifiers) { // Replace `.promise()` call with `.done()` if present. @@ -66,8 +59,7 @@ export const replaceS3UploadApi = ( } } - const uploadApi = getV3ClientTypeName("Upload", "lib_storage", importType); - return j.newExpression(j.identifier(uploadApi), [j.objectExpression(properties)]); + return j.newExpression(j.identifier("Upload"), [j.objectExpression(properties)]); }); } }; diff --git a/src/transforms/v2-to-v3/apis/replaceWaiterApi.ts b/src/transforms/v2-to-v3/apis/replaceWaiterApi.ts index b2d827a21..0beb8e88a 100644 --- a/src/transforms/v2-to-v3/apis/replaceWaiterApi.ts +++ b/src/transforms/v2-to-v3/apis/replaceWaiterApi.ts @@ -1,7 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; import { ClientIdentifier } from "../types"; import { getArgsWithoutWaiterConfig } from "./getArgsWithoutWaiterConfig"; import { getClientWaiterCallExpression } from "./getClientWaiterCallExpression"; @@ -10,27 +8,16 @@ import { getV3ClientWaiterApiName } from "./getV3ClientWaiterApiName"; import { getWaiterConfig } from "./getWaiterConfig"; import { getWaiterConfigValue } from "./getWaiterConfigValue"; -export interface ReplaceWaiterApiOptions { - clientIdentifiers: ClientIdentifier[]; - v2ClientName: string; - importType: ImportType; -} - // Updates .waitFor() API with waitUntil* API. export const replaceWaiterApi = ( j: JSCodeshift, source: Collection, - { clientIdentifiers, v2ClientName, importType }: ReplaceWaiterApiOptions + clientIdentifiers: ClientIdentifier[] ): void => { for (const clientId of clientIdentifiers) { const waiterStates = getClientWaiterStates(j, source, clientIdentifiers); for (const waiterState of waiterStates) { - const v3WaiterApiName = getV3ClientTypeName( - getV3ClientWaiterApiName(waiterState), - v2ClientName, - importType - ); source .find(j.CallExpression, getClientWaiterCallExpression(clientId, waiterState)) .replaceWith((callExpression) => { @@ -67,7 +54,7 @@ export const replaceWaiterApi = ( }) ); - return j.callExpression(j.identifier(v3WaiterApiName), [ + return j.callExpression(j.identifier(getV3ClientWaiterApiName(waiterState)), [ j.objectExpression(properties), getArgsWithoutWaiterConfig(callExpression.node.arguments[1]), ]); diff --git a/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts b/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts index 574027c6b..1fa2d752d 100644 --- a/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts +++ b/src/transforms/v2-to-v3/client-instances/getDynamoDBDocClientArgs.ts @@ -1,18 +1,11 @@ import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift"; import { OBJECT_PROPERTY_TYPE_LIST } from "../config"; -import { ImportType } from "../modules"; import { getDynamoDBForDocClient } from "./getDynamoDBForDocClient"; -export interface GetDynamoDBDocClientArgsOptions { - v2ClientName: string; - v2ClientLocalName?: string; - importType: ImportType; -} - export const getDynamoDBDocClientArgs = ( j: JSCodeshift, v2DocClientNewExpression: ASTPath, - options: GetDynamoDBDocClientArgsOptions + v2ClientLocalName?: string ) => { const dynamoDBDocClientOptions = j.objectExpression([]); @@ -57,7 +50,7 @@ export const getDynamoDBDocClientArgs = ( } return [ - getDynamoDBForDocClient(j, v2DocClientNewExpression, options), + getDynamoDBForDocClient(j, v2DocClientNewExpression, v2ClientLocalName), ...(dynamoDBDocClientOptions.properties.length > 0 ? [dynamoDBDocClientOptions] : []), ]; }; diff --git a/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts index 98d297874..b82943fec 100644 --- a/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts +++ b/src/transforms/v2-to-v3/client-instances/getDynamoDBForDocClient.ts @@ -1,19 +1,11 @@ import { ASTPath, JSCodeshift, NewExpression, ObjectProperty, Property } from "jscodeshift"; import { DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config"; -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; - -export interface GetDynamoDBForDocClientOptions { - v2ClientName: string; - v2ClientLocalName?: string; - importType: ImportType; -} export const getDynamoDBForDocClient = ( j: JSCodeshift, v2DocClientNewExpression: ASTPath, - { v2ClientName, v2ClientLocalName, importType }: GetDynamoDBForDocClientOptions + v2ClientLocalName?: string ) => { const v2DocClientArgs = v2DocClientNewExpression.node.arguments || []; @@ -67,10 +59,5 @@ export const getDynamoDBForDocClient = ( } } - const typeName = - importType === ImportType.IMPORT_EQUALS ? DYNAMODB : v2ClientLocalName ?? DYNAMODB; - return j.newExpression( - j.identifier(getV3ClientTypeName(typeName, v2ClientName, importType)), - v3DocClientNewExpressionArgs - ); + return j.newExpression(j.identifier(v2ClientLocalName ?? DYNAMODB), v3DocClientNewExpressionArgs); }; diff --git a/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts b/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts index 796fc8e24..eb99ff89e 100644 --- a/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts +++ b/src/transforms/v2-to-v3/client-instances/replaceClientCreation.ts @@ -1,7 +1,4 @@ import { Collection, JSCodeshift } from "jscodeshift"; - -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; import { getClientNewExpression } from "../utils"; export interface ReplaceClientCreationOptions { @@ -9,40 +6,27 @@ export interface ReplaceClientCreationOptions { v2ClientLocalName: string; v2GlobalName?: string; v3ClientName: string; - importType: ImportType; } // Replace v2 client creation with v3 client creation. export const replaceClientCreation = ( j: JSCodeshift, source: Collection, - { - v2ClientName, - v2ClientLocalName, - v2GlobalName, - v3ClientName, - importType, - }: ReplaceClientCreationOptions + { v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName }: ReplaceClientCreationOptions ): void => { - const clientName = - importType === ImportType.IMPORT_EQUALS || v2ClientName === v2ClientLocalName - ? v3ClientName - : v2ClientLocalName; - const clientLocalName = - importType === ImportType.IMPORT_EQUALS ? v2ClientName : v2ClientLocalName; - const v3ClientConstructor = getV3ClientTypeName(clientName, clientLocalName, importType); + const clientName = v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName; if (v2GlobalName) { source .find(j.NewExpression, getClientNewExpression({ v2GlobalName, v2ClientName })) .replaceWith((v2ClientNewExpression) => - j.newExpression(j.identifier(v3ClientConstructor), v2ClientNewExpression.node.arguments) + j.newExpression(j.identifier(clientName), v2ClientNewExpression.node.arguments) ); } source .find(j.NewExpression, getClientNewExpression({ v2ClientName, v2ClientLocalName })) .replaceWith((v2ClientNewExpression) => - j.newExpression(j.identifier(v3ClientConstructor), v2ClientNewExpression.node.arguments) + j.newExpression(j.identifier(clientName), v2ClientNewExpression.node.arguments) ); }; diff --git a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts index e0f87ee0e..b55cec448 100644 --- a/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts +++ b/src/transforms/v2-to-v3/client-instances/replaceDocClientCreation.ts @@ -1,8 +1,6 @@ import { Collection, JSCodeshift } from "jscodeshift"; import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT, DYNAMODB_DOCUMENT_CLIENT } from "../config"; -import { ImportType, getImportEqualsLocalNameSuffix } from "../modules"; -import { getV3ClientTypeName } from "../ts-type"; import { getClientNewExpression } from "../utils"; import { getDynamoDBDocClientArgs } from "./getDynamoDBDocClientArgs"; @@ -10,23 +8,15 @@ export interface ReplaceDocClientCreationOptions { v2ClientName: string; v2ClientLocalName: string; v2GlobalName?: string; - importType: ImportType; } export const replaceDocClientCreation = ( j: JSCodeshift, source: Collection, - { v2ClientName, v2ClientLocalName, v2GlobalName, importType }: ReplaceDocClientCreationOptions + { v2ClientName, v2ClientLocalName, v2GlobalName }: ReplaceDocClientCreationOptions ): void => { if (v2ClientName !== DYNAMODB) return; - const v3DocClientName = getV3ClientTypeName( - DYNAMODB_DOCUMENT, - getImportEqualsLocalNameSuffix(v2ClientName, "@aws-sdk/lib-dynamodb"), - importType - ); - const ddbDocClientOptions = { v2ClientName, v2ClientLocalName, importType }; - if (v2GlobalName) { source .find( @@ -35,8 +25,8 @@ export const replaceDocClientCreation = ( ) .replaceWith((v2DocClientNewExpression) => j.callExpression( - j.memberExpression(j.identifier(v3DocClientName), j.identifier("from")), - getDynamoDBDocClientArgs(j, v2DocClientNewExpression, ddbDocClientOptions) + j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")), + getDynamoDBDocClientArgs(j, v2DocClientNewExpression, v2ClientLocalName) ) ); } @@ -48,8 +38,8 @@ export const replaceDocClientCreation = ( ) .replaceWith((v2DocClientNewExpression) => j.callExpression( - j.memberExpression(j.identifier(v3DocClientName), j.identifier("from")), - getDynamoDBDocClientArgs(j, v2DocClientNewExpression, ddbDocClientOptions) + j.memberExpression(j.identifier(DYNAMODB_DOCUMENT), j.identifier("from")), + getDynamoDBDocClientArgs(j, v2DocClientNewExpression, v2ClientLocalName) ) ); }; diff --git a/src/transforms/v2-to-v3/modules/addClientModules.ts b/src/transforms/v2-to-v3/modules/addClientModules.ts index c69a84501..4c22d41c6 100644 --- a/src/transforms/v2-to-v3/modules/addClientModules.ts +++ b/src/transforms/v2-to-v3/modules/addClientModules.ts @@ -27,7 +27,7 @@ import { ClientModulesOptions, ImportType } from "./types"; export const addClientModules = ( j: JSCodeshift, source: Collection, - options: ClientModulesOptions + options: ClientModulesOptions & { importType: ImportType } ): void => { const { clientIdentifiers, v2ClientName, v3ClientName, v2ClientLocalName, importType } = options; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts new file mode 100644 index 000000000..75b861e68 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts @@ -0,0 +1,54 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { getDefaultLocalName } from "../../utils"; +import { getImportEqualsDeclaration } from "../getImportEqualsDeclaration"; +import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType"; +import { getImportEqualsLocalNameSuffix } from "../getImportEqualsLocalNameSuffix"; +import { ClientModulesOptions } from "../types"; + +export type ClientDefaultModuleOptions = Omit; + +export const addClientDefaultModule = ( + j: JSCodeshift, + source: Collection, + { v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: ClientDefaultModuleOptions +) => { + const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName); + const defaultLocalName = getDefaultLocalName(localNameSuffix); + const existingImportEquals = source.find( + j.TSImportEqualsDeclaration, + getImportEqualsDeclarationType(v3ClientPackageName) + ); + + if (existingImportEquals.size()) { + if ( + existingImportEquals + .nodes() + .some((importEqualsDeclaration) => importEqualsDeclaration.id.name === defaultLocalName) + ) { + return; + } + } + + // Insert after global, or service import equals. + const v2ImportEqualsDeclaration = getImportEqualsDeclaration(j, source, { + v2ClientName, + v2ClientLocalName, + v2GlobalName, + }).at(0); + + const v3importEqualsDeclaration = j.tsImportEqualsDeclaration( + j.identifier(defaultLocalName), + j.tsExternalModuleReference(j.stringLiteral(v3ClientPackageName)) + ); + + if (v2ImportEqualsDeclaration && v2ImportEqualsDeclaration.nodes().length > 0) { + v2ImportEqualsDeclaration.at(0).insertAfter(v3importEqualsDeclaration); + } else { + // Unreachable code, throw error + throw new Error( + "Base Import Equals Declaration not found to insert new Import Declaration.\n" + + "Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" + ); + } +}; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts index 0990eaafe..38de8793f 100644 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts @@ -1,56 +1,69 @@ import { Collection, JSCodeshift } from "jscodeshift"; import { getDefaultLocalName } from "../../utils"; -import { getImportEqualsDeclaration } from "../getImportEqualsDeclaration"; import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType"; import { getImportEqualsLocalNameSuffix } from "../getImportEqualsLocalNameSuffix"; -import { ClientModulesOptions } from "../types"; +import { ClientModulesOptions, ImportSpecifierOptions } from "../types"; +import { addClientDefaultModule } from "./addClientDefaultModule"; -/** - * Import Equals does not support named import. - * We just add a default import for the package. - */ export const addClientNamedModule = ( j: JSCodeshift, source: Collection, - { v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: ClientModulesOptions + options: ClientModulesOptions & ImportSpecifierOptions ) => { + const { importedName, localName = importedName, ...v3ClientModulesOptions } = options; + const { v2ClientName, v3ClientPackageName } = v3ClientModulesOptions; + const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName); const defaultLocalName = getDefaultLocalName(localNameSuffix); - const existingImportEquals = source.find( - j.TSImportEqualsDeclaration, - getImportEqualsDeclarationType(v3ClientPackageName) - ); - if (existingImportEquals.size()) { - if ( - existingImportEquals - .nodes() - .some((importEqualsDeclaration) => importEqualsDeclaration.id.name === defaultLocalName) - ) { - return; - } + const existingDeclaration = source.find(j.TSImportEqualsDeclaration, { + type: "TSImportEqualsDeclaration", + id: { + type: "Identifier", + name: localName, + }, + moduleReference: { + type: "TSQualifiedName", + left: { + type: "Identifier", + name: defaultLocalName, + }, + right: { + type: "Identifier", + name: importedName, + }, + }, + }); + + if (existingDeclaration.size()) { + return; } - // Insert after global, or service import equals. - const v2ImportEqualsDeclaration = getImportEqualsDeclaration(j, source, { - v2ClientName, - v2ClientLocalName, - v2GlobalName, - }).at(0); + const defaultDeclaration = getImportEqualsDeclarationType(v3ClientPackageName); + if (source.find(j.TSImportEqualsDeclaration, defaultDeclaration).size() === 0) { + addClientDefaultModule(j, source, v3ClientModulesOptions); + } - const v3importEqualsDeclaration = j.tsImportEqualsDeclaration( - j.identifier(defaultLocalName), - j.tsExternalModuleReference(j.stringLiteral(v3ClientPackageName)) + const importEqualsDeclaration = j.tsImportEqualsDeclaration( + j.identifier(localName), + j.tsQualifiedName(j.identifier(defaultLocalName), j.identifier(importedName)) ); - if (v2ImportEqualsDeclaration && v2ImportEqualsDeclaration.nodes().length > 0) { - v2ImportEqualsDeclaration.at(0).insertAfter(v3importEqualsDeclaration); - } else { - // Unreachable code, throw error - throw new Error( - "Base Import Equals Declaration not found to insert new Import Declaration.\n" + - "Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" + const v3ClientImportEquals = source + .find(j.TSImportEqualsDeclaration, defaultDeclaration) + .filter( + (importEqualsDeclaration) => importEqualsDeclaration.value.id.name === defaultLocalName ); + + if (v3ClientImportEquals.size() > 0) { + v3ClientImportEquals.at(0).insertAfter(importEqualsDeclaration); + return; } + + // Unreachable code, throw error + throw new Error( + "The named import equals can't exist on it's own.\n" + + "Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" + ); }; diff --git a/src/transforms/v2-to-v3/modules/types.ts b/src/transforms/v2-to-v3/modules/types.ts index 69bbefcbe..2209242ec 100644 --- a/src/transforms/v2-to-v3/modules/types.ts +++ b/src/transforms/v2-to-v3/modules/types.ts @@ -7,7 +7,6 @@ export interface ClientModulesOptions { v3ClientName: string; v3ClientPackageName: string; clientIdentifiers: ClientIdentifier[]; - importType: ImportType; } export interface ImportSpecifierOptions { diff --git a/src/transforms/v2-to-v3/transformer.ts b/src/transforms/v2-to-v3/transformer.ts index 0a47f0786..2a4007a84 100644 --- a/src/transforms/v2-to-v3/transformer.ts +++ b/src/transforms/v2-to-v3/transformer.ts @@ -72,24 +72,24 @@ const transformer = async (file: FileInfo, api: API) => { const v3Options = { v3ClientName, v3ClientPackageName }; addClientModules(j, source, { ...v2Options, ...v3Options, clientIdentifiers, importType }); - replaceTSQualifiedName(j, source, { ...v2Options, v3ClientName, importType }); + replaceTSQualifiedName(j, source, { ...v2Options, v3ClientName }); removeClientModule(j, source, { ...v2Options, importType }); if (v2ClientName === S3) { // Needs to be called before removing promise calls, as replacement has `.done()` call. - replaceS3UploadApi(j, source, { clientIdentifiers, importType }); + replaceS3UploadApi(j, source, clientIdentifiers); } removePromiseCalls(j, source, clientIdentifiers); if (v2ClientName === S3) { - replaceS3GetSignedUrlApi(j, source, { clientIdentifiers, importType }); + replaceS3GetSignedUrlApi(j, source, clientIdentifiers); } - replaceWaiterApi(j, source, { clientIdentifiers, v2ClientName, importType }); + replaceWaiterApi(j, source, clientIdentifiers); - replaceClientCreation(j, source, { ...v2Options, v3ClientName, importType }); - replaceDocClientCreation(j, source, { ...v2Options, importType }); + replaceClientCreation(j, source, { ...v2Options, v3ClientName }); + replaceDocClientCreation(j, source, v2Options); } replaceAwsUtilFunctions(j, source, v2GlobalName); removeGlobalModule(j, source, { v2GlobalName, importType }); diff --git a/src/transforms/v2-to-v3/ts-type/getTypeForString.ts b/src/transforms/v2-to-v3/ts-type/getTypeForString.ts index a1e2a021f..c634a7dcf 100644 --- a/src/transforms/v2-to-v3/ts-type/getTypeForString.ts +++ b/src/transforms/v2-to-v3/ts-type/getTypeForString.ts @@ -1,19 +1,12 @@ import { JSCodeshift, TSType } from "jscodeshift"; -import { ImportType } from "../modules"; -import { getV3ClientTypeName } from "./getV3ClientTypeName"; const arrayRegex = /^Array<(.*)>$/; const recordRegex = /^Record$/; -export interface GetTypeForStringOptions { - v2ClientLocalName: string; - v3ClientTypeString: string; -} - export const getTypeForString = ( j: JSCodeshift, - importType: ImportType, - { v2ClientLocalName, v3ClientTypeString }: GetTypeForStringOptions + v2ClientLocalName: string, + v3ClientTypeString: string ): TSType => { if (v3ClientTypeString === "string") { return j.tsStringKeyword(); @@ -34,10 +27,7 @@ export const getTypeForString = ( const arrayRegexMatches = arrayRegex.exec(v3ClientTypeString); if (arrayRegexMatches) { const type = arrayRegexMatches[1]; - const typeArgument = getTypeForString(j, importType, { - v2ClientLocalName, - v3ClientTypeString: type, - }); + const typeArgument = getTypeForString(j, v2ClientLocalName, type); return j.tsTypeReference.from({ typeName: j.identifier("Array"), // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -49,10 +39,7 @@ export const getTypeForString = ( const recordRegexMatches = recordRegex.exec(v3ClientTypeString); if (recordRegexMatches) { const type = recordRegexMatches[1]; - const typeArgument = getTypeForString(j, importType, { - v2ClientLocalName, - v3ClientTypeString: type, - }); + const typeArgument = getTypeForString(j, v2ClientLocalName, type); return j.tsTypeReference.from({ typeName: j.identifier("Record"), // eslint-disable-next-line @typescript-eslint/ban-ts-comment @@ -61,7 +48,5 @@ export const getTypeForString = ( }); } - return j.tsTypeReference( - j.identifier(getV3ClientTypeName(v3ClientTypeString, v2ClientLocalName, importType)) - ); + return j.tsTypeReference(j.identifier(v3ClientTypeString)); }; diff --git a/src/transforms/v2-to-v3/ts-type/getV3ClientType.ts b/src/transforms/v2-to-v3/ts-type/getV3ClientType.ts index 42edeca45..b6999c122 100644 --- a/src/transforms/v2-to-v3/ts-type/getV3ClientType.ts +++ b/src/transforms/v2-to-v3/ts-type/getV3ClientType.ts @@ -1,46 +1,29 @@ import { JSCodeshift, TSType } from "jscodeshift"; -import { CLIENT_TYPES_MAP, DYNAMODB_DOCUMENT_CLIENT } from "../config"; +import { CLIENT_TYPES_MAP } from "../config"; import { CLIENT_REQ_RESP_TYPES_MAP } from "../config/CLIENT_REQ_RESP_TYPES_MAP"; -import { ImportType } from "../modules"; import { getTypeForString } from "./getTypeForString"; -import { getV3ClientTypeName } from "./getV3ClientTypeName"; export interface GetV3ClientTypeOptions { v2ClientLocalName: string; v2ClientName: string; v2ClientTypeName: string; - importType: ImportType; } export const getV3ClientType = (j: JSCodeshift, options: GetV3ClientTypeOptions): TSType => { - const { v2ClientName, v2ClientTypeName, importType } = options; - - // ToDo: remove this hack for ddb-doc-client-input-output-type. - const v2ClientLocalName = - v2ClientName === DYNAMODB_DOCUMENT_CLIENT && importType === ImportType.IMPORT_EQUALS - ? "lib_dynamodb" - : options.v2ClientLocalName; - + const { v2ClientName, v2ClientLocalName, v2ClientTypeName } = options; const clientReqRespTypesMap = CLIENT_REQ_RESP_TYPES_MAP[v2ClientName]; if (Object.keys(clientReqRespTypesMap).includes(v2ClientTypeName)) { const v3ClientTypeName = clientReqRespTypesMap[v2ClientTypeName]; - return j.tsTypeReference( - j.identifier(getV3ClientTypeName(v3ClientTypeName, v2ClientLocalName, importType)) - ); + return j.tsTypeReference(j.identifier(v3ClientTypeName)); } const clientTypesMap = CLIENT_TYPES_MAP[v2ClientName]; if (Object.keys(clientTypesMap).includes(v2ClientTypeName)) { - return getTypeForString(j, importType, { - v2ClientLocalName, - v3ClientTypeString: clientTypesMap[v2ClientTypeName], - }); + return getTypeForString(j, v2ClientLocalName, clientTypesMap[v2ClientTypeName]); } - return j.tsTypeReference( - j.identifier(getV3ClientTypeName(v2ClientTypeName, v2ClientLocalName, importType)) - ); + return j.tsTypeReference(j.identifier(v2ClientTypeName)); }; diff --git a/src/transforms/v2-to-v3/ts-type/getV3ClientTypeName.ts b/src/transforms/v2-to-v3/ts-type/getV3ClientTypeName.ts deleted file mode 100644 index ee095be41..000000000 --- a/src/transforms/v2-to-v3/ts-type/getV3ClientTypeName.ts +++ /dev/null @@ -1,14 +0,0 @@ -import { ImportType } from "../modules"; -import { getDefaultLocalName } from "../utils"; - -export const getV3ClientTypeName = ( - typeName: string, - localName: string, - importType: ImportType -) => { - const v3ClientTypeNameSections = [typeName]; - if (importType === ImportType.IMPORT_EQUALS) { - v3ClientTypeNameSections.unshift(getDefaultLocalName(localName)); - } - return v3ClientTypeNameSections.join("."); -}; diff --git a/src/transforms/v2-to-v3/ts-type/index.ts b/src/transforms/v2-to-v3/ts-type/index.ts index b6238f626..af75419e8 100644 --- a/src/transforms/v2-to-v3/ts-type/index.ts +++ b/src/transforms/v2-to-v3/ts-type/index.ts @@ -1,5 +1,4 @@ export * from "./getClientTypeNames"; export * from "./getV3ClientType"; -export * from "./getV3ClientTypeName"; export * from "./getV3ClientTypes"; export * from "./replaceTSQualifiedName"; diff --git a/src/transforms/v2-to-v3/ts-type/replaceTSQualifiedName.ts b/src/transforms/v2-to-v3/ts-type/replaceTSQualifiedName.ts index 90f3bfc95..22f37fac3 100644 --- a/src/transforms/v2-to-v3/ts-type/replaceTSQualifiedName.ts +++ b/src/transforms/v2-to-v3/ts-type/replaceTSQualifiedName.ts @@ -1,7 +1,6 @@ import { ASTPath, Collection, Identifier, JSCodeshift, TSQualifiedName } from "jscodeshift"; import { DOCUMENT_CLIENT, DYNAMODB, DYNAMODB_DOCUMENT_CLIENT } from "../config"; -import { ImportType } from "../modules"; import { getClientTypeNames } from "./getClientTypeNames"; import { getTSQualifiedNameFromClientName } from "./getTSQualifiedNameFromClientName"; import { getV3ClientType } from "./getV3ClientType"; @@ -12,7 +11,6 @@ export interface ReplaceTSQualifiedNameOptions { v2ClientLocalName: string; v2GlobalName?: string; v3ClientName: string; - importType: ImportType; } const isRightSectionIdentifier = (node: TSQualifiedName) => node.right.type === "Identifier"; @@ -28,8 +26,8 @@ export const replaceTSQualifiedName = ( source: Collection, options: ReplaceTSQualifiedNameOptions ): void => { - const { v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName, importType } = options; - const clientTypeOptions = { v2ClientName, v2ClientLocalName, importType }; + const { v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientName } = options; + const clientTypeOptions = { v2ClientName, v2ClientLocalName }; if (v2GlobalName) { // Replace type reference to client created with global name. diff --git a/src/transforms/v2-to-v3/ts-type/updateV2ClientType.ts b/src/transforms/v2-to-v3/ts-type/updateV2ClientType.ts index 08326683a..a707f0c31 100644 --- a/src/transforms/v2-to-v3/ts-type/updateV2ClientType.ts +++ b/src/transforms/v2-to-v3/ts-type/updateV2ClientType.ts @@ -1,6 +1,4 @@ import { ASTPath, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift"; -import { DOCUMENT_CLIENT } from "../config"; -import { ImportType } from "../modules"; import { addTsTypeQueryToRefType } from "./addTsTypeQueryToRefType"; import { getV3ClientType } from "./getV3ClientType"; @@ -12,7 +10,6 @@ interface UpdateV2ClientTypeOptions { v2ClientName: string; v2ClientTypeName: string; v2ClientLocalName: string; - importType: ImportType; } export const updateV2ClientType = ( @@ -20,14 +17,7 @@ export const updateV2ClientType = ( v2ClientType: ASTPath, options: UpdateV2ClientTypeOptions ) => { - const { importType } = options; - const v2ClientLocalName = - importType === ImportType.IMPORT_EQUALS && - options.v2ClientLocalName.endsWith(`.${DOCUMENT_CLIENT}`) - ? "lib_dynamodb" - : options.v2ClientLocalName; - - const v3ClientType = getV3ClientType(j, { ...options, v2ClientLocalName }); + const v3ClientType = getV3ClientType(j, options); if (v2ClientType.parentPath?.value.type === "TSTypeQuery") { if (nativeTsRefTypes.includes(v3ClientType.type)) {