Created
May 21, 2016 16:56
-
-
Save kebot/36674d578e3c39d84833c9e110348309 to your computer and use it in GitHub Desktop.
codemod that upgrade lodash v3 import to v4
This file contains hidden or 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
// 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 | |
}) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Preview and test the codemod here