UNPKG

6.72 kBMarkdownView Raw
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
19Add 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/*
40function foo() {
41 return 'foo';
42}*/
43// this is normal comment
44function baz() {
45 'use strict';
46 // and this is another normal comment
47 // var bar = 'bar';
48 return 'baz';
49}
50```
51
52Produces 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
65Prints 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
74We want to use camelCase in variable names, but want to still allow
75underscores 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
83to 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
90There 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
95Both workarounds are hacky.
96
97I wrote a more flexible rule called [camel_case](camel_case.js)
98for [eslint](https://github.com/eslint/eslint). The rule looks one character *after*
99the identifier to see if it is followed by colon `:` character.
100If yes, this is a property name inside an object, and underscore character `_` is allowed.
101
102### no-for-loops
103
104Warns or errors if you use for loops in your code. I consider for loops harmful for their side effects,
105and even consider `.forEach` dangerous, see [Avoid forEach][avoid forEach].
106
107### no-single-line-objects
108
109Does not allow you to nest objects into single line. Single property object can be single line
110
111```js
112// allowed
113var foo = { foo: 'foo' };
114// not allowed
115var foo = { foo: 'foo', bar: 'bar' };
116var foo = { foo: { bar: 'bar' } };
117```
118
119### potential-point-free
120
121Warns if a function just calls another function passing arguments and can potentially
122become point-free. Point-free programming [eliminates complexity and superfluous variables][point-free].
123Only functions with single call expression are considered. The arguments must match exactly.
124
125```js
126/* eslint extra-rules/potential-point-free: "warn" */
127function 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
136Note: due to signatures and optional arguments, sometimes functions should not be point free directly.
137For 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
144In this case, you can use [unary adaptor](http://glebbahmutov.com/blog/iterator-callbacks/) or
1453rd party iterator with simpler signature, [R.forEach](http://ramdajs.com/docs/R.html#forEach).
146
147## Small print
148
149Author: Gleb Bahmutov © 2014
150
151* [@bahmutov](https://twitter.com/bahmutov)
152* [glebbahmutov.com](http://glebbahmutov.com)
153* [blog](http://glebbahmutov.com/blog/)
154
155License: MIT - do anything with the code, but don't blame me if it does not work.
156
157Spread the word: tweet, star on github, etc.
158
159Support: 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
165Copyright (c) 2014 Gleb Bahmutov
166
167Permission is hereby granted, free of charge, to any person
168obtaining a copy of this software and associated documentation
169files (the "Software"), to deal in the Software without
170restriction, including without limitation the rights to use,
171copy, modify, merge, publish, distribute, sublicense, and/or sell
172copies of the Software, and to permit persons to whom the
173Software is furnished to do so, subject to the following
174conditions:
175
176The above copyright notice and this permission notice shall be
177included in all copies or substantial portions of the Software.
178
179THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
180EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
181OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
182NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
183HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
184WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
185FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
186OTHER 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/