Skip to content

Commit 7603bda

Browse files
authored
Store whether code uses require/import/importEquals in importType (#604)
1 parent e5fcdb6 commit 7603bda

12 files changed

+92
-51
lines changed

.changeset/poor-shirts-travel.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+
Store whether code uses require/import/importEquals in importType

src/transforms/v2-to-v3/apis/addNotSupportedComments.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,14 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22
import { getClientNamesFromDeepImport } from "../client-names";
33
import { DYNAMODB } from "../config";
4+
import { ImportType } from "../modules";
45
import { getNodesWithDocClientNamedImportFromDeepPath } from "./getNodesWithDocClientNamedImportFromDeepPath";
56

6-
export const addNotSupportedComments = (j: JSCodeshift, source: Collection<unknown>) => {
7+
export const addNotSupportedComments = (
8+
j: JSCodeshift,
9+
source: Collection<unknown>,
10+
importType: ImportType
11+
) => {
712
const clientNamesFromDeepImport = getClientNamesFromDeepImport(source.toSource());
813

914
if (clientNamesFromDeepImport.includes(DYNAMODB)) {
@@ -14,7 +19,7 @@ export const addNotSupportedComments = (j: JSCodeshift, source: Collection<unkno
1419
j.commentLine(" Please convert to a default import, and re-run aws-sdk-js-codemod."),
1520
];
1621

17-
getNodesWithDocClientNamedImportFromDeepPath(j, source).forEach((node) => {
22+
getNodesWithDocClientNamedImportFromDeepPath(j, source, importType).forEach((node) => {
1823
const comments = node.value.comments || [];
1924
node.value.comments = [...comments, ...documentClientDeepNamedImportUnsupportedComments];
2025
});

src/transforms/v2-to-v3/apis/getNodesWithDocClientNamedImportFromDeepPath.ts

Lines changed: 18 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,17 @@ import {
66
VariableDeclaration,
77
} from "jscodeshift";
88
import { DOCUMENT_CLIENT, DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
9-
import { hasRequire } from "../modules";
9+
import { ImportType } from "../modules";
1010
import { getClientDeepImportPath } from "../utils";
1111

1212
export const getNodesWithDocClientNamedImportFromDeepPath = (
1313
j: JSCodeshift,
14-
source: Collection<unknown>
14+
source: Collection<unknown>,
15+
importType: ImportType
1516
) => {
1617
const deepImportPath = getClientDeepImportPath(DYNAMODB);
17-
if (hasRequire(j, source)) {
18+
19+
if (importType === ImportType.REQUIRE) {
1820
return source
1921
.find(j.VariableDeclarator, {
2022
init: {
@@ -37,18 +39,18 @@ export const getNodesWithDocClientNamedImportFromDeepPath = (
3739
.map(
3840
(variableDeclarator) => variableDeclarator.parentPath.parentPath
3941
) as Collection<VariableDeclaration>;
40-
} else {
41-
return source
42-
.find(j.ImportDeclaration, {
43-
type: "ImportDeclaration",
44-
source: { value: deepImportPath },
45-
})
46-
.filter((importDeclaration) =>
47-
(importDeclaration.value.specifiers || []).some(
48-
(importDeclaration) =>
49-
importDeclaration.type === "ImportSpecifier" &&
50-
importDeclaration.imported.name === DOCUMENT_CLIENT
51-
)
52-
);
5342
}
43+
44+
return source
45+
.find(j.ImportDeclaration, {
46+
type: "ImportDeclaration",
47+
source: { value: deepImportPath },
48+
})
49+
.filter((importDeclaration) =>
50+
(importDeclaration.value.specifiers || []).some(
51+
(importDeclaration) =>
52+
importDeclaration.type === "ImportSpecifier" &&
53+
importDeclaration.imported.name === DOCUMENT_CLIENT
54+
)
55+
);
5456
};
Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,17 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { hasRequire } from "../modules";
3+
import { ImportType } from "../modules";
44
import { getClientNamesFromDeepImport } from "./getClientNamesFromDeepImport";
55
import { getClientNamesRecordFromImport } from "./getClientNamesRecordFromImport";
66
import { getClientNamesRecordFromRequire } from "./getClientNamesRecordFromRequire";
77

8-
export const getClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) => {
8+
export const getClientNamesRecord = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
importType: ImportType
12+
) => {
913
const clientNamesFromDeepImport = getClientNamesFromDeepImport(source.toSource());
10-
return hasRequire(j, source)
14+
return importType === ImportType.REQUIRE
1115
? getClientNamesRecordFromRequire(j, source, clientNamesFromDeepImport)
1216
: getClientNamesRecordFromImport(j, source, clientNamesFromDeepImport);
1317
};

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

Lines changed: 9 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,26 +18,25 @@ import {
1818
import { getV3ClientTypesCount } from "../ts-type";
1919
import { getClientTSTypeRefCount } from "./getClientTSTypeRefCount";
2020
import { getNewExpressionCount } from "./getNewExpressionCount";
21-
import { hasImportEquals } from "./hasImportEquals";
22-
import { hasRequire } from "./hasRequire";
2321

2422
import * as importEqualsModule from "./importEqualsModule";
2523
import * as importModule from "./importModule";
2624
import * as requireModule from "./requireModule";
27-
import { ClientModulesOptions } from "./types";
25+
import { ClientModulesOptions, ImportType } from "./types";
2826

2927
export const addClientModules = (
3028
j: JSCodeshift,
3129
source: Collection<unknown>,
3230
options: ClientModulesOptions
3331
): void => {
34-
const { clientIdentifiers, v2ClientName, v3ClientName, v2ClientLocalName } = options;
35-
36-
const { addClientDefaultModule, addClientNamedModule } = hasRequire(j, source)
37-
? requireModule
38-
: hasImportEquals(j, source)
39-
? importEqualsModule
40-
: importModule;
32+
const { clientIdentifiers, v2ClientName, v3ClientName, v2ClientLocalName, importType } = options;
33+
34+
const { addClientDefaultModule, addClientNamedModule } =
35+
importType === ImportType.REQUIRE
36+
? requireModule
37+
: importType === ImportType.IMPORT_EQUALS
38+
? importEqualsModule
39+
: importModule;
4140

4241
const v3ClientTypesCount = getV3ClientTypesCount(j, source, options);
4342
const newExpressionCount = getNewExpressionCount(j, source, options);

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

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

88
export const getGlobalNameFromModule = (
99
j: JSCodeshift,
10-
source: Collection<unknown>
10+
source: Collection<unknown>,
11+
importType: ImportType
1112
): string | undefined => {
12-
if (hasRequire(j, source)) {
13+
if (importType === ImportType.REQUIRE) {
1314
const requireIdentifiers = source
1415
.find(j.VariableDeclarator, {
1516
id: { type: "Identifier" },
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 { hasImportEquals } from "./hasImportEquals";
3+
import { hasRequire } from "./hasRequire";
4+
import { ImportType } from "./types";
5+
6+
export const getImportType = (j: JSCodeshift, source: Collection<unknown>) =>
7+
hasRequire(j, source)
8+
? ImportType.REQUIRE
9+
: hasImportEquals(j, source)
10+
? ImportType.IMPORT_EQUALS
11+
: ImportType.IMPORT;

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@ export * from "./addClientModules";
22
export * from "./getGlobalNameFromModule";
33
export * from "./getImportEqualsDeclarationType";
44
export * from "./getImportSpecifiers";
5+
export * from "./getImportType";
56
export * from "./getRequireDeclaratorsWithProperty";
6-
export * from "./hasRequire";
77
export * from "./removeClientModule";
88
export * from "./removeGlobalModule";
9+
export * from "./types";

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

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,16 @@ import { Collection, JSCodeshift } from "jscodeshift";
33
import { PACKAGE_NAME } from "../config";
44
import { getClientTypeNames } from "../ts-type";
55
import { getClientDeepImportPath } from "../utils";
6-
import { hasImportEquals } from "./hasImportEquals";
7-
import { hasRequire } from "./hasRequire";
86
import { removeImportDefault } from "./removeImportDefault";
97
import { removeImportEquals } from "./removeImportEquals";
108
import { removeImportNamed } from "./removeImportNamed";
119
import { removeRequireIdentifier } from "./removeRequireIdentifier";
1210
import { removeRequireObjectProperty } from "./removeRequireObjectProperty";
1311
import { removeRequireProperty } from "./removeRequireProperty";
12+
import { ImportType } from "./types";
1413

1514
export interface RemoveClientModuleOptions {
15+
importType: ImportType;
1616
v2ClientName: string;
1717
v2ClientLocalName: string;
1818
v2GlobalName?: string;
@@ -23,17 +23,17 @@ export const removeClientModule = (
2323
source: Collection<unknown>,
2424
options: RemoveClientModuleOptions
2525
) => {
26-
const { v2ClientName, v2ClientLocalName } = options;
26+
const { importType, v2ClientName, v2ClientLocalName } = options;
2727
const deepImportPath = getClientDeepImportPath(v2ClientName);
2828

2929
const defaultOptions = { localName: v2ClientLocalName, sourceValue: deepImportPath };
3030
const namedOptions = { localName: v2ClientLocalName, sourceValue: PACKAGE_NAME };
3131

32-
if (hasRequire(j, source)) {
32+
if (importType === ImportType.REQUIRE) {
3333
removeRequireIdentifier(j, source, defaultOptions);
3434
removeRequireObjectProperty(j, source, namedOptions);
3535
removeRequireProperty(j, source, { ...namedOptions, propertyName: v2ClientName });
36-
} else if (hasImportEquals(j, source)) {
36+
} else if (importType === ImportType.IMPORT_EQUALS) {
3737
removeImportEquals(j, source, defaultOptions);
3838
} else {
3939
removeImportDefault(j, source, defaultOptions);

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

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { PACKAGE_NAME } from "../config";
4-
import { hasImportEquals } from "./hasImportEquals";
5-
import { hasRequire } from "./hasRequire";
64
import { removeImportDefault } from "./removeImportDefault";
75
import { removeImportEquals } from "./removeImportEquals";
86
import { removeRequireIdentifier } from "./removeRequireIdentifier";
7+
import { ImportType } from "./types";
8+
9+
export interface RemoveGlobalModuleOptions {
10+
importType: ImportType;
11+
v2GlobalName?: string;
12+
}
913

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

@@ -20,9 +24,9 @@ export const removeGlobalModule = (
2024
// Only usage is import/require.
2125
if (identifierUsages.size() === 1) {
2226
const defaultOptions = { localName: v2GlobalName, sourceValue: PACKAGE_NAME };
23-
if (hasRequire(j, source)) {
27+
if (importType === ImportType.REQUIRE) {
2428
removeRequireIdentifier(j, source, defaultOptions);
25-
} else if (hasImportEquals(j, source)) {
29+
} else if (importType === ImportType.IMPORT_EQUALS) {
2630
removeImportEquals(j, source, defaultOptions);
2731
} else {
2832
removeImportDefault(j, source, defaultOptions);

0 commit comments

Comments
 (0)