Created
July 30, 2015 17:18
-
-
Save cpojer/f54da69dd042001e2320 to your computer and use it in GitHub Desktop.
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(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); | |
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