Skip to content

Instantly share code, notes, and snippets.

@joecritch
Created July 14, 2016 16:29
Show Gist options
  • Save joecritch/79da92672f2d8f890b13581b21454cde to your computer and use it in GitHub Desktop.
Save joecritch/79da92672f2d8f890b13581b21454cde to your computer and use it in GitHub Desktop.
Converts all Object.assign calls to { ... obj } spread.
// Highly based on an example from https://medium.com/@cpojer/effective-javascript-codemods-5a6686bb46fb
module.exports = function(file, api) {
var j = api.jscodeshift;
var root = j(file.source);
const update = path =>
j(path).replaceWith(j.objectExpression(
flatten(path.value.arguments.map(p =>
p.type === 'ObjectExpression'
? p.properties
: j.spreadProperty(p)
))
));
const call = root.find(j.CallExpression, {
callee: {
object: {
name: 'Object'
},
property: {
name: 'assign'
}
}})
.forEach(update);
return root.toSource({
trailingComma: true,
tabWidth: 2,
quote: 'single',
});
};
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