Skip to content

Commit

Permalink
add support for single method package imports in LodashContext
Browse files Browse the repository at this point in the history
  • Loading branch information
ganimomer committed Oct 18, 2017
1 parent 2ed03fc commit 3037b9f
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/util/importUtil.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ function getNameFromCjsRequire(init) {

const isFullLodashImport = str => /^lodash(-es)?$/.test(str)
const getMethodImportFromName = str => {
const match = /^lodash(-es)?\/(?!fp)(\w+)$/.exec(str)
const match = /^lodash(-es\/|[./])(?!fp)(\w+)$/.exec(str)
return match && match[2]
}

Expand Down
38 changes: 37 additions & 1 deletion tests/lib/util/LodashContext.js
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,22 @@ describe('LodashContext', () => {
}
}))
})
it('should accept a single method package import as lodash', done => {
visitWithContext('import map from "lodash.map"; map(arr, x => x)', {sourceType: 'module'}, lodashContext => ({
CallExpression(node) {
assert(lodashContext.methods[node.callee.name] === 'map')
done()
}
}))
})
it('should not accept a single method packge import from lodash-es as lodash', done => {
visitWithContext('import map from "lodash-es.map"; map(arr, x => x)', {sourceType: 'module'}, lodashContext => ({
CallExpression(node) {
assert(!lodashContext.methods[node.callee.name])
done()
}
}))
})
it('should not throw error when trying to import lodash for side effects', () => {
traverser('import "lodash"', {sourceType: 'module'})
.runRuleCode(context => (new LodashContext(context)).getImportVisitors())
Expand Down Expand Up @@ -139,6 +155,26 @@ describe('LodashContext', () => {
}
}))
})
it('should accept a single method package require', done => {
visitWithContext('const map = require("lodash.map"); map(arr, x => x)', undefined, lodashContext => ({
CallExpression(node) {
if (node.callee.name === 'map') {
assert(lodashContext.methods[node.callee.name] === 'map')
done()
}
}
}))
})
it('should not accept a single method package require from lodash-es', done => {
visitWithContext('const map = require("lodash-es.map"); map(arr, x => x)', undefined, lodashContext => ({
CallExpression(node) {
if (node.callee.name === 'map') {
assert(!lodashContext.methods[node.callee.name])
done()
}
}
}))
})
it('should not collect arbitrary requires', done => {
visitWithContext('const map = require("some-other-map"); map(arr, x => x)', undefined, lodashContext => ({
CallExpression(node) {
Expand Down Expand Up @@ -319,4 +355,4 @@ describe('LodashContext', () => {
}))
})
})
})
})

0 comments on commit 3037b9f

Please sign in to comment.