-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Transform AWS.util.array functions (#587)
- Loading branch information
Showing
10 changed files
with
83 additions
and
0 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 | ||
--- | ||
|
||
Transform AWS.util.array functions |
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 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
AWS.util.arrayEach(["one", "two", "three"], (item, index) => { | ||
console.log({ index, item }); | ||
}); |
3 changes: 3 additions & 0 deletions
3
src/transforms/v2-to-v3/__fixtures__/util/arrayEach.output.js
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,3 @@ | ||
["one", "two", "three"].forEach((item, index) => { | ||
console.log({ index, item }); | ||
}); |
4 changes: 4 additions & 0 deletions
4
src/transforms/v2-to-v3/__fixtures__/util/arraySliceFn.input.js
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,4 @@ | ||
import AWS from "aws-sdk"; | ||
|
||
const data = ["one", "two", "three"]; | ||
const sliceFn = AWS.util.arraySliceFn(data); |
2 changes: 2 additions & 0 deletions
2
src/transforms/v2-to-v3/__fixtures__/util/arraySliceFn.output.js
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,2 @@ | ||
const data = ["one", "two", "three"]; | ||
const sliceFn = data.slice; |
26 changes: 26 additions & 0 deletions
26
src/transforms/v2-to-v3/aws-util/getAwsUtilCallExpression.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,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 }, | ||
}, | ||
}); |
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 @@ | ||
export * from "./replaceAwsUtilFunctions"; |
24 changes: 24 additions & 0 deletions
24
src/transforms/v2-to-v3/aws-util/replaceAwsUtilArrayFunctions.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,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
11
src/transforms/v2-to-v3/aws-util/replaceAwsUtilFunctions.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,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); | ||
}; |
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