|
1 | 1 | import { Collection, JSCodeshift } from "jscodeshift";
|
2 | 2 |
|
| 3 | +import { CLIENT_NAMES, PACKAGE_NAME } from "../config"; |
3 | 4 | import { ImportType } from "../modules";
|
| 5 | +import * as importEqualsModule from "../modules/importEqualsModule"; |
| 6 | +import * as importModule from "../modules/importModule"; |
| 7 | +import * as requireModule from "../modules/requireModule"; |
| 8 | +import { getClientDeepImportPath } from "../utils"; |
4 | 9 | import { getClientNamesFromDeepImport } from "./getClientNamesFromDeepImport";
|
5 |
| -import { getClientNamesRecordFromImport } from "./getClientNamesRecordFromImport"; |
6 |
| -import { getClientNamesRecordFromRequire } from "./getClientNamesRecordFromRequire"; |
7 | 10 |
|
8 | 11 | export const getClientNamesRecord = (
|
9 | 12 | j: JSCodeshift,
|
10 | 13 | source: Collection<unknown>,
|
11 | 14 | importType: ImportType
|
12 | 15 | ) => {
|
13 |
| - const clientNamesFromDeepImport = getClientNamesFromDeepImport(source.toSource()); |
| 16 | + const clientNamesRecord: Record<string, string> = {}; |
14 | 17 |
|
15 |
| - const clientNamesRecord = |
| 18 | + const { getImportSpecifiers } = |
16 | 19 | importType === ImportType.REQUIRE
|
17 |
| - ? getClientNamesRecordFromRequire(j, source, clientNamesFromDeepImport) |
18 |
| - : getClientNamesRecordFromImport(j, source, clientNamesFromDeepImport); |
| 20 | + ? requireModule |
| 21 | + : importType === ImportType.IMPORT_EQUALS |
| 22 | + ? importEqualsModule |
| 23 | + : importModule; |
| 24 | + |
| 25 | + const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter( |
| 26 | + (importSpecifier) => importSpecifier.importedName |
| 27 | + ); |
| 28 | + |
| 29 | + for (const { importedName, localName } of specifiersFromNamedImport) { |
| 30 | + const clientName = importedName ?? localName; |
| 31 | + if (CLIENT_NAMES.includes(clientName)) { |
| 32 | + clientNamesRecord[clientName] = localName; |
| 33 | + } |
| 34 | + } |
| 35 | + |
| 36 | + const clientNamesFromDeepImport = getClientNamesFromDeepImport(source.toSource()); |
| 37 | + for (const clientName of clientNamesFromDeepImport) { |
| 38 | + const deepImportPath = getClientDeepImportPath(clientName); |
| 39 | + |
| 40 | + const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter( |
| 41 | + (importSpecifier) => !importSpecifier.importedName |
| 42 | + ); |
| 43 | + if (specifiersFromDeepImport.length > 0) { |
| 44 | + clientNamesRecord[clientName] = specifiersFromDeepImport[0].localName; |
| 45 | + } |
| 46 | + } |
19 | 47 |
|
20 | 48 | // Populate client names for type transformations
|
21 | 49 | // Ref: https://github.com/aws/aws-sdk-js-codemod/issues/663
|
|
0 commit comments