Skip to content

Commit

Permalink
Use only default import while transforming import equals (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Oct 17, 2023
1 parent b541e88 commit 73264e6
Show file tree
Hide file tree
Showing 40 changed files with 209 additions and 334 deletions.
5 changes: 5 additions & 0 deletions .changeset/tall-roses-hunt.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": minor
---

Use only default import while transforming import equals
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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 @@ -7,7 +9,12 @@ export const getGlobalImportEqualsOutput = () => {
let content = ``;

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

return content;
};
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ export const getServiceImportEqualsInput = () => {
for (const clientName of CLIENTS_TO_TEST) {
content += `import ${clientName} = require("${getClientDeepImportPath(clientName)}");\n`;
}
content += `\n`;
content += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST);

return content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
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 @@ -6,7 +8,12 @@ export const getServiceImportEqualsOutput = () => {
let content = ``;

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

return content;
};
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const getServiceImportEqualsWithNameInput = () => {
const importName = getClientNameWithLocalSuffix(clientName);
content += `import ${importName} = require("${getClientDeepImportPath(clientName)}");\n`;
}
content += `\n`;
content += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST.map(getClientNameWithLocalSuffix));

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

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

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

return content;
};
export const getServiceImportEqualsWithNameOutput = getServiceImportEqualsOutput;
30 changes: 3 additions & 27 deletions scripts/generateNewClientTests/getV3PackageImportEqualsCode.ts
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
import {
CLIENT_NAMES,
CLIENT_NAMES_MAP,
CLIENT_PACKAGE_NAMES_MAP,
} from "../../src/transforms/v2-to-v3/config";
import { CLIENT_NAMES, 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 {
useLocalSuffix?: boolean;
}

export const getV3PackageImportEqualsCode = (
clientsToTest: typeof CLIENT_NAMES,
options?: V3PackageImportEqualsCodeOptions
) => {
export const getV3PackageImportEqualsCode = (clientsToTest: typeof CLIENT_NAMES) => {
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\n`;

const v3ClientName = CLIENT_NAMES_MAP[v2ClientName];
const v2ClientLocalName = useLocalSuffix
? getClientNameWithLocalSuffix(v2ClientName)
: v2ClientName;

const v3ObjectPattern =
v2ClientName === v2ClientLocalName ? v3ClientName : `${v3ClientName}: ${v2ClientLocalName}`;

content +=
`const {\n` + ` ${v3ObjectPattern}\n` + `} = ${getDefaultLocalName(v2ClientName)};\n\n`;
content += `import ${v3ClientDefaultLocalName} = require("${v3ClientPackageName}");\n`;
}

return content;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

import AWS_Lambda = require("@aws-sdk/client-lambda");

const {
Lambda
} = AWS_Lambda;

import AWS_STS = require("@aws-sdk/client-sts");

const {
STS
} = AWS_STS;

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

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

const lambdaClient = new Lambda({ region: "us-west-2" });
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
.invoke(invokeInput);
Original file line number Diff line number Diff line change
@@ -1,32 +1,18 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

import AWS_Lambda = require("@aws-sdk/client-lambda");

const {
Lambda
} = AWS_Lambda;

import AWS_STS = require("@aws-sdk/client-sts");

const {
STS
} = AWS_STS;

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

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

const lambdaClient = new Lambda({ region: "us-west-2" });
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
.invoke(invokeInput);
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

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

const {
DynamoDB
} = AWS_DynamoDB;

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

const {
DynamoDBDocument
} = AWS_lib_dynamodb;

import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

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

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

const docClientScanOutput: AWS_DynamoDBDocumentClient.ScanCommandOutput = await docClient
const docClientScanOutput: AWS_lib_dynamodb.ScanCommandOutput = await docClient
.scan(docClientScanInput);
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
import AWS_lib_dynamodb = require("@aws-sdk/lib-dynamodb");

const {
DynamoDBDocument
} = AWS_lib_dynamodb;

import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB: DynDB
} = AWS_DynamoDB;

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

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

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

const {
DynamoDBDocument
} = AWS_lib_dynamodb;

import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

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

const {
DynamoDBDocument
} = AWS_lib_dynamodb;

import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB: DynamoDBClient
} = AWS_DynamoDB;

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

const {
DynamoDBDocument
} = AWS_lib_dynamodb;

import AWS_DynamoDB = require("@aws-sdk/client-dynamodb");

const {
DynamoDB
} = AWS_DynamoDB;

const documentClient = DynamoDBDocument.from(new DynamoDB({ region: "us-west-2" }));
const documentClient = AWS_lib_dynamodb.DynamoDBDocument.from(new AWS_DynamoDB.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,23 +3,9 @@


import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");

const {
AccessAnalyzer
} = AWS_AccessAnalyzer;

import AWS_ACM = require("@aws-sdk/client-acm");

const {
ACM
} = AWS_ACM;

import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");

const {
ApplicationDiscoveryService
} = AWS_Discovery;

new ACM();
new AccessAnalyzer();
new ApplicationDiscoveryService();
new AWS_ACM.ACM();
new AWS_AccessAnalyzer.AccessAnalyzer();
new AWS_Discovery.ApplicationDiscoveryService();
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ACMClient = require("aws-sdk/clients/acm");
import AccessAnalyzerClient = require("aws-sdk/clients/accessanalyzer");
import DiscoveryClient = require("aws-sdk/clients/discovery");

new ACMClient();
new AccessAnalyzerClient();
new DiscoveryClient();
Original file line number Diff line number Diff line change
Expand Up @@ -3,23 +3,9 @@


import AWS_ACM = require("@aws-sdk/client-acm");

const {
ACM: ACMClient
} = AWS_ACM;

import AWS_AccessAnalyzer = require("@aws-sdk/client-accessanalyzer");

const {
AccessAnalyzer: AccessAnalyzerClient
} = AWS_AccessAnalyzer;

import AWS_Discovery = require("@aws-sdk/client-application-discovery-service");

const {
ApplicationDiscoveryService: DiscoveryClient
} = AWS_Discovery;

new ACMClient();
new AccessAnalyzerClient();
new DiscoveryClient();
new AWS_ACM.ACM();
new AWS_AccessAnalyzer.AccessAnalyzer();
new AWS_Discovery.ApplicationDiscoveryService();
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
import ACM = require("aws-sdk/clients/acm");
import AccessAnalyzer = require("aws-sdk/clients/accessanalyzer");
import Discovery = require("aws-sdk/clients/discovery");

new ACM();
new AccessAnalyzer();
new Discovery();
Loading

0 comments on commit 73264e6

Please sign in to comment.