jest-codemods
Version:
Codemods for migrating test files to Jest
33 lines (32 loc) • 960 B
JavaScript
Object.defineProperty(exports, "__esModule", { value: true });
exports.default = detectQuoteStyle;
/**
* As Recast is not preserving original quoting, we try to detect it.
* See https://github.com/benjamn/recast/issues/171
* and https://github.com/facebook/jscodeshift/issues/143
* @return 'double', 'single' or null
*/
function detectQuoteStyle(j, ast) {
var doubles = 0;
var singles = 0;
ast
.find(j.Literal, {
value: function (v) { return typeof v === 'string'; },
raw: function (v) { return typeof v === 'string'; },
})
.forEach(function (p) {
// The raw value is from the original babel source
var quote = p.value.raw[0];
if (quote === '"') {
doubles += 1;
}
if (quote === "'") {
singles += 1;
}
});
if (doubles === singles) {
return null;
}
return doubles > singles ? 'double' : 'single';
}
;