Skip to content

Commit

Permalink
Transform AWS.util.array functions (#587)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Sep 15, 2023
1 parent 6ebc9f4 commit bcee631
Show file tree
Hide file tree
Showing 10 changed files with 83 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/silly-lamps-tell.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform AWS.util.array functions
5 changes: 5 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/util/arrayEach.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

AWS.util.arrayEach(["one", "two", "three"], (item, index) => {
console.log({ index, item });
});
3 changes: 3 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/util/arrayEach.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
["one", "two", "three"].forEach((item, index) => {
console.log({ index, item });
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import AWS from "aws-sdk";

const data = ["one", "two", "three"];
const sliceFn = AWS.util.arraySliceFn(data);
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
const data = ["one", "two", "three"];
const sliceFn = data.slice;
26 changes: 26 additions & 0 deletions src/transforms/v2-to-v3/aws-util/getAwsUtilCallExpression.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { Collection, JSCodeshift } from "jscodeshift";

export interface GetAwsUtilCallExpressionOptions {
v2GlobalName: string;
functionName: string;
}

export const getAwsUtilCallExpression = (
j: JSCodeshift,
source: Collection<unknown>,
{ v2GlobalName, functionName }: GetAwsUtilCallExpressionOptions
) =>
source.find(j.CallExpression, {
callee: {
type: "MemberExpression",
object: {
type: "MemberExpression",
object: {
type: "Identifier",
name: v2GlobalName,
},
property: { name: "util" },
},
property: { name: functionName },
},
});
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/aws-util/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from "./replaceAwsUtilFunctions";
24 changes: 24 additions & 0 deletions src/transforms/v2-to-v3/aws-util/replaceAwsUtilArrayFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Collection, FunctionExpression, Identifier, JSCodeshift } from "jscodeshift";
import { getAwsUtilCallExpression } from "./getAwsUtilCallExpression";

export const replaceAwsUtilArrayFunctions = (
j: JSCodeshift,
source: Collection<unknown>,
v2GlobalName: string
) => {
// replace arrayEach
getAwsUtilCallExpression(j, source, { v2GlobalName, functionName: "arrayEach" })
.filter(({ node }) => node.arguments.length === 2)
.replaceWith(({ node }) => {
const array = node.arguments[0] as Identifier;
const iteratee = node.arguments[1] as FunctionExpression;
return j.callExpression(j.memberExpression(array, j.identifier("forEach")), [iteratee]);
});

// replace arraySliceFn
getAwsUtilCallExpression(j, source, { v2GlobalName, functionName: "arraySliceFn" })
.filter(({ node }) => node.arguments.length === 1)
.replaceWith(({ node }) =>
j.memberExpression(node.arguments[0] as Identifier, j.identifier("slice"))
);
};
11 changes: 11 additions & 0 deletions src/transforms/v2-to-v3/aws-util/replaceAwsUtilFunctions.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Collection, JSCodeshift } from "jscodeshift";
import { replaceAwsUtilArrayFunctions } from "./replaceAwsUtilArrayFunctions";

export const replaceAwsUtilFunctions = (
j: JSCodeshift,
source: Collection<unknown>,
v2GlobalName?: string
) => {
if (!v2GlobalName) return;
replaceAwsUtilArrayFunctions(j, source, v2GlobalName);
};
2 changes: 2 additions & 0 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import {
replaceS3GetSignedUrlApi,
getClientIdentifiersRecord,
} from "./apis";
import { replaceAwsUtilFunctions } from "./aws-util";
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
import {
getClientMetadataRecord,
Expand Down Expand Up @@ -88,6 +89,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceClientCreation(j, source, v2Options);
replaceDocClientCreation(j, source, v2Options);
}
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, v2GlobalName);

return source.toSource();
Expand Down

0 comments on commit bcee631

Please sign in to comment.