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 hidden or 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; | |
}; |
i was tried this, i think, the transformation function must modify something in the AST tree, or do nothing.
export default function transformer(file, api, { }) {
const j = api.jscodeshift;
console.log("Processing file", file.path)
return j(file.source)
.find(j.Literal)
.forEach(path => {
j(path).replaceWith(j.literal(path.value.value));
})
.toSource({ quote: "double" });
}
j.literal
is affected by {quote: "single"}
option
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Is this ok ? j(fileInfo.source).toSource({quote: 'single'})