Skip to content

Commit

Permalink
Require Node.js 8 and improve error messages and docs
Browse files Browse the repository at this point in the history
  • Loading branch information
sindresorhus committed May 22, 2019
1 parent d440daa commit 9b9d761
Show file tree
Hide file tree
Showing 67 changed files with 290 additions and 211 deletions.
3 changes: 1 addition & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,9 @@ node_js:
- '12'
- '10'
- '8'
- '6'
matrix:
include:
- node_js: 12
- node_js: '12'
env: INTEGRATION=true
script:
- if [ $INTEGRATION == true ]; then npm run integration; else npm test; fi
Expand Down
16 changes: 13 additions & 3 deletions docs/rules/catch-error-name.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,16 +82,26 @@ somePromise.catch(_ => {
You can set the `name` option like this:

```js
"unicorn/catch-error-name": ["error", {"name": "error"}]
"unicorn/catch-error-name": [
"error",
{
"name": "error"
}
]
```

### caughtErrorsIgnorePattern

```js
"unicorn/catch-error-name": ["error", {"caughtErrorsIgnorePattern": "^_$"}]
"unicorn/catch-error-name": [
"error",
{
"caughtErrorsIgnorePattern": "^_$"
}
]
```

This option lets you specify a regex pattern for matches to ignore. Default is `^_$`.
This option lets you specify a regex pattern for matches to ignore. The default is `^_$`.

With `^unicorn$`, this would fail:

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/custom-error-definition.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces the only valid way of `Error` subclassing. It works with any super class that ends in `Error`.

This rule is fixable.


## Fail

Expand Down
8 changes: 4 additions & 4 deletions docs/rules/error-message.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ throw new TypeError();
```

```js
const err = new Error();
throw err;
const error = new Error();
throw error;
```


Expand All @@ -34,6 +34,6 @@ throw new TypeError('Foo');
```

```js
const err = new Error('Foo');
throw err;
const error = new Error('Foo');
throw error;
```
2 changes: 2 additions & 0 deletions docs/rules/escape-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces defining escape sequence values with uppercase characters rather than lowercase ones. This promotes readability by making the escaped value more distinguishable from the identifier.

This rule is fixable.


## Fail

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/explicit-length-check.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforce explicitly checking the length of a value array in an `if` condition, rather than checking the truthiness of the length.

This rule is partly fixable.

### Fail

```js
Expand Down Expand Up @@ -57,5 +59,3 @@ The `non-zero` option can be configured with one of the following:
- Enforces non-zero to be checked with: `array.length > 0`
- `greater-than-or-equal`
- Enforces non-zero to be checked with: `array.length >= 1`

It can be auto-fixed.
9 changes: 7 additions & 2 deletions docs/rules/filename-case.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Enforce a case style for filenames

Enforces all linted files to have their names in a certain case style. Default is `kebabCase`.
Enforces all linted files to have their names in a certain case style. The default is `kebabCase`.

Files named `index.js` are ignored as they can't change case (Only a problem with `pascalCase`).

Expand Down Expand Up @@ -35,5 +35,10 @@ Files named `index.js` are ignored as they can't change case (Only a problem wit
You can set the `case` option like this:

```js
"unicorn/filename-case": ["error", {"case": "kebabCase"}]
"unicorn/filename-case": [
"error",
{
"case": "kebabCase"
}
]
```
2 changes: 2 additions & 0 deletions docs/rules/import-index.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces importing index file with `.` instead of `./`, `./index` or `./index.js`.

This rule is fixable.


## Fail

Expand Down
2 changes: 1 addition & 1 deletion docs/rules/new-for-builtins.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

They work the same, but `new` should be preferred for consistency with other constructors.

Enforces the use of `new` for following builtins.
Enforces the use of `new` for following builtins:

- `Object`
- `Array`
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-array-instanceof.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The `instanceof Array` check doesn't work across realms/contexts, for example, frames/windows in browsers or the `vm` module in Node.js.

This rule is fixable.


## Fail

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-console-spaces.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

The [`console.log()` method](https://developer.mozilla.org/en-US/docs/Web/API/Console/log) and similar methods joins the parameters with a space, so adding a leading/trailing space to a parameter, results in two spaces being added.

This rule is fixable.


## Fail

Expand Down
4 changes: 3 additions & 1 deletion docs/rules/no-fn-reference-in-iterator.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Prevents passing a function reference directly to iterator methods
# Prevent passing a function reference directly to iterator methods

Suppose you have a `unicorn` module:

Expand Down Expand Up @@ -30,6 +30,8 @@ const unicorn = require('unicorn');
//=> [2, 3, 5]
```

This rule is fixable.


## Fail

Expand Down
4 changes: 3 additions & 1 deletion docs/rules/no-for-loop.md
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
# Do not use a `for` loop that can be replaced with a `for-of` loop

There's no reason to use old school for loops anymore for the common case. You can instead use for-of loop (with `.entries()` if you need to access the index).
Off-by-one errors is one of the most common bugs in software. [Swift actually removed this completely from the language.](https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md).

Off-by-one errors are one of the most common bugs in software. [Swift actually removed this completely from the language.](https://github.com/apple/swift-evolution/blob/master/proposals/0004-remove-pre-post-inc-decrement.md).

This rule is fixable unless index or element variables were used outside of the loop.


## Fail

```js
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-hex-escape.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Enforce the use of unicode escapes instead of hexadecimal escapes
# Enforce the use of Unicode escapes instead of hexadecimal escapes

Enforces a convention of using [unicode escapes](https://mathiasbynens.be/notes/javascript-escapes#unicode) instead of [hexadecimal escapes](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) for consistency and clarity.
Enforces a convention of using [Unicode escapes](https://mathiasbynens.be/notes/javascript-escapes#unicode) instead of [hexadecimal escapes](https://mathiasbynens.be/notes/javascript-escapes#hexadecimal) for consistency and clarity.

This rule is fixable.


## Fail
Expand Down
14 changes: 8 additions & 6 deletions docs/rules/no-new-buffer.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,26 +2,28 @@

Enforces the use of [Buffer.from](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_from_array) and [Buffer.alloc()](https://nodejs.org/api/buffer.html#buffer_class_method_buffer_alloc_size_fill_encoding) instead of [new Buffer()](https://nodejs.org/api/buffer.html#buffer_new_buffer_array), which has been deprecated since Node.js 4.

This rule is fixable.


## Fail

```js
const buf = new Buffer('7468697320697320612074c3a97374', 'hex');
const buf = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
const buffer = new Buffer('7468697320697320612074c3a97374', 'hex');
const buffer = new Buffer([0x62, 0x75, 0x66, 0x66, 0x65, 0x72]);
```

```js
const buf = new Buffer(10);
const buffer = new Buffer(10);
```


## Pass

```js
const buf = Buffer.from('7468697320697320612074c3a97374', 'hex');
const buf = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
const buffer = Buffer.from('7468697320697320612074c3a97374', 'hex');
const buffer = Buffer.from([0x62, 0x75, 0x66, 0x66, 0x65, 0x72])
```

```js
const buf = Buffer.alloc(10);
const buffer = Buffer.alloc(10);
```
1 change: 1 addition & 0 deletions docs/rules/no-unsafe-regex.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

Uses [safe-regex](https://github.com/substack/safe-regex) to disallow potentially [catastrophic](http://regular-expressions.mobi/catastrophic.html) [exponential-time](http://perlgeek.de/blog-en/perl-tips/in-search-of-an-exponetial-regexp.html) regular expressions.


## Fail

```js
Expand Down
3 changes: 3 additions & 0 deletions docs/rules/no-unused-properties.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ Unused properties, much like unused variables, are often a result of incomplete

This rule is primarily useful when you use objects to group constants or model enumerations. It is much harder to predict class properties usage, and practically impossible to predict reflective property access. This rule ignores cases like that.


## Example use cases

When using [React](https://reactjs.org)'s inline styles or one of [many CSS-in-JS](http://michelebertoli.github.io/css-in-js/) like [glamor](https://github.com/threepointone/glamor), one might find it helpful to group component styles into a constant object. Later you might remove one of the styles, but forget to remove its definition, especially if the component grew in complexity by that time. If these were defined as separate constants, ESLint's builtin `no-unused-vars` rule would have helped, but they are not. That's when the `no-unused-properties` rules becomes useful.
Expand All @@ -27,6 +28,7 @@ const ClassName = {
};
```


## Fail

```js
Expand Down Expand Up @@ -64,6 +66,7 @@ const foo = {
};
```


## Scope and limitations

This rule tries hard not to report potentially used properties as unused. Basically, all supported use cases are enum-like as shown above, more complex cases are ignored. Detailed list of limitations follows.
Expand Down
1 change: 1 addition & 0 deletions docs/rules/no-zero-fractions.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ There is no difference in JavaScript between, for example, `1`, `1.0` and `1.`,

This rule is fixable.


## Fail

```js
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/number-literal-case.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces a convention of defining number literals where the literal identifier is written in lowercase and the value in uppercase. Differentiating the casing of the identifier and value clearly separates them and makes your code more readable.

This rule is fixable.


## Fail

Expand Down
15 changes: 11 additions & 4 deletions docs/rules/prefer-add-event-listener.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
# Prefer `addEventListener` and `removeEventListener` over `on`-functions
# Prefer `.addEventListener()` and `.removeEventListener()` over `on`-functions

Enforces the use of `addEventListener` and `removeEventListener` over their `on` counterparts. For example, `foo.addEventListener('click', handler);` is preferred over `foo.onclick = handler;` for HTML DOM Events. There are [numerous advantages of using `addEventListener`](https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick/35093997#35093997). Some of these advantages include registering unlimited event handlers and optionally having the event handler invoked only once.
Enforces the use of `.addEventListener()` and `.removeEventListener()` over their `on`-function counterparts. For example, `foo.addEventListener('click', handler);` is preferred over `foo.onclick = handler;` for HTML DOM Events. There are [numerous advantages of using `addEventListener`](https://stackoverflow.com/questions/6348494/addeventlistener-vs-onclick/35093997#35093997). Some of these advantages include registering unlimited event handlers and optionally having the event handler invoked only once.

This rule is fixable (only for `addEventListener`).
This rule is fixable (only for `.addEventListener()`).


## Fail
Expand Down Expand Up @@ -47,7 +47,14 @@ foo.removeEventListener('click', onClick);
### excludedPackages

```js
"unicorn/prefer-add-event-listener": ["error", {"excludedPackages": ["koa", "sax"]}]
"unicorn/prefer-add-event-listener": [
"error", {
"excludedPackages": [
"koa",
"sax"
]
}
]
```

This option lets you specify a list of packages that disable the rule when imported. By default, `koa` and `sax` are listed.
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/prefer-exponentiation-operator.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces the use of the [exponentiation operator](http://2ality.com/2016/02/exponentiation-operator.html) over [`Math.pow()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Math/pow).

This rule is fixable.


## Fail

Expand Down
1 change: 1 addition & 0 deletions docs/rules/prefer-flat-map.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ This rule is fixable.
[1, 2, 3].map(i => [i]).flat();
```


## Pass

```js
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/prefer-includes.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ All built-ins have `.includes()` in addition to `.indexOf()`. Prefer `.includes(

This rule is fixable.


## Fail

```js
Expand All @@ -14,6 +15,7 @@ str.indexOf('foo') > -1;
x.indexOf('foo') === -1
```


## Pass

```js
Expand Down
5 changes: 3 additions & 2 deletions docs/rules/prefer-node-append.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer `append` over `appendChild`
# Prefer `Node#append()` over `Node#appendChild()`

Enforces the use of, for example, `document.body.append(div);` over `document.body.appendChild(div);` for DOM nodes. There are [some advantages of using `.append()`](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append), like the ability to append multiple nodes and to append both [`DOMString`](https://developer.mozilla.org/en-US/docs/Web/API/DOMString) and DOM node objects.
Enforces the use of, for example, `document.body.append(div);` over `document.body.appendChild(div);` for DOM nodes. There are [some advantages of using `Node#append()`](https://developer.mozilla.org/en-US/docs/Web/API/ParentNode/append), like the ability to append multiple nodes and to append both [`DOMString`](https://developer.mozilla.org/en-US/docs/Web/API/DOMString) and DOM node objects.

This rule is fixable.

Expand All @@ -11,6 +11,7 @@ This rule is fixable.
foo.appendChild(bar);
```


## Pass

```js
Expand Down
5 changes: 3 additions & 2 deletions docs/rules/prefer-node-remove.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer `remove` over `parentNode.removeChild` and `parentElement.removeChild`
# Prefer `node.remove()` over `parentNode.removeChild(node)` and `parentElement.removeChild(node)`

Enforces the use of, for example, `child.remove();` over `child.parentNode.removeChild(child);` and `child.parentElement.removeChild(child);` for DOM nodes. The DOM function [`.remove()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove) is preferred over the indirect removal of an object with [`.removeChild()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild).
Enforces the use of, for example, `child.remove();` over `child.parentNode.removeChild(child);` and `child.parentElement.removeChild(child);` for DOM nodes. The DOM function [`Node#remove()`](https://developer.mozilla.org/en-US/docs/Web/API/ChildNode/remove) is preferred over the indirect removal of an object with [`Node#removeChild()`](https://developer.mozilla.org/en-US/docs/Web/API/Node/removeChild).

This rule is fixable.

Expand All @@ -18,6 +18,7 @@ this.parentNode.removeChild(foo);
this.parentElement.removeChild(foo);
```


## Pass

```js
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/prefer-query-selector.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Prefer `querySelector` over `getElementById`, `querySelectorAll` over `getElementsByClassName` and `getElementsByTagName`
# Prefer `.querySelector()` over `.getElementById()`, `.querySelectorAll()` over `.getElementsByClassName()` and `.getElementsByTagName()`

They are not faster than `querySelector` and it's better to be consistent.
They are not faster than `.querySelector()` and it's better to be consistent.

This rule is partly fixable.


## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/prefer-spread.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

Enforces the use of the spread operator over `Array.from()`. This rule adds on to the built-in [prefer-spread](https://eslint.org/docs/rules/prefer-spread) rule, which only flags uses of `.apply()`. Does not enforce for `TypedArray.from()`;

This rule is fixable.


## Fail

Expand Down
4 changes: 2 additions & 2 deletions docs/rules/prefer-starts-ends-with.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Prefer `String#startsWith` & `String#endsWith` over more complex alternatives
# Prefer `String#startsWith()` & `String#endsWith()` over more complex alternatives

There are several ways of checking whether a string starts or ends with a certain string, such as `string.indexOf('foo') === 0` or using regexes with `/^foo/` or `/foo$/`. ES2015 introduced simpler alternatives named [`String#startsWith`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). This rule enforces the use of those whenever possible.
There are several ways of checking whether a string starts or ends with a certain string, such as `string.indexOf('foo') === 0` or using a regex with `/^foo/` or `/foo$/`. ES2015 introduced simpler alternatives named [`String#startsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/startsWith) and [`String#endsWith()`](https://developer.mozilla.org/en/docs/Web/JavaScript/Reference/Global_Objects/String/endsWith). This rule enforces the use of those whenever possible.


## Fail
Expand Down
Loading

0 comments on commit 9b9d761

Please sign in to comment.