-
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.
Split getV2ClientModuleNames into import/require components (#72)
- Loading branch information
Showing
4 changed files
with
50 additions
and
39 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 | ||
--- | ||
|
||
Split getV2ClientModuleNames into import/require components |
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,15 @@ | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES } from "./config"; | ||
|
||
export const getV2ClientImportNames = (j: JSCodeshift, source: Collection<any>): string[] => | ||
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`) | ||
.map( | ||
(v2ClientImportSourceValue) => | ||
source | ||
.find(j.ImportDeclaration, { | ||
source: { value: v2ClientImportSourceValue }, | ||
}) | ||
.nodes()[0]?.specifiers[0]?.local.name | ||
) | ||
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined); |
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,41 +1,10 @@ | ||
import { Collection, Identifier, JSCodeshift } from "jscodeshift"; | ||
import { Collection, JSCodeshift } from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES } from "./config"; | ||
import { containsRequire } from "./containsRequire"; | ||
import { getV2ClientImportNames } from "./getV2ClientImportNames"; | ||
import { getV2ClientRequireNames } from "./getV2ClientRequireNames"; | ||
|
||
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => { | ||
const v2ClientModuleNames = []; | ||
|
||
for (const clientName of CLIENT_NAMES) { | ||
// Add specifier name to v2ClientImportNames if it is imported in the source. | ||
source | ||
.find(j.ImportDeclaration, { | ||
source: { value: `aws-sdk/clients/${clientName.toLowerCase()}` }, | ||
}) | ||
.forEach((declerationPath) => { | ||
declerationPath.value.specifiers.forEach((specifier) => { | ||
if ( | ||
specifier.type === "ImportDefaultSpecifier" || | ||
specifier.type === "ImportNamespaceSpecifier" | ||
) { | ||
v2ClientModuleNames.push(specifier.local.name); | ||
} | ||
}); | ||
}); | ||
|
||
// Add specifier name to v2ClientImportNames if it is required in the source. | ||
source | ||
.find(j.VariableDeclarator, { | ||
id: { type: "Identifier" }, | ||
init: { | ||
type: "CallExpression", | ||
callee: { type: "Identifier", name: "require" }, | ||
arguments: [{ type: "Literal", value: `aws-sdk/clients/${clientName.toLowerCase()}` }], | ||
}, | ||
}) | ||
.forEach((declerationPath) => { | ||
v2ClientModuleNames.push((declerationPath.value.id as Identifier).name); | ||
}); | ||
} | ||
|
||
return v2ClientModuleNames; | ||
}; | ||
export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] => | ||
containsRequire(j, source) | ||
? getV2ClientRequireNames(j, source) | ||
: getV2ClientImportNames(j, source); |
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,22 @@ | ||
import { Collection, Identifier, JSCodeshift } from "jscodeshift"; | ||
|
||
import { CLIENT_NAMES } from "./config"; | ||
|
||
export const getV2ClientRequireNames = (j: JSCodeshift, source: Collection<any>): string[] => | ||
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`) | ||
.map( | ||
(v2ClientImportSourceValue) => | ||
( | ||
source | ||
.find(j.VariableDeclarator, { | ||
id: { type: "Identifier" }, | ||
init: { | ||
type: "CallExpression", | ||
callee: { type: "Identifier", name: "require" }, | ||
arguments: [{ type: "Literal", value: v2ClientImportSourceValue }], | ||
}, | ||
}) | ||
.nodes()[0]?.id as Identifier | ||
)?.name | ||
) | ||
.filter((v2ClientImportLocalName) => v2ClientImportLocalName !== undefined); |