Skip to content

Commit

Permalink
Use package name as suffix in import equals default declaration (#620)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Oct 18, 2023
1 parent d68f434 commit 2161b67
Show file tree
Hide file tree
Showing 30 changed files with 103 additions and 109 deletions.
5 changes: 5 additions & 0 deletions .changeset/ten-stingrays-guess.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use package name as suffix in import equals default declaration
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import {
CLIENT_NAMES_MAP,
CLIENT_PACKAGE_NAMES_MAP,
} from "../../src/transforms/v2-to-v3/config";
import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";

export interface V3PackageImportEqualsCodeOptions {
Expand All @@ -18,7 +17,10 @@ export const getV3PackageImportEqualsCode = (
const { useLocalSuffix = false } = options || {};

for (const v2ClientName of clientsToTest) {
const v3ClientDefaultLocalName = getDefaultLocalName(v2ClientName);
const v3ClientDefaultLocalName = [
"AWS",
...CLIENT_PACKAGE_NAMES_MAP[v2ClientName].split("-"),
].join("_");
const v3ClientPackageName = `@aws-sdk/${CLIENT_PACKAGE_NAMES_MAP[v2ClientName]}`;
content += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n`;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import AWS_S3 = require("@aws-sdk/client-s3");
import Tag = AWS_S3.Tag;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import Tag = AWS_client_s3.Tag;

const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import AWS_S3 = require("@aws-sdk/client-s3");
import Tag = AWS_S3.Tag;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import Tag = AWS_client_s3.Tag;

const testTags: Tag[] = [{ Key: "key", Value: "value" }];
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import ListTablesCommandOutput = AWS_DynamoDB.ListTablesCommandOutput;
import ListTablesCommandInput = AWS_DynamoDB.ListTablesCommandInput;
import AWS_Lambda = require("@aws-sdk/client-lambda");
import Lambda = AWS_Lambda.Lambda;
import InvokeCommandOutput = AWS_Lambda.InvokeCommandOutput;
import InvokeCommandInput = AWS_Lambda.InvokeCommandInput;
import AWS_STS = require("@aws-sdk/client-sts");
import STS = AWS_STS.STS;
import GetCallerIdentityCommandOutput = AWS_STS.GetCallerIdentityCommandOutput;
import GetCallerIdentityCommandInput = AWS_STS.GetCallerIdentityCommandInput;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;
import ListTablesCommandOutput = AWS_client_dynamodb.ListTablesCommandOutput;
import ListTablesCommandInput = AWS_client_dynamodb.ListTablesCommandInput;
import AWS_client_lambda = require("@aws-sdk/client-lambda");
import Lambda = AWS_client_lambda.Lambda;
import InvokeCommandOutput = AWS_client_lambda.InvokeCommandOutput;
import InvokeCommandInput = AWS_client_lambda.InvokeCommandInput;
import AWS_client_sts = require("@aws-sdk/client-sts");
import STS = AWS_client_sts.STS;
import GetCallerIdentityCommandOutput = AWS_client_sts.GetCallerIdentityCommandOutput;
import GetCallerIdentityCommandInput = AWS_client_sts.GetCallerIdentityCommandInput;

const ddbClient = new DynamoDB({ region: "us-west-2" });
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import ListTablesCommandOutput = AWS_DynamoDB.ListTablesCommandOutput;
import ListTablesCommandInput = AWS_DynamoDB.ListTablesCommandInput;
import AWS_Lambda = require("@aws-sdk/client-lambda");
import Lambda = AWS_Lambda.Lambda;
import InvokeCommandOutput = AWS_Lambda.InvokeCommandOutput;
import InvokeCommandInput = AWS_Lambda.InvokeCommandInput;
import AWS_STS = require("@aws-sdk/client-sts");
import STS = AWS_STS.STS;
import GetCallerIdentityCommandOutput = AWS_STS.GetCallerIdentityCommandOutput;
import GetCallerIdentityCommandInput = AWS_STS.GetCallerIdentityCommandInput;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;
import ListTablesCommandOutput = AWS_client_dynamodb.ListTablesCommandOutput;
import ListTablesCommandInput = AWS_client_dynamodb.ListTablesCommandInput;
import AWS_client_lambda = require("@aws-sdk/client-lambda");
import Lambda = AWS_client_lambda.Lambda;
import InvokeCommandOutput = AWS_client_lambda.InvokeCommandOutput;
import InvokeCommandInput = AWS_client_lambda.InvokeCommandInput;
import AWS_client_sts = require("@aws-sdk/client-sts");
import STS = AWS_client_sts.STS;
import GetCallerIdentityCommandOutput = AWS_client_sts.GetCallerIdentityCommandOutput;
import GetCallerIdentityCommandInput = AWS_client_sts.GetCallerIdentityCommandInput;

const ddbClient = new DynamoDB({ region: "us-west-2" });
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;

const client = new DynamoDB();
const data = await client.listTables();
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;

const client = new DynamoDB();
const data = await client.listTables();
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AWS_APIGateway = require("@aws-sdk/client-api-gateway");
import MethodSnapshot = AWS_APIGateway.MethodSnapshot;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import AttributeValue = AWS_DynamoDB.AttributeValue;
import AWS_S3 = require("@aws-sdk/client-s3");
import Bucket = AWS_S3.Bucket;
import ChecksumAlgorithm = AWS_S3.ChecksumAlgorithm;
import AWS_client_api_gateway = require("@aws-sdk/client-api-gateway");
import MethodSnapshot = AWS_client_api_gateway.MethodSnapshot;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import AttributeValue = AWS_client_dynamodb.AttributeValue;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import Bucket = AWS_client_s3.Bucket;
import ChecksumAlgorithm = AWS_client_s3.ChecksumAlgorithm;

// Native types
const stringType: string = "string";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import AWS_APIGateway = require("@aws-sdk/client-api-gateway");
import MethodSnapshot = AWS_APIGateway.MethodSnapshot;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import AttributeValue = AWS_DynamoDB.AttributeValue;
import AWS_S3 = require("@aws-sdk/client-s3");
import Bucket = AWS_S3.Bucket;
import ChecksumAlgorithm = AWS_S3.ChecksumAlgorithm;
import AWS_client_api_gateway = require("@aws-sdk/client-api-gateway");
import MethodSnapshot = AWS_client_api_gateway.MethodSnapshot;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import AttributeValue = AWS_client_dynamodb.AttributeValue;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import Bucket = AWS_client_s3.Bucket;
import ChecksumAlgorithm = AWS_client_s3.ChecksumAlgorithm;

// Native types
const stringType: string = "string";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput;
import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;

const docClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" }));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput;
import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynDB = AWS_client_dynamodb.DynamoDB;

const docClient = DynamoDBDocument.from(new DynDB({ region: "us-west-2" }));

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;

const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" }));
const response = await documentClient.scan({ TableName: "TABLE_NAME" });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDBClient = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDBClient = AWS_client_dynamodb.DynamoDB;

const documentClient = DynamoDBDocument.from(new DynamoDBClient({ region: "us-west-2" }));
const response = await documentClient.scan({ TableName: "TABLE_NAME" });
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import AWS_client_dynamodb = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_client_dynamodb.DynamoDB;

const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" }));
const response = await documentClient.scan({ TableName: "TABLE_NAME" });
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.


import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");
import AccessAnalyzer = AWS_AccessAnalyzer.AccessAnalyzer;
import AWS_ACM = require("@aws-sdk/client-acm");
import ACM = AWS_ACM.ACM;
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");
import ApplicationDiscoveryService = AWS_Discovery.ApplicationDiscoveryService;
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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.


import AWS_ACM = require("@aws-sdk/client-acm");
import ACMClient = AWS_ACM.ACM;
import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");
import AccessAnalyzerClient = AWS_AccessAnalyzer.AccessAnalyzer;
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");
import DiscoveryClient = AWS_Discovery.ApplicationDiscoveryService;
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_application_discovery_service = require("@aws-sdk/client-application-discovery-service");
import DiscoveryClient = AWS_client_application_discovery_service.ApplicationDiscoveryService;

new ACMClient();
new AccessAnalyzerClient();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
// Do not edit this file directly. Instead, edit the script and run it to regenerate this file.


import AWS_ACM = require("@aws-sdk/client-acm");
import ACM = AWS_ACM.ACM;
import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");
import AccessAnalyzer = AWS_AccessAnalyzer.AccessAnalyzer;
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");
import ApplicationDiscoveryService = AWS_Discovery.ApplicationDiscoveryService;
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_application_discovery_service = require("@aws-sdk/client-application-discovery-service");
import ApplicationDiscoveryService = AWS_client_application_discovery_service.ApplicationDiscoveryService;

new ACM();
new AccessAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import AWS_s3_request_presigner = require("@aws-sdk/s3-request-presigner");
import getSignedUrl = AWS_s3_request_presigner.getSignedUrl;
import AWS_S3 = require("@aws-sdk/client-s3");
import GetObjectCommand = AWS_S3.GetObjectCommand;
import S3 = AWS_S3.S3;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import GetObjectCommand = AWS_client_s3.GetObjectCommand;
import S3 = AWS_client_s3.S3;

const s3 = new S3();

Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AWS_lib_storage = require("@aws-sdk/lib-storage");
import Upload = AWS_lib_storage.Upload;
import AWS_S3 = require("@aws-sdk/client-s3");
import S3 = AWS_S3.S3;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import S3 = AWS_client_s3.S3;

const client = new S3({ region: "REGION" });
await new Upload({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import AWS_lib_storage = require("@aws-sdk/lib-storage");
import Upload = AWS_lib_storage.Upload;
import AWS_S3 = require("@aws-sdk/client-s3");
import S3 = AWS_S3.S3;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import S3 = AWS_client_s3.S3;

const client = new S3({ region: "REGION" });
await new Upload({
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AWS_S3 = require("@aws-sdk/client-s3");
import waitUntilBucketExists = AWS_S3.waitUntilBucketExists;
import S3 = AWS_S3.S3;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import waitUntilBucketExists = AWS_client_s3.waitUntilBucketExists;
import S3 = AWS_client_s3.S3;

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import AWS_S3 = require("@aws-sdk/client-s3");
import waitUntilBucketExists = AWS_S3.waitUntilBucketExists;
import S3 = AWS_S3.S3;
import AWS_client_s3 = require("@aws-sdk/client-s3");
import waitUntilBucketExists = AWS_client_s3.waitUntilBucketExists;
import S3 = AWS_client_s3.S3;

const Bucket = "BUCKET_NAME";
const client = new S3({ region: "REGION" });
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getDefaultLocalName } from "../../utils";
import { getImportEqualsDeclaration } from "../getImportEqualsDeclaration";
import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType";
import { getImportEqualsLocalNameSuffix } from "../getImportEqualsLocalNameSuffix";
import { ClientModulesOptions } from "../types";
import { getImportEqualsDefaultName } from "./getImportEqualsDefaultName";

export type ClientDefaultModuleOptions = Omit<ClientModulesOptions, "clientIdentifiers">;

Expand All @@ -13,8 +12,7 @@ export const addClientDefaultModule = (
source: Collection<unknown>,
{ v2ClientLocalName, v2ClientName, v2GlobalName, v3ClientPackageName }: ClientDefaultModuleOptions
) => {
const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName);
const defaultLocalName = getDefaultLocalName(localNameSuffix);
const defaultLocalName = getImportEqualsDefaultName(v3ClientPackageName);
const existingImportEquals = source.find(
j.TSImportEqualsDeclaration,
getImportEqualsDeclarationType(v3ClientPackageName)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,21 +1,19 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { getDefaultLocalName } from "../../utils";
import { getImportEqualsDeclarationType } from "../getImportEqualsDeclarationType";
import { getImportEqualsLocalNameSuffix } from "../getImportEqualsLocalNameSuffix";
import { ClientModulesOptions, ImportSpecifierOptions } from "../types";
import { addClientDefaultModule } from "./addClientDefaultModule";
import { getImportEqualsDefaultName } from "./getImportEqualsDefaultName";

export const addClientNamedModule = (
j: JSCodeshift,
source: Collection<unknown>,
options: ClientModulesOptions & ImportSpecifierOptions
) => {
const { importedName, localName = importedName, ...v3ClientModulesOptions } = options;
const { v2ClientName, v3ClientPackageName } = v3ClientModulesOptions;
const { v3ClientPackageName } = v3ClientModulesOptions;

const localNameSuffix = getImportEqualsLocalNameSuffix(v2ClientName, v3ClientPackageName);
const defaultLocalName = getDefaultLocalName(localNameSuffix);
const defaultLocalName = getImportEqualsDefaultName(v3ClientPackageName);

const existingDeclaration = source.find(j.TSImportEqualsDeclaration, {
type: "TSImportEqualsDeclaration",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export const getImportEqualsDefaultName = (packageName: string) =>
["AWS", ...packageName.split("/").pop()!.split("-")].join("_");
1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/modules/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
export * from "./addClientModules";
export * from "./getGlobalNameFromModule";
export * from "./getImportEqualsDeclarationType";
export * from "./getImportEqualsLocalNameSuffix";
export * from "./getImportSpecifiers";
export * from "./getImportType";
export * from "./getRequireDeclaratorsWithProperty";
Expand Down
2 changes: 0 additions & 2 deletions src/transforms/v2-to-v3/utils/getDefaultLocalName.ts

This file was deleted.

1 change: 0 additions & 1 deletion src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from "./getClientDeepImportPath";
export * from "./getClientNewExpression";
export * from "./getDefaultLocalName";
export * from "./isTypeScriptFile";

0 comments on commit 2161b67

Please sign in to comment.