diff --git a/.changeset/eight-hornets-remain.md b/.changeset/eight-hornets-remain.md new file mode 100644 index 000000000..05178f3bc --- /dev/null +++ b/.changeset/eight-hornets-remain.md @@ -0,0 +1,5 @@ +--- +"aws-sdk-js-codemod": patch +--- + +Add imports after the first occurrence of aws-sdk diff --git a/scripts/generateNewClientTests/config.ts b/scripts/generateNewClientTests/config.ts index 3b9ecbfb9..1449136b5 100644 --- a/scripts/generateNewClientTests/config.ts +++ b/scripts/generateNewClientTests/config.ts @@ -1,3 +1,3 @@ -export const CLIENTS_TO_TEST = ["ACM", "AccessAnalyzer", "Discovery"]; +export const CLIENTS_TO_TEST = ["AccessAnalyzer", "Discovery", "ACM"]; export const LOCAL_NAME_SUFFIX = "Client"; diff --git a/scripts/generateNewClientTests/getServiceImportDeepOutput.ts b/scripts/generateNewClientTests/getServiceImportDeepOutput.ts index b070e79ca..e0c1e66cd 100644 --- a/scripts/generateNewClientTests/getServiceImportDeepOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportDeepOutput.ts @@ -1,11 +1,12 @@ import { CLIENTS_TO_TEST } from "./config"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageImportsCode } from "./getV3PackageImportsCode"; export const getServiceImportDeepOutput = () => { let content = ``; - content += getV3PackageImportsCode(CLIENTS_TO_TEST); + content += getV3PackageImportsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST)); content += `\n`; content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); diff --git a/scripts/generateNewClientTests/getServiceImportDeepWithNameOutput.ts b/scripts/generateNewClientTests/getServiceImportDeepWithNameOutput.ts index df361064f..e52d6e42e 100644 --- a/scripts/generateNewClientTests/getServiceImportDeepWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportDeepWithNameOutput.ts @@ -1,12 +1,15 @@ import { CLIENTS_TO_TEST } from "./config"; import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageImportsCode } from "./getV3PackageImportsCode"; export const getServiceImportDeepWithNameOutput = () => { let content = ``; - content += getV3PackageImportsCode(CLIENTS_TO_TEST, { useLocalSuffix: true }); + content += getV3PackageImportsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST), { + useLocalSuffix: true, + }); content += `\n`; content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix)); diff --git a/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts b/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts index 4b2dc9442..899c1801d 100644 --- a/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportEqualsOutput.ts @@ -1,11 +1,12 @@ import { CLIENTS_TO_TEST } from "./config"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode"; export const getServiceImportEqualsOutput = () => { let content = ``; - content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST); + content += getV3PackageImportEqualsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST)); content += "\n"; content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); diff --git a/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts b/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts index ea7a7a85b..ad81b5f05 100644 --- a/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceImportEqualsWithNameOutput.ts @@ -1,12 +1,15 @@ import { CLIENTS_TO_TEST } from "./config"; import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode"; export const getServiceImportEqualsWithNameOutput = () => { let content = ``; - content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST, { useLocalSuffix: true }); + content += getV3PackageImportEqualsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST), { + useLocalSuffix: true, + }); content += "\n"; content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix)); diff --git a/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts b/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts index 1e2aa8834..f9301a511 100644 --- a/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts +++ b/scripts/generateNewClientTests/getServiceRequireDeepOutput.ts @@ -1,11 +1,12 @@ import { CLIENTS_TO_TEST } from "./config"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getServiceRequireDeepOutput = () => { let content = `\n\n`; - content += getV3PackageRequiresCode(CLIENTS_TO_TEST); + content += getV3PackageRequiresCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST)); content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST); return content; diff --git a/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts b/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts index ec335dcef..82d79d1f6 100644 --- a/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts +++ b/scripts/generateNewClientTests/getServiceRequireDeepWithNameOutput.ts @@ -1,12 +1,15 @@ import { CLIENTS_TO_TEST } from "./config"; import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix"; +import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName"; import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode"; import { getV3PackageRequiresCode } from "./getV3PackageRequiresCode"; export const getServiceRequireDeepWithNameOutput = () => { let content = `\n\n`; - content += getV3PackageRequiresCode(CLIENTS_TO_TEST, { useLocalSuffix: true }); + content += getV3PackageRequiresCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST), { + useLocalSuffix: true, + }); content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix)); return content; diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.input.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.input.ts index e796c6204..729b0aef5 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.input.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-equals.input.ts @@ -2,6 +2,6 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. import AWS = require("aws-sdk"); -new AWS.ACM(); new AWS.AccessAnalyzer(); new AWS.Discovery(); +new AWS.ACM(); 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 7c6d2cdcb..4778fa877 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 @@ -9,6 +9,6 @@ import ACM = AWS_client_acm.ACM; import AWS_client_application_discovery_service = require("@aws-sdk/client-application-discovery-service"); import ApplicationDiscoveryService = AWS_client_application_discovery_service.ApplicationDiscoveryService; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.input.js index 20fec1668..6288ab099 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.input.js @@ -2,6 +2,6 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. import * as AWS from "aws-sdk"; -new AWS.ACM(); new AWS.AccessAnalyzer(); new AWS.Discovery(); +new AWS.ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.output.js index 48e3365e0..d70263997 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-import-star.output.js @@ -6,6 +6,6 @@ import { AccessAnalyzer } from "@aws-sdk/client-accessanalyzer"; import { ACM } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService } from "@aws-sdk/client-application-discovery-service"; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.input.js index 3797863b6..24519f979 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property-with-name.input.js @@ -1,9 +1,9 @@ // 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 ACMClient = require("aws-sdk").ACM; const AccessAnalyzerClient = require("aws-sdk").AccessAnalyzer; const DiscoveryClient = require("aws-sdk").Discovery; +const ACMClient = require("aws-sdk").ACM; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); 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 0cdabe3ed..1d9a68df6 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 @@ -2,18 +2,18 @@ // 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 { + ACM: ACMClient +} = require("@aws-sdk/client-acm"); + const { ApplicationDiscoveryService: DiscoveryClient } = require("@aws-sdk/client-application-discovery-service"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.input.js index 5b65a4ddf..793d2ba9b 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require-property.input.js @@ -1,9 +1,9 @@ // 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").ACM; const AccessAnalyzer = require("aws-sdk").AccessAnalyzer; const Discovery = require("aws-sdk").Discovery; +const ACM = require("aws-sdk").ACM; -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); 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 539521ccf..c37b050ce 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 @@ -2,18 +2,18 @@ // 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 { + ACM +} = require("@aws-sdk/client-acm"); + const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.input.js index 409299748..5593b5f2c 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/global-require.input.js @@ -2,6 +2,6 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. const AWS = require("aws-sdk"); -new AWS.ACM(); new AWS.AccessAnalyzer(); new AWS.Discovery(); +new AWS.ACM(); 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 73d783b4d..c37b050ce 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 @@ -14,6 +14,6 @@ const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.input.js index be09bd490..b980839fe 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.input.js @@ -1,9 +1,9 @@ // 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. -import * as ACMClient from "aws-sdk/clients/acm"; import * as AccessAnalyzerClient from "aws-sdk/clients/accessanalyzer"; import * as DiscoveryClient from "aws-sdk/clients/discovery"; +import * as ACMClient from "aws-sdk/clients/acm"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.output.js index c8e3aa230..b8c838b9a 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star-with-name.output.js @@ -2,10 +2,10 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import { ACM as ACMClient } from "@aws-sdk/client-acm"; import { AccessAnalyzer as AccessAnalyzerClient } from "@aws-sdk/client-accessanalyzer"; +import { ACM as ACMClient } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService as DiscoveryClient } from "@aws-sdk/client-application-discovery-service"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.input.js index 9af6aa361..ed8e39ea1 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.input.js @@ -1,9 +1,9 @@ // 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. -import * as ACM from "aws-sdk/clients/acm"; import * as AccessAnalyzer from "aws-sdk/clients/accessanalyzer"; import * as Discovery from "aws-sdk/clients/discovery"; +import * as ACM from "aws-sdk/clients/acm"; -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.output.js index 311486bc7..d70263997 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-star.output.js @@ -2,10 +2,10 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import { ACM } from "@aws-sdk/client-acm"; import { AccessAnalyzer } from "@aws-sdk/client-accessanalyzer"; +import { ACM } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService } from "@aws-sdk/client-application-discovery-service"; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.input.js index 231cafebd..0f30f118d 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.input.js @@ -1,9 +1,9 @@ // 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. -import ACMClient from "aws-sdk/clients/acm"; import AccessAnalyzerClient from "aws-sdk/clients/accessanalyzer"; import DiscoveryClient from "aws-sdk/clients/discovery"; +import ACMClient from "aws-sdk/clients/acm"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.output.js index c8e3aa230..b8c838b9a 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep-with-name.output.js @@ -2,10 +2,10 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import { ACM as ACMClient } from "@aws-sdk/client-acm"; import { AccessAnalyzer as AccessAnalyzerClient } from "@aws-sdk/client-accessanalyzer"; +import { ACM as ACMClient } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService as DiscoveryClient } from "@aws-sdk/client-application-discovery-service"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.input.js index cf5d0cf45..114d23f44 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.input.js @@ -1,9 +1,9 @@ // 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. -import ACM from "aws-sdk/clients/acm"; import AccessAnalyzer from "aws-sdk/clients/accessanalyzer"; import Discovery from "aws-sdk/clients/discovery"; +import ACM from "aws-sdk/clients/acm"; -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.output.js index 311486bc7..d70263997 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-deep.output.js @@ -2,10 +2,10 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import { ACM } from "@aws-sdk/client-acm"; import { AccessAnalyzer } from "@aws-sdk/client-accessanalyzer"; +import { ACM } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService } from "@aws-sdk/client-application-discovery-service"; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.input.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.input.ts index 57b719c65..abd330289 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.input.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals-with-name.input.ts @@ -1,9 +1,9 @@ // 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. -import ACMClient = require("aws-sdk/clients/acm"); import AccessAnalyzerClient = require("aws-sdk/clients/accessanalyzer"); import DiscoveryClient = require("aws-sdk/clients/discovery"); +import ACMClient = require("aws-sdk/clients/acm"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); 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 2a8a4f245..46ed2c0f3 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 @@ -2,13 +2,13 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import AWS_client_acm = require("@aws-sdk/client-acm"); -import ACMClient = AWS_client_acm.ACM; import AWS_client_accessanalyzer = require("@aws-sdk/client-accessanalyzer"); import AccessAnalyzerClient = AWS_client_accessanalyzer.AccessAnalyzer; +import AWS_client_acm = require("@aws-sdk/client-acm"); +import ACMClient = AWS_client_acm.ACM; import AWS_client_application_discovery_service = require("@aws-sdk/client-application-discovery-service"); import DiscoveryClient = AWS_client_application_discovery_service.ApplicationDiscoveryService; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.input.ts b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.input.ts index fa2c49b4b..94767b092 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.input.ts +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-equals.input.ts @@ -1,9 +1,9 @@ // 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. -import ACM = require("aws-sdk/clients/acm"); import AccessAnalyzer = require("aws-sdk/clients/accessanalyzer"); import Discovery = require("aws-sdk/clients/discovery"); +import ACM = require("aws-sdk/clients/acm"); -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); 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 abdd8dd6d..4778fa877 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 @@ -2,13 +2,13 @@ // Do not edit this file directly. Instead, edit the script and run it to regenerate this file. -import AWS_client_acm = require("@aws-sdk/client-acm"); -import ACM = AWS_client_acm.ACM; import AWS_client_accessanalyzer = require("@aws-sdk/client-accessanalyzer"); import AccessAnalyzer = AWS_client_accessanalyzer.AccessAnalyzer; +import AWS_client_acm = require("@aws-sdk/client-acm"); +import ACM = AWS_client_acm.ACM; import AWS_client_application_discovery_service = require("@aws-sdk/client-application-discovery-service"); import ApplicationDiscoveryService = AWS_client_application_discovery_service.ApplicationDiscoveryService; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.input.js index 5081e7fe4..0588dc23e 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.input.js @@ -1,11 +1,11 @@ // 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. import { - ACM as ACMClient, AccessAnalyzer as AccessAnalyzerClient, - Discovery as DiscoveryClient + Discovery as DiscoveryClient, + ACM as ACMClient } from "aws-sdk"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.output.js index b687c1a12..b8c838b9a 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import-with-name.output.js @@ -6,6 +6,6 @@ import { AccessAnalyzer as AccessAnalyzerClient } from "@aws-sdk/client-accessan import { ACM as ACMClient } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService as DiscoveryClient } from "@aws-sdk/client-application-discovery-service"; -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.input.js index e6c5f854c..c8295c210 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.input.js @@ -1,7 +1,7 @@ // 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. -import { ACM, AccessAnalyzer, Discovery } from "aws-sdk"; +import { AccessAnalyzer, Discovery, ACM } from "aws-sdk"; -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.output.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.output.js index 48e3365e0..d70263997 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.output.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-import.output.js @@ -6,6 +6,6 @@ import { AccessAnalyzer } from "@aws-sdk/client-accessanalyzer"; import { ACM } from "@aws-sdk/client-acm"; import { ApplicationDiscoveryService } from "@aws-sdk/client-application-discovery-service"; -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.input.js index e27ba1529..7f239b31c 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep-with-name.input.js @@ -1,9 +1,9 @@ // 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 ACMClient = require("aws-sdk/clients/acm"); const AccessAnalyzerClient = require("aws-sdk/clients/accessanalyzer"); const DiscoveryClient = require("aws-sdk/clients/discovery"); +const ACMClient = require("aws-sdk/clients/acm"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); 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 0cdabe3ed..1d9a68df6 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 @@ -2,18 +2,18 @@ // 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 { + ACM: ACMClient +} = require("@aws-sdk/client-acm"); + const { ApplicationDiscoveryService: DiscoveryClient } = require("@aws-sdk/client-application-discovery-service"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.input.js index adedf86e9..be41735ff 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-deep.input.js @@ -1,9 +1,9 @@ // 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/clients/acm"); const AccessAnalyzer = require("aws-sdk/clients/accessanalyzer"); const Discovery = require("aws-sdk/clients/discovery"); +const ACM = require("aws-sdk/clients/acm"); -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); 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 539521ccf..c37b050ce 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 @@ -2,18 +2,18 @@ // 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 { + ACM +} = require("@aws-sdk/client-acm"); + const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.input.js index 8cd90d6fe..cfa8587ab 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require-with-name.input.js @@ -1,11 +1,11 @@ // 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, AccessAnalyzer: AccessAnalyzerClient, - Discovery: DiscoveryClient + Discovery: DiscoveryClient, + ACM: ACMClient } = require("aws-sdk"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); 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 8b47c4f95..1d9a68df6 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 @@ -14,6 +14,6 @@ const { ApplicationDiscoveryService: DiscoveryClient } = require("@aws-sdk/client-application-discovery-service"); -new ACMClient(); new AccessAnalyzerClient(); new DiscoveryClient(); +new ACMClient(); diff --git a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.input.js b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.input.js index 2a32c2feb..5dc24ee4e 100644 --- a/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.input.js +++ b/src/transforms/v2-to-v3/__fixtures__/new-client/service-require.input.js @@ -1,7 +1,7 @@ // 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, AccessAnalyzer, Discovery } = require("aws-sdk"); +const { AccessAnalyzer, Discovery, ACM } = require("aws-sdk"); -new ACM(); new AccessAnalyzer(); new Discovery(); +new ACM(); 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 73d783b4d..c37b050ce 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 @@ -14,6 +14,6 @@ const { ApplicationDiscoveryService } = require("@aws-sdk/client-application-discovery-service"); -new ACM(); new AccessAnalyzer(); new ApplicationDiscoveryService(); +new ACM(); diff --git a/src/transforms/v2-to-v3/modules/addClientModules.ts b/src/transforms/v2-to-v3/modules/addClientModules.ts index 4c22d41c6..8e870b136 100644 --- a/src/transforms/v2-to-v3/modules/addClientModules.ts +++ b/src/transforms/v2-to-v3/modules/addClientModules.ts @@ -27,11 +27,18 @@ import { ClientModulesOptions, ImportType } from "./types"; export const addClientModules = ( j: JSCodeshift, source: Collection, - options: ClientModulesOptions & { importType: ImportType } + options: ClientModulesOptions ): void => { - const { clientIdentifiers, v2ClientName, v3ClientName, v2ClientLocalName, importType } = options; - - const { addClientNamedModule } = + const { + clientIdentifiers, + v2ClientName, + v3ClientName, + v3ClientPackageName, + v2ClientLocalName, + importType, + } = options; + + const { addNamedModule } = importType === ImportType.REQUIRE ? requireModule : importType === ImportType.IMPORT_EQUALS @@ -45,47 +52,45 @@ export const addClientModules = ( // Add named import for types, if needed. for (const v3ClientType of v3ClientTypes) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: v3ClientType, + packageName: v3ClientPackageName, }); } if (newExpressionCount > 0 || clientTSTypeRefCount > 0) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: v3ClientName, localName: v2ClientName === v2ClientLocalName ? v3ClientName : v2ClientLocalName, + packageName: v3ClientPackageName, }); } for (const waiterState of waiterStates) { const v3WaiterApiName = getV3ClientWaiterApiName(waiterState); - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: v3WaiterApiName, + packageName: v3ClientPackageName, }); } if (v2ClientName === S3) { if (isS3UploadApiUsed(j, source, clientIdentifiers)) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: "Upload", - v3ClientPackageName: "@aws-sdk/lib-storage", + packageName: "@aws-sdk/lib-storage", }); } if (isS3GetSignedUrlApiUsed(j, source, clientIdentifiers)) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: "getSignedUrl", - v3ClientPackageName: "@aws-sdk/s3-request-presigner", + packageName: "@aws-sdk/s3-request-presigner", }); for (const apiName of getS3SignedUrlApiNames(j, source, clientIdentifiers)) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: getCommandName(apiName), + packageName: v3ClientPackageName, }); } } @@ -105,25 +110,18 @@ export const addClientModules = ( const docClientTypes = getV3ClientTypes(j, source, docClientOptions); const docClientNewExpressionCount = getNewExpressionCount(j, source, docClientOptions); - const docClientModuleOptions = { - ...options, - v2ClientLocalName: `${v2ClientLocalName}.${DOCUMENT_CLIENT}`, - v3ClientPackageName: "@aws-sdk/lib-dynamodb", - }; - // Add named import for types, if needed. for (const docClientType of docClientTypes) { - addClientNamedModule(j, source, { - ...options, + addNamedModule(j, source, { importedName: docClientType, - v3ClientPackageName: "@aws-sdk/lib-dynamodb", + packageName: "@aws-sdk/lib-dynamodb", }); } if (docClientNewExpressionCount > 0) { - addClientNamedModule(j, source, { - ...docClientModuleOptions, + addNamedModule(j, source, { importedName: DYNAMODB_DOCUMENT, + packageName: "@aws-sdk/lib-dynamodb", }); } } diff --git a/src/transforms/v2-to-v3/modules/getImportDeclaration.ts b/src/transforms/v2-to-v3/modules/getImportDeclaration.ts deleted file mode 100644 index e7ffbb009..000000000 --- a/src/transforms/v2-to-v3/modules/getImportDeclaration.ts +++ /dev/null @@ -1,47 +0,0 @@ -import { Collection, JSCodeshift } from "jscodeshift"; - -import { PACKAGE_NAME } from "../config"; -import { getClientDeepImportPath } from "../utils"; - -export interface GetImportDeclarationOptions { - v2ClientName: string; - v2ClientLocalName: string; -} - -export const getImportDeclaration = ( - j: JSCodeshift, - source: Collection, - options: GetImportDeclarationOptions -) => { - // Support DynamoDB.DocumentClient - const v2ClientLocalName = options.v2ClientLocalName.split(".")[0]; - - // Return global or service import declaration. - return source.find(j.ImportDeclaration).filter((importDeclaration) => { - const sourceValue = importDeclaration.value.source.value as string; - - if ( - sourceValue === PACKAGE_NAME && - importDeclaration.value.specifiers?.some( - (specifier) => - ["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) || - (specifier.type === "ImportSpecifier" && specifier.local?.name === v2ClientLocalName) - ) - ) { - return true; - } - - if ( - sourceValue === getClientDeepImportPath(options.v2ClientName) && - importDeclaration.value.specifiers?.some( - (specifier) => - ["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) && - specifier.local?.name === v2ClientLocalName - ) - ) { - return true; - } - - return false; - }); -}; diff --git a/src/transforms/v2-to-v3/modules/getImportEqualsDeclaration.ts b/src/transforms/v2-to-v3/modules/getImportEqualsDeclaration.ts deleted file mode 100644 index c0fa9fe9d..000000000 --- a/src/transforms/v2-to-v3/modules/getImportEqualsDeclaration.ts +++ /dev/null @@ -1,43 +0,0 @@ -import { Collection, JSCodeshift, TSExternalModuleReference } from "jscodeshift"; - -import { PACKAGE_NAME } from "../config"; -import { getClientDeepImportPath } from "../utils"; -import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType"; - -export interface GetImportEqualsDeclarationOptions { - v2ClientName: string; - v2ClientLocalName: string; - v2GlobalName?: string; -} - -export const getImportEqualsDeclaration = ( - j: JSCodeshift, - source: Collection, - options: GetImportEqualsDeclarationOptions -) => { - // Support DynamoDB.DocumentClient - const v2ClientLocalName = options.v2ClientLocalName.split(".")[0]; - - // Return global or service import declaration. - return source - .find(j.TSImportEqualsDeclaration, getImportEqualsDeclarationType()) - .filter((importEqualsDeclaration) => { - const identifierName = importEqualsDeclaration.value.id.name; - const importEqualsModuleRef = importEqualsDeclaration.value - .moduleReference as TSExternalModuleReference; - const expressionValue = importEqualsModuleRef.expression.value; - - if (expressionValue === PACKAGE_NAME && identifierName === options.v2GlobalName) { - return true; - } - - if ( - expressionValue === getClientDeepImportPath(options.v2ClientName) && - identifierName === v2ClientLocalName - ) { - return true; - } - - return false; - }); -}; diff --git a/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts b/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts deleted file mode 100644 index 2a64016a8..000000000 --- a/src/transforms/v2-to-v3/modules/getRequireDeclarator.ts +++ /dev/null @@ -1,102 +0,0 @@ -import { Collection, Identifier, JSCodeshift, VariableDeclarator } from "jscodeshift"; -import { PACKAGE_NAME } from "../config"; - -export interface GetRequireDeclaratorOptions { - v2ClientName: string; - v2ClientLocalName: string; - v2GlobalName?: string; -} - -export const getRequireDeclarator = ( - j: JSCodeshift, - source: Collection, - options: GetRequireDeclaratorOptions -) => { - const { v2ClientName, v2GlobalName } = options; - - // Support DynamoDB.DocumentClient - const v2ClientLocalName = options.v2ClientLocalName.split(".")[0]; - - // 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; - - 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 init = declaration.init; - if (!init) return false; - if (init.type !== "CallExpression") return false; - - const callee = init.callee; - if (!callee) return false; - if (callee.type !== "Identifier") return false; - if (callee.name !== "require") return false; - - 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; - - return true; - } - ) - ); - if (v2RequireCallExpressions.size()) { - return v2RequireCallExpressions; - } - - 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; - - return true; - } - ) - ); - if (v2RequireProperties.size()) { - return v2RequireProperties; - } -}; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts deleted file mode 100644 index 7bd5e9d0e..000000000 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientDefaultModule.ts +++ /dev/null @@ -1,52 +0,0 @@ -import { Collection, JSCodeshift } from "jscodeshift"; - -import { getImportEqualsDeclaration } from "../getImportEqualsDeclaration"; -import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType"; -import { ClientModulesOptions } from "../types"; -import { getImportEqualsDefaultName } from "./getImportEqualsDefaultName"; - -export type ClientDefaultModuleOptions = Omit; - -export const addClientDefaultModule = ( - j: JSCodeshift, - source: Collection, - { v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: ClientDefaultModuleOptions -) => { - const defaultLocalName = getImportEqualsDefaultName(v3ClientPackageName); - 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/addDefaultModule.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/addDefaultModule.ts new file mode 100644 index 000000000..361f847a1 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/addDefaultModule.ts @@ -0,0 +1,53 @@ +import { Collection, JSCodeshift } from "jscodeshift"; + +import { PACKAGE_NAME } from "../../config"; +import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType"; +import { getDefaultName } from "./getDefaultName"; + +export const addDefaultModule = ( + j: JSCodeshift, + source: Collection, + packageName: string +) => { + const defaultLocalName = getDefaultName(packageName); + const existingImportEquals = source.find( + j.TSImportEqualsDeclaration, + getImportEqualsDeclarationType(packageName) + ); + + if (existingImportEquals.size()) { + if ( + existingImportEquals + .nodes() + .some((importEqualsDeclaration) => importEqualsDeclaration.id.name === defaultLocalName) + ) { + return; + } + } + + // Build a new import equals declaration. + const v3importEqualsDeclaration = j.tsImportEqualsDeclaration( + j.identifier(defaultLocalName), + j.tsExternalModuleReference(j.stringLiteral(packageName)) + ); + + const v2ImportEquals = source + .find(j.TSImportEqualsDeclaration, getImportEqualsDeclarationType()) + .filter((importEqualsDeclaration) => { + const { moduleReference } = importEqualsDeclaration.value; + if (moduleReference.type !== "TSExternalModuleReference") return false; + const { expression } = moduleReference; + if (expression.type !== "StringLiteral") return false; + const { value } = expression; + return value.startsWith(PACKAGE_NAME); + }); + + if (v2ImportEquals.size()) { + // Insert it after the first import equals declaration. + v2ImportEquals.at(0).insertAfter(v3importEqualsDeclaration); + return; + } + + // Insert it at the top of the document. + source.get().node.program.body.unshift(v3importEqualsDeclaration); +}; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/addNamedModule.ts similarity index 68% rename from src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts rename to src/transforms/v2-to-v3/modules/importEqualsModule/addNamedModule.ts index 2c3443856..aa854bbb9 100644 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/addClientNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/addNamedModule.ts @@ -1,19 +1,18 @@ import { Collection, JSCodeshift } from "jscodeshift"; import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType"; -import { ClientModulesOptions, ImportSpecifierOptions } from "../types"; -import { addClientDefaultModule } from "./addClientDefaultModule"; -import { getImportEqualsDefaultName } from "./getImportEqualsDefaultName"; +import { ModulesOptions } from "../types"; +import { addDefaultModule } from "./addDefaultModule"; +import { getDefaultName } from "./getDefaultName"; -export const addClientNamedModule = ( +export const addNamedModule = ( j: JSCodeshift, source: Collection, - options: ClientModulesOptions & ImportSpecifierOptions + options: ModulesOptions ) => { - const { importedName, localName = importedName, ...v3ClientModulesOptions } = options; - const { v3ClientPackageName } = v3ClientModulesOptions; + const { importedName, localName = importedName, packageName } = options; - const defaultLocalName = getImportEqualsDefaultName(v3ClientPackageName); + const defaultLocalName = getDefaultName(packageName); const existingDeclaration = source.find(j.TSImportEqualsDeclaration, { type: "TSImportEqualsDeclaration", @@ -38,9 +37,9 @@ export const addClientNamedModule = ( return; } - const defaultDeclaration = getImportEqualsDeclarationType(v3ClientPackageName); + const defaultDeclaration = getImportEqualsDeclarationType(packageName); if (source.find(j.TSImportEqualsDeclaration, defaultDeclaration).size() === 0) { - addClientDefaultModule(j, source, v3ClientModulesOptions); + addDefaultModule(j, source, packageName); } const importEqualsDeclaration = j.tsImportEqualsDeclaration( @@ -54,6 +53,7 @@ export const addClientNamedModule = ( (importEqualsDeclaration) => importEqualsDeclaration.value.id.name === defaultLocalName ); + // Insert import equals after the package import equals. if (v3ClientImportEquals.size() > 0) { v3ClientImportEquals.at(0).insertAfter(importEqualsDeclaration); return; diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/getDefaultName.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/getDefaultName.ts new file mode 100644 index 000000000..697e18160 --- /dev/null +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/getDefaultName.ts @@ -0,0 +1,2 @@ +export const getDefaultName = (packageName: string) => + ["AWS", ...packageName.split("/").pop()!.split("-")].join("_"); diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/getImportEqualsDefaultName.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/getImportEqualsDefaultName.ts deleted file mode 100644 index 127c8390f..000000000 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/getImportEqualsDefaultName.ts +++ /dev/null @@ -1,2 +0,0 @@ -export const getImportEqualsDefaultName = (packageName: string) => - ["AWS", ...packageName.split("/").pop()!.split("-")].join("_"); diff --git a/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts b/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts index 2f41b4e2f..0a5092ed4 100644 --- a/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts +++ b/src/transforms/v2-to-v3/modules/importEqualsModule/index.ts @@ -1 +1 @@ -export * from "./addClientNamedModule"; +export * from "./addNamedModule"; diff --git a/src/transforms/v2-to-v3/modules/importModule/addClientNamedModule.ts b/src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts similarity index 74% rename from src/transforms/v2-to-v3/modules/importModule/addClientNamedModule.ts rename to src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts index d054604af..7d021bea5 100644 --- a/src/transforms/v2-to-v3/modules/importModule/addClientNamedModule.ts +++ b/src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts @@ -7,29 +7,27 @@ import { ObjectProperty, } from "jscodeshift"; -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; -import { getImportDeclaration } from "../getImportDeclaration"; +import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config"; import { getImportSpecifier } from "../getImportSpecifier"; import { getImportSpecifiers } from "../getImportSpecifiers"; import { getRequireProperty } from "../getRequireProperty"; import { importSpecifierCompareFn } from "../importSpecifierCompareFn"; import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn"; -import { ClientModulesOptions, ImportSpecifierOptions } from "../types"; +import { ModulesOptions } from "../types"; -export const addClientNamedModule = ( +export const addNamedModule = ( j: JSCodeshift, source: Collection, - options: ClientModulesOptions & ImportSpecifierOptions + options: ModulesOptions ) => { - const { importedName, v2ClientName, v2ClientLocalName, v3ClientPackageName } = options; - const localName = options.localName ?? importedName; + const { importedName, localName = importedName, packageName } = options; const importDeclarations = source.find(j.ImportDeclaration, { - source: { value: v3ClientPackageName }, + source: { value: packageName }, }); if (importDeclarations.size()) { - const importSpecifiers = getImportSpecifiers(j, source, v3ClientPackageName); + const importSpecifiers = getImportSpecifiers(j, source, packageName); // Return if the import specifier already exists. if ( @@ -94,7 +92,7 @@ export const addClientNamedModule = ( .find(j.ImportDeclaration, { type: "ImportDeclaration", specifiers: [importNamespaceSpecifiers[0]], - source: { value: v3ClientPackageName }, + source: { value: packageName }, }) .insertAfter(varDeclaration); return; @@ -109,24 +107,23 @@ export const addClientNamedModule = ( } } - // Insert after global import, or service import. - const v2ImportDeclaration = getImportDeclaration(j, source, { - v2ClientName, - v2ClientLocalName, - }); - + // Build a new import declaration. const v3ImportDeclaration = j.importDeclaration( [getImportSpecifier(j, { importedName, localName })], - j.stringLiteral(v3ClientPackageName) + j.stringLiteral(packageName) ); - if (v2ImportDeclaration && v2ImportDeclaration.nodes().length > 0) { - v2ImportDeclaration.at(0).insertAfter(v3ImportDeclaration); - } else { - // Unreachable code, throw error - throw new Error( - "Base Import Declaration not found to insert new Import Declaration.\n" + - "Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" - ); + const v2importDeclarations = source.find(j.ImportDeclaration).filter((path) => { + const { value } = path.node.source; + return typeof value === "string" && value.startsWith(PACKAGE_NAME); + }); + + if (v2importDeclarations.size()) { + // Insert it after the last import declaration. + v2importDeclarations.at(0).insertAfter(v3ImportDeclaration); + return; } + + // Insert it at the top of the document. + source.get().node.program.body.unshift(v3ImportDeclaration); }; diff --git a/src/transforms/v2-to-v3/modules/importModule/index.ts b/src/transforms/v2-to-v3/modules/importModule/index.ts index 2f41b4e2f..0a5092ed4 100644 --- a/src/transforms/v2-to-v3/modules/importModule/index.ts +++ b/src/transforms/v2-to-v3/modules/importModule/index.ts @@ -1 +1 @@ -export * from "./addClientNamedModule"; +export * from "./addNamedModule"; diff --git a/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts b/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts deleted file mode 100644 index fddad288c..000000000 --- a/src/transforms/v2-to-v3/modules/requireModule/addClientNamedModule.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift"; - -import { OBJECT_PROPERTY_TYPE_LIST } from "../../config"; -import { getRequireDeclarator } from "../getRequireDeclarator"; -import { getRequireDeclarators } from "../getRequireDeclarators"; -import { getRequireProperty } from "../getRequireProperty"; -import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn"; -import { ClientModulesOptions, ImportSpecifierOptions } from "../types"; - -export const addClientNamedModule = ( - j: JSCodeshift, - source: Collection, - options: ClientModulesOptions & ImportSpecifierOptions -) => { - const { importedName, v2ClientName, v2ClientLocalName, v2GlobalName, v3ClientPackageName } = - options; - const localName = options.localName ?? importedName; - - const clientObjectProperty = getRequireProperty(j, { importedName, localName }); - const existingRequires = getRequireDeclarators(j, source, v3ClientPackageName); - - if (existingRequires && existingRequires.nodes().length > 0) { - const existingRequireProperties = existingRequires - .filter((variableDeclarator) => variableDeclarator.value.id.type === "ObjectPattern") - .nodes(); - - if ( - existingRequireProperties.length > 0 && - existingRequireProperties.find( - (variableDeclarator) => - variableDeclarator.id.type === "ObjectPattern" && - variableDeclarator.id.properties.find((property) => { - if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false; - const key = (property as Property | ObjectProperty).key; - const value = (property as Property | ObjectProperty).value; - if (key.type !== "Identifier" || value.type !== "Identifier") { - return false; - } - return key.name === importedName && value.name === localName; - }) - ) - ) { - return; - } - - if (existingRequireProperties.length > 0) { - const firstRequireProperties = (existingRequireProperties[0].id as ObjectPattern).properties; - firstRequireProperties.push(clientObjectProperty); - firstRequireProperties.sort(objectPatternPropertyCompareFn); - return; - } - } - - const v3RequireDeclarator = j.variableDeclarator( - j.objectPattern([clientObjectProperty]), - j.callExpression(j.identifier("require"), [j.literal(v3ClientPackageName)]) - ); - - // prettier-ignore - const v2RequireDeclarator = - getRequireDeclarator(j, source, { v2ClientName, v2ClientLocalName, v2GlobalName }); - - if (v2RequireDeclarator && v2RequireDeclarator.nodes().length > 0) { - v2RequireDeclarator.insertAfter(j.variableDeclaration("const", [v3RequireDeclarator])); - } else { - // Unreachable code, throw error - throw new Error( - "Base Require Declarator not found to insert new Require Declarator.\n" + - "Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod" - ); - } -}; diff --git a/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts new file mode 100644 index 000000000..e14598f4b --- /dev/null +++ b/src/transforms/v2-to-v3/modules/requireModule/addNamedModule.ts @@ -0,0 +1,115 @@ +import { Collection, JSCodeshift, ObjectPattern, ObjectProperty, Property } from "jscodeshift"; + +import { OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../../config"; +import { getRequireDeclarators } from "../getRequireDeclarators"; +import { getRequireProperty } from "../getRequireProperty"; +import { objectPatternPropertyCompareFn } from "../objectPatternPropertyCompareFn"; +import { ModulesOptions } from "../types"; + +export const addNamedModule = ( + j: JSCodeshift, + source: Collection, + options: ModulesOptions +) => { + const { importedName, localName = importedName, packageName } = options; + + const clientObjectProperty = getRequireProperty(j, { importedName, localName }); + const existingRequires = getRequireDeclarators(j, source, packageName); + + if (existingRequires && existingRequires.nodes().length > 0) { + const existingRequireProperties = existingRequires + .filter((variableDeclarator) => variableDeclarator.value.id.type === "ObjectPattern") + .nodes(); + + if ( + existingRequireProperties.length > 0 && + existingRequireProperties.find( + (variableDeclarator) => + variableDeclarator.id.type === "ObjectPattern" && + variableDeclarator.id.properties.find((property) => { + if (!OBJECT_PROPERTY_TYPE_LIST.includes(property.type)) return false; + const key = (property as Property | ObjectProperty).key; + const value = (property as Property | ObjectProperty).value; + if (key.type !== "Identifier" || value.type !== "Identifier") { + return false; + } + return key.name === importedName && value.name === localName; + }) + ) + ) { + return; + } + + if (existingRequireProperties.length > 0) { + const firstRequireProperties = (existingRequireProperties[0].id as ObjectPattern).properties; + firstRequireProperties.push(clientObjectProperty); + firstRequireProperties.sort(objectPatternPropertyCompareFn); + return; + } + } + + // Build a new require declarator. + const v3RequireDeclaration = j.variableDeclaration("const", [ + j.variableDeclarator( + j.objectPattern([clientObjectProperty]), + j.callExpression(j.identifier("require"), [j.literal(packageName)]) + ), + ]); + + const v2RequireDeclarations = 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; + + // Checks for require identifier or object pattern. + if (init.type === "CallExpression") { + const callee = init.callee; + if (!callee) return false; + if (callee.type !== "Identifier") return false; + if (callee.name !== "require") return false; + + 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; + + return true; + } + + // Checks for require property. + if (init.type === "MemberExpression") { + 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; + + return true; + } + + return false; + } + ) + ); + + if (v2RequireDeclarations.size()) { + // Insert it after the first v2 require declaration. + v2RequireDeclarations.at(0).insertAfter(v3RequireDeclaration); + return; + } + + // Insert require declarator at the top of the document. + source.get().node.program.body.unshift(v3RequireDeclaration); +}; diff --git a/src/transforms/v2-to-v3/modules/requireModule/index.ts b/src/transforms/v2-to-v3/modules/requireModule/index.ts index 2f41b4e2f..0a5092ed4 100644 --- a/src/transforms/v2-to-v3/modules/requireModule/index.ts +++ b/src/transforms/v2-to-v3/modules/requireModule/index.ts @@ -1 +1 @@ -export * from "./addClientNamedModule"; +export * from "./addNamedModule"; diff --git a/src/transforms/v2-to-v3/modules/types.ts b/src/transforms/v2-to-v3/modules/types.ts index 2209242ec..21bdfff4e 100644 --- a/src/transforms/v2-to-v3/modules/types.ts +++ b/src/transforms/v2-to-v3/modules/types.ts @@ -7,6 +7,7 @@ export interface ClientModulesOptions { v3ClientName: string; v3ClientPackageName: string; clientIdentifiers: ClientIdentifier[]; + importType: ImportType; } export interface ImportSpecifierOptions { @@ -14,6 +15,10 @@ export interface ImportSpecifierOptions { localName?: string; } +export interface ModulesOptions extends ImportSpecifierOptions { + packageName: string; +} + export enum ImportType { REQUIRE = "require", IMPORT = "import",