Skip to content

Instantly share code, notes, and snippets.

@cpojer
Created July 30, 2015 17:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cpojer/f54da69dd042001e2320 to your computer and use it in GitHub Desktop.
Save cpojer/f54da69dd042001e2320 to your computer and use it in GitHub Desktop.
module.exports = function(file, api) {
var j = api.jscodeshift; // alias the jscodeshift API
var root = j(file.source); // parse JS code into an AST
// the main update method replaces merge with ObjectExpression
const update = path =>
j(path).replaceWith(j.objectExpression(
flatten(path.value.arguments.map(p =>
p.type == 'ObjectExpression'
? p.properties
: j.spreadProperty(p)
))
));
// find and update all merge calls
root.find(j.CallExpression, {callee: {name: 'merge'}})
.forEach(update);
// print
return root.toSource();
};
// A lovely recursive helper
const flatten =
a => Array.isArray(a) ? [].concat(...a.map(flatten)) : a;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment