Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save frontEnd-fucker/8d23629f0d3f4516c83dc6537f07458a to your computer and use it in GitHub Desktop.

Select an option

Save frontEnd-fucker/8d23629f0d3f4516c83dc6537f07458a to your computer and use it in GitHub Desktop.
jscodeshift transform for converting double quotes to single quotes – *except* in JSX (HTML) attributes
module.exports = function(fileInfo, api) {
var j = api.jscodeshift;
var out = j(fileInfo.source)
.find(j.Literal)
.forEach(function (path) {
// Only consider literals that start/end w/ double quotes
if (!/^".*"$/.test(path.value.raw)) {
return;
}
// Skip JSX element attributes
if (path.parent.value.type === 'JSXAttribute') {
return;
}
j(path).replaceWith(j.literal(path.value.value));
})
.toSource({quote: 'single'});
return out;
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment