Skip to content

Commit bcee631

Browse files
authored
Transform AWS.util.array functions (#587)
1 parent 6ebc9f4 commit bcee631

File tree

10 files changed

+83
-0
lines changed

10 files changed

+83
-0
lines changed

.changeset/silly-lamps-tell.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"aws-sdk-js-codemod": patch
3+
---
4+
5+
Transform AWS.util.array functions
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import AWS from "aws-sdk";
2+
3+
AWS.util.arrayEach(["one", "two", "three"], (item, index) => {
4+
console.log({ index, item });
5+
});
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
["one", "two", "three"].forEach((item, index) => {
2+
console.log({ index, item });
3+
});
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import AWS from "aws-sdk";
2+
3+
const data = ["one", "two", "three"];
4+
const sliceFn = AWS.util.arraySliceFn(data);
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
const data = ["one", "two", "three"];
2+
const sliceFn = data.slice;
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export interface GetAwsUtilCallExpressionOptions {
4+
v2GlobalName: string;
5+
functionName: string;
6+
}
7+
8+
export const getAwsUtilCallExpression = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>,
11+
{ v2GlobalName, functionName }: GetAwsUtilCallExpressionOptions
12+
) =>
13+
source.find(j.CallExpression, {
14+
callee: {
15+
type: "MemberExpression",
16+
object: {
17+
type: "MemberExpression",
18+
object: {
19+
type: "Identifier",
20+
name: v2GlobalName,
21+
},
22+
property: { name: "util" },
23+
},
24+
property: { name: functionName },
25+
},
26+
});
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export * from "./replaceAwsUtilFunctions";
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
import { Collection, FunctionExpression, Identifier, JSCodeshift } from "jscodeshift";
2+
import { getAwsUtilCallExpression } from "./getAwsUtilCallExpression";
3+
4+
export const replaceAwsUtilArrayFunctions = (
5+
j: JSCodeshift,
6+
source: Collection<unknown>,
7+
v2GlobalName: string
8+
) => {
9+
// replace arrayEach
10+
getAwsUtilCallExpression(j, source, { v2GlobalName, functionName: "arrayEach" })
11+
.filter(({ node }) => node.arguments.length === 2)
12+
.replaceWith(({ node }) => {
13+
const array = node.arguments[0] as Identifier;
14+
const iteratee = node.arguments[1] as FunctionExpression;
15+
return j.callExpression(j.memberExpression(array, j.identifier("forEach")), [iteratee]);
16+
});
17+
18+
// replace arraySliceFn
19+
getAwsUtilCallExpression(j, source, { v2GlobalName, functionName: "arraySliceFn" })
20+
.filter(({ node }) => node.arguments.length === 1)
21+
.replaceWith(({ node }) =>
22+
j.memberExpression(node.arguments[0] as Identifier, j.identifier("slice"))
23+
);
24+
};
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
import { replaceAwsUtilArrayFunctions } from "./replaceAwsUtilArrayFunctions";
3+
4+
export const replaceAwsUtilFunctions = (
5+
j: JSCodeshift,
6+
source: Collection<unknown>,
7+
v2GlobalName?: string
8+
) => {
9+
if (!v2GlobalName) return;
10+
replaceAwsUtilArrayFunctions(j, source, v2GlobalName);
11+
};

src/transforms/v2-to-v3/transformer.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import {
99
replaceS3GetSignedUrlApi,
1010
getClientIdentifiersRecord,
1111
} from "./apis";
12+
import { replaceAwsUtilFunctions } from "./aws-util";
1213
import { replaceClientCreation, replaceDocClientCreation } from "./client-instances";
1314
import {
1415
getClientMetadataRecord,
@@ -88,6 +89,7 @@ const transformer = async (file: FileInfo, api: API) => {
8889
replaceClientCreation(j, source, v2Options);
8990
replaceDocClientCreation(j, source, v2Options);
9091
}
92+
replaceAwsUtilFunctions(j, source, v2GlobalName);
9193
removeGlobalModule(j, source, v2GlobalName);
9294

9395
return source.toSource();

0 commit comments

Comments
 (0)