-
Notifications
You must be signed in to change notification settings - Fork 65
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add prefer-lodash-method and prefer-lodash-typecheck (implements #3)
- Loading branch information
Showing
8 changed files
with
224 additions
and
2 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# Prefer Lodash method | ||
|
||
When using native functions like forEach and map, it's often better to use the Lodash implementation. | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
var b = a.map(f); | ||
|
||
if (arr.some(f)) { | ||
// ... | ||
} | ||
|
||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
_.map(a, f); | ||
|
||
_(arr).map(f).reduce(g); | ||
|
||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using Lodash methods, you should not use this rule. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
# Prefer Lodash typecheck | ||
|
||
Getting the specific type of a variable or expression can be done with `typeof` or `instanceof`. However, it's often more expressive to use the Lodash equivalent function | ||
|
||
## Rule Details | ||
|
||
This rule takes no arguments. | ||
|
||
The following patterns are considered warnings: | ||
|
||
```js | ||
|
||
if (typeof a === 'number') { | ||
// ... | ||
} | ||
|
||
var isNotString = typeof b !== 'string'; | ||
|
||
var isArray = a instanceof Array; | ||
|
||
``` | ||
|
||
The following patterns are not considered warnings: | ||
|
||
```js | ||
|
||
var areSameType = typeof a === typeof b; | ||
|
||
var isCar = truck instanceof Car; | ||
|
||
``` | ||
|
||
|
||
## When Not To Use It | ||
|
||
If you do not want to enforce using Lodash methods for type checks, you should not use this rule. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/** | ||
* @fileoverview Rule to check if there's a method in the chain start that can be in the chain | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
var astUtil = require('../util/astUtil'); | ||
|
||
var REPORT_MESSAGE = 'Prefer \'_.{{method}}\' over the native function.'; | ||
|
||
return { | ||
CallExpression: function (node) { | ||
if (!lodashUtil.isLodashCall(node) && !lodashUtil.isLodashWrapper(astUtil.getCaller(node)) && lodashUtil.canBeLodashMethod(astUtil.getMethodName(node))) { | ||
context.report(node, REPORT_MESSAGE, {method: astUtil.getMethodName(node)}); | ||
} | ||
} | ||
}; | ||
}; |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/** | ||
* @fileoverview Rule to check if there's a method in the chain start that can be in the chain | ||
*/ | ||
'use strict'; | ||
|
||
//------------------------------------------------------------------------------ | ||
// Rule Definition | ||
//------------------------------------------------------------------------------ | ||
|
||
module.exports = function (context) { | ||
var lodashUtil = require('../util/lodashUtil'); | ||
|
||
function isTypeOf(node) { | ||
return node && node.type === 'UnaryExpression' && node.operator === 'typeof'; | ||
} | ||
|
||
function isLiteral(node) { | ||
return node && node.type === 'Literal'; | ||
} | ||
|
||
function isStrictComparison(node) { | ||
return (node.operator === '===' || node.operator === '!=='); | ||
} | ||
|
||
function isCompareTypeOfToLiteral(node) { | ||
return isStrictComparison(node) && | ||
((isTypeOf(node.left) && isLiteral(node.right)) || (isTypeOf(node.right) && isLiteral(node.left))); | ||
} | ||
|
||
var REPORT_MESSAGE = 'Prefer \'_.{{method}}\' over {{actual}}.'; | ||
|
||
return { | ||
BinaryExpression: function (node) { | ||
if (isCompareTypeOfToLiteral(node)) { | ||
context.report(node, REPORT_MESSAGE, { | ||
method: lodashUtil.getIsTypeMethod(isLiteral(node.left) ? node.left.value : node.right.value), | ||
actual: '\'typeof\' comparison' | ||
}); | ||
} else if (node.operator === 'instanceof') { | ||
var lodashEquivalent = lodashUtil.getIsTypeMethod(node.right.name); | ||
if (node.right.type === 'Identifier' && lodashEquivalent) { | ||
context.report(node, REPORT_MESSAGE, {method: lodashEquivalent, actual: '\'instanceof ' + node.right.name + '\''}); | ||
} | ||
} | ||
} | ||
}; | ||
}; |
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-lodash-method'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = [{message: 'Prefer \'_.map\' over the native function.'}]; | ||
ruleTester.run('prefer-lodash-method', rule, { | ||
valid: [{ | ||
code: 'var x = _.map(arr, f)' | ||
}, { | ||
code: 'var x = _(arr).map(f).reduce(g)' | ||
}, { | ||
code: 'var x = _.chain(arr).map(f).reduce(g).value()' | ||
}], | ||
invalid: [{ | ||
code: 'var x = a.map(function(x) {return x.f()});', | ||
errors: errors | ||
}, { | ||
code: 'var x = arr.map(x => x.f())', | ||
ecmaFeatures: {arrowFunctions: true}, | ||
errors: errors | ||
}] | ||
}); |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
'use strict'; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Requirements | ||
// ------------------------------------------------------------------------------ | ||
|
||
var rule = require('../../../lib/rules/prefer-lodash-typecheck'); | ||
var RuleTester = require('eslint').RuleTester; | ||
|
||
// ------------------------------------------------------------------------------ | ||
// Tests | ||
// ------------------------------------------------------------------------------ | ||
|
||
var ruleTester = new RuleTester(); | ||
var errors = { | ||
typeof: [{message: 'Prefer \'_.isNumber\' over \'typeof\' comparison.'}], | ||
instanceof: [{message: 'Prefer \'_.isArray\' over \'instanceof Array\'.'}] | ||
}; | ||
ruleTester.run('prefer-lodash-typecheck', rule, { | ||
valid: [{ | ||
code: 'var x = a instanceof B' | ||
}, { | ||
code: 'var x = a > b ? a : b' | ||
}, { | ||
code: 'var x = typeof a === typeof b' | ||
}], | ||
invalid: [{ | ||
code: 'var x = typeof a === "number"', | ||
errors: errors.typeof | ||
}, { | ||
code: 'var x = "number" !== typeof a', | ||
errors: errors.typeof | ||
}, { | ||
code: 'var x = a instanceof Array', | ||
errors: errors.instanceof | ||
}] | ||
}); |