Created
July 14, 2016 16:29
-
-
Save joecritch/79da92672f2d8f890b13581b21454cde to your computer and use it in GitHub Desktop.
Converts all Object.assign calls to { ... obj } spread.
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
// 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