aws-sdk-js-codemod
Version:
Collection of codemod scripts that help update AWS SDK for JavaScript APIs
34 lines (33 loc) • 1.48 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.getMostUsedStringLiteralQuote = exports.StringLiteralQuoteType = void 0;
var StringLiteralQuoteType;
(function (StringLiteralQuoteType) {
StringLiteralQuoteType["SINGLE"] = "single";
StringLiteralQuoteType["DOUBLE"] = "double";
})(StringLiteralQuoteType || (exports.StringLiteralQuoteType = StringLiteralQuoteType = {}));
const getMostUsedStringLiteralQuote = (j, source) => {
const quoteCount = {
[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") {
// @ts-expect-error Property 'raw' does not exist on type 'Literal'.
const rawValue = path.node.raw || path.node.extra?.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;
};
exports.getMostUsedStringLiteralQuote = getMostUsedStringLiteralQuote;