Skip to content

Commit dd83c82

Browse files
authored
Split getV2DefaultModuleName into import/require components (#71)
1 parent cef55a8 commit dd83c82

File tree

4 files changed

+43
-36
lines changed

4 files changed

+43
-36
lines changed

.changeset/tasty-monkeys-turn.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+
Split getV2DefaultModuleName into import/require components
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+
3+
export const getV2DefaultImportName = (
4+
j: JSCodeshift,
5+
source: Collection<any>
6+
): string | undefined =>
7+
source
8+
.find(j.ImportDeclaration, {
9+
source: { value: "aws-sdk" },
10+
})
11+
.nodes()[0]?.specifiers[0]?.local.name;
Lines changed: 9 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,13 @@
1-
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { containsRequire } from "./containsRequire";
4+
import { getV2DefaultImportName } from "./getV2DefaultImportName";
5+
import { getV2DefaultRequireName } from "./getV2DefaultRequireName";
26

37
export const getV2DefaultModuleName = (
48
j: JSCodeshift,
59
source: Collection<any>
6-
): string | undefined => {
7-
let v2DefaultModuleName = undefined;
8-
9-
// Set specifier name in v2DefaultImportName if it is imported in the source.
10-
source
11-
.find(j.ImportDeclaration, {
12-
source: { value: "aws-sdk" },
13-
})
14-
.forEach((declerationPath) => {
15-
declerationPath.value.specifiers.forEach((specifier) => {
16-
if (
17-
specifier.type === "ImportDefaultSpecifier" ||
18-
specifier.type === "ImportNamespaceSpecifier"
19-
) {
20-
v2DefaultModuleName = specifier.local.name;
21-
}
22-
});
23-
});
24-
25-
// Set specifier name in v2DefaultImportName if it is required in the source.
26-
source
27-
.find(j.VariableDeclarator, {
28-
id: { type: "Identifier" },
29-
init: {
30-
type: "CallExpression",
31-
callee: { type: "Identifier", name: "require" },
32-
arguments: [{ type: "Literal", value: "aws-sdk" }],
33-
},
34-
})
35-
.forEach((declerationPath) => {
36-
v2DefaultModuleName = (declerationPath.value.id as Identifier).name;
37-
});
38-
39-
return v2DefaultModuleName;
40-
};
10+
): string | undefined =>
11+
containsRequire(j, source)
12+
? getV2DefaultRequireName(j, source)
13+
: getV2DefaultImportName(j, source);
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
import { Collection, Identifier, JSCodeshift } from "jscodeshift";
2+
3+
export const getV2DefaultRequireName = (
4+
j: JSCodeshift,
5+
source: Collection<any>
6+
): string | undefined =>
7+
(
8+
source
9+
.find(j.VariableDeclarator, {
10+
id: { type: "Identifier" },
11+
init: {
12+
type: "CallExpression",
13+
callee: { type: "Identifier", name: "require" },
14+
arguments: [{ type: "Literal", value: "aws-sdk" }],
15+
},
16+
})
17+
.nodes()[0]?.id as Identifier
18+
)?.name;

0 commit comments

Comments
 (0)