eslint-plugin-no-for-each
What it does? Fix forEach
, for in
, and for of
loops.
Before:
After:
['error', 'cache-length']
['error']
https://jsperf.com/foreach-vs-for-loop-vs-for-in-vs-for-of-vs-babel-for-of
Installation
You'll first need to install ESLint:
$ npm i eslint --save-dev
Next, install eslint-plugin-no-for-each
:
$ npm install eslint-plugin-no-for-each --save-dev
Note: If you installed ESLint globally (using the -g
flag) then you must also install eslint-plugin-no-for-each
globally.
Configuration
Add no-for-each
to the plugins section of your .eslintrc
configuration file.
Then configure the rules you want to use under the rules section.
- default:
1
or["error"]
- cache-length:
2
two additional WIP rules
"rules":
Resources
- https://www.kenneth-truyers.net/2016/05/27/writing-custom-eslint-rules/
- https://github.com/airbnb/javascript/issues/851
- https://insideops.wordpress.com/2015/12/08/creating-custom-rules-for-eslint/
- https://medium.com/tumblbug-engineering/creating-an-eslint-plugin-87f1cb42767f#.z5t30ge87
- http://stackoverflow.com/questions/34130718/how-to-create-customized-eslint-rules/34186003
- http://stackoverflow.com/questions/38278273/eslint-code-vs-whitespace-values-for-fixable
- https://github.com/eslint/eslint/commit/a9a4652b2ce92858d90243e7bb8693f458a14783
- https://github.com/facebookincubator/create-react-app/issues/272
- https://github.com/facebookincubator/create-react-app/issues/274
- https://github.com/yannickcr/eslint-plugin-react/blob/master/lib/rules/jsx-equals-spacing.js#L18
- https://github.com/jfmengels/eslint-rule-documentation/blob/master/contributing.md
- https://github.com/Gillespie59/eslint-plugin-angular
- http://eslint.org/docs/developer-guide/working-with-rules
- http://eslint.org/blog/2016/07/eslint-new-rule-format
- http://eslint.org/docs/developer-guide/working-with-rules-new
- https://github.com/buildo/eslint-plugin-no-loops (had no fixing, was no loops in general)
- https://github.com/airbnb/javascript#iterators--nope
- https://www.paypal-engineering.com/2014/12/12/maintaining-javascript-code-quality-with-eslint/
- https://babeljs.io/repl/
- http://eslint.org/docs/rules/
known bugs
- properly parse
Object.keys(apples).forEach(apple => delete apples[apple])
because it replacesapple
with new var, but notapples
so it leaves thes
- nested loops, will do this soon
- truncating body at some point
jsperf
var testData = ;for var i = 0; i < 100; i++ testData; // forEachvar res = 0;testData; // forvar res = 0;for var i = 0; i < testDatalength; i++ res += testDatai; // for optimizedvar res = 0;for var i = 0 len = testDatalength; i < len; i++ res += testDatai; // reducevar res = testData; // whilevar res = 0;var i = testDatalength;while i-- res += testDatai; // for invar res = 0;for var data in testData res += testDatai; // for ofvar res = 0;for var data of testData res += testDatai; // for of babelvar res = 0;var _iteratorNormalCompletion = true;var _didIteratorError = false;var _iteratorError = undefined; try for var _iterator = testDataSymboliterator _step; !_iteratorNormalCompletion = _step = _iteratornextdone; _iteratorNormalCompletion = true var value = _stepvalue; console; catch err _didIteratorError = true; _iteratorError = err; finally try res += testDatai; if !_iteratorNormalCompletion && _iteratorreturn _iterator; finally if _didIteratorError throw _iteratorError;
@NOTES:
cache-length
but since we are using an object,
seems silly to call object.keys 2x
could keep option we do not use body.body because
-
- not always there
-
- we want to keep curly braces (or lack of from the source
(when it has proper configs and has large files to test, then release 1.0.0)
@TODO:
- add config option for
cache-length-inside-loop
(var i = 0, len = varName.length; ...) - recommend let => const rule
- add large js files to test failed fixing & safety
- isDev | isTest helper funcs
- ^ add args in tests for
devtest
- comment on readmes
- move todos to issues
- use type in the
left
of the loop such asconst
,let
,var
- o.hasOwnProperty for for in/of
- make a guide how to make an eslint plugin