Skip to content

Instantly share code, notes, and snippets.

@benvinegar
Created October 30, 2015 21:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save benvinegar/620a0667087539954213 to your computer and use it in GitHub Desktop.
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
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;
};
@chowei
Copy link

chowei commented Oct 20, 2021

Is this ok ? j(fileInfo.source).toSource({quote: 'single'})

@developerworks
Copy link

i was tried this, i think, the transformation function must modify something in the AST tree, or do nothing.

@developerworks
Copy link

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