Skip to content

Commit

Permalink
refactor so each single rule can be configured
Browse files Browse the repository at this point in the history
  • Loading branch information
stevemao committed Jul 2, 2016
1 parent 4652fdd commit eb8eb24
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 69 deletions.
3 changes: 2 additions & 1 deletion README.md
Expand Up @@ -63,7 +63,8 @@ npm install --save-dev eslint-plugin-you-dont-need-lodash-underscore
```
'plugins': ['you-dont-need-lodash-underscore'],
'rules': {
'you-dont-need-lodash-underscore/all': 1,
'you-dont-need-lodash-underscore/for-each': 1,
'you-dont-need-lodash-underscore/concat': 1,
...
}
```
Expand Down
4 changes: 1 addition & 3 deletions index.js
@@ -1,5 +1,3 @@
'use strict';

module.exports.rules = {
all: require('./lib/rules/all')
}
module.exports.rules = require('./lib/rules/all');
73 changes: 22 additions & 51 deletions lib/rules/all.js
Expand Up @@ -4,59 +4,30 @@
*/
'use strict';

//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
// ------------------------------------------------------------------------------
// Requirements
// ------------------------------------------------------------------------------

const errorMessages = {
concat: 'Consider using the native Array.prototype.concat()',
fill: 'Consider using the native ES6 Array.prototype.fill()',
find: 'Consider using the native ES6 Array.prototype.find()',
findIndex: 'Consider using the native ES6 Array.prototype.findIndex()',
indexOf: 'Consider using the native Array.prototype.indexOf()',
join: 'Consider using the native Array.prototype.join()',
lastIndexOf: 'Consider using the native Array.prototype.lastIndexOf()',
reverse: 'Consider using the native Array.prototype.reverse()',
each: 'Consider using the native Array.prototype.forEach()',
every: 'Consider using the native Array.prototype.every()',
filter: 'Consider using the native Array.prototype.filter()',
includes: 'Consider using the native Array.prototype.includes()',
map: 'Consider using the native Array.prototype.map()',
reduce: 'Consider using the native Array.prototype.reduce()',
reduceRight: 'Consider using the native Array.prototype.reduceRight()',
size: 'Consider using the native Array.prototype.length',
some: 'Consider using the native Array.prototype.some()',
isNaN: 'Consider using the native isNaN() or ES6 Number.isNaN()',
extendOwn: 'Consider using the native ES6 Object.assign()',
assign: 'Consider using the native ES6 Object.assign()',
keys: 'Consider using the native Object.keys()',
repeat: 'Consider using the native ES6 String.prototype.repeat()',
toLower: 'Consider using the native String.prototype.toLowerCase()',
toUpper: 'Consider using the native String.prototype.toUpperCase()',
trim: 'Consider using the native String.prototype.trim()',
};
const kebabCase = require('kebab-case');
const rules = require('./rules');

module.exports = {
create (context) {
function checkForUnneededLibraryFunctions (node) {
const functionName = get_FunctionName(node);
const errorMessage = errorMessages[functionName];
for (const rule in rules) {
const errorMessage = rules[rule];
module.exports[kebabCase(rule)] = {
create (context) {
return {
CallExpression: function (node) {
const callee = node.callee;
const objectName = callee.object.name;

if (errorMessage) {
context.report({
node,
message: errorMessages[functionName],
});
}
if ((objectName === '_' || objectName === 'lodash' || objectName === 'underscore') && callee.property.name === rule) {
context.report({
node,
message: errorMessage
});
}
},
};
}

return {
'CallExpression': checkForUnneededLibraryFunctions,
};
}
};

function get_FunctionName (node) {
const callee = node.callee;
return callee && callee.object && (callee.object.name === '_' || callee.object.name === 'lodash' || callee.object.name === 'underscore') && callee.property && callee.property.name;
};
}
37 changes: 37 additions & 0 deletions lib/rules/rules.json
@@ -0,0 +1,37 @@
{
"concat": "Consider using the native Array.prototype.concat()",
"fill": "Consider using the native ES6 Array.prototype.fill()",
"find": "Consider using the native ES6 Array.prototype.find()",
"detect": "Consider using the native ES6 Array.prototype.find()",
"findIndex": "Consider using the native ES6 Array.prototype.findIndex()",
"indexOf": "Consider using the native Array.prototype.indexOf()",
"join": "Consider using the native Array.prototype.join()",
"lastIndexOf": "Consider using the native Array.prototype.lastIndexOf()",
"reverse": "Consider using the native Array.prototype.reverse()",
"each": "Consider using the native Array.prototype.forEach()",
"forEach": "Consider using the native Array.prototype.forEach()",
"every": "Consider using the native Array.prototype.every()",
"all": "Consider using the native Array.prototype.every()",
"filter": "Consider using the native Array.prototype.filter()",
"select": "Consider using the native Array.prototype.filter()",
"includes": "Consider using the native Array.prototype.includes()",
"contains": "Consider using the native Array.prototype.includes()",
"map": "Consider using the native Array.prototype.map()",
"collect": "Consider using the native Array.prototype.map()",
"reduce": "Consider using the native Array.prototype.reduce()",
"inject": "Consider using the native Array.prototype.reduce()",
"foldl": "Consider using the native Array.prototype.reduce()",
"reduceRight": "Consider using the native Array.prototype.reduceRight()",
"foldr": "Consider using the native Array.prototype.reduceRight()",
"size": "Consider using the native Array.prototype.length",
"some": "Consider using the native Array.prototype.some()",
"any": "Consider using the native Array.prototype.some()",
"isNaN": "Consider using the native isNaN() or ES6 Number.isNaN()",
"extendOwn": "Consider using the native ES6 Object.assign()",
"assign": "Consider using the native ES6 Object.assign()",
"keys": "Consider using the native Object.keys()",
"repeat": "Consider using the native ES6 String.prototype.repeat()",
"toLower": "Consider using the native String.prototype.toLowerCase()",
"toUpper": "Consider using the native String.prototype.toUpperCase()",
"trim": "Consider using the native String.prototype.trim()"
}
4 changes: 3 additions & 1 deletion package.json
Expand Up @@ -25,7 +25,9 @@
"test": "npm run unit-test",
"unit-test": "istanbul cover --dir reports/coverage node_modules/mocha/bin/_mocha tests/**/*.js -- --reporter dot"
},
"dependencies": {},
"dependencies": {
"kebab-case": "^1.0.0"
},
"devDependencies": {
"coveralls": "^2.11.9",
"eslint": "^3.0.0",
Expand Down
40 changes: 27 additions & 13 deletions tests/lib/rules/all.js
Expand Up @@ -8,25 +8,39 @@
// Requirements
// ------------------------------------------------------------------------------

const rule = require('../../../lib/rules/all');
const rules = require('../../../lib/rules/all');
const RuleTester = require('eslint').RuleTester;

// Only a couple of smoke tests because otherwise it would get very reduntant

const ruleTester = new RuleTester();
ruleTester.run('no-lodash-underscore', rule, {

ruleTester.run('_.concat', rules.concat, {
valid: [
'array.concat(2, [3], [[4]])'
],
invalid: [{
code: '_.concat(array, 2, [3], [[4]])',
errors: ['Consider using the native Array.prototype.concat()']
}]
});

ruleTester.run('lodash.keys', rules.keys, {
valid: [
'array.concat(2, [3], [[4]])',
'Object.keys({one: 1, two: 2, three: 3})'
],
invalid: [
{
code: '_.concat(array, 2, [3], [[4]])',
errors: ['Consider using the native Array.prototype.concat()']
},
{
code: '_.keys({one: 1, two: 2, three: 3})',
errors: ['Consider using the native Object.keys()']
}
]
invalid: [{
code: 'lodash.keys({one: 1, two: 2, three: 3})',
errors: ['Consider using the native Object.keys()']
}]
});

ruleTester.run('underscore.forEach', rules['for-each'], {
valid: [
'[0, 1].forEach()'
],
invalid: [{
code: 'underscore.forEach()',
errors: ['Consider using the native Array.prototype.forEach()']
}]
});

0 comments on commit eb8eb24

Please sign in to comment.