Skip to content

Commit

Permalink
Use filter API to shortlist import declarations to be deleted (#321)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Jan 6, 2023
1 parent 9a501bf commit e623543
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 11 deletions.
5 changes: 5 additions & 0 deletions .changeset/nervous-worms-grin.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use filter API to shortlist import declarations to be deleted
14 changes: 12 additions & 2 deletions scripts/generateNewClientTests/getServiceImportDeepOutput.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,13 @@
import { getServiceImportOutput } from "./getServiceImportOutput";
import { CLIENTS_TO_TEST } from "./config";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
import { getV3PackageImportsCode } from "./getV3PackageImportsCode";

export const getServiceImportDeepOutput = getServiceImportOutput;
export const getServiceImportDeepOutput = (codegenComment: string) => {
let serviceImportOutputContent = `${codegenComment}\n`;

serviceImportOutputContent += getV3PackageImportsCode(CLIENTS_TO_TEST);
serviceImportOutputContent += `\n`;
serviceImportOutputContent += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST);

return serviceImportOutputContent;
};
4 changes: 1 addition & 3 deletions scripts/generateNewClientTests/getServiceImportInput.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,7 @@ import { getV2ClientsNewExpressionCode } from "./getV2ClientsNewExpressionCode";
export const getServiceImportInput = (codegenComment: string) => {
let serviceImportInputContent = `${codegenComment}\n`;

for (const clientName of CLIENTS_TO_TEST) {
serviceImportInputContent += `import { ${clientName} } from "aws-sdk";\n`;
}
serviceImportInputContent += `import { ${CLIENTS_TO_TEST.join(", ")} } from "aws-sdk";\n`;
serviceImportInputContent += `\n`;
serviceImportInputContent += getV2ClientsNewExpressionCode(CLIENTS_TO_TEST);

Expand Down
5 changes: 4 additions & 1 deletion scripts/generateNewClientTests/getServiceImportOutput.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { CLIENTS_TO_TEST } from "./config";
import { getClientNamesSortedByPackageName } from "./getClientNamesSortedByPackageName";
import { getV3ClientsNewExpressionCode } from "./getV3ClientsNewExpressionCode";
import { getV3PackageImportsCode } from "./getV3PackageImportsCode";

export const getServiceImportOutput = (codegenComment: string) => {
let serviceImportOutputContent = `${codegenComment}\n`;

serviceImportOutputContent += getV3PackageImportsCode(CLIENTS_TO_TEST);
serviceImportOutputContent += getV3PackageImportsCode(
getClientNamesSortedByPackageName(CLIENTS_TO_TEST)
);
serviceImportOutputContent += `\n`;
serviceImportOutputContent += getV3ClientsNewExpressionCode(CLIENTS_TO_TEST);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +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.
"use strict";
import { ACM } from "aws-sdk";
import { AccessAnalyzer } from "aws-sdk";
import { Discovery } from "aws-sdk";
import { ACM, AccessAnalyzer, Discovery } from "aws-sdk";

new ACM();
new AccessAnalyzer();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
// 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.
"use strict";
import { ACM } from "@aws-sdk/client-acm";
import { AccessAnalyzer } from "@aws-sdk/client-accessanalyzer";
import { ACM } from "@aws-sdk/client-acm";
import { ApplicationDiscoveryService as Discovery } from "@aws-sdk/client-application-discovery-service";

new ACM();
Expand Down
8 changes: 7 additions & 1 deletion src/transforms/v2-to-v3/modules/removeImportNamed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,15 @@ export const removeImportNamed = (
) => {
source
.find(j.ImportDeclaration, {
specifiers: [{ local: { name: localName } }],
source: { value: sourceValue },
})
.filter(
(importDeclaration) =>
importDeclaration.value.specifiers !== undefined &&
importDeclaration.value.specifiers.some(
(specifier) => specifier.type === "ImportSpecifier" && specifier.local?.name === localName
)
)
.forEach((declarationPath) => {
// Remove named import from ImportDeclaration if there is a match.
declarationPath.value.specifiers = declarationPath.value.specifiers?.filter((specifier) => {
Expand Down

0 comments on commit e623543

Please sign in to comment.