🌜
🌞
eslint-plugin-unicorn

eslint-plugin-unicorn

v43.0.2

Various awesome ESLint rules

npm install eslint-plugin-unicorn

README

eslint-plugin-unicorn Coverage Status npm version

eslint plugin unicorn

More than 100 powerful ESLint rules

You might want to check out XO, which includes this plugin.

Propose or contribute a new rule ➑

Install

npm install --save-dev eslint eslint-plugin-unicorn

Usage

Use a preset config or configure each rules in package.json.

{
    "name": "my-awesome-project",
    "eslintConfig": {
        "env": {
            "es2022": true
        },
        "parserOptions": {
            "ecmaVersion": "latest",
            "sourceType": "module"
        },
        "plugins": [
            "unicorn"
        ],
        "rules": {
            "unicorn/better-regex": "error",
            "unicorn/…": "error"
        }
    }
}

Rules

Each rule has emojis denoting:

  • βœ… if it belongs to the recommended configuration
  • πŸ”§ if some problems reported by the rule are automatically fixable by the --fix command line option
  • πŸ’‘ if some problems reported by the rule are manually fixable by editor suggestions
Name                                         Description βœ… πŸ”§ πŸ’‘
better-regex Improve regexes by making them shorter, consistent, and safer. βœ… πŸ”§
catch-error-name Enforce a specific parameter name in catch clauses. βœ… πŸ”§
consistent-destructuring Use destructured variables over properties. βœ… πŸ”§ πŸ’‘
consistent-function-scoping Move function definitions to the highest possible scope. βœ…
custom-error-definition Enforce correct Error subclassing. πŸ”§
empty-brace-spaces Enforce no spaces between braces. βœ… πŸ”§
error-message Enforce passing a message value when creating a built-in error. βœ…
escape-case Require escape sequences to use uppercase values. βœ… πŸ”§
expiring-todo-comments Add expiration conditions to TODO comments. βœ…
explicit-length-check Enforce explicitly comparing the length or size property of a value. βœ… πŸ”§ πŸ’‘
filename-case Enforce a case style for filenames. βœ…
import-style Enforce specific import styles per module. βœ…
new-for-builtins Enforce the use of new for all builtins, except String, Number, Boolean, Symbol and BigInt. βœ… πŸ”§
no-abusive-eslint-disable Enforce specifying rules to disable in eslint-disable comments. βœ…
no-array-callback-reference Prevent passing a function reference directly to iterator methods. βœ… πŸ’‘
no-array-for-each Prefer for…of over the forEach method. βœ… πŸ”§ πŸ’‘
no-array-method-this-argument Disallow using the this argument in array methods. βœ… πŸ”§ πŸ’‘
no-array-push-push Enforce combining multiple Array#push() into one call. βœ… πŸ”§ πŸ’‘
no-array-reduce Disallow Array#reduce() and Array#reduceRight(). βœ…
no-await-expression-member Disallow member access from await expression. βœ… πŸ”§
no-console-spaces Do not use leading/trailing space between console.log parameters. βœ… πŸ”§
no-document-cookie Do not use document.cookie directly. βœ…
no-empty-file Disallow empty files. βœ…
no-for-loop Do not use a for loop that can be replaced with a for-of loop. βœ… πŸ”§
no-hex-escape Enforce the use of Unicode escapes instead of hexadecimal escapes. βœ… πŸ”§
no-instanceof-array Require Array.isArray() instead of instanceof Array. βœ… πŸ”§
no-invalid-remove-event-listener Prevent calling EventTarget#removeEventListener() with the result of an expression. βœ…
no-keyword-prefix Disallow identifiers starting with new or class.
no-lonely-if Disallow if statements as the only statement in if blocks without else. βœ… πŸ”§
no-nested-ternary Disallow nested ternary expressions. βœ… πŸ”§
no-new-array Disallow new Array(). βœ… πŸ”§ πŸ’‘
no-new-buffer Enforce the use of Buffer.from() and Buffer.alloc() instead of the deprecated new Buffer(). βœ… πŸ”§ πŸ’‘
no-null Disallow the use of the null literal. βœ… πŸ”§ πŸ’‘
no-object-as-default-parameter Disallow the use of objects as default parameters. βœ…
no-process-exit Disallow process.exit(). βœ…
no-static-only-class Disallow classes that only have static members. βœ… πŸ”§
no-thenable Disallow then property. βœ…
no-this-assignment Disallow assigning this to a variable. βœ…
no-unreadable-array-destructuring Disallow unreadable array destructuring. βœ… πŸ”§
no-unreadable-iife Disallow unreadable IIFEs. βœ…
no-unsafe-regex Disallow unsafe regular expressions.
no-unused-properties Disallow unused object properties.
no-useless-fallback-in-spread Disallow useless fallback when spreading in object literals. βœ… πŸ”§
no-useless-length-check Disallow useless array length check. βœ… πŸ”§
no-useless-promise-resolve-reject Disallow returning/yielding Promise.resolve/reject() in async functions or promise callbacks βœ… πŸ”§
no-useless-spread Disallow unnecessary spread. βœ… πŸ”§
no-useless-switch-case Disallow useless case in switch statements. βœ… πŸ’‘
no-useless-undefined Disallow useless undefined. βœ… πŸ”§
no-zero-fractions Disallow number literals with zero fractions or dangling dots. βœ… πŸ”§
number-literal-case Enforce proper case for numeric literals. βœ… πŸ”§
numeric-separators-style Enforce the style of numeric separators by correctly grouping digits. βœ… πŸ”§
prefer-add-event-listener Prefer .addEventListener() and .removeEventListener() over on-functions. βœ… πŸ”§
prefer-array-find Prefer .find(…) over the first element from .filter(…). βœ… πŸ”§ πŸ’‘
prefer-array-flat Prefer Array#flat() over legacy techniques to flatten arrays. βœ… πŸ”§
prefer-array-flat-map Prefer .flatMap(…) over .map(…).flat(). βœ… πŸ”§
prefer-array-index-of Prefer Array#indexOf() over Array#findIndex() when looking for the index of an item. βœ… πŸ”§ πŸ’‘
prefer-array-some Prefer .some(…) over .filter(…).length check and .find(…). βœ… πŸ”§ πŸ’‘
prefer-at Prefer .at() method for index access and String#charAt(). πŸ”§ πŸ’‘
prefer-code-point Prefer String#codePointAt(…) over String#charCodeAt(…) and String.fromCodePoint(…) over String.fromCharCode(…). βœ… πŸ’‘
prefer-date-now Prefer Date.now() to get the number of milliseconds since the Unix Epoch. βœ… πŸ”§
prefer-default-parameters Prefer default parameters over reassignment. βœ… πŸ”§ πŸ’‘
prefer-dom-node-append Prefer Node#append() over Node#appendChild(). βœ… πŸ”§
prefer-dom-node-dataset Prefer using .dataset on DOM elements over calling attribute methods. βœ… πŸ”§
prefer-dom-node-remove Prefer childNode.remove() over parentNode.removeChild(childNode). βœ… πŸ”§ πŸ’‘
prefer-dom-node-text-content Prefer .textContent over .innerText. βœ… πŸ’‘
prefer-event-target Prefer EventTarget over EventEmitter.
prefer-export-from Prefer export…from when re-exporting. βœ… πŸ”§ πŸ’‘
prefer-includes Prefer .includes() over .indexOf() and Array#some() when checking for existence or non-existence. βœ… πŸ”§ πŸ’‘
prefer-json-parse-buffer Prefer reading a JSON file as a buffer. πŸ”§
prefer-keyboard-event-key Prefer KeyboardEvent#key over KeyboardEvent#keyCode. βœ… πŸ”§
prefer-logical-operator-over-ternary Prefer using a logical operator over a ternary. βœ… πŸ’‘
prefer-math-trunc Enforce the use of Math.trunc instead of bitwise operators. βœ… πŸ”§ πŸ’‘
prefer-modern-dom-apis Prefer .before() over .insertBefore(), .replaceWith() over .replaceChild(), prefer one of .before(), .after(), .append() or .prepend() over insertAdjacentText() and insertAdjacentElement(). βœ… πŸ”§
prefer-modern-math-apis Prefer modern Math APIs over legacy patterns. βœ… πŸ”§
prefer-module Prefer JavaScript modules (ESM) over CommonJS. βœ… πŸ”§ πŸ’‘
prefer-native-coercion-functions Prefer using String, Number, BigInt, Boolean, and Symbol directly. βœ… πŸ”§
prefer-negative-index Prefer negative index over .length - index for {String,Array,TypedArray}#slice(), Array#splice() and Array#at(). βœ… πŸ”§
prefer-node-protocol Prefer using the node: protocol when importing Node.js builtin modules. βœ… πŸ”§
prefer-number-properties Prefer Number static properties over global ones. βœ… πŸ”§ πŸ’‘
prefer-object-from-entries Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object. βœ… πŸ”§
prefer-optional-catch-binding Prefer omitting the catch binding parameter. βœ… πŸ”§
prefer-prototype-methods Prefer borrowing methods from the prototype instead of the instance. βœ… πŸ”§
prefer-query-selector Prefer .querySelector() over .getElementById(), .querySelectorAll() over .getElementsByClassName() and .getElementsByTagName(). βœ… πŸ”§
prefer-reflect-apply Prefer Reflect.apply() over Function#apply(). βœ… πŸ”§
prefer-regexp-test Prefer RegExp#test() over String#match() and RegExp#exec(). βœ… πŸ”§
prefer-set-has Prefer Set#has() over Array#includes() when checking for existence or non-existence. βœ… πŸ”§ πŸ’‘
prefer-spread Prefer the spread operator over Array.from(…), Array#concat(…), Array#slice() and String#split(''). βœ… πŸ”§ πŸ’‘
prefer-string-replace-all Prefer String#replaceAll() over regex searches with the global flag. πŸ”§
prefer-string-slice Prefer String#slice() over String#substr() and String#substring(). βœ… πŸ”§
prefer-string-starts-ends-with Prefer String#startsWith() & String#endsWith() over RegExp#test(). βœ… πŸ”§ πŸ’‘
prefer-string-trim-start-end Prefer String#trimStart() / String#trimEnd() over String#trimLeft() / String#trimRight(). βœ… πŸ”§
prefer-switch Prefer switch over multiple else-if. βœ… πŸ”§
prefer-ternary Prefer ternary expressions over simple if-else statements. βœ… πŸ”§
prefer-top-level-await Prefer top-level await over top-level promises and async function calls. βœ… πŸ’‘
prefer-type-error Enforce throwing TypeError in type checking conditions. βœ… πŸ”§
prevent-abbreviations Prevent abbreviations. βœ… πŸ”§
relative-url-style Enforce consistent relative URL style. βœ… πŸ”§ πŸ’‘
require-array-join-separator Enforce using the separator argument with Array#join(). βœ… πŸ”§
require-number-to-fixed-digits-argument Enforce using the digits argument with Number#toFixed(). βœ… πŸ”§
require-post-message-target-origin Enforce using the targetOrigin argument with window.postMessage(). πŸ’‘
string-content Enforce better string content. πŸ”§ πŸ’‘
template-indent Fix whitespace-insensitive template indentation. βœ… πŸ”§
text-encoding-identifier-case Enforce consistent case for text encoding identifiers. βœ… πŸ”§ πŸ’‘
throw-new-error Require new when throwing an error. βœ… πŸ”§

Deprecated Rules

See docs/deprecated-rules.md

Preset configs

See the ESLint docs for more information about extending config files.

Note: Preset configs will also enable the correct parser options and environment.

This plugin exports a recommended config that enforces good practices.

{
    "name": "my-awesome-project",
    "eslintConfig": {
        "extends": "plugin:unicorn/recommended"
    }
}

All config

This plugin exports an all config that makes use of all rules (except for deprecated ones).

{
    "name": "my-awesome-project",
    "eslintConfig": {
        "extends": "plugin:unicorn/all"
    }
}

Maintainers

Former

Release Notes

43.0.2
By Sindre Sorhus β€’ Published on July 18, 2022
  • prefer-spread: Ignore Array#join().concat() and (a + b).concat() (#1859) ab71971

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v43.0.1...v43.0.2

43.0.1
By Sindre Sorhus β€’ Published on July 7, 2022
  • Remove prefer-event-target from the recommended preset 4d2faac
    • EventTarget requires Node.js 16 and we target Node.js 14. We'll enable it again in a year when we can target Node.js 16.

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v43.0.0...v43.0.1

43.0.0
By Sindre Sorhus β€’ Published on June 30, 2022

New rules

Breaking

  • Require Node.js 14

Improvements

  • Add prefer-top-level-await rule to recommended preset (#1803) 6f5ecf5
  • Deprecate import-index rule (#1787) 0034e69
  • Update abbreviations to support Next.js functions (#1804) 4b96057
  • text-encoding-identifier-case: Ignore JSX meta[charset="utf-8"] (#1817) c67a70f
  • prefer-number-properties: Check any use of global functions (#1834) 51d7e06
  • prefer-number-properties: Detect usage via global object (#1832) 80c4af2
  • no-array-for-each: Check reassign in for..in and for..of (#1824) d3b2548
  • no-useless-undefined: Ignore ref(undefined) in Vue project (#1828) e7306e5
  • prefer-node-protocol: Always check require() (#1827) 2c914d4
  • no-array-for-each: Improve parameter reassign detection (#1823) 56df468
  • no-array-for-each: Add fix for arrow function body (#1785) 59218e3

Fixes

  • no-array-for-each: Fix auto-fix causing SyntaxError (#1813) 47c8337

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v42.0.0...v43.0.0

42.0.0
By Sindre Sorhus β€’ Published on April 1, 2022

The project has passed 100 lint rules πŸŽ‰

Thanks to everyone that has contributed so far.

And special thanks to our team member @fisker for doing most of the work on this project. We could not have gotten this far without him.

New rules

Improvements

  • template-indent: Set the rule error instead of warn in recommended preset (#1781) 09923af
  • prefer-json-parse-buffer: Remove from recommended preset (#1750) 93f49f2
  • no-array-for-each: Add fixer for parenthesized call (#1784) 5f39c37
  • no-array-for-each: Handle optional chaining (#1753) 1d32db4
  • prefer-object-from-entries: Stop checking unknown cases (#1771) 309771d
  • prefer-string-trim-start-end: Check optional chaining (#1768) 1920597
  • no-useless-undefined: Ignore arguments in Function#bind() (#1762) c501243
  • prefer-ternary: Skip fix if there are comments (#1763) 3013565
  • text-encoding-identifier-case: Auto-fix encoding in fs.{readFile,readFileSync}() (#1755) 5e1c932
  • Add dist and dst abbreviations (#1756) dffcea8

Fixes

  • no-array-for-each: Handle ChainExpression correctly (#1772) e615a37

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v41.0.1...v42.0.0

41.0.1
By Sindre Sorhus β€’ Published on March 17, 2022
41.0.0
By Sindre Sorhus β€’ Published on February 16, 2022

New rules

Breaking

  • Require ESLint 8 (#1724) 608a90c

Fixes

  • prefer-export-from: Fix TypeScript compatibility (#1728) f14aa95
  • prefer-at: Remove auto-fix for arguments (#1705) bba518e

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v40.1.0...v41.0.0

40.1.0
By Sindre Sorhus β€’ Published on January 16, 2022
  • expiring-todo-comments: Add date option (#1683) 16bc33a
  • consistent-function-scoping: Allow React.useHook (#1691) e910633
  • no-useless-undefined: Allow React.createContext(undefined) (#1688) 40bc603

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v40.0.0...v40.1.0

40.0.0
By Sindre Sorhus β€’ Published on December 31, 2021

New rules

Improvements

  • prefer-object-has-own: Deprecate in favor of the built-in prefer-object-has-own rule (#1646) e59a856
  • filename-case: Ignore $ in filenames (#1628) a43a174
  • no-useless-promise-resolve-reject: Detect unnecessary Promise.resolve/reject in promise callback functions (#1666) f6215f3
  • prefer-export-from: Support import assertions (#1618) 1d7a6b6
  • prefer-top-level-await: Improve top-level expression detection (#1526) b054d65
  • prefer-dom-node-dataset: Check .hasAttribute() and .getAttribute() (#1673) 152f153
  • prefer-dom-node-dataset: Check .removeAttribute() (#1668) 22d8d03
  • prefer-string-slice: Improve fix (#1675) 267115a
  • prefer-math-trunc: Improve fix logic (#1670) 784c7a8
  • prefer-string-slice: Improve argument type detection (#1664) 03b0946
  • prefer-add-event-listener: Remove fix for onerror (#1650) 7bf63bb
  • no-array-push-push: Ignore process.{stdin,stdout,stderr} (#1654) 10ad699
  • no-new-array & no-new-buffer: Improve argument type detection (#1648) 9b04e43
  • no-array-for-each: Ignore pIteration.forEach (#1649) 2b92385
  • no-array-callback-reference: Ignore primitive wrappers in Array#map() (#1642) 0362c09
  • prefer-export-from: Support "string literal specifier" (#1636) 0866b41
  • template-indent: Check Jest inline snapshots by default (#1637) 64460e2

Fixes

  • prefer-dom-node-dataset: Fix name conversion (#1674) 7fb6f7b
  • prefer-export-from: Fix crash in TypeScript files (#1647) 1ff8e42

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v39.0.0...v40.0.0

39.0.0
By Sindre Sorhus β€’ Published on November 21, 2021

New rules

Improvements

  • prefer-export-from: Add ignoreUsedVariables option (#1590) a8d52e4
  • prevent-abbreviations: Show file basename instead of full path (#1593) 7c2867d
  • Remove require-post-message-target-origin rule from recommended config (#1581) 014b4dc

Fixes

  • no-empty-file: Fix false positive with triple-slash directives (#1605) 6354bb8
  • require-number-to-fixed-digits-argument: Ignore .toFixed from new expression (#1601) 20d61e7
  • prefer-export-from: Fix bug on fixing export namespace as default (#1583) b564ff1
  • Add missing additionalProperties to schema (#1589) 579d05d

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v38.0.1...v39.0.0

38.0.1
By Sindre Sorhus β€’ Published on November 8, 2021
  • prefer-export-from: Ignore variables with type (#1580) ea49b21

https://github.com/sindresorhus/eslint-plugin-unicorn/compare/v38.0.0...v38.0.1

General

License
MIT
Typescript Types
None found
Tree-shakeable
No

Popularity

GitHub Stargazers
2,742
Community Interest
2,491
Number of Forks
305

Maintenance

Commits
10/219/22045
Last Commit
Open Issues
287
Closed Issues
600
Open Pull Requests
15
Closed Pull Requests
89

Versions

Versions Released
10/219/2203
Latest Version Released
Jul 18, 2022
Current Tags
latest43.0.2

Contributors

fisker
fisker
Commits: 529
sindresorhus
sindresorhus
Commits: 185
bmish
bmish
Commits: 23
futpib
futpib
Commits: 22
MrHen
MrHen
Commits: 21
SamVerschueren
SamVerschueren
Commits: 18
jfmengels
jfmengels
Commits: 14
medusalix
medusalix
Commits: 8
noftaly
noftaly
Commits: 8
brettz9
brettz9
Commits: 7
lubien
lubien
Commits: 7
AriPerkkio
AriPerkkio
Commits: 5
manovotny
manovotny
Commits: 4
fregante
fregante
Commits: 4
voxpelli
voxpelli
Commits: 4