Last active
June 18, 2020 16:38
-
-
Save 40thieves/0b495af3fb0ad5fe08915ce5159a2b7b to your computer and use it in GitHub Desktop.
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
module.exports = function (file, api, options) { | |
const useDouble = options.useDouble || false; | |
const noSemi = options.noSemi || false; | |
const useRequire = options.useRequire || false; | |
const j = api.jscodeshift; | |
const isContainsLodashImport = | |
file.source.match(/(const|let|var)\s+_\s+=\s+require\(["']lodash["']\)/g) || | |
file.source.match(/import\s+_\s+from\s+["']lodash["']/g); | |
if (isContainsLodashImport) { | |
return; | |
} else { | |
let isLodashDetected = false; | |
j(file.source) | |
.find(j.MemberExpression) | |
.forEach((path) => { | |
if (path.value.object.name === "_") { | |
isLodashDetected = true; | |
} | |
}); | |
const quote = useDouble ? '"' : "'"; | |
const semi = noSemi ? "" : ";"; | |
const require = `const _ = require(${quote}lodash${quote})${semi}`; | |
const es6Import = `import _ from ${quote}lodash${quote}${semi}`; | |
const importString = useRequire ? require : es6Import; | |
if (isLodashDetected) { | |
return `${importString}\n${file.source}`; | |
} | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment