Skip to content

Commit

Permalink
Use quotation style from the input code (#705)
Browse files Browse the repository at this point in the history
  • Loading branch information
trivikr authored Nov 11, 2023
1 parent c6cb30d commit 06a1242
Show file tree
Hide file tree
Showing 6 changed files with 54 additions and 2 deletions.
5 changes: 5 additions & 0 deletions .changeset/cuddly-games-burn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"aws-sdk-js-codemod": patch
---

Use quotation style from the input code
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import AWS from 'aws-sdk';

const client = new AWS.DynamoDB();
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
import { DynamoDB } from '@aws-sdk/client-dynamodb';

const client = new DynamoDB();
7 changes: 5 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,7 @@ import {
removeGlobalModule,
} from "./modules";
import { replaceTSTypeReference } from "./ts-type";
import { isTypeScriptFile } from "./utils";
import { 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 @@ -70,6 +70,9 @@ const transformer = async (file: FileInfo, api: API) => {
return source.toSource();
}

// Compute recast options before doing transformations
const quote = getMostUsedStringLiteralQuote(j, source);

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

return source.toSource();
return source.toSource({ quote });
};

export default transformer;
Expand Down
37 changes: 37 additions & 0 deletions src/transforms/v2-to-v3/utils/getMostUsedStringLiteralQuote.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import { Collection, JSCodeshift } from "jscodeshift";

export enum StringLiteralQuoteType {
SINGLE = "single",
DOUBLE = "double",
}

export const getMostUsedStringLiteralQuote = (
j: JSCodeshift,
source: Collection<unknown>
): StringLiteralQuoteType => {
const quoteCount: Record<StringLiteralQuoteType, number> = {
[StringLiteralQuoteType.SINGLE]: 0,
[StringLiteralQuoteType.DOUBLE]: 0,
};

source.find(j.Literal).forEach((path) => {
const value = path.node.value;

// Check if the literal value is a string and contains single quotes
if (typeof value === "string") {
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
// @ts-ignore Property 'raw' does not exist on type 'Literal'.
const rawValue = path.node.raw || "";
if (rawValue.startsWith("'")) {
quoteCount[StringLiteralQuoteType.SINGLE]++;
} else if (rawValue.startsWith('"')) {
quoteCount[StringLiteralQuoteType.DOUBLE]++;
}
}
});

if (quoteCount[StringLiteralQuoteType.SINGLE] > quoteCount[StringLiteralQuoteType.DOUBLE]) {
return StringLiteralQuoteType.SINGLE;
}
return StringLiteralQuoteType.DOUBLE;
};
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,3 +1,4 @@
export * from "./getClientDeepImportPath";
export * from "./getClientNewExpression";
export * from "./getMostUsedStringLiteralQuote";
export * from "./isTypeScriptFile";

0 comments on commit 06a1242

Please sign in to comment.