Skip to content

Commit bee398b

Browse files
authored
Add utility requireModule getImportSpecifiers (#781)
1 parent e1d5beb commit bee398b

20 files changed

+172
-139
lines changed

.changeset/brave-hounds-do.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+
Add utility requireModule getImportSpecifiers

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

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "jscodeshift";
88
import { DOCUMENT_CLIENT, DYNAMODB, OBJECT_PROPERTY_TYPE_LIST } from "../config";
99
import { ImportType } from "../modules";
10+
import { getRequireDeclarators } from "../modules/requireModule";
1011
import { getClientDeepImportPath } from "../utils";
1112

1213
export const getNodesWithDocClientNamedImportFromDeepPath = (
@@ -17,14 +18,7 @@ export const getNodesWithDocClientNamedImportFromDeepPath = (
1718
const deepImportPath = getClientDeepImportPath(DYNAMODB);
1819

1920
if (importType === ImportType.REQUIRE) {
20-
return source
21-
.find(j.VariableDeclarator, {
22-
init: {
23-
type: "CallExpression",
24-
callee: { type: "Identifier", name: "require" },
25-
arguments: [{ value: deepImportPath }],
26-
},
27-
})
21+
return getRequireDeclarators(j, source, deepImportPath)
2822
.filter(
2923
(variableDeclarator) =>
3024
variableDeclarator.value.id.type === "ObjectPattern" &&

src/transforms/v2-to-v3/client-names/getClientNamesRecordFromRequire.ts

Lines changed: 14 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,9 @@
1-
import {
2-
Collection,
3-
Identifier,
4-
JSCodeshift,
5-
ObjectPattern,
6-
ObjectProperty,
7-
Property,
8-
} from "jscodeshift";
1+
import { Collection, JSCodeshift } from "jscodeshift";
92

10-
import { CLIENT_NAMES, OBJECT_PROPERTY_TYPE_LIST, PACKAGE_NAME } from "../config";
11-
import { getRequireDeclaratorsWithProperty } from "../modules";
3+
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4+
import { ImportSpecifierDefault, ImportSpecifierPattern } from "../modules";
5+
import { getImportSpecifiers } from "../modules/requireModule";
126
import { getClientDeepImportPath } from "../utils";
13-
import { getRequireIds } from "./getRequireIds";
147

158
export const getClientNamesRecordFromRequire = (
169
j: JSCodeshift,
@@ -19,51 +12,23 @@ export const getClientNamesRecordFromRequire = (
1912
) => {
2013
const clientNamesRecord: Record<string, string> = {};
2114

22-
const idPropertiesFromObjectPattern = getRequireIds(j, source, PACKAGE_NAME)
23-
.filter((id) => id.type === "ObjectPattern")
24-
.map((objectPattern) => (objectPattern as ObjectPattern).properties)
25-
.flat();
15+
const idPropertiesFromObjectPattern = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
16+
(importSpecifier) => typeof importSpecifier === "object"
17+
) as ImportSpecifierPattern[];
2618

27-
for (const idProperty of idPropertiesFromObjectPattern) {
28-
if (!OBJECT_PROPERTY_TYPE_LIST.includes(idProperty.type)) {
29-
continue;
30-
}
31-
const key = (idProperty as Property | ObjectProperty).key;
32-
const value = (idProperty as Property | ObjectProperty).value;
33-
if (key.type !== "Identifier" || value.type !== "Identifier") {
34-
continue;
35-
}
36-
if (CLIENT_NAMES.includes(key.name)) {
37-
clientNamesRecord[key.name] = value.name;
38-
}
39-
}
40-
41-
const declaratorsWithProperty = getRequireDeclaratorsWithProperty(j, source, {
42-
sourceValue: PACKAGE_NAME,
43-
}).nodes();
44-
45-
for (const declaratorWithProperty of declaratorsWithProperty) {
46-
const { id, init } = declaratorWithProperty;
47-
if (
48-
id.type === "Identifier" &&
49-
init != undefined &&
50-
init.type === "MemberExpression" &&
51-
init.property.type === "Identifier"
52-
) {
53-
const clientName = (init.property as Identifier).name;
54-
if (CLIENT_NAMES.includes(clientName)) {
55-
clientNamesRecord[clientName] = (id as Identifier).name;
56-
}
19+
for (const { importedName, localName } of idPropertiesFromObjectPattern) {
20+
if (CLIENT_NAMES.includes(importedName)) {
21+
clientNamesRecord[importedName] = localName || importedName;
5722
}
5823
}
5924

6025
for (const clientName of clientNamesFromDeepImport) {
6126
const deepImportPath = getClientDeepImportPath(clientName);
62-
const idsFromDefaultImport = getRequireIds(j, source, deepImportPath).filter(
63-
(id) => id.type === "Identifier"
64-
);
27+
const idsFromDefaultImport = getImportSpecifiers(j, source, deepImportPath).filter(
28+
(importSpecifier) => typeof importSpecifier === "string"
29+
) as ImportSpecifierDefault[];
6530
if (idsFromDefaultImport.length) {
66-
clientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name;
31+
clientNamesRecord[clientName] = idsFromDefaultImport[0];
6732
}
6833
}
6934

src/transforms/v2-to-v3/client-names/getRequireIds.ts

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

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

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,20 +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 { getRequireDeclarators } from "./requireModule";
67

78
export const getGlobalNameFromModule = (
89
j: JSCodeshift,
910
source: Collection<unknown>
1011
): string | undefined => {
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-
})
12+
const requireIdentifiers = getRequireDeclarators(j, source, PACKAGE_NAME)
13+
.filter((declarator) => declarator.value.id.type === "Identifier")
2014
.nodes();
2115

2216
if (requireIdentifiers.length > 0) {

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

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

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

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

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

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import { Collection, JSCodeshift } from "jscodeshift";
2-
3-
import { getRequireDeclarators } from "./getRequireDeclarators";
2+
import { getRequireDeclarators } from "./requireModule";
43

54
export interface GetRequireDeclaratorsWithIdentifier {
65
identifierName: string;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { Collection, JSCodeshift, ObjectProperty, Property } from "jscodeshift";
22

33
import { OBJECT_PROPERTY_TYPE_LIST } from "../config";
4-
import { getRequireDeclarators } from "./getRequireDeclarators";
4+
import { getRequireDeclarators } from "./requireModule";
55

66
export interface GetRequireDeclaratorsWithObjectPattern {
77
identifierName: string;
Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
import { Collection, JSCodeshift } from "jscodeshift";
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
2+
import { getRequireDeclarators } from "./requireModule";
23

34
export interface GetRequireDeclaratorsWithPropertyOptions {
45
localName?: string;
@@ -11,15 +12,19 @@ export const getRequireDeclaratorsWithProperty = (
1112
source: Collection<unknown>,
1213
{ localName, identifierName, sourceValue }: GetRequireDeclaratorsWithPropertyOptions
1314
) =>
14-
source.find(j.VariableDeclarator, {
15-
id: { type: "Identifier", ...(localName && { name: localName }) },
16-
init: {
17-
type: "MemberExpression",
18-
object: {
19-
type: "CallExpression",
20-
callee: { type: "Identifier", name: "require" },
21-
arguments: [{ value: sourceValue }],
22-
},
23-
property: { type: "Identifier", ...(identifierName && { name: identifierName }) },
24-
},
15+
getRequireDeclarators(j, source, sourceValue).filter((varDeclarator) => {
16+
const declaratorId = varDeclarator.value.id;
17+
const declaratorInit = varDeclarator.value.init;
18+
19+
if (declaratorId.type === "Identifier") {
20+
const declaratorIdName = declaratorId.name;
21+
if (declaratorInit!.type === "MemberExpression") {
22+
const importedName = (declaratorInit.property as Identifier).name;
23+
if (localName === declaratorIdName && identifierName === importedName) {
24+
return true;
25+
}
26+
}
27+
}
28+
29+
return false;
2530
});

0 commit comments

Comments
 (0)