Skip to content

Commit f2185da

Browse files
authored
Prefer import over require when both are present (#652)
1 parent b6c5f83 commit f2185da

File tree

9 files changed

+47
-56
lines changed

9 files changed

+47
-56
lines changed

.changeset/stale-geese-grow.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Prefer import over require when both are present
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { DynamoDB } from "aws-sdk";
2+
const AWS = require("aws-sdk");
3+
4+
const client: DynamoDB = new AWS.DynamoDB();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
const client: DynamoDB = new DynamoDB();

src/transforms/v2-to-v3/modules/getGlobalNameFromModule.ts

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,28 +3,24 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
33
import { PACKAGE_NAME } from "../config";
44
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
55
import { getImportSpecifiers } from "./getImportSpecifiers";
6-
import { ImportType } from "./types";
76

87
export const getGlobalNameFromModule = (
98
j: JSCodeshift,
10-
source: Collection<unknown>,
11-
importType: ImportType
9+
source: Collection<unknown>
1210
): string | undefined => {
13-
if (importType === ImportType.REQUIRE) {
14-
const requireIdentifiers = source
15-
.find(j.VariableDeclarator, {
16-
id: { type: "Identifier" },
17-
init: {
18-
type: "CallExpression",
19-
callee: { type: "Identifier", name: "require" },
20-
arguments: [{ value: PACKAGE_NAME }],
21-
},
22-
})
23-
.nodes();
24-
25-
if (requireIdentifiers.length > 0) {
26-
return (requireIdentifiers[0]?.id as Identifier).name;
27-
}
11+
const requireIdentifiers = source
12+
.find(j.VariableDeclarator, {
13+
id: { type: "Identifier" },
14+
init: {
15+
type: "CallExpression",
16+
callee: { type: "Identifier", name: "require" },
17+
arguments: [{ value: PACKAGE_NAME }],
18+
},
19+
})
20+
.nodes();
21+
22+
if (requireIdentifiers.length > 0) {
23+
return (requireIdentifiers[0]?.id as Identifier).name;
2824
}
2925

3026
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter((specifier) =>
Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { hasImport } from "./hasImport";
23
import { hasImportEquals } from "./hasImportEquals";
3-
import { hasRequire } from "./hasRequire";
44
import { ImportType } from "./types";
55

66
export const getImportType = (j: JSCodeshift, source: Collection<unknown>) =>
7-
hasRequire(j, source)
8-
? ImportType.REQUIRE
7+
hasImport(j, source)
8+
? ImportType.IMPORT
99
: hasImportEquals(j, source)
1010
? ImportType.IMPORT_EQUALS
11-
: ImportType.IMPORT;
11+
: ImportType.REQUIRE;
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { PACKAGE_NAME } from "../config";
3+
4+
export const hasImport = (j: JSCodeshift, source: Collection<unknown>) =>
5+
source
6+
.find(j.ImportDeclaration)
7+
.filter((importDeclaration) => {
8+
const { value: sourceValue } = importDeclaration.value.source;
9+
return typeof sourceValue === "string" && sourceValue.startsWith(PACKAGE_NAME);
10+
})
11+
.size() > 0;

src/transforms/v2-to-v3/modules/hasRequire.ts

Lines changed: 0 additions & 18 deletions
This file was deleted.

src/transforms/v2-to-v3/modules/removeGlobalModule.ts

Lines changed: 4 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,18 +4,12 @@ import { PACKAGE_NAME } from "../config";
44
import { removeImportDefault } from "./removeImportDefault";
55
import { removeImportEquals } from "./removeImportEquals";
66
import { removeRequireIdentifier } from "./removeRequireIdentifier";
7-
import { ImportType } from "./types";
8-
9-
export interface RemoveGlobalModuleOptions {
10-
importType: ImportType;
11-
v2GlobalName?: string;
12-
}
137

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

@@ -24,12 +18,8 @@ export const removeGlobalModule = (
2418
// Only usage is import/require.
2519
if (identifierUsages.size() === 1) {
2620
const defaultOptions = { localName: v2GlobalName, sourceValue: PACKAGE_NAME };
27-
if (importType === ImportType.REQUIRE) {
28-
removeRequireIdentifier(j, source, defaultOptions);
29-
} else if (importType === ImportType.IMPORT_EQUALS) {
30-
removeImportEquals(j, source, defaultOptions);
31-
} else {
32-
removeImportDefault(j, source, defaultOptions);
33-
}
21+
removeRequireIdentifier(j, source, defaultOptions);
22+
removeImportEquals(j, source, defaultOptions);
23+
removeImportDefault(j, source, defaultOptions);
3424
}
3525
};

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ const transformer = async (file: FileInfo, api: API) => {
3939

4040
addNotSupportedComments(j, source, importType);
4141

42-
const v2GlobalName = getGlobalNameFromModule(j, source, importType);
42+
const v2GlobalName = getGlobalNameFromModule(j, source);
4343
const v2ClientNamesRecord = getClientNamesRecord(j, source, importType);
4444

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

104104
return source.toSource();
105105
};

0 commit comments

Comments
 (0)