Skip to content

Commit

Permalink
Transform explicitly set keys in AWS.config (#669)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Oct 31, 2023
1 parent 35690a1 commit 43b2991
Show file tree
Hide file tree
Showing 14 changed files with 146 additions and 7 deletions.
5 changes: 5 additions & 0 deletions .changeset/old-ears-pull.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Transform explicitly set keys in AWS.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const client = new AWS.DynamoDB({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const client = new DynamoDB({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const client = new AWS.DynamoDB({
logger: console
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const client = new DynamoDB({
logger: console,
region: "us-west-2"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const client = new AWS.DynamoDB();
12 changes: 12 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/config-key/client.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import AWS from "aws-sdk";

import { DynamoDB } from "@aws-sdk/client-dynamodb";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const client = new DynamoDB({
region: "us-west-2"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const client = new AWS.Config({
region: "us-east-1"
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const client = {
region: "us-east-1"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const client = new AWS.Config({
logger: console
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const client = {
logger: console,
region: "us-west-2"
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

AWS.config.region = "us-west-2";

const config = new AWS.Config();
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import AWS from "aws-sdk";

// JS SDK v3 does not support global configuration.
// Codemod has attempted to pass values to each service client in this file.
// You may need to update clients outside of this file, if they use global config.
AWS.config.region = "us-west-2";

const config = {
region: "us-west-2"
};
42 changes: 35 additions & 7 deletions src/transforms/v2-to-v3/client-instances/getAwsGlobalConfig.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,10 @@
import { Collection, JSCodeshift, ObjectExpression } from "jscodeshift";
import { Collection, JSCodeshift, MemberExpression, ObjectExpression } from "jscodeshift";

const getUnsupportedComments = (): string[] => [
" JS SDK v3 does not support global configuration.",
" Codemod has attempted to pass values to each service client in this file.",
" You may need to update clients outside of this file, if they use global config.",
];

export const getAwsGlobalConfig = (
j: JSCodeshift,
Expand All @@ -9,6 +15,7 @@ export const getAwsGlobalConfig = (

if (!v2GlobalName) return objectExpression;

// Get config from AWS.config.update({ ... })
source
.find(j.CallExpression, {
callee: {
Expand All @@ -32,13 +39,34 @@ export const getAwsGlobalConfig = (
});

const comments = node.comments || [];
comments.push(
j.commentLine(" JS SDK v3 does not support global configuration."),
j.commentLine(" Codemod has attempted to pass values to each service client in this file."),
j.commentLine(
" You may need to update clients outside of this file, if they use global config."
)
for (const comment of getUnsupportedComments()) {
comments.push(j.commentLine(comment));
}
node.comments = comments;
});

// Get config from AWS.config.key = value
source
.find(j.AssignmentExpression, {
left: {
type: "MemberExpression",
object: {
type: "MemberExpression",
object: { type: "Identifier", name: v2GlobalName },
property: { type: "Identifier", name: "config" },
},
property: { type: "Identifier" },
},
})
.forEach(({ node }) => {
objectExpression.properties.push(
j.objectProperty((node.left as MemberExpression).property, node.right)
);

const comments = node.comments || [];
for (const comment of getUnsupportedComments()) {
comments.push(j.commentLine(comment));
}
node.comments = comments;
});

Expand Down

0 comments on commit 43b2991

Please sign in to comment.