Skip to content

Commit

Permalink
Use formatting convention of trailing comma from input code (#713)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Nov 12, 2023
1 parent 3d1dd7f commit b4b12b3
Show file tree
Hide file tree
Showing 35 changed files with 65 additions and 35 deletions.
5 changes: 5 additions & 0 deletions .changeset/calm-goats-try.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use formatting convention of trailing comma from input code
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,14 @@ const credsBefore = new AWS.Config({
accessKeyId: "KEY",
secretAccessKey: "SECRET",
logger: console,
region: "us-east-1",
region: "us-east-1"
});

const credsInBetween = new AWS.Config({
logger: console,
accessKeyId: "KEY",
secretAccessKey: "SECRET",
region: "us-east-1",
region: "us-east-1"
});

const credsDispersedBefore = new AWS.Config({
Expand All @@ -32,5 +32,5 @@ const credsDispersedAfter = new AWS.Config({
logger: console,
accessKeyId: "KEY",
region: "us-east-1",
secretAccessKey: "SECRET",
secretAccessKey: "SECRET"
});
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ import AWS from "aws-sdk";

const config = new AWS.Config({
httpOptions: {
connectTimeout: 1000,
connectTimeout: 1000
}
});
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ const config = {
// Refer to UPGRADING.md on aws-sdk-js-v3 for changes needed.
// Please create/upvote feature request on aws-sdk-js-codemod for httpOptions.
httpOptions: {
connectTimeout: 1000,
connectTimeout: 1000
}
};
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS from "aws-sdk";

const documentClient = new AWS.DynamoDB.DocumentClient({
convertEmptyValues: true,
convertEmptyValues: true
});
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import AWS from "aws-sdk";

const documentClient = new AWS.DynamoDB.DocumentClient({
wrapNumbers: true,
wrapNumbers: true
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import AWS from "aws-sdk";

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

const client = new DynamoDB({
region: "us-west-2",
});
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ const uploadParams = {
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
};
const uploadOptions = {
partSize: 1024 * 1024 * 5,
queueSize: 1,
leavePartsOnError: true,
tags: [],
tags: []
};

await client.upload(uploadParams, uploadOptions).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ const uploadParams = {
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
};
const uploadOptions = {
partSize: 1024 * 1024 * 5,
queueSize: 1,
leavePartsOnError: true,
tags: [],
tags: []
};

await new Upload({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ await client
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
},
{
partSize: 1024 * 1024 * 5,
queueSize: 1,
leavePartsOnError: true,
tags: [],
tags: []
}
)
.promise();
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
},

partSize: 1024 * 1024 * 5,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ const uploadParams = {
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
};

await client.upload(uploadParams).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const uploadParams = {
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
};

await new Upload({
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
Original file line number Diff line number Diff line change
Expand Up @@ -5,5 +5,5 @@ await client.upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}).promise();
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,6 @@ await new Upload({
Body: "BODY",
Bucket: "Bucket",
ContentType: "ContentType",
Key: "Key",
Key: "Key"
}
}).done();
4 changes: 3 additions & 1 deletion src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ import {
getMostUsedIndentationType,
getMostUsedStringLiteralQuote,
getValueIndentedWithTabs,
isTrailingCommaUsed,
isTypeScriptFile,
} from "./utils";

Expand Down Expand Up @@ -79,6 +80,7 @@ const transformer = async (file: FileInfo, api: API) => {
// Compute recast options before doing transformations
const quote = getMostUsedStringLiteralQuote(j, source);
const useTabs = getMostUsedIndentationType(file.source) === IndentationType.TAB;
const trailingComma = isTrailingCommaUsed(j, source);

const awsGlobalConfig = getAwsGlobalConfig(j, source, v2GlobalName);
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
Expand Down Expand Up @@ -113,7 +115,7 @@ const transformer = async (file: FileInfo, api: API) => {
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, v2GlobalName);

const sourceString = source.toSource({ quote, useTabs });
const sourceString = source.toSource({ quote, useTabs, trailingComma });

if (useTabs) {
// Refs: https://github.com/benjamn/recast/issues/315
Expand Down
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@ export * from "./getClientNewExpression";
export * from "./getMostUsedStringLiteralQuote";
export * from "./getMostUsedIndentationType";
export * from "./getValueIndentedWithTabs";
export * from "./isTrailingCommaUsed";
export * from "./isTypeScriptFile";
12 changes: 12 additions & 0 deletions src/transforms/v2-to-v3/utils/isTrailingCommaUsed.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import { Collection, JSCodeshift } from "jscodeshift";

export const isTrailingCommaUsed = (j: JSCodeshift, source: Collection<unknown>) => {
for (const node of source.find(j.ObjectExpression).nodes()) {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Property 'extra' does not exist on type 'ObjectExpression'.
if (node.extra && node.extra.trailingComma) {
return true;
}
}
return false;
};

0 comments on commit b4b12b3

Please sign in to comment.