Skip to content

Commit

Permalink
Add standardized recommended/fixable/suggestions notices to rule docs (
Browse files Browse the repository at this point in the history
  • Loading branch information
bmish authored Nov 10, 2021
1 parent 0485924 commit ed33dd8
Show file tree
Hide file tree
Showing 96 changed files with 395 additions and 108 deletions.
6 changes: 4 additions & 2 deletions docs/rules/better-regex.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Improve regexes by making them shorter, consistent, and safer

Note: This rule uses [`regexp-tree`](https://github.com/DmitrySoshnikov/regexp-tree) and [`clean-regexp`](https://github.com/samverschueren/clean-regexp) under the hood.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
Note: This rule uses [`regexp-tree`](https://github.com/DmitrySoshnikov/regexp-tree) and [`clean-regexp`](https://github.com/samverschueren/clean-regexp) under the hood.

## Fail

Expand Down
6 changes: 4 additions & 2 deletions docs/rules/catch-error-name.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Enforce a specific parameter name in catch clauses

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

Applies to

- `try/catch` clauses handlers
Expand All @@ -14,8 +18,6 @@ The following names are ignored:
- Descriptive names, for example, `fsError` or `authError`.
- Names matching [`options.ignore`](#ignore).

This rule is fixable.

## Fail

```js
Expand Down
6 changes: 5 additions & 1 deletion docs/rules/consistent-destructuring.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Use destructured variables over properties

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

Enforces the use of already destructured objects and their variables over accessing each property individually. Previous destructurings are easily missed which leads to an inconsistent code style.

This rule is partly fixable. It does not fix nested destructuring.
Expand All @@ -13,7 +17,7 @@ console.log(a, foo.b);

```js
const {a} = foo;
console.log(foo.a);
console.log(foo.a);
```

```js
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/consistent-function-scoping.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Move function definitions to the highest possible scope

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

A function definition should be placed as close to the top-level scope as possible without breaking its captured values. This improves readability, [directly improves performance](https://stackoverflow.com/a/81329/207247) and allows JavaScript engines to [better optimize performance](https://ponyfoo.com/articles/javascript-performance-pitfalls-v8#optimization-limit).


Expand Down
6 changes: 4 additions & 2 deletions docs/rules/custom-error-definition.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Enforce correct `Error` subclassing

Enforces the only valid way of `Error` subclassing. It works with any super class that ends in `Error`.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

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


## Fail
Expand Down
4 changes: 3 additions & 1 deletion docs/rules/empty-brace-spaces.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Enforce no spaces between braces

This rule is fixable.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

## Fail

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/error-message.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Enforce passing a `message` value when creating a built-in error

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

This rule enforces a `message` value to be passed in when creating an instance of a built-in [`Error`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Error) object, which leads to more readable and debuggable code.


Expand Down
6 changes: 4 additions & 2 deletions docs/rules/escape-case.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Require escape sequences to use uppercase values

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 part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
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.


## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/expiring-todo-comments.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Add expiration conditions to TODO comments

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

This rule makes it possible to pass arguments to TODO, FIXME and XXX comments to trigger ESLint to report.

From [ESLint's documentation](https://eslint.org/docs/rules/no-warning-comments):
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/explicit-length-check.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Enforce explicitly comparing the `length` or `size` property of a value

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

Enforce explicitly checking the length of an object and enforce the comparison style.

This rule is fixable, unless it's [unsafe to fix](#unsafe-to-fix-case).
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/filename-case.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Enforce a case style for filenames

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

Enforces all linted files to have their names in a certain case style and lowercase file extension. The default is `kebabCase`.

Files named `index.js`, `index.mjs`, `index.cjs`, `index.ts`, `index.tsx`, `index.vue` are ignored as they can't change case (Only a problem with `pascalCase`).
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/import-index.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Enforce importing index files with `.`

Enforces importing index file with `.` instead of `./`, `./index` or `./index.js`.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
Enforces importing index file with `.` instead of `./`, `./index` or `./index.js`.


## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/import-style.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Enforce specific import styles per module

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

Sometimes a module contains unrelated functions, like `util`, thus it is a good practice to enforce destructuring or named imports here. Other times, in modules like `path`, it is good to use default import as they have similar functions, all likely to be utilized.

This rule defines 4 import styles:
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/new-for-builtins.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Enforce the use of `new` for all builtins, except `String`, `Number`, `Boolean`, `Symbol` and `BigInt`

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

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

Enforces the use of `new` for following builtins:
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-abusive-eslint-disable.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Enforce specifying rules to disable in `eslint-disable` comments

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

This rule makes you specify the rules you want to disable when using `eslint-disable`, `eslint-disable-line` or `eslint-disable-next-line` comments.

If you want to disable an ESLint rule in a file or on a specific line, you can add a comment.
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-array-callback-reference.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Prevent passing a function reference directly to iterator methods

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

💡 *This rule provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

Suppose you have a `unicorn` module:

```js
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-array-for-each.md
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
# Prefer `for…of` over `Array#forEach(…)`

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

Benefits of [`for…of` statement](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Statements/for...of) over [`Array#forEach(…)`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/forEach) can include:

- Faster
- Better readability
- Ability to exit early with `break` or `return`

This rule is partly fixable.

## Fail

```js
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-array-method-this-argument.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Disallow using the `this` argument in array methods

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

The rule forbids using the `thisArg` argument in array methods:

- If the callback is an arrow function or a bound function, the `thisArg` won't affect it.
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-array-push-push.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Enforce combining multiple `Array#push()` into one call

[`Array#push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) accepts multiple arguments. Multiple calls should be combined into one.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

This rule is partly fixable.
[`Array#push()`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Array/push) accepts multiple arguments. Multiple calls should be combined into one.

## Fail

Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-array-reduce.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow `Array#reduce()` and `Array#reduceRight()`

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

`Array#reduce()` and `Array#reduceRight()` usually result in [hard-to-read](https://twitter.com/jaffathecake/status/1213077702300852224) and [less performant](https://www.richsnapp.com/article/2019/06-09-reduce-spread-anti-pattern) code. In almost every case, it can be replaced by `.map`, `.filter`, or a `for-of` loop.

It's only somewhat useful in the rare case of summing up numbers, which is allowed by default.
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-console-spaces.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Do not use leading/trailing space between `console.log` parameters

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 part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
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.


## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-document-cookie.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Do not use `document.cookie` directly

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

It's not recommended to use [`document.cookie`](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie) directly as it's easy to get the string wrong. Instead, you should use the [Cookie Store API](https://developer.mozilla.org/en-US/docs/Web/API/Cookie_Store_API) or a [cookie library](https://www.npmjs.com/search?q=cookie).

## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-empty-file.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow empty files

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

Meaningless files clutter a codebase.

Disallow any files only containing the following:
Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-for-loop.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Do not use a `for` loop that can be replaced with a `for-of` loop

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

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 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/0007-remove-c-style-for-loops.md).
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,8 +1,10 @@
# 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.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
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.


## Fail
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-instanceof-array.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Require `Array.isArray()` instead of `instanceof Array`

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 part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

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


## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-invalid-remove-event-listener.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Prevent calling `EventTarget#removeEventListener()` with the result of an expression

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

The [`removeEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/removeEventListener) function must be called with a reference to the same function that was passed to [`addEventListener`](https://developer.mozilla.org/en-US/docs/Web/API/EventTarget/addEventListener). Calling `removeEventListener` with an inline function or the result of an inline `.bind()` call is indicative of an error, and won't actually remove the listener.

## Fail
Expand Down
2 changes: 2 additions & 0 deletions docs/rules/no-keyword-prefix.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Disallow identifiers starting with `new` or `class`

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

`new Foo` and `newFoo` look very similar. Use alternatives that do not look like keyword usage.

## Fail
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-lonely-if.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Disallow `if` statements as the only statement in `if` blocks without `else`

This rule adds onto the built-in [`no-lonely-if`](https://eslint.org/docs/rules/no-lonely-if) rule, which only forbids `if` statements in `else`, not in `if`. It is recommended to use `unicorn/no-lonely-if` together with the core ESLint `no-lonely-if` rule.
*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

This rule is fixable.
This rule adds onto the built-in [`no-lonely-if`](https://eslint.org/docs/rules/no-lonely-if) rule, which only forbids `if` statements in `else`, not in `if`. It is recommended to use `unicorn/no-lonely-if` together with the core ESLint `no-lonely-if` rule.

## Fail

Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-nested-ternary.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Disallow nested ternary expressions

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems).*

Improved version of the [`no-nested-ternary`](https://eslint.org/docs/rules/no-nested-ternary) ESLint rule, which allows cases where the nested ternary is only one level and wrapped in parens.


Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-new-array.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Disallow `new Array()`

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

The ESLint built-in rule [`no-array-constructor`](https://eslint.org/docs/rules/no-array-constructor) enforces using an array literal instead of the `Array` constructor, but it still allows using the `Array` constructor with **one** argument. This rule fills that gap.

When using the `Array` constructor with one argument, it's not clear whether the argument is meant to be the length of the array or the only element.
Expand Down
6 changes: 4 additions & 2 deletions docs/rules/no-new-buffer.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Enforce the use of `Buffer.from()` and `Buffer.alloc()` instead of the deprecated `new Buffer()`

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 part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

This rule is partly fixable.
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.

## Fail

Expand Down
4 changes: 4 additions & 0 deletions docs/rules/no-null.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Disallow the use of the `null` literal

*This rule is part of the [recommended](https://github.com/sindresorhus/eslint-plugin-unicorn#recommended-config) config.*

🔧💡 *This rule is [auto-fixable](https://eslint.org/docs/user-guide/command-line-interface#fixing-problems) and provides [suggestions](https://eslint.org/docs/developer-guide/working-with-rules#providing-suggestions).*

Disallow the use of the `null` literal, to encourage using `undefined` instead.

## Fail
Expand Down
Loading

0 comments on commit ed33dd8

Please sign in to comment.