Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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
You can’t perform that action at this time.