1 | # eslint-plugin-extra-rules
|
2 |
|
3 | > Additional rules for eslint
|
4 |
|
5 | [![NPM][eslint-rules-icon] ][eslint-rules-url]
|
6 |
|
7 | [![Build status][eslint-rules-ci-image] ][eslint-rules-ci-url]
|
8 | [![dependencies][eslint-rules-dependencies-image] ][eslint-rules-dependencies-url]
|
9 | [![devdependencies][eslint-rules-devdependencies-image] ][eslint-rules-devdependencies-url]
|
10 | [![semantic-release][semantic-image] ][semantic-url]
|
11 | [![manpm](https://img.shields.io/badge/manpm-%E2%9C%93-3399ff.svg)](https://github.com/bahmutov/manpm)
|
12 |
|
13 | ## Install
|
14 |
|
15 | npm install --save-dev eslint-plugin-extra-rules
|
16 |
|
17 | ## Example Configuration
|
18 |
|
19 | Add to your `.eslintrc`:
|
20 |
|
21 | ``` js
|
22 | {
|
23 | "plugins": ["extra-rules"],
|
24 | "rules": {
|
25 | "extra-rules/no-commented-out-code": "warn",
|
26 | // Your other rules...
|
27 | }
|
28 | }
|
29 | ```
|
30 |
|
31 | ## Rules
|
32 |
|
33 | ### no-commented-out-code
|
34 |
|
35 | > Detects code in the single or multiline comments
|
36 |
|
37 | ```js
|
38 | /* eslint extra-rules/no-commented-out-code: "warn" */
|
39 | /*
|
40 | function foo() {
|
41 | return 'foo';
|
42 | }*/
|
43 | // this is normal comment
|
44 | function baz() {
|
45 | 'use strict';
|
46 | // and this is another normal comment
|
47 | // var bar = 'bar';
|
48 | return 'baz';
|
49 | }
|
50 | ```
|
51 |
|
52 | Produces the following output:
|
53 |
|
54 | 2:0 warning commented out code "function foo() {" (4 lines) no-commented-out-code
|
55 | 10:2 warning commented out code "var bar = 'bar';" (1 line) no-commented-out-code
|
56 |
|
57 | ### no-long-files
|
58 |
|
59 | > Detect source files with too many lines
|
60 |
|
61 | first argument: rule severity (0 - no check, 1 - warning, 2 - error)
|
62 | second argument: max number of allowed lines
|
63 | "no-long-files": [2, 70]
|
64 |
|
65 | Prints something like
|
66 |
|
67 | potential-point-free.js
|
68 | 0:0 error file line count 51 exceeded line limit 50 no-long-files
|
69 |
|
70 | ### camel_case
|
71 |
|
72 | > ESLint rule for enforcing camelCame names but allowing _ in property names
|
73 |
|
74 | We want to use camelCase in variable names, but want to still allow
|
75 | underscores in JSON objects:
|
76 |
|
77 | var goodObject = {
|
78 | property_name: 1,
|
79 | another_property: 2
|
80 | };
|
81 |
|
82 | [jshint](http://jshint.com/docs/) has *camelcase* rule that forces EVERY name
|
83 | to be camelCased
|
84 |
|
85 | $ jshint index.js
|
86 | index.js: line 2, col 0, Identifier 'property_name' is not in camel case.
|
87 | index.js: line 3, col 0, Identifier 'another_property' is not in camel case.
|
88 | 2 errors
|
89 |
|
90 | There are manual workarounds:
|
91 |
|
92 | * disable this specific rule using `// jshint ignore:lint` or `// jshint -W106`
|
93 | * write property names using quotes, for example `'property_name': 1`
|
94 |
|
95 | Both workarounds are hacky.
|
96 |
|
97 | I wrote a more flexible rule called [camel_case](camel_case.js)
|
98 | for [eslint](https://github.com/eslint/eslint). The rule looks one character *after*
|
99 | the identifier to see if it is followed by colon `:` character.
|
100 | If yes, this is a property name inside an object, and underscore character `_` is allowed.
|
101 |
|
102 | ### no-for-loops
|
103 |
|
104 | Warns or errors if you use for loops in your code. I consider for loops harmful for their side effects,
|
105 | and even consider `.forEach` dangerous, see [Avoid forEach][avoid forEach].
|
106 |
|
107 | ### no-single-line-objects
|
108 |
|
109 | Does not allow you to nest objects into single line. Single property object can be single line
|
110 |
|
111 | ```js
|
112 | // allowed
|
113 | var foo = { foo: 'foo' };
|
114 | // not allowed
|
115 | var foo = { foo: 'foo', bar: 'bar' };
|
116 | var foo = { foo: { bar: 'bar' } };
|
117 | ```
|
118 |
|
119 | ### potential-point-free
|
120 |
|
121 | Warns if a function just calls another function passing arguments and can potentially
|
122 | become point-free. Point-free programming [eliminates complexity and superfluous variables][point-free].
|
123 | Only functions with single call expression are considered. The arguments must match exactly.
|
124 |
|
125 | ```js
|
126 | /* eslint extra-rules/potential-point-free: "warn" */
|
127 | function print(x) {
|
128 | console.log(x);
|
129 | }
|
130 | [1, 2, 3].forEach(function printX(x) {
|
131 | print(x);
|
132 | });
|
133 | // output 7:18 warning printX potential-point-free
|
134 | ```
|
135 |
|
136 | Note: due to signatures and optional arguments, sometimes functions should not be point free directly.
|
137 | For example the array iterators pass item, index and the array itself, which causes problems for `parseInt`
|
138 |
|
139 | ```js
|
140 | ['1', '2', '3'].forEach(parseInt);
|
141 | // [1, 'NaN', 'NaN']
|
142 | ```
|
143 |
|
144 | In this case, you can use [unary adaptor](http://glebbahmutov.com/blog/iterator-callbacks/) or
|
145 | 3rd party iterator with simpler signature, [R.forEach](http://ramdajs.com/docs/R.html#forEach).
|
146 |
|
147 | ## Small print
|
148 |
|
149 | Author: Gleb Bahmutov © 2014
|
150 |
|
151 | * [@bahmutov](https://twitter.com/bahmutov)
|
152 | * [glebbahmutov.com](http://glebbahmutov.com)
|
153 | * [blog](http://glebbahmutov.com/blog/)
|
154 |
|
155 | License: MIT - do anything with the code, but don't blame me if it does not work.
|
156 |
|
157 | Spread the word: tweet, star on github, etc.
|
158 |
|
159 | Support: if you find any problems with this module, email / tweet /
|
160 | [open issue](https://github.com/bahmutov/eslint-rules/issues) on Github
|
161 |
|
162 |
|
163 | ## MIT License
|
164 |
|
165 | Copyright (c) 2014 Gleb Bahmutov
|
166 |
|
167 | Permission is hereby granted, free of charge, to any person
|
168 | obtaining a copy of this software and associated documentation
|
169 | files (the "Software"), to deal in the Software without
|
170 | restriction, including without limitation the rights to use,
|
171 | copy, modify, merge, publish, distribute, sublicense, and/or sell
|
172 | copies of the Software, and to permit persons to whom the
|
173 | Software is furnished to do so, subject to the following
|
174 | conditions:
|
175 |
|
176 | The above copyright notice and this permission notice shall be
|
177 | included in all copies or substantial portions of the Software.
|
178 |
|
179 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
180 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
|
181 | OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
182 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
|
183 | HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
|
184 | WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
185 | FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
|
186 | OTHER DEALINGS IN THE SOFTWARE.
|
187 |
|
188 | [eslint-rules-icon]: https://nodei.co/npm/eslint-plugin-extra-rules.png?downloads=true
|
189 | [eslint-rules-url]: https://npmjs.org/package/eslint-plugin-extra-rules
|
190 | [eslint-rules-ci-image]: https://travis-ci.org/bahmutov/eslint-rules.png?branch=master
|
191 | [eslint-rules-ci-url]: https://travis-ci.org/bahmutov/eslint-rules
|
192 | [eslint-rules-dependencies-image]: https://david-dm.org/bahmutov/eslint-rules.png
|
193 | [eslint-rules-dependencies-url]: https://david-dm.org/bahmutov/eslint-rules
|
194 | [eslint-rules-devdependencies-image]: https://david-dm.org/bahmutov/eslint-rules/dev-status.png
|
195 | [eslint-rules-devdependencies-url]: https://david-dm.org/bahmutov/eslint-rules#info=devDependencies
|
196 | [semantic-image]: https://img.shields.io/badge/%20%20%F0%9F%93%A6%F0%9F%9A%80-semantic--release-e10079.svg
|
197 | [semantic-url]: https://github.com/semantic-release/semantic-release
|
198 |
|
199 | [avoid forEach]: http://aeflash.com/2014-11/avoid-foreach.html
|
200 | [point-free]: http://glebbahmutov.com/blog/point-free-programming-is-not-pointless/
|