Skip to content

Commit

Permalink
Rename Import to Module wherever required (#68)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Mar 8, 2022
1 parent 9e80878 commit a72bd0c
Show file tree
Hide file tree
Showing 9 changed files with 41 additions and 36 deletions.
5 changes: 5 additions & 0 deletions .changeset/afraid-sheep-beam.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Rename Import to Module wherever required
18 changes: 9 additions & 9 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ import { API, FileInfo } from "jscodeshift";
import {
addV3ClientModule,
getClientMetadata,
getV2ClientImportNames,
getV2ClientModuleNames,
getV2ClientNames,
getV2DefaultImportName,
getV2DefaultModuleName,
removeDefaultModuleIfNotUsed,
removePromiseCalls,
removeV2ClientModule,
Expand All @@ -16,24 +16,24 @@ export default function transformer(file: FileInfo, api: API) {
const j = api.jscodeshift;
const source = j(file.source);

const v2DefaultImportName = getV2DefaultImportName(j, source);
const v2ClientImportNames = getV2ClientImportNames(j, source);
if (!v2DefaultImportName && v2ClientImportNames.length === 0) {
const v2DefaultModuleName = getV2DefaultModuleName(j, source);
const v2ClientModuleNames = getV2ClientModuleNames(j, source);
if (!v2DefaultModuleName && v2ClientModuleNames.length === 0) {
return source.toSource();
}

const v2ClientNames = getV2ClientNames(j, source, { v2DefaultImportName, v2ClientImportNames });
const v2ClientNames = getV2ClientNames(j, source, { v2DefaultModuleName, v2ClientModuleNames });
const clientMetadata = getClientMetadata(v2ClientNames);

for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadata).reverse()) {
const { v3ClientName, v3ClientPackageName } = v3ClientMetadata;
addV3ClientModule(j, source, { v2ClientName, v3ClientName, v3ClientPackageName });
removeV2ClientModule(j, source, v2ClientName);
removePromiseCalls(j, source, { v2DefaultImportName, v2ClientName });
replaceClientCreation(j, source, { v2DefaultImportName, v2ClientName, v3ClientName });
removePromiseCalls(j, source, { v2DefaultModuleName, v2ClientName });
replaceClientCreation(j, source, { v2DefaultModuleName, v2ClientName, v3ClientName });
}

removeDefaultModuleIfNotUsed(j, source, v2DefaultImportName);
removeDefaultModuleIfNotUsed(j, source, v2DefaultModuleName);

return source.toSource();
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";

export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>): string[] => {
const v2ClientImportNames = [];
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => {
const v2ClientModuleNames = [];

for (const clientName of CLIENT_NAMES) {
// Add specifier name to v2ClientImportNames if it is imported in the source.
Expand All @@ -17,7 +17,7 @@ export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>):
specifier.type === "ImportDefaultSpecifier" ||
specifier.type === "ImportNamespaceSpecifier"
) {
v2ClientImportNames.push(specifier.local.name);
v2ClientModuleNames.push(specifier.local.name);
}
});
});
Expand All @@ -33,9 +33,9 @@ export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>):
},
})
.forEach((declerationPath) => {
v2ClientImportNames.push((declerationPath.value.id as Identifier).name);
v2ClientModuleNames.push((declerationPath.value.id as Identifier).name);
});
}

return v2ClientImportNames;
return v2ClientModuleNames;
};
10 changes: 5 additions & 5 deletions src/transforms/v2-to-v3/utils/getV2ClientNames.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshift";

export interface GetV2ClientNamesOptions {
v2DefaultImportName: string;
v2ClientImportNames: string[];
v2DefaultModuleName: string;
v2ClientModuleNames: string[];
}

export const getV2ClientNames = (
j: JSCodeshift,
source: Collection<any>,
{ v2DefaultImportName, v2ClientImportNames }: GetV2ClientNamesOptions
{ v2DefaultModuleName, v2ClientModuleNames }: GetV2ClientNamesOptions
): Array<string> => {
const v2ClientNamesFromDefaultImport = source
.find(j.NewExpression, {
callee: {
type: "MemberExpression",
object: { type: "Identifier", name: v2DefaultImportName },
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier" },
},
})
Expand All @@ -25,7 +25,7 @@ export const getV2ClientNames = (

// Merge v2ClientNamesFromDefaultImport with v2ClientImportNames with duplicates removed.
return v2ClientNamesFromDefaultImport.concat(
v2ClientImportNames.filter(
v2ClientModuleNames.filter(
(v2ClientImportName) => v2ClientNamesFromDefaultImport.indexOf(v2ClientImportName) < 0
)
);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

export const getV2DefaultImportName = (
export const getV2DefaultModuleName = (
j: JSCodeshift,
source: Collection<any>
): string | undefined => {
let v2DefaultImportName = undefined;
let v2DefaultModuleName = undefined;

// Set specifier name in v2DefaultImportName if it is imported in the source.
source
Expand All @@ -17,7 +17,7 @@ export const getV2DefaultImportName = (
specifier.type === "ImportDefaultSpecifier" ||
specifier.type === "ImportNamespaceSpecifier"
) {
v2DefaultImportName = specifier.local.name;
v2DefaultModuleName = specifier.local.name;
}
});
});
Expand All @@ -33,8 +33,8 @@ export const getV2DefaultImportName = (
},
})
.forEach((declerationPath) => {
v2DefaultImportName = (declerationPath.value.id as Identifier).name;
v2DefaultModuleName = (declerationPath.value.id as Identifier).name;
});

return v2DefaultImportName;
return v2DefaultModuleName;
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export * from "./addV3ClientModule";
export * from "./containsRequire";
export * from "./getClientMetadata";
export * from "./getV2ClientImportNames";
export * from "./getV2ClientModuleNames";
export * from "./getV2ClientNames";
export * from "./getV2DefaultImportName";
export * from "./getV2DefaultModuleName";
export * from "./removeDefaultModuleIfNotUsed";
export * from "./removePromiseCalls";
export * from "./removeV2ClientModule";
Expand Down
8 changes: 4 additions & 4 deletions src/transforms/v2-to-v3/utils/removeDefaultModuleIfNotUsed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ import { removeDefaultRequire } from "./removeDefaultRequire";
export const removeDefaultModuleIfNotUsed = (
j: JSCodeshift,
source: Collection<any>,
defaultImportName: string
defaultModuleName: string
) => {
const identifierUsages = source.find(j.Identifier, { name: defaultImportName });
const identifierUsages = source.find(j.Identifier, { name: defaultModuleName });

// Only usage is import/require.
if (identifierUsages.size() === 1) {
if (containsRequire(j, source)) {
removeDefaultRequire(j, source, defaultImportName);
removeDefaultRequire(j, source, defaultModuleName);
} else {
removeDefaultImport(j, source, defaultImportName);
removeDefaultImport(j, source, defaultModuleName);
}
}
};
6 changes: 3 additions & 3 deletions src/transforms/v2-to-v3/utils/removePromiseCalls.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,22 @@ import { Collection, Identifier, JSCodeshift, MemberExpression } from "jscodeshi

export interface RemovePromiseCallsOptions {
v2ClientName: string;
v2DefaultImportName: string;
v2DefaultModuleName: string;
}

// Removes .promise() from client API calls.
export const removePromiseCalls = (
j: JSCodeshift,
source: Collection<any>,
{ v2DefaultImportName, v2ClientName }: RemovePromiseCallsOptions
{ v2DefaultModuleName, v2ClientName }: RemovePromiseCallsOptions
): void => {
source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "NewExpression",
callee: {
object: { type: "Identifier", name: v2DefaultImportName },
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier", name: v2ClientName },
},
},
Expand Down
6 changes: 3 additions & 3 deletions src/transforms/v2-to-v3/utils/replaceClientCreation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,20 +3,20 @@ import { Collection, JSCodeshift } from "jscodeshift";
export interface ReplaceClientCreationOptions {
v2ClientName: string;
v3ClientName: string;
v2DefaultImportName: string;
v2DefaultModuleName: string;
}

// Replace v2 client creation with v3 client creation.
export const replaceClientCreation = (
j: JSCodeshift,
source: Collection<any>,
{ v2DefaultImportName, v2ClientName, v3ClientName }: ReplaceClientCreationOptions
{ v2DefaultModuleName, v2ClientName, v3ClientName }: ReplaceClientCreationOptions
): void => {
// Replace clients created with default import.
source
.find(j.NewExpression, {
callee: {
object: { type: "Identifier", name: v2DefaultImportName },
object: { type: "Identifier", name: v2DefaultModuleName },
property: { type: "Identifier", name: v2ClientName },
},
})
Expand Down

0 comments on commit a72bd0c

Please sign in to comment.