More than 100 powerful ESLint rules
npm install 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 β‘
``sh`
npm install --save-dev eslint eslint-plugin-unicorn
Requires ESLint >=9.20.0, flat config, and ESM.
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',
},
},
// β¦
];
`
πΌ Configurations enabled in.\
β
Set in the recommended configuration.\unopinionated
βοΈ Set in the 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-assert | Enforce consistent assertion style with node:assert. | β
| π§ | |Date
| consistent-date-clone | Prefer passing directly to the constructor when cloning. | β
βοΈ | π§ | |indexOf()
| consistent-destructuring | Use destructured variables over properties. | | | π‘ |
| consistent-empty-array-spread | Prefer consistent types when spreading a ternary in an array literal. | β
| π§ | |
| consistent-existence-index-check | Enforce consistent style for element existence checks with , lastIndexOf(), findIndex(), and findLastIndex(). | β
βοΈ | π§ | |Error
| consistent-function-scoping | Move function definitions to the highest possible scope. | β
| | |
| custom-error-definition | Enforce correct 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 or lowercase 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. | β
βοΈ | | |this
| no-accessor-recursion | Disallow recursive access to within getters and setters. | β
βοΈ | | |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#reduce()
| no-array-reduce | Disallow and Array#reduceRight(). | β
| | |Array#toReversed()
| no-array-reverse | Prefer over Array#reverse(). | β
βοΈ | | π‘ |Array#toSorted()
| no-array-sort | Prefer over Array#sort(). | β
βοΈ | | π‘ |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. | β
| π§ | π‘ |instanceof
| no-hex-escape | Enforce the use of Unicode escapes instead of hexadecimal escapes. | β
βοΈ | π§ | |
| no-immediate-mutation | Disallow immediate mutation after variable assignment. | β
| π§ | π‘ |
| no-instanceof-builtins | Disallow with built-in objects | β
βοΈ | π§ | π‘ |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. | | | |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-named-default | Disallow named usage of default import and export. | β
βοΈ | π§ | |
| 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. | β
βοΈ | π§ | π‘ |1
| no-unnecessary-array-flat-depth | Disallow using as the depth argument of Array#flat(). | β
βοΈ | π§ | |.length
| no-unnecessary-array-splice-count | Disallow using or Infinity as the deleteCount or skipCount argument of Array#{splice,toSpliced}(). | β
βοΈ | π§ | |.length
| no-unnecessary-await | Disallow awaiting non-promise values. | β
βοΈ | π§ | |
| no-unnecessary-polyfills | Enforce the use of built-in methods instead of unnecessary polyfills. | β
βοΈ | | |
| no-unnecessary-slice-end | Disallow using or Infinity as the end argument of {Array,String,TypedArray}#slice(). | β
βοΈ | π§ | |Set
| no-unreadable-array-destructuring | Disallow unreadable array destructuring. | β
βοΈ | π§ | |
| no-unreadable-iife | Disallow unreadable IIFEs. | β
βοΈ | | |
| no-unused-properties | Disallow unused object properties. | | | |
| no-useless-collection-argument | Disallow useless values or fallbacks in , Map, WeakSet, or WeakMap. | β
βοΈ | π§ | |Error.captureStackTrace(β¦)
| no-useless-error-capture-stack-trace | Disallow unnecessary . | β
βοΈ | π§ | |Promise.resolve/reject()
| 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(). | β
βοΈ | π§ | π‘ |BigInt
| prefer-bigint-literals | Prefer literals over the constructor. | β
βοΈ | π§ | π‘ |Blob#arrayBuffer()
| prefer-blob-reading-methods | Prefer over FileReader#readAsArrayBuffer(β¦) and Blob#text() over FileReader#readAsText(β¦). | β
βοΈ | | |this
| prefer-class-fields | Prefer class field declarations over assignments in constructors. | β
βοΈ | π§ | π‘ |Element#classList.toggle()
| prefer-classlist-toggle | Prefer using to toggle class names. | β
βοΈ | π§ | π‘ |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. | β
| π§ | π‘ |globalThis
| prefer-global-this | Prefer over window, self, and global. | β
βοΈ | π§ | |import.meta.{dirname,filename}
| prefer-import-meta-properties | Prefer over legacy techniques for getting file paths. | | π§ | |.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.min()
| prefer-logical-operator-over-ternary | Prefer using a logical operator over a ternary. | β
βοΈ | | π‘ |
| prefer-math-min-max | Prefer and Math.max() over ternaries for simple comparisons. | β
βοΈ | π§ | |Math.trunc
| 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(). | β
βοΈ | π§ | π‘ |Response.json()
| prefer-response-static-json | Prefer over new Response(JSON.stringify()). | β
βοΈ | π§ | |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#push()
| prefer-single-call | Enforce combining multiple , Element#classList.{add,remove}(), and importScripts() into one call. | β
βοΈ | π§ | π‘ |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-module-attributes | Require non-empty module attributes for imports and exports | β
βοΈ | π§ | |
| require-module-specifiers | Require non-empty specifier list in import and export statements. | β
βοΈ | π§ | π‘ |
| 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 list.
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.
`js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
export default [
// β¦
eslintPluginUnicorn.configs.recommended,
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
`
This plugin exports an all that makes use of all rules (except for deprecated ones).
`js
import eslintPluginUnicorn from 'eslint-plugin-unicorn';
export default [
// β¦
eslintPluginUnicorn.configs.all,
{
rules: {
'unicorn/better-regex': 'warn',
},
},
];
``
- Sindre Sorhus
- Fisker Cheung
- Bryan Mishkin
- futpib