eslint-plugin-you-dont-need-lodash-underscore
Advanced tools
Comparing version 6.8.0 to 6.9.0
@@ -5,2 +5,4 @@ 'use strict'; | ||
const forbiddenLibs = ['lodash', 'lodash-es']; | ||
function getAssignmentLeftHandSide(node) { | ||
@@ -23,3 +25,3 @@ // For VariableDeclarator nodes, the left hand side is called `id` | ||
const ruleName = rules[rule].ruleName || kebabCase(rule); | ||
const forbiddenImports = { [`lodash/${rule}`]: 1, [`lodash.${rule.toLowerCase()}`]: 1 }; | ||
const forbiddenImports = { [`lodash/${rule}`]: 1, [`lodash-es/${rule}`]: 1, [`lodash.${rule.toLowerCase()}`]: 1 }; | ||
module.exports[ruleName] = { | ||
@@ -35,3 +37,3 @@ create(context) { | ||
const { parent } = node; | ||
if (requiredModuleName === 'lodash') { | ||
if (forbiddenLibs.includes(requiredModuleName)) { | ||
const leftHandSide = getAssignmentLeftHandSide(parent); | ||
@@ -66,3 +68,3 @@ // ex: const { indexOf } = require('lodash'); | ||
ImportDeclaration(node) { | ||
if (node.source.value === 'lodash') { | ||
if (forbiddenLibs.includes(node.source.value)) { | ||
// ex: import { indexOf } from 'lodash'; | ||
@@ -69,0 +71,0 @@ // ex: import { indexOf as x } from 'lodash'; |
@@ -222,5 +222,11 @@ { | ||
"startsWith": { | ||
"ruleName": "starts-with", | ||
"compatible": true, | ||
"alternative": "String.prototype.startsWith()" | ||
}, | ||
"endsWith": { | ||
"ruleName": "ends-with", | ||
"compatible": true, | ||
"alternative": "String.prototype.endsWith()" | ||
}, | ||
"toLower": { | ||
@@ -270,3 +276,7 @@ "compatible": true, | ||
"alternative": "Array.prototype.reduce((a,b) => a.concat(b), [])" | ||
}, | ||
"throttle": { | ||
"compatible": true, | ||
"alternative": "Example of native implementation: https://github.com/you-dont-need/You-Dont-Need-Lodash-Underscore#_throttle" | ||
} | ||
} |
{ | ||
"name": "eslint-plugin-you-dont-need-lodash-underscore", | ||
"version": "6.8.0", | ||
"version": "6.9.0", | ||
"description": "Check methods you can use natively without lodash/underscore", | ||
@@ -5,0 +5,0 @@ "repository": { |
@@ -59,2 +59,26 @@ 'use strict'; | ||
ruleTester.run(`Import { isNaN } from lodash-es`, rules['is-nan'], { | ||
valid: [`{ x: require('lodash-es') }`], | ||
invalid: [{ | ||
code: `import { isNaN } from 'lodash-es';`, | ||
errors: [`Import { isNaN } from 'lodash-es' detected. Consider using the native Number.isNaN()`] | ||
}, | ||
{ | ||
code: `import isNaN from 'lodash-es/isNaN';`, | ||
errors: [`Import from 'lodash-es/isNaN' detected. Consider using the native Number.isNaN()`] | ||
}, { | ||
code: `import { isNaN as x } from 'lodash-es';`, | ||
errors: [`Import { isNaN } from 'lodash-es' detected. Consider using the native Number.isNaN()`] | ||
}, { | ||
code: `const { isNaN: x } = require('lodash-es');`, | ||
errors: [`{ isNaN } = require('lodash-es') detected. Consider using the native Number.isNaN()`] | ||
}, { | ||
code: `({ isNaN: x } = require('lodash-es'));`, | ||
errors: [`{ isNaN } = require('lodash-es') detected. Consider using the native Number.isNaN()`] | ||
}, { | ||
code: `require('lodash-es/isNaN');`, | ||
errors: [`require('lodash-es/isNaN') detected. Consider using the native Number.isNaN()`] | ||
}] | ||
}); | ||
ruleTester.run('underscore.forEach', rules['for-each'], { | ||
@@ -130,3 +154,3 @@ valid: [ | ||
/*This is to make sure that You-Dont-Need-Lodash can handle the | ||
/*This is to make sure that You-Dont-Need-Lodash can handle the | ||
evaluation of nested functions that had caused an error noted in the comments of | ||
@@ -162,2 +186,45 @@ Pull Request #219*/ | ||
ruleTester.run('_.isUndefined', rules['is-undefined'], { | ||
valid: [ | ||
'2 === undefined' | ||
], | ||
invalid: [{ | ||
code: '_.isUndefined(2)', | ||
errors: ['Consider using the native value === undefined'] | ||
},{ | ||
code: '_(2).isUndefined()', | ||
errors: ['Consider using the native value === undefined'] | ||
}] | ||
}); | ||
ruleTester.run('_.startsWith', rules['starts-with'], { | ||
valid: [ | ||
'"abc".startsWith("a")', | ||
'"abc".startsWith("b", 1)' | ||
], | ||
invalid: [{ | ||
code: '_.startsWith("abc", "a")', | ||
errors: ['Consider using the native String.prototype.startsWith()'] | ||
},{ | ||
code: '_.startsWith("abc", "b", 1)', | ||
errors: ['Consider using the native String.prototype.startsWith()'] | ||
}] | ||
}); | ||
ruleTester.run('_.endsWith', rules['ends-with'], { | ||
valid: [ | ||
'"abc".endsWith("c")', | ||
'"abc".endsWith("b", 1)' | ||
], | ||
invalid: [{ | ||
code: '_.endsWith("abc", "c")', | ||
errors: ['Consider using the native String.prototype.endsWith()'] | ||
},{ | ||
code: '_.endsWith("abc", "b", 1)', | ||
errors: ['Consider using the native String.prototype.endsWith()'] | ||
}] | ||
}); | ||
@@ -235,8 +235,17 @@ 'use strict'; | ||
const get = (obj, path, defaultValue) => { | ||
const result = String.prototype.split.call(path, /[,[\].]+?/) | ||
.filter(Boolean) | ||
.reduce((res, key) => (res !== null && res !== undefined) ? res[key] : res, obj); | ||
return (result === undefined || result === obj) ? defaultValue : result; | ||
} | ||
var obj = { aa: [{ b: { c: 0 }, 1: 0 }], dd: { ee: { ff: 2 } } }; | ||
const travel = regexp => | ||
String.prototype.split | ||
.call(path, regexp) | ||
.filter(Boolean) | ||
.reduce((res, key) => (res !== null && res !== undefined ? res[key] : res), obj); | ||
const result = travel(/[,[\]]+?/) || travel(/[,[\].]+?/); | ||
return result === undefined || result === obj ? defaultValue : result; | ||
}; | ||
var obj = { | ||
aa: [{ b: { c: 0 }, 1: 0 }], | ||
dd: { ee: { ff: 2 } }, | ||
gg: { h: 2 }, | ||
"gg.h": 1, | ||
"kk.ll": { "mm.n": [3, 4, { "oo.p": 5 }] } | ||
}; | ||
@@ -304,2 +313,14 @@ it ("should handle falsey values", () => { | ||
}) | ||
it ("should handle path contains a key with dots", () => { | ||
var val = _.get(obj, 'gg.h') | ||
assert.strictEqual(val, get(obj, 'gg.h')) | ||
assert.strictEqual(val, 1) | ||
}) | ||
it ("should handle array path of keys with dots", () => { | ||
var val = _.get(obj, ["kk.ll", "mm.n", 0, "oo.p"]) | ||
assert.strictEqual( | ||
val, | ||
get(obj, ["kk.ll", "mm.n", 0, "oo.p"]) | ||
); | ||
}) | ||
}) | ||
@@ -668,3 +689,2 @@ describe('split', () => { | ||
assert.deepEqual(lodashOutput,nativeOutput); | ||
}); | ||
@@ -692,6 +712,70 @@ | ||
assert.deepEqual(lodashOutput,nativeOutput); | ||
}); | ||
}); | ||
describe('startsWith', () => { | ||
it(`_.startsWith('abc', 'a')`, () => { | ||
assert.deepEqual( | ||
_.startsWith('abc', 'a'), | ||
'abc'.startsWith('a') | ||
); | ||
}); | ||
it(`_.startsWith('abc', 'b')`, () => { | ||
assert.deepEqual( | ||
_.startsWith('abc', 'b'), | ||
'abc'.startsWith('b') | ||
); | ||
}); | ||
it(`_.startsWith('abc', 'b', 1)`, () => { | ||
assert.deepEqual( | ||
_.startsWith('abc', 'b', 1), | ||
'abc'.startsWith('b', 1) | ||
); | ||
}); | ||
}); | ||
}) | ||
describe('endsWith', () => { | ||
it(`_.endsWith('abc', 'c')`, () => { | ||
assert.deepEqual( | ||
_.endsWith('abc', 'c'), | ||
'abc'.endsWith('c') | ||
); | ||
}); | ||
it(`_.endsWith('abc', 'b')`, () => { | ||
assert.deepEqual( | ||
_.endsWith('abc', 'b'), | ||
'abc'.endsWith('b') | ||
); | ||
}); | ||
it(`_.endsWith('abc', 'b', 2)`, () => { | ||
assert.deepEqual( | ||
_.endsWith('abc', 'b', 2), | ||
'abc'.endsWith('b', 2) | ||
); | ||
}); | ||
}); | ||
describe('throttle', () => { | ||
function throttle(func, timeFrame) { | ||
var lastTime = 0; | ||
return function () { | ||
var now = new Date(); | ||
if (now - lastTime >= timeFrame) { | ||
func(); | ||
lastTime = now; | ||
} | ||
}; | ||
} | ||
it('throttle is not called more than once within timeframe', () => { | ||
let callCount = 0; | ||
const fn = throttle(() => callCount++, 100); | ||
fn(); | ||
fn(); | ||
fn(); | ||
assert.equal(callCount, 1); | ||
}); | ||
}) | ||
}); |
Sorry, the diff of this file is too big to display
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
1319
2785
124174
12