Skip to content

Commit

Permalink
Add utils get(Require|Import)IdentifierName (#73)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Mar 8, 2022
1 parent d0d459a commit 38a16cc
Show file tree
Hide file tree
Showing 7 changed files with 27 additions and 52 deletions.
5 changes: 5 additions & 0 deletions .changeset/spicy-maps-bow.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Add utils get(Require|Import)IdentifierName
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { Collection, JSCodeshift } from "jscodeshift";

export const getV2DefaultImportName = (
export const getImportIdentifierName = (
j: JSCodeshift,
source: Collection<any>
source: Collection<any>,
literalValue: string
): string | undefined =>
source
.find(j.ImportDeclaration, {
source: { value: "aws-sdk" },
source: { type: "Literal", value: literalValue },
})
.nodes()[0]?.specifiers[0]?.local.name;
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Collection, Identifier, JSCodeshift } from "jscodeshift";

export const getV2DefaultRequireName = (
export const getRequireIdentifierName = (
j: JSCodeshift,
source: Collection<any>
source: Collection<any>,
literalValue: string
): string | undefined =>
(
source
Expand All @@ -11,7 +12,7 @@ export const getV2DefaultRequireName = (
init: {
type: "CallExpression",
callee: { type: "Identifier", name: "require" },
arguments: [{ type: "Literal", value: "aws-sdk" }],
arguments: [{ type: "Literal", value: literalValue }],
},
})
.nodes()[0]?.id as Identifier
Expand Down
15 changes: 0 additions & 15 deletions src/transforms/v2-to-v3/utils/getV2ClientImportNames.ts

This file was deleted.

15 changes: 10 additions & 5 deletions src/transforms/v2-to-v3/utils/getV2ClientModuleNames.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { CLIENT_NAMES } from "./config";
import { containsRequire } from "./containsRequire";
import { getV2ClientImportNames } from "./getV2ClientImportNames";
import { getV2ClientRequireNames } from "./getV2ClientRequireNames";
import { getImportIdentifierName } from "./getImportIdentifierName";
import { getRequireIdentifierName } from "./getRequireIdentifierName";

export const getV2ClientModuleNames = (j: JSCodeshift, source: Collection<any>): string[] =>
containsRequire(j, source)
? getV2ClientRequireNames(j, source)
: getV2ClientImportNames(j, source);
CLIENT_NAMES.map((clientName) => `aws-sdk/clients/${clientName.toLowerCase()}`)
.map((v2ClientLiteralValue) =>
containsRequire(j, source)
? getRequireIdentifierName(j, source, v2ClientLiteralValue)
: getImportIdentifierName(j, source, v2ClientLiteralValue)
)
.filter((v2ClientModuleName) => v2ClientModuleName !== undefined);
22 changes: 0 additions & 22 deletions src/transforms/v2-to-v3/utils/getV2ClientRequireNames.ts

This file was deleted.

8 changes: 4 additions & 4 deletions src/transforms/v2-to-v3/utils/getV2DefaultModuleName.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { Collection, JSCodeshift } from "jscodeshift";

import { containsRequire } from "./containsRequire";
import { getV2DefaultImportName } from "./getV2DefaultImportName";
import { getV2DefaultRequireName } from "./getV2DefaultRequireName";
import { getImportIdentifierName } from "./getImportIdentifierName";
import { getRequireIdentifierName } from "./getRequireIdentifierName";

export const getV2DefaultModuleName = (
j: JSCodeshift,
source: Collection<any>
): string | undefined =>
containsRequire(j, source)
? getV2DefaultRequireName(j, source)
: getV2DefaultImportName(j, source);
? getRequireIdentifierName(j, source, "aws-sdk")
: getImportIdentifierName(j, source, "aws-sdk");

0 comments on commit 38a16cc

Please sign in to comment.