-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Reduce number of calls to source API in getV2ClientNamesRecord (#260)
- Loading branch information
Showing
6 changed files
with
109 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"aws-sdk-js-codemod": patch | ||
--- | ||
|
||
Reduce number of calls to source API in getV2ClientNamesRecord |
43 changes: 0 additions & 43 deletions
43
src/transforms/v2-to-v3/utils/get/getImportLocalNameForClient.ts
This file was deleted.
Oops, something went wrong.
42 changes: 0 additions & 42 deletions
42
src/transforms/v2-to-v3/utils/get/getRequireLocalNameForClient.ts
This file was deleted.
Oops, something went wrong.
24 changes: 6 additions & 18 deletions
24
src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecord.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,22 +1,10 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES } from "../config"; | ||
import { hasRequire } from "../has"; | ||
import { getImportLocalNameForClient } from "./getImportLocalNameForClient"; | ||
import { getRequireLocalNameForClient } from "./getRequireLocalNameForClient"; | ||
import { getV2ClientNamesRecordFromImport } from "./getV2ClientNamesRecordFromImport"; | ||
import { getV2ClientNamesRecordFromRequire } from "./getV2ClientNamesRecordFromRequire"; | ||
|
||
export const getV2ClientNamesRecord = ( | ||
j: JSCodeshift, | ||
source: Collection<unknown> | ||
): Record<string, string> => { | ||
const getIdentifierNameFn = hasRequire(j, source) | ||
? getRequireLocalNameForClient | ||
: getImportLocalNameForClient; | ||
|
||
return Object.fromEntries( | ||
CLIENT_NAMES.map((clientName) => [ | ||
clientName, | ||
getIdentifierNameFn(j, source, clientName), | ||
]).filter(([, v2ClientLocalName]) => v2ClientLocalName !== undefined) | ||
); | ||
}; | ||
export const getV2ClientNamesRecord = (j: JSCodeshift, source: Collection<unknown>) => | ||
hasRequire(j, source) | ||
? getV2ClientNamesRecordFromRequire(j, source) | ||
: getV2ClientNamesRecordFromImport(j, source); |
52 changes: 52 additions & 0 deletions
52
src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromImport.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import { | ||
Collection, | ||
Identifier, | ||
ImportDefaultSpecifier, | ||
ImportNamespaceSpecifier, | ||
ImportSpecifier, | ||
JSCodeshift, | ||
} from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES, PACKAGE_NAME } from "../config"; | ||
import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; | ||
|
||
const getImportSpecifiers = (j: JSCodeshift, source: Collection<unknown>, sourceValue: string) => | ||
source | ||
.find(j.ImportDeclaration, { | ||
type: "ImportDeclaration", | ||
source: { value: sourceValue }, | ||
}) | ||
.nodes() | ||
.map((importDeclaration) => importDeclaration.specifiers) | ||
.flat() as (ImportSpecifier | ImportDefaultSpecifier | ImportNamespaceSpecifier)[]; | ||
|
||
export const getV2ClientNamesRecordFromImport = (j: JSCodeshift, source: Collection<unknown>) => { | ||
const v2ClientNamesRecord: Record<string, string> = {}; | ||
|
||
const specifiersFromNamedImport = getImportSpecifiers(j, source, PACKAGE_NAME).filter( | ||
(specifier) => specifier?.type === "ImportSpecifier" | ||
) as ImportSpecifier[]; | ||
|
||
for (const clientName of CLIENT_NAMES) { | ||
const clientImportSpecifier = specifiersFromNamedImport.find( | ||
(specifier) => specifier?.imported.name === clientName | ||
); | ||
|
||
if (clientImportSpecifier) { | ||
v2ClientNamesRecord[clientName] = (clientImportSpecifier.local as Identifier).name; | ||
continue; | ||
} | ||
|
||
const deepImportPath = getV2ServiceModulePath(clientName); | ||
const specifiersFromDeepImport = getImportSpecifiers(j, source, deepImportPath).filter( | ||
(specifier) => | ||
["ImportDefaultSpecifier", "ImportNamespaceSpecifier"].includes(specifier?.type as string) | ||
); | ||
|
||
if (specifiersFromDeepImport.length > 0) { | ||
v2ClientNamesRecord[clientName] = (specifiersFromDeepImport[0]?.local as Identifier).name; | ||
} | ||
} | ||
|
||
return v2ClientNamesRecord; | ||
}; |
46 changes: 46 additions & 0 deletions
46
src/transforms/v2-to-v3/utils/get/getV2ClientNamesRecordFromRequire.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,46 @@ | ||
import { Collection, Identifier, JSCodeshift, ObjectPattern, Property } from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES, PACKAGE_NAME } from "../config"; | ||
import { getV2ServiceModulePath } from "./getV2ServiceModulePath"; | ||
|
||
const getRequireIds = (j: JSCodeshift, source: Collection<unknown>, sourceValue: string) => | ||
source | ||
.find(j.VariableDeclarator, { | ||
init: { | ||
type: "CallExpression", | ||
callee: { type: "Identifier", name: "require" }, | ||
arguments: [{ value: sourceValue }], | ||
}, | ||
}) | ||
.nodes() | ||
.map((variableDeclarator) => variableDeclarator.id); | ||
|
||
export const getV2ClientNamesRecordFromRequire = (j: JSCodeshift, source: Collection<unknown>) => { | ||
const v2ClientNamesRecord: Record<string, string> = {}; | ||
|
||
const idPropertiesFromNamedImport = getRequireIds(j, source, PACKAGE_NAME) | ||
.filter((id) => id.type === "ObjectPattern") | ||
.map((objectPattern) => (objectPattern as ObjectPattern).properties) | ||
.flat() as Property[]; | ||
|
||
for (const clientName of CLIENT_NAMES) { | ||
const propertyWithClientName = idPropertiesFromNamedImport.find( | ||
(property) => (property?.key as Identifier).name === clientName | ||
); | ||
|
||
if (propertyWithClientName) { | ||
v2ClientNamesRecord[clientName] = (propertyWithClientName.value as Identifier).name; | ||
continue; | ||
} | ||
|
||
const deepRequirePath = getV2ServiceModulePath(clientName); | ||
const idsFromDefaultImport = getRequireIds(j, source, deepRequirePath).filter( | ||
(id) => id.type === "Identifier" | ||
); | ||
if (idsFromDefaultImport.length) { | ||
v2ClientNamesRecord[clientName] = (idsFromDefaultImport[0] as Identifier).name; | ||
} | ||
} | ||
|
||
return v2ClientNamesRecord; | ||
}; |