Skip to content

Commit 0d9e469

Browse files
authored
Use formatting conventions of tabs or spaces from input code (#710)
1 parent c2ee5d1 commit 0d9e469

File tree

6 files changed

+52
-2
lines changed

6 files changed

+52
-2
lines changed

.changeset/wicked-lions-happen.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+
Use formatting conventions of tabs or spaces from input code
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import AWS from "aws-sdk";
2+
3+
// Client creation with tabs instead of spaces for indentation.
4+
const client = new AWS.DynamoDB({
5+
accessKeyId: "KEY",
6+
secretAccessKey: "SECRET"
7+
});
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { DynamoDB } from "@aws-sdk/client-dynamodb";
2+
3+
// Client creation with tabs instead of spaces for indentation.
4+
const client = new DynamoDB({
5+
credentials: {
6+
accessKeyId: "KEY",
7+
secretAccessKey: "SECRET"
8+
}
9+
});

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

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,12 @@ import {
3131
removeGlobalModule,
3232
} from "./modules";
3333
import { replaceTSTypeReference } from "./ts-type";
34-
import { getMostUsedStringLiteralQuote, isTypeScriptFile } from "./utils";
34+
import {
35+
IndentationType,
36+
getMostUsedIndentationType,
37+
getMostUsedStringLiteralQuote,
38+
isTypeScriptFile,
39+
} from "./utils";
3540

3641
const transformer = async (file: FileInfo, api: API) => {
3742
const j = isTypeScriptFile(file.path) ? api.jscodeshift.withParser("ts") : api.jscodeshift;
@@ -72,6 +77,7 @@ const transformer = async (file: FileInfo, api: API) => {
7277

7378
// Compute recast options before doing transformations
7479
const quote = getMostUsedStringLiteralQuote(j, source);
80+
const useTabs = getMostUsedIndentationType(file.source) === IndentationType.TAB;
7581

7682
const awsGlobalConfig = getAwsGlobalConfig(j, source, v2GlobalName);
7783
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
@@ -106,7 +112,14 @@ const transformer = async (file: FileInfo, api: API) => {
106112
replaceAwsUtilFunctions(j, source, v2GlobalName);
107113
removeGlobalModule(j, source, v2GlobalName);
108114

109-
return source.toSource({ quote });
115+
const sourceString = source.toSource({ quote, useTabs });
116+
117+
if (useTabs) {
118+
// Refs: https://github.com/benjamn/recast/issues/315
119+
return sourceString.replace(/ {4,}/g, "\t");
120+
}
121+
122+
return sourceString;
110123
};
111124

112125
export default transformer;
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
export enum IndentationType {
2+
TAB = "tab",
3+
SPACE = "space",
4+
}
5+
6+
export const getMostUsedIndentationType = (source: string) => {
7+
const tabCount = (source.match(/\t/g) || []).length;
8+
const spaceCount = (source.match(/ {2}/g) || []).length;
9+
console.log({ tabCount, spaceCount });
10+
11+
if (tabCount > spaceCount) {
12+
return IndentationType.TAB;
13+
}
14+
return IndentationType.SPACE;
15+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from "./getClientDeepImportPath";
22
export * from "./getClientNewExpression";
33
export * from "./getMostUsedStringLiteralQuote";
4+
export * from "./getMostUsedIndentationType";
45
export * from "./isTypeScriptFile";

0 commit comments

Comments
 (0)