Skip to content

Commit

Permalink
Use formatting conventions of tabs or spaces from input code (#710)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr committed Nov 11, 2023
1 parent c2ee5d1 commit 0d9e469
Show file tree
Hide file tree
Showing 6 changed files with 52 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/wicked-lions-happen.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use formatting conventions of tabs or spaces from input code
7 changes: 7 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/misc/tabs.input.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import AWS from "aws-sdk";

// Client creation with tabs instead of spaces for indentation.
const client = new AWS.DynamoDB({
accessKeyId: "KEY",
secretAccessKey: "SECRET"
});
9 changes: 9 additions & 0 deletions src/transforms/v2-to-v3/__fixtures__/misc/tabs.output.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import { DynamoDB } from "@aws-sdk/client-dynamodb";

// Client creation with tabs instead of spaces for indentation.
const client = new DynamoDB({
credentials: {
accessKeyId: "KEY",
secretAccessKey: "SECRET"
}
});
17 changes: 15 additions & 2 deletions src/transforms/v2-to-v3/transformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import {
removeGlobalModule,
} from "./modules";
import { replaceTSTypeReference } from "./ts-type";
import { getMostUsedStringLiteralQuote, isTypeScriptFile } from "./utils";
import {
IndentationType,
getMostUsedIndentationType,
getMostUsedStringLiteralQuote,
isTypeScriptFile,
} from "./utils";

const transformer = async (file: FileInfo, api: API) => {
const j = isTypeScriptFile(file.path) ? api.jscodeshift.withParser("ts") : api.jscodeshift;
Expand Down Expand Up @@ -72,6 +77,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 awsGlobalConfig = getAwsGlobalConfig(j, source, v2GlobalName);
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
Expand Down Expand Up @@ -106,7 +112,14 @@ const transformer = async (file: FileInfo, api: API) => {
replaceAwsUtilFunctions(j, source, v2GlobalName);
removeGlobalModule(j, source, v2GlobalName);

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

if (useTabs) {
// Refs: https://github.com/benjamn/recast/issues/315
return sourceString.replace(/ {4,}/g, "\t");
}

return sourceString;
};

export default transformer;
Expand Down
15 changes: 15 additions & 0 deletions src/transforms/v2-to-v3/utils/getMostUsedIndentationType.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
export enum IndentationType {
TAB = "tab",
SPACE = "space",
}

export const getMostUsedIndentationType = (source: string) => {
const tabCount = (source.match(/\t/g) || []).length;
const spaceCount = (source.match(/ {2}/g) || []).length;
console.log({ tabCount, spaceCount });

if (tabCount > spaceCount) {
return IndentationType.TAB;
}
return IndentationType.SPACE;
};
1 change: 1 addition & 0 deletions src/transforms/v2-to-v3/utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from "./getClientDeepImportPath";
export * from "./getClientNewExpression";
export * from "./getMostUsedStringLiteralQuote";
export * from "./getMostUsedIndentationType";
export * from "./isTypeScriptFile";

0 comments on commit 0d9e469

Please sign in to comment.