Skip to content

Instantly share code, notes, and snippets.

@kebot
Created May 21, 2016 16:56
Show Gist options
  • Save kebot/36674d578e3c39d84833c9e110348309 to your computer and use it in GitHub Desktop.
Save kebot/36674d578e3c39d84833c9e110348309 to your computer and use it in GitHub Desktop.
codemod that upgrade lodash v3 import to v4
// this codemod will replace all 'lodash/<whatever>/<func>' -> 'lodash/<func>'
// usage: jscodeshift -t tools/lodash-3-4.js src
let transformModuleName = (name) => {
let matchObj = name.match(/^lodash\/(.*)\/(.*)/)
if(matchObj) {
return ['lodash', matchObj[2]].join('/')
}
return name
}
export default function transformer(file, api) {
const j = api.jscodeshift
const {expression, statement, statements} = j.template
return j(file.source)
.find(j.ImportDeclaration)
.replaceWith((p) => {
// "lodash/${a}/${b}" -> "lodash/${b}"
var tree = p.value
, pkgName = transformModuleName(tree.source.value)
, specifiers = tree.specifiers;
return Object.assign(tree, {
source: j.literal(pkgName)
})
})
.toSource({
tabWidth: 2
, lineTerminator: '\r\n'
, trailingComma: false
})
};
@kebot
Copy link
Author

kebot commented May 21, 2016

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment