Skip to content

Commit 06a1242

Browse files
authored
Use quotation style from the input code (#705)
1 parent c6cb30d commit 06a1242

File tree

6 files changed

+54
-2
lines changed

6 files changed

+54
-2
lines changed

.changeset/cuddly-games-burn.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 quotation style from the input code
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import AWS from 'aws-sdk';
2+
3+
const client = new AWS.DynamoDB();
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
import { DynamoDB } from '@aws-sdk/client-dynamodb';
2+
3+
const client = new DynamoDB();

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

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ import {
3131
removeGlobalModule,
3232
} from "./modules";
3333
import { replaceTSTypeReference } from "./ts-type";
34-
import { isTypeScriptFile } from "./utils";
34+
import { getMostUsedStringLiteralQuote, isTypeScriptFile } from "./utils";
3535

3636
const transformer = async (file: FileInfo, api: API) => {
3737
const j = isTypeScriptFile(file.path) ? api.jscodeshift.withParser("ts") : api.jscodeshift;
@@ -70,6 +70,9 @@ const transformer = async (file: FileInfo, api: API) => {
7070
return source.toSource();
7171
}
7272

73+
// Compute recast options before doing transformations
74+
const quote = getMostUsedStringLiteralQuote(j, source);
75+
7376
const awsGlobalConfig = getAwsGlobalConfig(j, source, v2GlobalName);
7477
for (const [v2ClientName, v3ClientMetadata] of Object.entries(clientMetadataRecord)) {
7578
const clientIdentifiers = clientIdentifiersRecord[v2ClientName];
@@ -103,7 +106,7 @@ const transformer = async (file: FileInfo, api: API) => {
103106
replaceAwsUtilFunctions(j, source, v2GlobalName);
104107
removeGlobalModule(j, source, v2GlobalName);
105108

106-
return source.toSource();
109+
return source.toSource({ quote });
107110
};
108111

109112
export default transformer;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { Collection, JSCodeshift } from "jscodeshift";
2+
3+
export enum StringLiteralQuoteType {
4+
SINGLE = "single",
5+
DOUBLE = "double",
6+
}
7+
8+
export const getMostUsedStringLiteralQuote = (
9+
j: JSCodeshift,
10+
source: Collection<unknown>
11+
): StringLiteralQuoteType => {
12+
const quoteCount: Record<StringLiteralQuoteType, number> = {
13+
[StringLiteralQuoteType.SINGLE]: 0,
14+
[StringLiteralQuoteType.DOUBLE]: 0,
15+
};
16+
17+
source.find(j.Literal).forEach((path) => {
18+
const value = path.node.value;
19+
20+
// Check if the literal value is a string and contains single quotes
21+
if (typeof value === "string") {
22+
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
23+
// @ts-ignore Property 'raw' does not exist on type 'Literal'.
24+
const rawValue = path.node.raw || "";
25+
if (rawValue.startsWith("'")) {
26+
quoteCount[StringLiteralQuoteType.SINGLE]++;
27+
} else if (rawValue.startsWith('"')) {
28+
quoteCount[StringLiteralQuoteType.DOUBLE]++;
29+
}
30+
}
31+
});
32+
33+
if (quoteCount[StringLiteralQuoteType.SINGLE] > quoteCount[StringLiteralQuoteType.DOUBLE]) {
34+
return StringLiteralQuoteType.SINGLE;
35+
}
36+
return StringLiteralQuoteType.DOUBLE;
37+
};
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
11
export * from "./getClientDeepImportPath";
22
export * from "./getClientNewExpression";
3+
export * from "./getMostUsedStringLiteralQuote";
34
export * from "./isTypeScriptFile";

0 commit comments

Comments
 (0)