Skip to content

Commit d8249e8

Browse files
authored
Add utility importModule getImportSpecifiers (#786)
1 parent 10ba465 commit d8249e8

File tree

10 files changed

+93
-67
lines changed

10 files changed

+93
-67
lines changed

.changeset/two-stingrays-reply.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 importModule getImportSpecifiers

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

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1-
import { Collection, Identifier, ImportSpecifier, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift } from "jscodeshift";
22

33
import { CLIENT_NAMES, PACKAGE_NAME } from "../config";
4-
import { getImportEqualsDeclarationType, getImportSpecifiers } from "../modules";
4+
import {
5+
ImportSpecifierDefault,
6+
ImportSpecifierPattern,
7+
getImportEqualsDeclarationType,
8+
} from "../modules";
9+
import { getImportSpecifiers } from "../modules/importModule";
510
import { getClientDeepImportPath } from "../utils";
611

712
export const getClientNamesRecordFromImport = (
@@ -12,12 +17,10 @@ export const getClientNamesRecordFromImport = (
1217
const clientNamesRecord: Record<string, string> = {};
1318

1419
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
15-
(specifier) => specifier?.type === "ImportSpecifier"
16-
) as ImportSpecifier[];
20+
(importSpecifier) => typeof importSpecifier === "object"
21+
) as ImportSpecifierPattern[];
1722

18-
for (const specifier of specifiersFromNamedImport) {
19-
const importedName = specifier.imported.name;
20-
const localName = (specifier.local as Identifier).name;
23+
for (const { importedName, localName } of specifiersFromNamedImport) {
2124
if (CLIENT_NAMES.includes(importedName)) {
2225
clientNamesRecord[importedName] = localName ?? importedName;
2326
}
@@ -27,11 +30,10 @@ export const getClientNamesRecordFromImport = (
2730
const deepImportPath = getClientDeepImportPath(clientName);
2831

2932
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter(
30-
(specifier) =>
31-
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
32-
);
33+
(importSpecifier) => typeof importSpecifier === "string"
34+
) as ImportSpecifierDefault[];
3335
if (specifiersFromDeepImport.length > 0) {
34-
clientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name;
36+
clientNamesRecord[clientName] = specifiersFromDeepImport[0];
3537
}
3638

3739
const identifiersFromImportEquals = source.find(

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ import { Collection, Identifier, JSCodeshift } from "jscodeshift";
22

33
import { PACKAGE_NAME } from "../config";
44
import { getImportEqualsDeclarationType } from "./getImportEqualsDeclarationType";
5-
import { getImportSpecifiers } from "./getImportSpecifiers";
5+
import { getImportSpecifiers } from "./importModule";
66
import { getRequireDeclarators } from "./requireModule";
7+
import { ImportSpecifierDefault } from ".";
78

89
export const getGlobalNameFromModule = (
910
j: JSCodeshift,
@@ -17,12 +18,12 @@ export const getGlobalNameFromModule = (
1718
return (requireIdentifiers[0]?.id as Identifier).name;
1819
}
1920

20-
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter((specifier) =>
21-
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string)
22-
);
21+
const importDefaultSpecifiers = getImportSpecifiers(j, source, PACKAGE_NAME).filter(
22+
(importSpecifier) => typeof importSpecifier === "string"
23+
) as ImportSpecifierDefault[];
2324

2425
if (importDefaultSpecifiers.length > 0) {
25-
return (importDefaultSpecifiers[0]?.local as Identifier).name;
26+
return importDefaultSpecifiers[0];
2627
}
2728

2829
const importEqualsDeclarations = source.find(

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

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

src/transforms/v2-to-v3/modules/importModule/addNamedModule.ts

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import { Collection, ImportSpecifier, JSCodeshift } from "jscodeshift";
1+
import { Collection, JSCodeshift } from "jscodeshift";
22

3-
import { PACKAGE_NAME } from "../../config";
4-
import { getImportSpecifiers } from "../getImportSpecifiers";
3+
import { getImportDeclarations, getImportSpecifiers } from "../importModule";
54
import { importSpecifierCompareFn } from "../importSpecifierCompareFn";
65
import { ModulesOptions } from "../types";
76

@@ -12,28 +11,24 @@ export const addNamedModule = (
1211
) => {
1312
const { importedName, localName = importedName, packageName } = options;
1413

15-
const importDeclarations = source.find(j.ImportDeclaration, {
16-
source: { value: packageName },
17-
});
18-
19-
if (importDeclarations.size()) {
20-
const importSpecifiers = getImportSpecifiers(j, source, packageName);
14+
const importSpecifiers = getImportSpecifiers(j, source, packageName);
2115

16+
if (importSpecifiers.length > 0) {
2217
// Return if the import specifier already exists.
2318
if (
24-
importSpecifiers
25-
.filter((importSpecifier) => importSpecifier?.type === "ImportSpecifier")
26-
.find(
27-
(specifier) =>
28-
(specifier as ImportSpecifier)?.imported?.name === importedName &&
29-
specifier?.local?.name === localName
30-
)
19+
importSpecifiers.find(
20+
(specifier) =>
21+
typeof specifier === "object" &&
22+
specifier.importedName === importedName &&
23+
specifier.localName === localName
24+
)
3125
) {
3226
return;
3327
}
3428

3529
// Add named import to the first import declaration.
36-
const firstImportDeclSpecifiers = importDeclarations.nodes()[0].specifiers;
30+
const firstImportDeclSpecifiers = getImportDeclarations(j, source, packageName).nodes()[0]
31+
.specifiers;
3732
if (firstImportDeclSpecifiers) {
3833
firstImportDeclSpecifiers.push(
3934
j.importSpecifier(j.identifier(importedName), j.identifier(localName))
@@ -49,10 +44,7 @@ export const addNamedModule = (
4944
j.stringLiteral(packageName)
5045
);
5146

52-
const v2importDeclarations = source.find(j.ImportDeclaration).filter((path) => {
53-
const { value } = path.node.source;
54-
return typeof value === "string" && value.startsWith(PACKAGE_NAME);
55-
});
47+
const v2importDeclarations = getImportDeclarations(j, source);
5648

5749
if (v2importDeclarations.size()) {
5850
// Insert it after the last import declaration.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { PACKAGE_NAME } from "../../config";
3+
4+
export const getImportDeclarations = (j: JSCodeshift, source: Collection<unknown>, path?: string) =>
5+
source.find(j.ImportDeclaration).filter((importDeclaration) => {
6+
const sourceValue = importDeclaration.value.source.value;
7+
if (typeof sourceValue !== "string") {
8+
return false;
9+
}
10+
if (path) {
11+
return sourceValue === path;
12+
}
13+
return sourceValue.startsWith(PACKAGE_NAME);
14+
});
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { ImportSpecifierType } from "../types";
3+
import { getImportDeclarations } from "./getImportDeclarations";
4+
5+
export const getImportSpecifiers = (
6+
j: JSCodeshift,
7+
source: Collection<unknown>,
8+
path?: string
9+
): ImportSpecifierType[] => {
10+
const importSpecifiers = new Set<ImportSpecifierType>();
11+
12+
getImportDeclarations(j, source, path).forEach((importDeclaration) => {
13+
const specifiers = importDeclaration.value.specifiers || [];
14+
for (const specifier of specifiers) {
15+
switch (specifier.type) {
16+
case "ImportSpecifier": {
17+
const importedName = specifier.imported.name;
18+
importSpecifiers.add({
19+
importedName,
20+
localName: specifier.local!.name || importedName,
21+
});
22+
break;
23+
}
24+
case "ImportNamespaceSpecifier":
25+
case "ImportDefaultSpecifier": {
26+
if (specifier.local) {
27+
importSpecifiers.add(specifier.local.name);
28+
}
29+
break;
30+
}
31+
}
32+
}
33+
});
34+
35+
return Array.from(importSpecifiers);
36+
};
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,3 @@
11
export * from "./addNamedModule";
2+
export * from "./getImportSpecifiers";
3+
export * from "./getImportDeclarations";

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ export * from "./addClientModules";
22
export * from "./addNamedModule";
33
export * from "./getGlobalNameFromModule";
44
export * from "./getImportEqualsDeclarationType";
5-
export * from "./getImportSpecifiers";
65
export * from "./getImportType";
76
export * from "./getRequireDeclaratorsWithProperty";
87
export * from "./removeClientModule";

src/transforms/v2-to-v3/ts-type/getClientTypeNames.ts

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { Collection, Identifier, JSCodeshift, TSQualifiedName, TSTypeReference } from "jscodeshift";
22

3-
import { getImportSpecifiers } from "../modules";
3+
import { ImportSpecifierPattern } from "../modules";
4+
import { getImportSpecifiers } from "../modules/importModule";
45
import { getClientDeepImportPath } from "../utils";
56

67
export interface GetClientTypeNamesOptions {
@@ -76,13 +77,8 @@ export const getClientTypeNames = (
7677

7778
clientTypeNames.push(
7879
...getImportSpecifiers(j, source, getClientDeepImportPath(v2ClientName))
79-
.filter(
80-
(importSpecifier) =>
81-
importSpecifier.type === "ImportSpecifier" &&
82-
importSpecifier.local &&
83-
importSpecifier.local.type === "Identifier"
84-
)
85-
.map((importSpecifier) => (importSpecifier.local as Identifier).name)
80+
.filter((importSpecifier) => typeof importSpecifier === "object")
81+
.map((importSpecifier) => (importSpecifier as ImportSpecifierPattern).localName!)
8682
);
8783

8884
return [...new Set(clientTypeNames)];

0 commit comments

Comments
 (0)