Skip to content

Commit 4947b8c

Browse files
authored
Add utility getV2ImportDeclaration (#305)
1 parent e64574d commit 4947b8c

File tree

3 files changed

+96
-36
lines changed

3 files changed

+96
-36
lines changed

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

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
11
import { Collection, ImportDefaultSpecifier, JSCodeshift } from "jscodeshift";
22

33
import { getV3ClientDefaultLocalName } from "../utils";
4+
import { getV2ImportDeclaration } from "./getV2ImportDeclaration";
45
import { V3ClientModulesOptions } from "./types";
56

67
export const addV3ClientDefaultImport = (
78
j: JSCodeshift,
89
source: Collection<unknown>,
9-
{ v2ClientLocalName, v3ClientPackageName }: V3ClientModulesOptions
10+
{ v2ClientLocalName, v2ClientName, v3ClientPackageName }: V3ClientModulesOptions
1011
) => {
1112
const localName = getV3ClientDefaultLocalName(v2ClientLocalName);
13+
const defaultImportSpecifier = j.importDefaultSpecifier(j.identifier(localName));
1214
const existingImports = source.find(j.ImportDeclaration, {
1315
source: { value: v3ClientPackageName },
1416
});
@@ -21,7 +23,33 @@ export const addV3ClientDefaultImport = (
2123
(importSpecifier) => importSpecifier?.type === "ImportDefaultSpecifier"
2224
) as ImportDefaultSpecifier[];
2325

24-
if (!existingImportDefaultSpecifiers.find((specifier) => specifier?.local?.name === localName)) {
25-
existingImports.nodes()[0].specifiers?.push(j.importDefaultSpecifier(j.identifier(localName)));
26+
if (existingImports.length) {
27+
if (
28+
!existingImportDefaultSpecifiers.find((specifier) => specifier?.local?.name === localName)
29+
) {
30+
existingImports.nodes()[0].specifiers?.push(defaultImportSpecifier);
31+
return;
32+
}
33+
}
34+
35+
// Insert after global import, or service import.
36+
const v2ImportDeclaration = getV2ImportDeclaration(j, source, {
37+
v2ClientName,
38+
v2ClientLocalName,
39+
});
40+
41+
const importDeclaration = j.importDeclaration(
42+
[defaultImportSpecifier],
43+
j.stringLiteral(v3ClientPackageName)
44+
);
45+
46+
if (v2ImportDeclaration && v2ImportDeclaration.nodes().length > 0) {
47+
v2ImportDeclaration.at(0).insertAfter(importDeclaration);
48+
} else {
49+
// Unreachable code, throw error
50+
throw new Error(
51+
"Base Import Declaration not found to insert new Import Declaration.\n" +
52+
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod"
53+
);
2654
}
2755
};
Lines changed: 22 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
import { Collection, ImportSpecifier, JSCodeshift } from "jscodeshift";
22

3-
import { PACKAGE_NAME } from "../config";
4-
import { getV2ServiceModulePath } from "../utils";
3+
import { getV2ImportDeclaration } from "./getV2ImportDeclaration";
54
import { getV3ClientImportSpecifier } from "./getV3ClientImportSpecifier";
65
import { V3ClientModulesOptions } from "./types";
76

@@ -41,38 +40,28 @@ export const addV3ClientNamedImport = (
4140
}
4241

4342
// Insert after global import, or service import.
44-
source
45-
.find(j.ImportDeclaration)
46-
.filter((importDeclaration) => {
47-
const sourceValue = importDeclaration.value.source.value as string;
48-
49-
if (
50-
sourceValue === PACKAGE_NAME &&
51-
importDeclaration.value.specifiers?.some(
52-
(specifier) =>
53-
["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) ||
54-
(specifier.type === "ImportSpecifier" && specifier.local?.name === v2ClientLocalName)
55-
)
56-
) {
57-
return true;
58-
}
43+
const v2ImportDeclaration = getV2ImportDeclaration(j, source, {
44+
v2ClientName,
45+
v2ClientLocalName,
46+
});
5947

60-
if (sourceValue === getV2ServiceModulePath(v2ClientName)) {
61-
return true;
62-
}
48+
const importDeclaration = j.importDeclaration(
49+
[
50+
getV3ClientImportSpecifier(j, {
51+
localName: v2ClientLocalName,
52+
importedName: v3ClientName,
53+
}),
54+
],
55+
j.stringLiteral(v3ClientPackageName)
56+
);
6357

64-
return false;
65-
})
66-
.at(0)
67-
.insertAfter(
68-
j.importDeclaration(
69-
[
70-
getV3ClientImportSpecifier(j, {
71-
localName: v2ClientLocalName,
72-
importedName: v3ClientName,
73-
}),
74-
],
75-
j.stringLiteral(v3ClientPackageName)
76-
)
58+
if (v2ImportDeclaration && v2ImportDeclaration.nodes().length > 0) {
59+
v2ImportDeclaration.at(0).insertAfter(importDeclaration);
60+
} else {
61+
// Unreachable code, throw error
62+
throw new Error(
63+
"Base Import Declaration not found to insert new Import Declaration.\n" +
64+
"Please report your use case on https://github.com/awslabs/aws-sdk-js-codemod"
7765
);
66+
}
7867
};
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
import { PACKAGE_NAME } from "../config";
4+
import { getV2ServiceModulePath } from "../utils";
5+
6+
export interface GetV2ImportDeclarationOptions {
7+
v2ClientName: string;
8+
v2ClientLocalName: string;
9+
}
10+
11+
export const getV2ImportDeclaration = (
12+
j: JSCodeshift,
13+
source: Collection<unknown>,
14+
{ v2ClientName, v2ClientLocalName }: GetV2ImportDeclarationOptions
15+
) =>
16+
// Return global or service import declaration.
17+
source.find(j.ImportDeclaration).filter((importDeclaration) => {
18+
const sourceValue = importDeclaration.value.source.value as string;
19+
20+
if (
21+
sourceValue === PACKAGE_NAME &&
22+
importDeclaration.value.specifiers?.some(
23+
(specifier) =>
24+
["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) ||
25+
(specifier.type === "ImportSpecifier" && specifier.local?.name === v2ClientLocalName)
26+
)
27+
) {
28+
return true;
29+
}
30+
31+
if (
32+
sourceValue === getV2ServiceModulePath(v2ClientName) &&
33+
importDeclaration.value.specifiers?.some(
34+
(specifier) =>
35+
["ImportNamespaceSpecifier", "ImportDefaultSpecifier"].includes(specifier.type) &&
36+
specifier.local?.name === v2ClientLocalName
37+
)
38+
) {
39+
return true;
40+
}
41+
42+
return false;
43+
});

0 commit comments

Comments
 (0)