More than 100 powerful ESLint rules
npm install eslint-unicorn
> More than 100 powerful ESLint rules
You might want to check out XO, which includes this plugin.
Propose or contribute a new rule ➡
``sh`
npm install --save-dev eslint eslint-plugin-unicorn
)Requires ESLint >=8.56.0.
Use a preset config or configure each rule in eslint.config.js.
If you don't use the preset, ensure you use the same languageOptions config as below.
`js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import globals from 'globals';
export default [
{
languageOptions: {
globals: globals.builtin,
},
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
'unicorn/better-regex': 'error',
'unicorn/…': 'error',
},
},
// …
];
`
`js
'use strict';
const eslintPluginUnicorn = require('eslint-plugin-unicorn');
const globals = require('globals');
module.exports = [
{
languageOptions: {
globals: globals.builtin,
},
plugins: {
unicorn: eslintPluginUnicorn,
},
rules: {
'unicorn/better-regex': 'error',
'unicorn/…': 'error',
},
},
// …
];
`
or package.json)Use a preset config or configure each rule in package.json.
If you don't use the preset, ensure you use the same env and parserOptions config as below.
`json`
{
"name": "my-awesome-project",
"eslintConfig": {
"env": {
"es2024": true
},
"parserOptions": {
"ecmaVersion": "latest",
"sourceType": "module"
},
"plugins": [
"unicorn"
],
"rules": {
"unicorn/better-regex": "error",
"unicorn/…": "error"
}
}
}
💼 Configurations enabled in.\
✅ Set in the recommended configuration.\--fix
🔧 Automatically fixable by the CLI option.\
💡 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-empty-array-spread | Prefer consistent types when spreading a ternary in an array literal. | ✅ | 🔧 | |
| consistent-function-scoping | Move function definitions to the highest possible scope. | ✅ | | |
| custom-error-definition | Enforce correct Error subclassing. | | 🔧 | |message
| empty-brace-spaces | Enforce no spaces between braces. | ✅ | 🔧 | |
| error-message | Enforce passing a value when creating a built-in error. | ✅ | | |length
| 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 or size property of a value. | ✅ | 🔧 | 💡 |new
| filename-case | Enforce a case style for filenames. | ✅ | | |
| import-style | Enforce specific import styles per module. | ✅ | | |
| new-for-builtins | Enforce the use of for all builtins, except String, Number, Boolean, Symbol and BigInt. | ✅ | 🔧 | |eslint-disable
| no-abusive-eslint-disable | Enforce specifying rules to disable in comments. | ✅ | | |for…of
| no-anonymous-default-export | Disallow anonymous functions and classes as the default export. | ✅ | | 💡 |
| no-array-callback-reference | Prevent passing a function reference directly to iterator methods. | ✅ | | 💡 |
| no-array-for-each | Prefer over the forEach method. | ✅ | 🔧 | 💡 |this
| no-array-method-this-argument | Disallow using the argument in array methods. | ✅ | 🔧 | 💡 |Array#push()
| no-array-push-push | Enforce combining multiple into one call. | ✅ | 🔧 | 💡 |Array#reduce()
| no-array-reduce | Disallow and Array#reduceRight(). | ✅ | | |await
| no-await-expression-member | Disallow member access from await expression. | ✅ | 🔧 | |
| no-await-in-promise-methods | Disallow using in Promise method parameters. | ✅ | | 💡 |console.log
| no-console-spaces | Do not use leading/trailing space between parameters. | ✅ | 🔧 | |document.cookie
| no-document-cookie | Do not use directly. | ✅ | | |for
| no-empty-file | Disallow empty files. | ✅ | | |
| no-for-loop | Do not use a loop that can be replaced with a for-of loop. | ✅ | 🔧 | 💡 |Array.isArray()
| no-hex-escape | Enforce the use of Unicode escapes instead of hexadecimal escapes. | ✅ | 🔧 | |
| no-instanceof-array | Require instead of instanceof Array. | ✅ | 🔧 | |fetch()
| no-invalid-fetch-options | Disallow invalid options in and new Request(). | ✅ | | |EventTarget#removeEventListener()
| no-invalid-remove-event-listener | Prevent calling with the result of an expression. | ✅ | | |new
| no-keyword-prefix | Disallow identifiers starting with or class. | | | |.length
| no-length-as-slice-end | Disallow using as the end argument of {Array,String,TypedArray}#slice(). | ✅ | 🔧 | |if
| no-lonely-if | Disallow statements as the only statement in if blocks without else. | ✅ | 🔧 | |depth
| no-magic-array-flat-depth | Disallow a magic number as the argument in Array#flat(…). | ✅ | | |new Array()
| no-negated-condition | Disallow negated conditions. | ✅ | 🔧 | |
| no-negation-in-equality-check | Disallow negated expression in equality check. | ✅ | | 💡 |
| no-nested-ternary | Disallow nested ternary expressions. | ✅ | 🔧 | |
| no-new-array | Disallow . | ✅ | 🔧 | 💡 |Buffer.from()
| no-new-buffer | Enforce the use of and Buffer.alloc() instead of the deprecated new Buffer(). | ✅ | 🔧 | 💡 |null
| no-null | Disallow the use of the literal. | ✅ | 🔧 | 💡 |process.exit()
| no-object-as-default-parameter | Disallow the use of objects as default parameters. | ✅ | | |
| no-process-exit | Disallow . | ✅ | | |Promise
| no-single-promise-in-promise-methods | Disallow passing single-element arrays to methods. | ✅ | 🔧 | 💡 |then
| no-static-only-class | Disallow classes that only have static members. | ✅ | 🔧 | |
| no-thenable | Disallow property. | ✅ | | |this
| no-this-assignment | Disallow assigning to a variable. | ✅ | | |undefined
| no-typeof-undefined | Disallow comparing using typeof. | ✅ | 🔧 | 💡 |Promise.resolve/reject()
| no-unnecessary-await | Disallow awaiting non-promise values. | ✅ | 🔧 | |
| no-unnecessary-polyfills | Enforce the use of built-in methods instead of unnecessary polyfills. | ✅ | | |
| no-unreadable-array-destructuring | Disallow unreadable array destructuring. | ✅ | 🔧 | |
| no-unreadable-iife | Disallow unreadable IIFEs. | ✅ | | |
| 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 in async functions or promise callbacks | ✅ | 🔧 | |undefined
| no-useless-spread | Disallow unnecessary spread. | ✅ | 🔧 | |
| no-useless-switch-case | Disallow useless case in switch statements. | ✅ | | 💡 |
| no-useless-undefined | Disallow useless . | ✅ | 🔧 | |.addEventListener()
| 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 and .removeEventListener() over on-functions. | ✅ | 🔧 | |.find(…)
| prefer-array-find | Prefer and .findLast(…) over the first or last element from .filter(…). | ✅ | 🔧 | 💡 |Array#flat()
| prefer-array-flat | Prefer over legacy techniques to flatten arrays. | ✅ | 🔧 | |.flatMap(…)
| prefer-array-flat-map | Prefer over .map(…).flat(). | ✅ | 🔧 | |Array#{indexOf,lastIndexOf}()
| prefer-array-index-of | Prefer over Array#{findIndex,findLastIndex}() when looking for the index of an item. | ✅ | 🔧 | 💡 |.some(…)
| prefer-array-some | Prefer over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…). | ✅ | 🔧 | 💡 |.at()
| prefer-at | Prefer method for index access and String#charAt(). | ✅ | 🔧 | 💡 |Blob#arrayBuffer()
| prefer-blob-reading-methods | Prefer over FileReader#readAsArrayBuffer(…) and Blob#text() over FileReader#readAsText(…). | ✅ | | |String#codePointAt(…)
| prefer-code-point | Prefer over String#charCodeAt(…) and String.fromCodePoint(…) over String.fromCharCode(…). | ✅ | | 💡 |Date.now()
| prefer-date-now | Prefer to get the number of milliseconds since the Unix Epoch. | ✅ | 🔧 | |Node#append()
| prefer-default-parameters | Prefer default parameters over reassignment. | ✅ | 🔧 | 💡 |
| prefer-dom-node-append | Prefer over Node#appendChild(). | ✅ | 🔧 | |.dataset
| prefer-dom-node-dataset | Prefer using on DOM elements over calling attribute methods. | ✅ | 🔧 | |childNode.remove()
| prefer-dom-node-remove | Prefer over parentNode.removeChild(childNode). | ✅ | 🔧 | 💡 |.textContent
| prefer-dom-node-text-content | Prefer over .innerText. | ✅ | | 💡 |EventTarget
| prefer-event-target | Prefer over EventEmitter. | ✅ | | |export…from
| prefer-export-from | Prefer when re-exporting. | ✅ | 🔧 | 💡 |.includes()
| prefer-includes | Prefer over .indexOf(), .lastIndexOf(), and Array#some() when checking for existence or non-existence. | ✅ | 🔧 | 💡 |KeyboardEvent#key
| prefer-json-parse-buffer | Prefer reading a JSON file as a buffer. | | 🔧 | |
| prefer-keyboard-event-key | Prefer over KeyboardEvent#keyCode. | ✅ | 🔧 | |Math.trunc
| prefer-logical-operator-over-ternary | Prefer using a logical operator over a ternary. | ✅ | | 💡 |
| prefer-math-trunc | Enforce the use of instead of bitwise operators. | ✅ | 🔧 | 💡 |.before()
| prefer-modern-dom-apis | Prefer over .insertBefore(), .replaceWith() over .replaceChild(), prefer one of .before(), .after(), .append() or .prepend() over insertAdjacentText() and insertAdjacentElement(). | ✅ | 🔧 | |Math
| prefer-modern-math-apis | Prefer modern APIs over legacy patterns. | ✅ | 🔧 | |String
| prefer-module | Prefer JavaScript modules (ESM) over CommonJS. | ✅ | 🔧 | 💡 |
| prefer-native-coercion-functions | Prefer using , Number, BigInt, Boolean, and Symbol directly. | ✅ | 🔧 | |.length - index
| prefer-negative-index | Prefer negative index over when possible. | ✅ | 🔧 | |node:
| prefer-node-protocol | Prefer using the protocol when importing Node.js builtin modules. | ✅ | 🔧 | |Number
| prefer-number-properties | Prefer static properties over global ones. | ✅ | 🔧 | 💡 |Object.fromEntries(…)
| prefer-object-from-entries | Prefer using to transform a list of key-value pairs into an object. | ✅ | 🔧 | |catch
| prefer-optional-catch-binding | Prefer omitting the binding parameter. | ✅ | 🔧 | |.querySelector()
| prefer-prototype-methods | Prefer borrowing methods from the prototype instead of the instance. | ✅ | 🔧 | |
| prefer-query-selector | Prefer over .getElementById(), .querySelectorAll() over .getElementsByClassName() and .getElementsByTagName() and .getElementsByName(). | ✅ | 🔧 | |Reflect.apply()
| prefer-reflect-apply | Prefer over Function#apply(). | ✅ | 🔧 | |RegExp#test()
| prefer-regexp-test | Prefer over String#match() and RegExp#exec(). | ✅ | 🔧 | 💡 |Set#has()
| prefer-set-has | Prefer over Array#includes() when checking for existence or non-existence. | ✅ | 🔧 | 💡 |Set#size
| prefer-set-size | Prefer using instead of Array#length. | ✅ | 🔧 | |Array.from(…)
| prefer-spread | Prefer the spread operator over , Array#concat(…), Array#{slice,toSpliced}() and String#split(''). | ✅ | 🔧 | 💡 |String.raw
| prefer-string-raw | Prefer using the tag to avoid escaping \. | ✅ | 🔧 | |String#replaceAll()
| prefer-string-replace-all | Prefer over regex searches with the global flag. | ✅ | 🔧 | |String#slice()
| prefer-string-slice | Prefer over String#substr() and String#substring(). | ✅ | 🔧 | |String#startsWith()
| prefer-string-starts-ends-with | Prefer & String#endsWith() over RegExp#test(). | ✅ | 🔧 | 💡 |String#trimStart()
| prefer-string-trim-start-end | Prefer / String#trimEnd() over String#trimLeft() / String#trimRight(). | ✅ | 🔧 | |structuredClone
| prefer-structured-clone | Prefer using to create a deep clone. | ✅ | | 💡 |switch
| prefer-switch | Prefer over multiple else-if. | ✅ | 🔧 | |if-else
| prefer-ternary | Prefer ternary expressions over simple statements. | ✅ | 🔧 | |TypeError
| prefer-top-level-await | Prefer top-level await over top-level promises and async function calls. | ✅ | | 💡 |
| prefer-type-error | Enforce throwing in type checking conditions. | ✅ | 🔧 | |Array#join()
| prevent-abbreviations | Prevent abbreviations. | ✅ | 🔧 | |
| relative-url-style | Enforce consistent relative URL style. | ✅ | 🔧 | 💡 |
| require-array-join-separator | Enforce using the separator argument with . | ✅ | 🔧 | |Number#toFixed()
| require-number-to-fixed-digits-argument | Enforce using the digits argument with . | ✅ | 🔧 | |targetOrigin
| require-post-message-target-origin | Enforce using the argument with window.postMessage(). | | | 💡 |case
| string-content | Enforce better string content. | | 🔧 | 💡 |
| switch-case-braces | Enforce consistent brace style for clauses. | ✅ | 🔧 | |new
| template-indent | Fix whitespace-insensitive template indentation. | ✅ | 🔧 | |
| text-encoding-identifier-case | Enforce consistent case for text encoding identifiers. | ✅ | 🔧 | 💡 |
| throw-new-error | Require when creating an error. | ✅ | 🔧 | |
)See the ESLint docs for more information about extending config files.
Note: Preset configs will also enable the correct language options.
This plugin exports a recommended config that enforces good practices.
#### ES Module (Recommended)
`js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
export default [
// …
eslintPluginUnicorn.configs['flat/recommended'],
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
`
#### CommonJS
`js
'use strict';
const eslintPluginUnicorn = require('eslint-plugin-unicorn');
module.exports = [
// …
eslintPluginUnicorn.configs['flat/recommended'],
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
`
This plugin exports an all config that makes use of all rules (except for deprecated ones).
#### ES Module (Recommended)
`js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
export default [
// …
eslintPluginUnicorn.configs['flat/all'],
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
`
#### CommonJS
`js
'use strict';
const eslintPluginUnicorn = require('eslint-plugin-unicorn');
module.exports = [
// …
eslintPluginUnicorn.configs['flat/all'],
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
`
or package.json)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.
`json`
{
"name": "my-awesome-project",
"eslintConfig": {
"extends": "plugin:unicorn/recommended",
"rules": {
"unicorn/better-regex": "warn"
}
}
}
This plugin exports an all config that makes use of all rules (except for deprecated ones).
`json``
{
"name": "my-awesome-project",
"eslintConfig": {
"extends": "plugin:unicorn/all"
}
}
- Sindre Sorhus
- Fisker Cheung
- Bryan Mishkin
- futpib