Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add import equals declaration for named imports #615

Merged
merged 17 commits into from
Oct 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/rotten-dragons-obey.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add import equals declaration for named imports
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/config";
import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils";
import { CLIENTS_TO_TEST } from "./config";
import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
Expand All @@ -10,11 +8,7 @@ export const getGlobalImportEqualsOutput = () => {

content += getV3PackageImportEqualsCode(getClientNamesSortedByPackageName(CLIENTS_TO_TEST));
content += "\n";
content += getV3ClientsNewExpressionCode(
CLIENTS_TO_TEST.map((clientName) =>
[getDefaultLocalName(clientName), CLIENT_NAMES_MAP[clientName]].join(".")
)
);
content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST);

return content;
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import { CLIENT_NAMES_MAP } from "../../src/transforms/v2-to-v3/config";
import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils";
import { CLIENTS_TO_TEST } from "./config";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode";
Expand All @@ -9,11 +7,7 @@ export const getServiceImportEqualsOutput = () => {

content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST);
content += "\n";
content += getV3ClientsNewExpressionCode(
CLIENTS_TO_TEST.map((clientName) =>
[getDefaultLocalName(clientName), CLIENT_NAMES_MAP[clientName]].join(".")
)
);
content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST);

return content;
};
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import { getServiceImportEqualsOutput } from "./getServiceImportEqualsOutput";
import { CLIENTS_TO_TEST } from "./config";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
import { getV3PackageImportEqualsCode } from "./getV3PackageImportEqualsCode";

export const getServiceImportEqualsWithNameOutput = getServiceImportEqualsOutput;
export const getServiceImportEqualsWithNameOutput = () => {
let content = ``;

content += getV3PackageImportEqualsCode(CLIENTS_TO_TEST, { useLocalSuffix: true });
content += "\n";
content += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix));

return content;
};
21 changes: 19 additions & 2 deletions scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,30 @@
import { CLIENT_NAMES, CLIENT_PACKAGE_NAMES_MAP } from "../../src/transforms/v2-to-v3/config";
import {
CLIENT_NAMES,
CLIENT_NAMES_MAP,
CLIENT_PACKAGE_NAMES_MAP,
} from "../../src/transforms/v2-to-v3/config";
import { getDefaultLocalName } from "../../src/transforms/v2-to-v3/utils";
import { getClientNameWithLocalSuffix } from "./getClientNameWithLocalSuffix";

export const getV3PackageImportEqualsCode = (clientsToTest: typeof CLIENT_NAMES) => {
export interface V3PackageImportEqualsCodeOptions {
useLocalSuffix?: boolean;
}

export const getV3PackageImportEqualsCode = (
clientsToTest: typeof CLIENT_NAMES,
options?: V3PackageImportEqualsCodeOptions
) => {
let content = ``;
const { useLocalSuffix = false } = options || {};

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

const v3ClientName = CLIENT_NAMES_MAP[v2ClientName];
const importName = useLocalSuffix ? getClientNameWithLocalSuffix(v2ClientName) : v3ClientName;
content += `import ${importName} = ${[v3ClientDefaultLocalName, v3ClientName].join(".")};\n`;
}

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

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

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

const ddbClient = new AWS_DynamoDB.DynamoDB({ region: "us-west-2" });
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
const ddbClient = new DynamoDB({ region: "us-west-2" });
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
const listTablesOutput: ListTablesCommandOutput = await ddbClient
.listTables(listTablesInput);

const stsClient = new AWS_STS.STS({ region: "us-west-2" });
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
const stsClient = new STS({ region: "us-west-2" });
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
.getCallerIdentity(getCallerIdentityInput);

const lambdaClient = new AWS_Lambda.Lambda({ region: "us-west-2" });
const invokeInput: AWS_Lambda.InvokeCommandInput = { FunctionName: "my-function" };
const invokeOutput: AWS_Lambda.InvokeCommandOutput = await lambdaClient
const lambdaClient = new Lambda({ region: "us-west-2" });
const invokeInput: InvokeCommandInput = { FunctionName: "my-function" };
const invokeOutput: InvokeCommandOutput = await lambdaClient
.invoke(invokeInput);
Original file line number Diff line number Diff line change
@@ -1,18 +1,27 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;
import ListTablesCommandOutput = AWS_DynamoDB.ListTablesCommandOutput;
import ListTablesCommandInput = AWS_DynamoDB.ListTablesCommandInput;
import AWS_Lambda = require("@aws-sdk/client-lambda");
import Lambda = AWS_Lambda.Lambda;
import InvokeCommandOutput = AWS_Lambda.InvokeCommandOutput;
import InvokeCommandInput = AWS_Lambda.InvokeCommandInput;
import AWS_STS = require("@aws-sdk/client-sts");
import STS = AWS_STS.STS;
import GetCallerIdentityCommandOutput = AWS_STS.GetCallerIdentityCommandOutput;
import GetCallerIdentityCommandInput = AWS_STS.GetCallerIdentityCommandInput;

const ddbClient = new AWS_DynamoDB.DynamoDB({ region: "us-west-2" });
const listTablesInput: AWS_DynamoDB.ListTablesCommandInput = { Limit: 10 };
const listTablesOutput: AWS_DynamoDB.ListTablesCommandOutput = await ddbClient
const ddbClient = new DynamoDB({ region: "us-west-2" });
const listTablesInput: ListTablesCommandInput = { Limit: 10 };
const listTablesOutput: ListTablesCommandOutput = await ddbClient
.listTables(listTablesInput);

const stsClient = new AWS_STS.STS({ region: "us-west-2" });
const getCallerIdentityInput: AWS_STS.GetCallerIdentityCommandInput = {};
const getCallerIdentityOutput: AWS_STS.GetCallerIdentityCommandOutput = await stsClient
const stsClient = new STS({ region: "us-west-2" });
const getCallerIdentityInput: GetCallerIdentityCommandInput = {};
const getCallerIdentityOutput: GetCallerIdentityCommandOutput = await stsClient
.getCallerIdentity(getCallerIdentityInput);

const lambdaClient = new AWS_Lambda.Lambda({ region: "us-west-2" });
const invokeInput: AWS_Lambda.InvokeCommandInput = { FunctionName: "my-function" };
const invokeOutput: AWS_Lambda.InvokeCommandOutput = await lambdaClient
const lambdaClient = new Lambda({ region: "us-west-2" });
const invokeInput: InvokeCommandInput = { FunctionName: "my-function" };
const invokeOutput: InvokeCommandOutput = await lambdaClient
.invoke(invokeInput);
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;

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

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

// Native types
const stringType: string = "string";
Expand All @@ -18,14 +22,14 @@ const stringArray: Array<string> = ["string1", "string2"];
const booleanArray: Array<boolean> = [true, false];
const numberArray: Array<number> = [123, 456];
const blobArray: Array<Uint8Array> = [new Uint8Array()];
const enumArray: Array<AWS_S3.ChecksumAlgorithm> = ["CRC32"];
const structureArray: Array<AWS_S3.Bucket> = [{ Name: "bucketName" }];
const enumArray: Array<ChecksumAlgorithm> = ["CRC32"];
const structureArray: Array<Bucket> = [{ Name: "bucketName" }];

// Maps
const stringMap: Record<string, string> = { key: "value" };
const booleanMap: Record<string, boolean> = { key: true };
const numberMap: Record<string, number> = { key: 123 };
const structureMap: Record<string, AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
const structureMap: Record<string, MethodSnapshot> = { key: { apiKeyRequired: true } };

// Nested arrays
const arrayNestedTwice: Array<Array<number>> = [[1, 2], [3, 4]];
Expand All @@ -37,12 +41,12 @@ const arrayNestedFour: Array<Array<Array<Array<number>>>> = [

// Nested maps
const mapNestedTwice: Record<string, Record<string, string>> = { key: stringMap };
const mapNestedTwiceStruct: Record<string, Record<string, AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
const mapNestedTwiceStruct: Record<string, Record<string, MethodSnapshot>> = { key: structureMap };

// Nested arrays and maps
const mapOfArrays: Record<string, Array<string>> = { key: ["value"] };
const mapOfMapOfArrays: Record<string, Record<string, Array<string>>> = { key: mapOfArrays };
const mapOfArrayOfMaps: Record<string, Array<Record<string, AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
const mapOfArrayOfMaps: Record<string, Array<Record<string, AttributeValue>>> = { key: [{ key: { S:"A" }}] };
const mapOfArrayOfArrays: Record<string, Array<Array<number>>> = { key: [[1], [2]] };
const arrayOfMaps: Array<Record<string, string>> = [stringMap];
const arrayOfMapOfArrays: Array<Record<string, Array<string>>> = [mapOfArrays];
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
import AWS_APIGateway = require("@aws-sdk/client-api-gateway");
import MethodSnapshot = AWS_APIGateway.MethodSnapshot;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import AttributeValue = AWS_DynamoDB.AttributeValue;
import AWS_S3 = require("@aws-sdk/client-s3");
import Bucket = AWS_S3.Bucket;
import ChecksumAlgorithm = AWS_S3.ChecksumAlgorithm;

// Native types
const stringType: string = "string";
Expand All @@ -18,14 +22,14 @@ const stringArray: Array<string> = ["string1", "string2"];
const booleanArray: Array<boolean> = [true, false];
const numberArray: Array<number> = [123, 456];
const blobArray: Array<Uint8Array> = [new Uint8Array()];
const enumArray: Array<AWS_S3.ChecksumAlgorithm> = ["CRC32"];
const structureArray: Array<AWS_S3.Bucket> = [{ Name: "bucketName" }];
const enumArray: Array<ChecksumAlgorithm> = ["CRC32"];
const structureArray: Array<Bucket> = [{ Name: "bucketName" }];

// Maps
const stringMap: Record<string, string> = { key: "value" };
const booleanMap: Record<string, boolean> = { key: true };
const numberMap: Record<string, number> = { key: 123 };
const structureMap: Record<string, AWS_APIGateway.MethodSnapshot> = { key: { apiKeyRequired: true } };
const structureMap: Record<string, MethodSnapshot> = { key: { apiKeyRequired: true } };

// Nested arrays
const arrayNestedTwice: Array<Array<number>> = [[1, 2], [3, 4]];
Expand All @@ -37,12 +41,12 @@ const arrayNestedFour: Array<Array<Array<Array<number>>>> = [

// Nested maps
const mapNestedTwice: Record<string, Record<string, string>> = { key: stringMap };
const mapNestedTwiceStruct: Record<string, Record<string, AWS_APIGateway.MethodSnapshot>> = { key: structureMap };
const mapNestedTwiceStruct: Record<string, Record<string, MethodSnapshot>> = { key: structureMap };

// Nested arrays and maps
const mapOfArrays: Record<string, Array<string>> = { key: ["value"] };
const mapOfMapOfArrays: Record<string, Record<string, Array<string>>> = { key: mapOfArrays };
const mapOfArrayOfMaps: Record<string, Array<Record<string, AWS_DynamoDB.AttributeValue>>> = { key: [{ key: { S:"A" }}] };
const mapOfArrayOfMaps: Record<string, Array<Record<string, AttributeValue>>> = { key: [{ key: { S:"A" }}] };
const mapOfArrayOfArrays: Record<string, Array<Array<number>>> = { key: [[1], [2]] };
const arrayOfMaps: Array<Record<string, string>> = [stringMap];
const arrayOfMapOfArrays: Array<Record<string, Array<string>>> = [mapOfArrays];
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput;
import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;

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

const docClientScanInput: AWS_lib_dynamodb.ScanCommandInput = {
const docClientScanInput: ScanCommandInput = {
TableName: "TableName"
};

const docClientScanOutput: AWS_lib_dynamodb.ScanCommandOutput = await docClient
const docClientScanOutput: ScanCommandOutput = await docClient
.scan(docClientScanInput);
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import ScanCommandOutput = AWS_lib_dynamodb.ScanCommandOutput;
import ScanCommandInput = AWS_lib_dynamodb.ScanCommandInput;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynDB = AWS_DynamoDB.DynamoDB;

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

const docClientScanInput: AWS_lib_dynamodb.ScanCommandInput = {
const docClientScanInput: ScanCommandInput = {
TableName: "TableName"
};

const docClientScanOutput: AWS_lib_dynamodb.ScanCommandOutput = await docClient
const docClientScanOutput: ScanCommandOutput = await docClient
.scan(docClientScanInput);
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");
import DynamoDBDocument = AWS_lib_dynamodb.DynamoDBDocument;
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");
import DynamoDB = AWS_DynamoDB.DynamoDB;

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

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

const documentClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.DynamoDB({ region: "us-west-2" }));
const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" }));
const response = await documentClient.scan({ TableName: "TABLE_NAME" });
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@


import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");
import AccessAnalyzer = AWS_AccessAnalyzer.AccessAnalyzer;
import AWS_ACM = require("@aws-sdk/client-acm");
import ACM = AWS_ACM.ACM;
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");
import ApplicationDiscoveryService = AWS_Discovery.ApplicationDiscoveryService;

new AWS_ACM.ACM();
new AWS_AccessAnalyzer.AccessAnalyzer();
new AWS_Discovery.ApplicationDiscoveryService();
new ACM();
new AccessAnalyzer();
new ApplicationDiscoveryService();
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,12 @@


import AWS_ACM = require("@aws-sdk/client-acm");
import ACMClient = AWS_ACM.ACM;
import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");
import AccessAnalyzerClient = AWS_AccessAnalyzer.AccessAnalyzer;
import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");
import DiscoveryClient = AWS_Discovery.ApplicationDiscoveryService;

new AWS_ACM.ACM();
new AWS_AccessAnalyzer.AccessAnalyzer();
new AWS_Discovery.ApplicationDiscoveryService();
new ACMClient();
new AccessAnalyzerClient();
new DiscoveryClient();
Loading