Skip to content

Commit

Permalink
Merge pull request #251 from doniyor2109/lodash_es_support
Browse files Browse the repository at this point in the history
Add lodash-es support
  • Loading branch information
stevemao committed Jan 18, 2020
2 parents fbb6fd1 + 8879498 commit 30407d5
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 4 deletions.
8 changes: 5 additions & 3 deletions lib/rules/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
const kebabCase = require('kebab-case');
const rules = require('./rules');

const forbiddenLibs = ['lodash', 'lodash-es'];

function getAssignmentLeftHandSide(node) {
// For VariableDeclarator nodes, the left hand side is called `id`
// The `x` on `var x = 3;
Expand All @@ -19,7 +21,7 @@ function getAssignmentLeftHandSide(node) {
for (const rule in rules) {
const alternative = rules[rule].alternative;
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] = {
create(context) {
return {
Expand All @@ -30,7 +32,7 @@ for (const rule in rules) {
if (objectName === 'require' && node.arguments.length === 1) {
const requiredModuleName = node.arguments[0].value;
const { parent } = node;
if (requiredModuleName === 'lodash') {
if (forbiddenLibs.includes(requiredModuleName)) {
const leftHandSide = getAssignmentLeftHandSide(parent);
// ex: const { indexOf } = require('lodash');
// ex: ({ indexOf } = require('lodash'));
Expand Down Expand Up @@ -60,7 +62,7 @@ for (const rule in rules) {
}
},
ImportDeclaration(node) {
if (node.source.value === 'lodash') {
if (forbiddenLibs.includes(node.source.value)) {
// ex: import { indexOf } from 'lodash';
// ex: import { indexOf as x } from 'lodash';
node.specifiers.forEach(specifier => {
Expand Down
26 changes: 25 additions & 1 deletion tests/lib/rules/all.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,30 @@ ruleTester.run(`Import lodash.isnan`, rules['is-nan'], {
}]
});

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'], {
valid: [
'[0, 1].forEach()',
Expand Down Expand Up @@ -126,7 +150,7 @@ ruleTester.run('_.isUndefined', rules['is-undefined'], {
}]
});

/*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
Pull Request #219*/
ruleTester.run('Nested functions', rules['is-undefined'], {
Expand Down

0 comments on commit 30407d5

Please sign in to comment.