Created
October 30, 2015 21:32
-
-
Save benvinegar/620a0667087539954213 to your computer and use it in GitHub Desktop.
jscodeshift transform for converting double quotes to single quotes – *except* in JSX (HTML) attributes
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
j.literal
is affected by{quote: "single"}
option