Skip to content

Instantly share code, notes, and snippets.

@somebody32
Created September 12, 2015 14:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save somebody32/a50894d6599db7e2a99f to your computer and use it in GitHub Desktop.
Save somebody32/a50894d6599db7e2a99f to your computer and use it in GitHub Desktop.
How to convert multiline vars into separate let/const declarations

You can convert js-code like this:

var a = '1',
    b,
    c = '2';

into

const a = '1';
let b;
const c = '2';

just install npm-package jscodeshift and run:
jscodeshift -t transform.js <file_to_transform>

module.exports = function(fileInfo, api, options) {
const j = api.jscodeshift;
return j(fileInfo.source)
.find(j.VariableDeclaration)
.replaceWith(path => {
return path.value.declarations.map(declaration => {
let var_dec;
if (path.value.kind === 'var') {
var_dec = declaration.init ? 'const' : 'let';
} else {
var_dec = path.value.kind;
}
return j.variableDeclaration(var_dec, [declaration]);
});
})
.toSource();
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment