Skip to content

Commit

Permalink
Prefer import over require when both are present (#652)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Oct 26, 2023
1 parent b6c5f83 commit f2185da
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 56 deletions.
5 changes: 5 additions & 0 deletions .changeset/stale-geese-grow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Prefer import over require when both are present
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import { DynamoDB } from "aws-sdk";
const AWS = require("aws-sdk");

const client: DynamoDB = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from "@aws-sdk/client-dynamodb";

const client: DynamoDB = new DynamoDB();
32 changes: 14 additions & 18 deletions src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,28 +3,24 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
import { PACKAGE_NAME } from "../config";
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
import { getImportSpecifiers } from "./getImportSpecifiers";
import { ImportType } from "./types";

export const getGlobalNameFromModule = (
j: JSCodeshift,
source: Collection<unknown>,
importType: ImportType
source: Collection<unknown>
): string | undefined => {
if (importType === ImportType.REQUIRE) {
const requireIdentifiers = source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ value: PACKAGE_NAME }],
},
})
.nodes();

if (requireIdentifiers.length > 0) {
return (requireIdentifiers[0]?.id as Identifier).name;
}
const requireIdentifiers = source
.find(j.VariableDeclarator, {
id: { type: "Identifier" },
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ value: PACKAGE_NAME }],
},
})
.nodes();

if (requireIdentifiers.length > 0) {
return (requireIdentifiers[0]?.id as Identifier).name;
}

const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter((specifier) =>
Expand Down
8 changes: 4 additions & 4 deletions src/transforms/v2-to-v3/modules/getImportType.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { hasImport } from "./hasImport";
import { hasImportEquals } from "./hasImportEquals";
import { hasRequire } from "./hasRequire";
import { ImportType } from "./types";

export const getImportType = (j: JSCodeshift, source: Collection<unknown>) =>
hasRequire(j, source)
? ImportType.REQUIRE
hasImport(j, source)
? ImportType.IMPORT
: hasImportEquals(j, source)
? ImportType.IMPORT_EQUALS
: ImportType.IMPORT;
: ImportType.REQUIRE;
11 changes: 11 additions & 0 deletions src/transforms/v2-to-v3/modules/hasImport.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { PACKAGE_NAME } from "../config";

export const hasImport = (j: JSCodeshift, source: Collection<unknown>) =>
source
.find(j.ImportDeclaration)
.filter((importDeclaration) => {
const { value: sourceValue } = importDeclaration.value.source;
return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME);
})
.size() > 0;
18 changes: 0 additions & 18 deletions src/transforms/v2-to-v3/modules/hasRequire.ts

This file was deleted.

18 changes: 4 additions & 14 deletions src/transforms/v2-to-v3/modules/removeGlobalModule.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,18 +4,12 @@ import { PACKAGE_NAME } from "../config";
import { removeImportDefault } from "./removeImportDefault";
import { removeImportEquals } from "./removeImportEquals";
import { removeRequireIdentifier } from "./removeRequireIdentifier";
import { ImportType } from "./types";

export interface RemoveGlobalModuleOptions {
importType: ImportType;
v2GlobalName?: string;
}

// Removes the import of "aws-sdk" if it's not used.
export const removeGlobalModule = (
j: JSCodeshift,
source: Collection<unknown>,
{ importType, v2GlobalName }: RemoveGlobalModuleOptions
v2GlobalName?: string
) => {
if (!v2GlobalName) return;

Expand All @@ -24,12 +18,8 @@ export const removeGlobalModule = (
// Only usage is import/require.
if (identifierUsages.size() === 1) {
const defaultOptions = { localName: v2GlobalName, sourceValue: PACKAGE_NAME };
if (importType === ImportType.REQUIRE) {
removeRequireIdentifier(j, source, defaultOptions);
} else if (importType === ImportType.IMPORT_EQUALS) {
removeImportEquals(j, source, defaultOptions);
} else {
removeImportDefault(j, source, defaultOptions);
}
removeRequireIdentifier(j, source, defaultOptions);
removeImportEquals(j, source, defaultOptions);
removeImportDefault(j, source, defaultOptions);
}
};
4 changes: 2 additions & 2 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ const transformer = async (file: FileInfo, api: API) => {

addNotSupportedComments(j, source, importType);

const v2GlobalName = getGlobalNameFromModule(j, source, importType);
const v2GlobalName = getGlobalNameFromModule(j, source);
const v2ClientNamesRecord = getClientNamesRecord(j, source, importType);

if (!v2GlobalName && Object.keys(v2ClientNamesRecord).length === 0) {
Expand Down Expand Up @@ -99,7 +99,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceAwsConfig(j, source, v2GlobalName);
replaceAwsIdentity(j, source, { v2GlobalName, importType });
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, { v2GlobalName, importType });
removeGlobalModule(j, source, v2GlobalName);

return source.toSource();
};
Expand Down

0 comments on commit f2185da

Please sign in to comment.