diff --git a/.changeset/great-laws-sit.md b/.changeset/great-laws-sit.md new file mode 100644 index 000000000..2b3eb1466 --- /dev/null +++ b/.changeset/great-laws-sit.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": minor +--- + +Add require declaration per client diff --git a/scripts/generateNewClientTests/getGlobalRequireOutput.ts b/scripts/generateNewClientTests/getGlobalRequireOutput.ts index 17746b89f..b95bfa183 100644 --- a/scripts/generateNewClientTests/getGlobalRequireOutput.ts +++ b/scripts/generateNewClientTests/getGlobalRequireOutput.ts @@ -4,7 +4,7 @@ import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getGlobalRequireOutput = () => { - let content = ``; + let content = `\n\n`; content += getV3PackageRequiresCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST)); content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); diff --git a/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts b/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts index 9f08926ef..1e2aa8834 100644 --- a/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts +++ b/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts @@ -3,10 +3,9 @@ import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getServiceRequireDeepOutput = () => { - let content = ``; + let content = `\n\n`; - content += getV3PackageRequiresCode(CLIENTS_TO_TEST, { declarationPerClient: true }); - content += `\n`; + content += getV3PackageRequiresCode(CLIENTS_TO_TEST); content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); return content; diff --git a/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts b/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts index 9db81f6d0..ec335dcef 100644 --- a/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts @@ -4,13 +4,9 @@ import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getServiceRequireDeepWithNameOutput = () => { - let content = ``; + let content = `\n\n`; - content += getV3PackageRequiresCode(CLIENTS_TO_TEST, { - declarationPerClient: true, - useLocalSuffix: true, - }); - content += `\n`; + content += getV3PackageRequiresCode(CLIENTS_TO_TEST, { useLocalSuffix: true }); content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix)); return content; diff --git a/scripts/generateNewClientTests/getServiceRequireWithNameOutput.ts b/scripts/generateNewClientTests/getServiceRequireWithNameOutput.ts index 27692beff..6b5ac4392 100644 --- a/scripts/generateNewClientTests/getServiceRequireWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceRequireWithNameOutput.ts @@ -5,7 +5,7 @@ import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getServiceRequireWithNameOutput = () => { - let content = ``; + let content = `\n\n`; content += getV3PackageRequiresCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST), { useLocalSuffix: true, diff --git a/scripts/generateNewClientTests/getV3PackageRequiresCode.ts b/scripts/generateNewClientTests/getV3PackageRequiresCode.ts index a60893ec6..801df5685 100644 --- a/scripts/generateNewClientTests/getV3PackageRequiresCode.ts +++ b/scripts/generateNewClientTests/getV3PackageRequiresCode.ts @@ -7,7 +7,6 @@ import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; export interface V3PackageRequiresCodeOptions { useLocalSuffix?: boolean; - declarationPerClient?: boolean; } export const getV3PackageRequiresCode = ( @@ -15,11 +14,7 @@ export const getV3PackageRequiresCode = ( options?: V3PackageRequiresCodeOptions ) => { let content = ``; - const { useLocalSuffix = false, declarationPerClient = false } = options || {}; - - if (!declarationPerClient) { - content += `const `; - } + const { useLocalSuffix = false } = options || {}; for (const v2ClientName of clientsToTest) { const v3ClientName = CLIENT_NAMES_MAP[v2ClientName]; @@ -30,13 +25,7 @@ export const getV3PackageRequiresCode = ( const v3RequireKeyValuePair = v2ClientName === v2ClientLocalName ? v3ClientName : `${v3ClientName}: ${v2ClientLocalName}`; - content += declarationPerClient - ? `const {\n ${v3RequireKeyValuePair}\n} = require("${v3ClientPackageName}");\n` - : `{\n ${v3RequireKeyValuePair}\n } = require("${v3ClientPackageName}"),\n `; - } - - if (!declarationPerClient) { - content = content.replace(/,\n {6}$/, ";\n\n"); + content += `const {\n ${v3RequireKeyValuePair}\n} = require("${v3ClientPackageName}");\n\n`; } return content; diff --git a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-require.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-require.output.js index bbcd71829..573cefcdd 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/global-require.output.js @@ -1,9 +1,10 @@ const { - DynamoDBDocument - } = require("@aws-sdk/lib-dynamodb"), - { - DynamoDB - } = require("@aws-sdk/client-dynamodb"); + DynamoDBDocument +} = require("@aws-sdk/lib-dynamodb"); + +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); 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-require-deep-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep-with-name.output.js index ece414bd1..992ab4b9e 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep-with-name.output.js @@ -1,9 +1,10 @@ const { - DynamoDBDocument - } = require("@aws-sdk/lib-dynamodb"), - { - DynamoDB: DynamoDBClient - } = require("@aws-sdk/client-dynamodb"); + DynamoDBDocument +} = require("@aws-sdk/lib-dynamodb"); + +const { + DynamoDB: DynamoDBClient +} = require("@aws-sdk/client-dynamodb"); 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-require-deep.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep.output.js index bbcd71829..573cefcdd 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-deep.output.js @@ -1,9 +1,10 @@ const { - DynamoDBDocument - } = require("@aws-sdk/lib-dynamodb"), - { - DynamoDB - } = require("@aws-sdk/client-dynamodb"); + DynamoDBDocument +} = require("@aws-sdk/lib-dynamodb"); + +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); 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-require-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-with-name.output.js index ece414bd1..992ab4b9e 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require-with-name.output.js @@ -1,9 +1,10 @@ const { - DynamoDBDocument - } = require("@aws-sdk/lib-dynamodb"), - { - DynamoDB: DynamoDBClient - } = require("@aws-sdk/client-dynamodb"); + DynamoDBDocument +} = require("@aws-sdk/lib-dynamodb"); + +const { + DynamoDB: DynamoDBClient +} = require("@aws-sdk/client-dynamodb"); 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-require.output.js b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require.output.js index bbcd71829..573cefcdd 100644 --- a/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/ddb-doc-client/service-require.output.js @@ -1,9 +1,10 @@ const { - DynamoDBDocument - } = require("@aws-sdk/lib-dynamodb"), - { - DynamoDB - } = require("@aws-sdk/client-dynamodb"); + DynamoDBDocument +} = require("@aws-sdk/lib-dynamodb"); + +const { + DynamoDB +} = require("@aws-sdk/client-dynamodb"); 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__/multiple-declarators/service.input.js b/src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.input.js similarity index 72% rename from src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service.input.js rename to src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.input.js index bfd4e0c4e..4ca8f93c6 100644 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.input.js @@ -1,6 +1,7 @@ const DynamoDB = require("aws-sdk/clients/dynamodb"), - Discovery = require("aws-sdk/clients/discovery"), - ddbClient = new DynamoDB(), + Discovery = require("aws-sdk/clients/discovery"); + +const ddbClient = new DynamoDB(), discoveryClient = new Discovery(); const ddbResponse = await ddbClient.listTables().promise(); diff --git a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service.output.js b/src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.output.js similarity index 51% rename from src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service.output.js rename to src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.output.js index 7154ce6da..05ffaf3a7 100644 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/misc/multiple-declarators.output.js @@ -1,10 +1,12 @@ +const { + ApplicationDiscoveryService +} = require("@aws-sdk/client-application-discovery-service"); + const { DynamoDB -} = require("@aws-sdk/client-dynamodb"), - { - ApplicationDiscoveryService - } = require("@aws-sdk/client-application-discovery-service"), - ddbClient = new DynamoDB(), +} = require("@aws-sdk/client-dynamodb"); + +const ddbClient = new DynamoDB(), discoveryClient = new ApplicationDiscoveryService(); const ddbResponse = await ddbClient.listTables(); diff --git a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.input.js b/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.input.js deleted file mode 100644 index bddb77f97..000000000 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.input.js +++ /dev/null @@ -1,6 +0,0 @@ -const AWS = require("aws-sdk"), - ddbClient = new AWS.DynamoDB(), - discoveryClient = new AWS.Discovery(); - -const ddbResponse = await ddbClient.listTables().promise(); -const discoveryResponse = await discoveryClient.describeAgents().promise(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.output.js b/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.output.js deleted file mode 100644 index e947345bd..000000000 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/global.output.js +++ /dev/null @@ -1,11 +0,0 @@ -const { - ApplicationDiscoveryService - } = require("@aws-sdk/client-application-discovery-service"), - { - DynamoDB - } = require("@aws-sdk/client-dynamodb"), - ddbClient = new DynamoDB(), - discoveryClient = new ApplicationDiscoveryService(); - -const ddbResponse = await ddbClient.listTables(); -const discoveryResponse = await discoveryClient.describeAgents(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.input.js b/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.input.js deleted file mode 100644 index 35a5d8760..000000000 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.input.js +++ /dev/null @@ -1,6 +0,0 @@ -const { DynamoDB, Discovery } = require("aws-sdk"), - ddbClient = new DynamoDB(), - discoveryClient = new Discovery(); - -const ddbResponse = await ddbClient.listTables().promise(); -const discoveryResponse = await discoveryClient.describeAgents().promise(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.output.js b/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.output.js deleted file mode 100644 index e947345bd..000000000 --- a/src/transforms/v2-to-v3/__fixtures__/multiple-declarators/service-named.output.js +++ /dev/null @@ -1,11 +0,0 @@ -const { - ApplicationDiscoveryService - } = require("@aws-sdk/client-application-discovery-service"), - { - DynamoDB - } = require("@aws-sdk/client-dynamodb"), - ddbClient = new DynamoDB(), - discoveryClient = new ApplicationDiscoveryService(); - -const ddbResponse = await ddbClient.listTables(); -const discoveryResponse = await discoveryClient.describeAgents(); \ No newline at end of file diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.output.js index 9b5cce8bf..0cdabe3ed 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.output.js @@ -1,11 +1,15 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + const { ACM: ACMClient } = require("@aws-sdk/client-acm"); + const { AccessAnalyzer: AccessAnalyzerClient } = require("@aws-sdk/client-accessanalyzer"); + const { ApplicationDiscoveryService: DiscoveryClient } = require("@aws-sdk/client-application-discovery-service"); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.output.js index a7bf9815c..539521ccf 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.output.js @@ -1,11 +1,15 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + const { ACM } = require("@aws-sdk/client-acm"); + const { AccessAnalyzer } = require("@aws-sdk/client-accessanalyzer"); + const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.output.js index ddd442888..73d783b4d 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.output.js @@ -1,14 +1,18 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + +const { + AccessAnalyzer +} = require("@aws-sdk/client-accessanalyzer"); + +const { + ACM +} = require("@aws-sdk/client-acm"); + const { - AccessAnalyzer - } = require("@aws-sdk/client-accessanalyzer"), - { - ACM - } = require("@aws-sdk/client-acm"), - { - ApplicationDiscoveryService - } = require("@aws-sdk/client-application-discovery-service"); + ApplicationDiscoveryService +} = require("@aws-sdk/client-application-discovery-service"); new ACM(); new AccessAnalyzer(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.output.js index 9b5cce8bf..0cdabe3ed 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.output.js @@ -1,11 +1,15 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + const { ACM: ACMClient } = require("@aws-sdk/client-acm"); + const { AccessAnalyzer: AccessAnalyzerClient } = require("@aws-sdk/client-accessanalyzer"); + const { ApplicationDiscoveryService: DiscoveryClient } = require("@aws-sdk/client-application-discovery-service"); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.output.js index a7bf9815c..539521ccf 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.output.js @@ -1,11 +1,15 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + const { ACM } = require("@aws-sdk/client-acm"); + const { AccessAnalyzer } = require("@aws-sdk/client-accessanalyzer"); + const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.output.js index 2b5f09fc7..8b47c4f95 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.output.js @@ -1,14 +1,18 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + +const { + AccessAnalyzer: AccessAnalyzerClient +} = require("@aws-sdk/client-accessanalyzer"); + +const { + ACM: ACMClient +} = require("@aws-sdk/client-acm"); + const { - AccessAnalyzer: AccessAnalyzerClient - } = require("@aws-sdk/client-accessanalyzer"), - { - ACM: ACMClient - } = require("@aws-sdk/client-acm"), - { - ApplicationDiscoveryService: DiscoveryClient - } = require("@aws-sdk/client-application-discovery-service"); + ApplicationDiscoveryService: DiscoveryClient +} = require("@aws-sdk/client-application-discovery-service"); new ACMClient(); new AccessAnalyzerClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.output.js index ddd442888..73d783b4d 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.output.js @@ -1,14 +1,18 @@ // This file is generated by scripts/generateNewClientTests/index.ts // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. + + +const { + AccessAnalyzer +} = require("@aws-sdk/client-accessanalyzer"); + +const { + ACM +} = require("@aws-sdk/client-acm"); + const { - AccessAnalyzer - } = require("@aws-sdk/client-accessanalyzer"), - { - ACM - } = require("@aws-sdk/client-acm"), - { - ApplicationDiscoveryService - } = require("@aws-sdk/client-application-discovery-service"); + ApplicationDiscoveryService +} = require("@aws-sdk/client-application-discovery-service"); new ACM(); new AccessAnalyzer(); diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.output.js index 77c5923cf..51498f2d1 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/s3-get-signed-url/getObject.require.output.js @@ -1,10 +1,11 @@ const { - getSignedUrl - } = require("@aws-sdk/s3-request-presigner"), - { - GetObjectCommand, - S3 - } = require("@aws-sdk/client-s3"); + getSignedUrl +} = require("@aws-sdk/s3-request-presigner"); + +const { + GetObjectCommand, + S3 +} = require("@aws-sdk/client-s3"); const s3 = new S3(); diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require-property.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require-property.output.js index 30e45f449..29fb64113 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require-property.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require-property.output.js @@ -1,9 +1,10 @@ const { - Upload - } = require("@aws-sdk/lib-storage"), - { - S3 - } = require("@aws-sdk/client-s3"); + Upload +} = require("@aws-sdk/lib-storage"); + +const { + S3 +} = require("@aws-sdk/client-s3"); const client = new S3({ region: "REGION" }); await new Upload({ diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require.output.js index 30e45f449..29fb64113 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/global-require.output.js @@ -1,9 +1,10 @@ const { - Upload - } = require("@aws-sdk/lib-storage"), - { - S3 - } = require("@aws-sdk/client-s3"); + Upload +} = require("@aws-sdk/lib-storage"); + +const { + S3 +} = require("@aws-sdk/client-s3"); const client = new S3({ region: "REGION" }); await new Upload({ diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require-deep.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require-deep.output.js index 30e45f449..29fb64113 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require-deep.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require-deep.output.js @@ -1,9 +1,10 @@ const { - Upload - } = require("@aws-sdk/lib-storage"), - { - S3 - } = require("@aws-sdk/client-s3"); + Upload +} = require("@aws-sdk/lib-storage"); + +const { + S3 +} = require("@aws-sdk/client-s3"); const client = new S3({ region: "REGION" }); await new Upload({ diff --git a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require.output.js b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require.output.js index 30e45f449..29fb64113 100644 --- a/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/s3-upload/service-require.output.js @@ -1,9 +1,10 @@ const { - Upload - } = require("@aws-sdk/lib-storage"), - { - S3 - } = require("@aws-sdk/client-s3"); + Upload +} = require("@aws-sdk/lib-storage"); + +const { + S3 +} = require("@aws-sdk/client-s3"); const client = new S3({ region: "REGION" }); await new Upload({ diff --git a/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts b/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts index 1b858736e..2a64016a8 100644 --- a/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts +++ b/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts @@ -1,10 +1,5 @@ -import { Collection, JSCodeshift } from "jscodeshift"; - +import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift"; import { PACKAGE_NAME } from "../config"; -import { getClientDeepImportPath } from "../utils"; -import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier"; -import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern"; -import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty"; export interface GetRequireDeclaratorOptions { v2ClientName: string; @@ -22,41 +17,86 @@ export const getRequireDeclarator = ( // Support DynamoDB.DocumentClient const v2ClientLocalName = options.v2ClientLocalName.split(".")[0]; - if (v2GlobalName) { - const requireDeclaratorsWithIdentifier = getRequireDeclaratorsWithIdentifier(j, source, { - identifierName: v2GlobalName, - sourceValue: PACKAGE_NAME, - }); + // Temporary fix, will be removed in https://github.com/awslabs/aws-sdk-js-codemod/pull/622 + const v2RequireCallExpressions = source + .find(j.VariableDeclaration) + .filter((variableDeclaration) => + variableDeclaration.value.declarations.some( + // @ts-expect-error Type 'JSXIdentifier' is not assignable to type 'Identifier'. + (declaration: VariableDeclarator | Identifier) => { + if (declaration.type === "Identifier") return false; - if (requireDeclaratorsWithIdentifier.size() > 0) { - return requireDeclaratorsWithIdentifier; - } - } + const id = declaration.id; + if (id.type === "Identifier") { + if (![v2GlobalName, v2ClientName, v2ClientLocalName].includes(id.name)) return false; + } + if (id.type === "ObjectPattern") { + if ( + !id.properties.some( + (property) => + property.type === "Property" && + property.key.type === "Identifier" && + [v2GlobalName, v2ClientName, v2ClientLocalName].includes(property.key.name) + ) + ) + return false; + } - const requireDeclaratorsWithObjectPattern = getRequireDeclaratorsWithObjectPattern(j, source, { - identifierName: v2ClientLocalName, - sourceValue: PACKAGE_NAME, - }); + const init = declaration.init; + if (!init) return false; + if (init.type !== "CallExpression") return false; - if (requireDeclaratorsWithObjectPattern.size() > 0) { - return requireDeclaratorsWithObjectPattern; - } + const callee = init.callee; + if (!callee) return false; + if (callee.type !== "Identifier") return false; + if (callee.name !== "require") return false; - const requireDeclaratorsWithProperty = getRequireDeclaratorsWithProperty(j, source, { - identifierName: v2ClientName, - sourceValue: PACKAGE_NAME, - }); + const args = init.arguments; + if (!args) return false; + if (args.length !== 1) return false; + if (args[0].type !== "Literal") return false; + if (typeof args[0].value !== "string") return false; + if (!args[0].value.startsWith(PACKAGE_NAME)) return false; - if (requireDeclaratorsWithProperty.size() > 0) { - return requireDeclaratorsWithProperty; + return true; + } + ) + ); + if (v2RequireCallExpressions.size()) { + return v2RequireCallExpressions; } - const requireDeclaratorsWithIdentifier = getRequireDeclaratorsWithIdentifier(j, source, { - identifierName: v2ClientLocalName, - sourceValue: getClientDeepImportPath(v2ClientName), - }); + const v2RequireProperties = source.find(j.VariableDeclaration).filter((variableDeclaration) => + variableDeclaration.value.declarations.some( + // @ts-expect-error Type 'JSXIdentifier' is not assignable to type 'Identifier'. + (declaration: VariableDeclarator | Identifier) => { + if (declaration.type === "Identifier") return false; + + const init = declaration.init; + if (!init) return false; + if (init.type !== "MemberExpression") return false; + + const object = init.object; + if (object.type !== "CallExpression") return false; + + const callee = object.callee; + if (callee.type !== "Identifier") return false; + if (callee.name !== "require") return false; + + const args = object.arguments; + if (args.length !== 1) return false; + if (args[0].type !== "Literal") return false; + if (args[0].value !== PACKAGE_NAME) return false; + + const property = init.property; + if (property.type !== "Identifier") return false; + if (![v2GlobalName, v2ClientName, v2ClientLocalName].includes(property.name)) return false; - if (requireDeclaratorsWithIdentifier.size() > 0) { - return requireDeclaratorsWithIdentifier; + return true; + } + ) + ); + if (v2RequireProperties.size()) { + return v2RequireProperties; } }; diff --git a/src/transforms/v2-to-v3/modules/removeImportDeclaration.ts b/src/transforms/v2-to-v3/modules/removeDeclaration.ts similarity index 53% rename from src/transforms/v2-to-v3/modules/removeImportDeclaration.ts rename to src/transforms/v2-to-v3/modules/removeDeclaration.ts index 1a9796c02..8d1761eba 100644 --- a/src/transforms/v2-to-v3/modules/removeImportDeclaration.ts +++ b/src/transforms/v2-to-v3/modules/removeDeclaration.ts @@ -1,12 +1,18 @@ -import { ASTPath, Collection, ImportDeclaration, JSCodeshift } from "jscodeshift"; +import { + ASTPath, + Collection, + ImportDeclaration, + JSCodeshift, + VariableDeclaration, +} from "jscodeshift"; /** - * Removes import declaration, but preserves comments if they're top level comments. + * Removes import/variable declaration, but preserves comments if they're top level comments. */ -export const removeImportDeclaration = ( +export const removeDeclaration = ( j: JSCodeshift, source: Collection, - declarationPath: ASTPath + declarationPath: ASTPath ) => { const firstNode = source.find(j.Program).get("body", 0).node; if (firstNode === declarationPath.node) { diff --git a/src/transforms/v2-to-v3/modules/removeImportDefault.ts b/src/transforms/v2-to-v3/modules/removeImportDefault.ts index d330762b3..2b4b5aa94 100644 --- a/src/transforms/v2-to-v3/modules/removeImportDefault.ts +++ b/src/transforms/v2-to-v3/modules/removeImportDefault.ts @@ -1,5 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; -import { removeImportDeclaration } from "./removeImportDeclaration"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveImportDefaultOptions { localName: string; @@ -27,7 +27,7 @@ export const removeImportDefault = ( // Remove ImportDeclaration if there are no import specifiers. if (declarationPath.value.specifiers?.length === 0) { - removeImportDeclaration(j, source, declarationPath); + removeDeclaration(j, source, declarationPath); } }); }; diff --git a/src/transforms/v2-to-v3/modules/removeImportEquals.ts b/src/transforms/v2-to-v3/modules/removeImportEquals.ts index 942c019cd..befcfacd0 100644 --- a/src/transforms/v2-to-v3/modules/removeImportEquals.ts +++ b/src/transforms/v2-to-v3/modules/removeImportEquals.ts @@ -1,5 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; -import { removeImportDeclaration } from "./removeImportDeclaration"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveImportEqualsOptions { localName: string; @@ -20,6 +20,6 @@ export const removeImportEquals = ( }, }); if (importEqualsDeclaration.length) { - removeImportDeclaration(j, source, importEqualsDeclaration.get()); + removeDeclaration(j, source, importEqualsDeclaration.get()); } }; diff --git a/src/transforms/v2-to-v3/modules/removeImportNamed.ts b/src/transforms/v2-to-v3/modules/removeImportNamed.ts index df474cdf0..30c572fc1 100644 --- a/src/transforms/v2-to-v3/modules/removeImportNamed.ts +++ b/src/transforms/v2-to-v3/modules/removeImportNamed.ts @@ -1,5 +1,5 @@ import { Collection, JSCodeshift } from "jscodeshift"; -import { removeImportDeclaration } from "./removeImportDeclaration"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveImportNamedOptions { importedName?: string; @@ -37,7 +37,7 @@ export const removeImportNamed = ( // Remove ImportDeclaration if there are no import specifiers. if (declarationPath.value.specifiers?.length === 0) { - removeImportDeclaration(j, source, declarationPath); + removeDeclaration(j, source, declarationPath); } }); }; diff --git a/src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts b/src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts index 805f00aca..e104f35cf 100644 --- a/src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts +++ b/src/transforms/v2-to-v3/modules/removeRequireIdentifier.ts @@ -1,6 +1,7 @@ import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift"; import { getRequireDeclaratorsWithIdentifier } from "./getRequireDeclaratorsWithIdentifier"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveRequireIdentifierOptions { localName: string; @@ -50,7 +51,7 @@ export const removeRequireIdentifier = ( // Remove VariableDeclaration if there are no declarations. if (varDeclaration.value.declarations?.length === 0) { - j(varDeclaration).remove(); + removeDeclaration(j, source, varDeclaration); } }); }; diff --git a/src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts b/src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts index 2057d1aed..631b6e92f 100644 --- a/src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts +++ b/src/transforms/v2-to-v3/modules/removeRequireObjectPattern.ts @@ -10,6 +10,7 @@ import { import { OBJECT_PROPERTY_TYPE_LIST } from "../config"; import { getRequireDeclaratorsWithObjectPattern } from "./getRequireDeclaratorsWithObjectPattern"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveRequireObjectPropertyOptions { localName: string; @@ -44,7 +45,7 @@ export const removeRequireObjectPattern = ( // Remove VariableDeclaration if there are no declarations. if (varDeclaration.value.declarations?.length === 0) { - j(varDeclaration).remove(); + removeDeclaration(j, source, varDeclaration); } } }); diff --git a/src/transforms/v2-to-v3/modules/removeRequireProperty.ts b/src/transforms/v2-to-v3/modules/removeRequireProperty.ts index 40adf66fb..4500d2160 100644 --- a/src/transforms/v2-to-v3/modules/removeRequireProperty.ts +++ b/src/transforms/v2-to-v3/modules/removeRequireProperty.ts @@ -1,6 +1,7 @@ import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift"; import { getRequireDeclaratorsWithProperty } from "./getRequireDeclaratorsWithProperty"; +import { removeDeclaration } from "./removeDeclaration"; export interface RemoveRequireObjectPropertyOptions { localName: string; @@ -57,7 +58,7 @@ export const removeRequireProperty = ( // Remove VariableDeclaration if there are no declarations. if (varDeclaration.value.declarations?.length === 0) { - j(varDeclaration).remove(); + removeDeclaration(j, source, varDeclaration); } }); }; diff --git a/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts b/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts index d285dd74a..fddad288c 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts @@ -61,7 +61,7 @@ export const addClientNamedModule = ( getRequireDeclarator(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName }); if (v2RequireDeclarator && v2RequireDeclarator.nodes().length > 0) { - v2RequireDeclarator.insertAfter(v3RequireDeclarator); + v2RequireDeclarator.insertAfter(j.variableDeclaration("const", [v3RequireDeclarator])); } else { // Unreachable code, throw error throw new Error(