Skip to main content

Documentation Index

Fetch the complete documentation index at: https://mintlify.com/sindresorhus/eslint-plugin-unicorn/llms.txt

Use this file to discover all available pages before exploring further.

This page lists all rules provided by eslint-plugin-unicorn. Rules are grouped by category: Best Practices, Prefer Modern APIs, and Code Style & Consistency.

Legend

SymbolMeaning
Set in the recommended configuration
☑️Set in the unopinionated configuration
🔧Automatically fixable by the --fix CLI option
💡Manually fixable by editor suggestions
The recommended config enables a curated subset of rules marked ✅. The all config enables every rule in the plugin, including those not in any preset. Rules marked ☑️ (without ✅) appear only in the unopinionated config. See preset configs for details.

Best Practices

Rules that prevent bugs, enforce sound coding patterns, and improve correctness.
RuleDescriptionConfigFixSuggestion
better-regexImprove regexes by making them shorter, consistent, and safer.🔧
catch-error-nameEnforce a specific parameter name in catch clauses.🔧
consistent-assertEnforce consistent assertion style with node:assert.🔧
consistent-date-clonePrefer passing Date directly to the constructor when cloning.✅ ☑️🔧
consistent-destructuringUse destructured variables over properties.💡
consistent-empty-array-spreadPrefer consistent types when spreading a ternary in an array literal.🔧
consistent-existence-index-checkEnforce consistent style for element existence checks with indexOf(), lastIndexOf(), findIndex(), and findLastIndex().✅ ☑️🔧
consistent-function-scopingMove function definitions to the highest possible scope.
consistent-template-literal-escapeEnforce consistent style for escaping ${ in template literals.🔧
custom-error-definitionEnforce correct Error subclassing.🔧
error-messageEnforce passing a message value when creating a built-in error.✅ ☑️
expiring-todo-commentsAdd expiration conditions to TODO comments.✅ ☑️
isolated-functionsPrevent usage of variables from outside the scope of isolated functions.
new-for-builtinsEnforce the use of new for all builtins, except String, Number, Boolean, Symbol and BigInt.✅ ☑️🔧💡
no-abusive-eslint-disableEnforce specifying rules to disable in eslint-disable comments.✅ ☑️
no-accessor-recursionDisallow recursive access to this within getters and setters.✅ ☑️
no-anonymous-default-exportDisallow anonymous functions and classes as the default export.✅ ☑️💡
no-array-callback-referencePrevent passing a function reference directly to iterator methods.💡
no-array-for-eachPrefer for…of over the forEach method.✅ ☑️🔧💡
no-array-method-this-argumentDisallow using the this argument in array methods.✅ ☑️🔧💡
no-array-reduceDisallow Array#reduce() and Array#reduceRight().
no-array-reversePrefer Array#toReversed() over Array#reverse().✅ ☑️💡
no-array-sortPrefer Array#toSorted() over Array#sort().✅ ☑️💡
no-await-expression-memberDisallow member access from await expression.🔧
no-await-in-promise-methodsDisallow using await in Promise method parameters.✅ ☑️💡
no-document-cookieDo not use document.cookie directly.✅ ☑️
no-empty-fileDisallow empty files.✅ ☑️
no-for-loopDo not use a for loop that can be replaced with a for-of loop.🔧💡
no-immediate-mutationDisallow immediate mutation after variable assignment.🔧💡
no-instanceof-builtinsDisallow instanceof with built-in objects.✅ ☑️🔧💡
no-invalid-fetch-optionsDisallow invalid options in fetch() and new Request().✅ ☑️
no-invalid-remove-event-listenerPrevent calling EventTarget#removeEventListener() with the result of an expression.✅ ☑️
no-keyword-prefixDisallow identifiers starting with new or class.
no-magic-array-flat-depthDisallow a magic number as the depth argument in Array#flat(…).✅ ☑️
no-object-as-default-parameterDisallow the use of objects as default parameters.✅ ☑️
no-process-exitDisallow process.exit().✅ ☑️
no-single-promise-in-promise-methodsDisallow passing single-element arrays to Promise methods.✅ ☑️🔧💡
no-static-only-classDisallow classes that only have static members.✅ ☑️🔧
no-thenableDisallow then property.✅ ☑️
no-this-assignmentDisallow assigning this to a variable.✅ ☑️
no-typeof-undefinedDisallow comparing undefined using typeof.✅ ☑️🔧💡
no-unnecessary-array-flat-depthDisallow using 1 as the depth argument of Array#flat().✅ ☑️🔧
no-unnecessary-array-splice-countDisallow using .length or Infinity as the deleteCount or skipCount argument of Array#{splice,toSpliced}().✅ ☑️🔧
no-unnecessary-awaitDisallow awaiting non-promise values.✅ ☑️🔧
no-unnecessary-polyfillsEnforce the use of built-in methods instead of unnecessary polyfills.✅ ☑️
no-unnecessary-slice-endDisallow using .length or Infinity as the end argument of {Array,String,TypedArray}#slice().✅ ☑️🔧
no-unreadable-array-destructuringDisallow unreadable array destructuring.✅ ☑️🔧
no-unreadable-iifeDisallow unreadable IIFEs.✅ ☑️
no-unused-propertiesDisallow unused object properties.
no-useless-collection-argumentDisallow useless values or fallbacks in Set, Map, WeakSet, or WeakMap.✅ ☑️🔧💡
no-useless-error-capture-stack-traceDisallow unnecessary Error.captureStackTrace(…).✅ ☑️🔧
no-useless-fallback-in-spreadDisallow useless fallback when spreading in object literals.✅ ☑️🔧
no-useless-iterator-to-arrayDisallow unnecessary .toArray() on iterators.✅ ☑️🔧💡
no-useless-length-checkDisallow useless array length check.✅ ☑️🔧
no-useless-promise-resolve-rejectDisallow returning/yielding Promise.resolve/reject() in async functions or promise callbacks.✅ ☑️🔧
no-useless-spreadDisallow unnecessary spread.✅ ☑️🔧
no-useless-switch-caseDisallow useless case in switch statements.✅ ☑️💡
no-useless-undefinedDisallow useless undefined.✅ ☑️🔧
no-zero-fractionsDisallow number literals with zero fractions or dangling dots.✅ ☑️🔧
prefer-type-errorEnforce throwing TypeError in type checking conditions.✅ ☑️🔧
prevent-abbreviationsPrevent abbreviations.🔧
require-post-message-target-originEnforce using the targetOrigin argument with window.postMessage().💡
string-contentEnforce better string content.🔧💡
throw-new-errorRequire new when creating an error.✅ ☑️🔧

Prefer Modern APIs

Rules that encourage modern JavaScript and Node.js APIs over legacy equivalents.
RuleDescriptionConfigFixSuggestion
prefer-add-event-listenerPrefer .addEventListener() and .removeEventListener() over on-functions.✅ ☑️🔧
prefer-array-findPrefer .find(…) and .findLast(…) over the first or last element from .filter(…).✅ ☑️🔧💡
prefer-array-flatPrefer Array#flat() over legacy techniques to flatten arrays.✅ ☑️🔧
prefer-array-flat-mapPrefer .flatMap(…) over .map(…).flat().✅ ☑️🔧
prefer-array-index-ofPrefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item.✅ ☑️🔧💡
prefer-array-somePrefer .some(…) over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…).✅ ☑️🔧💡
prefer-atPrefer .at() method for index access and String#charAt().✅ ☑️🔧💡
prefer-bigint-literalsPrefer BigInt literals over the constructor.✅ ☑️🔧💡
prefer-blob-reading-methodsPrefer Blob#arrayBuffer() over FileReader#readAsArrayBuffer(…) and Blob#text() over FileReader#readAsText(…).✅ ☑️
prefer-class-fieldsPrefer class field declarations over this assignments in constructors.✅ ☑️🔧💡
prefer-classlist-togglePrefer using Element#classList.toggle() to toggle class names.✅ ☑️🔧💡
prefer-code-pointPrefer String#codePointAt(…) over String#charCodeAt(…) and String.fromCodePoint(…) over String.fromCharCode(…).✅ ☑️💡
prefer-date-nowPrefer Date.now() to get the number of milliseconds since the Unix Epoch.✅ ☑️🔧
prefer-default-parametersPrefer default parameters over reassignment.✅ ☑️💡
prefer-dom-node-appendPrefer Node#append() over Node#appendChild().✅ ☑️🔧
prefer-dom-node-datasetPrefer using .dataset on DOM elements over calling attribute methods.✅ ☑️🔧
prefer-dom-node-removePrefer childNode.remove() over parentNode.removeChild(childNode).✅ ☑️🔧💡
prefer-dom-node-text-contentPrefer .textContent over .innerText.✅ ☑️💡
prefer-event-targetPrefer EventTarget over EventEmitter.✅ ☑️
prefer-export-fromPrefer export…from when re-exporting.🔧💡
prefer-global-thisPrefer globalThis over window, self, and global.✅ ☑️🔧
prefer-import-meta-propertiesPrefer import.meta.{dirname,filename} over legacy techniques for getting file paths.🔧
prefer-includesPrefer .includes() over .indexOf(), .lastIndexOf(), and Array#some() when checking for existence or non-existence.✅ ☑️🔧💡
prefer-json-parse-bufferPrefer reading a JSON file as a buffer.🔧
prefer-keyboard-event-keyPrefer KeyboardEvent#key over KeyboardEvent#keyCode.✅ ☑️🔧
prefer-math-min-maxPrefer Math.min() and Math.max() over ternaries for simple comparisons.✅ ☑️🔧
prefer-math-truncEnforce the use of Math.trunc instead of bitwise operators.✅ ☑️🔧💡
prefer-modern-dom-apisPrefer .before() over .insertBefore(), .replaceWith() over .replaceChild(), and modern insertion methods over insertAdjacentText() and insertAdjacentElement().✅ ☑️🔧
prefer-modern-math-apisPrefer modern Math APIs over legacy patterns.✅ ☑️🔧
prefer-modulePrefer JavaScript modules (ESM) over CommonJS.✅ ☑️🔧💡
prefer-native-coercion-functionsPrefer using String, Number, BigInt, Boolean, and Symbol directly.✅ ☑️🔧
prefer-negative-indexPrefer negative index over .length - index when possible.✅ ☑️🔧
prefer-node-protocolPrefer using the node: protocol when importing Node.js builtin modules.✅ ☑️🔧
prefer-number-propertiesPrefer Number static properties over global ones.✅ ☑️🔧💡
prefer-object-from-entriesPrefer using Object.fromEntries(…) to transform a list of key-value pairs into an object.✅ ☑️🔧
prefer-optional-catch-bindingPrefer omitting the catch binding parameter.✅ ☑️🔧
prefer-prototype-methodsPrefer borrowing methods from the prototype instead of the instance.✅ ☑️🔧
prefer-query-selectorPrefer .querySelector() over .getElementById(), .querySelectorAll() over .getElementsByClassName(), .getElementsByTagName(), and .getElementsByName().🔧
prefer-reflect-applyPrefer Reflect.apply() over Function#apply().✅ ☑️🔧
prefer-regexp-testPrefer RegExp#test() over String#match() and RegExp#exec().✅ ☑️🔧💡
prefer-response-static-jsonPrefer Response.json() over new Response(JSON.stringify()).✅ ☑️🔧
prefer-set-hasPrefer Set#has() over Array#includes() when checking for existence or non-existence.✅ ☑️🔧💡
prefer-set-sizePrefer using Set#size instead of Array#length.✅ ☑️🔧
prefer-spreadPrefer the spread operator over Array.from(…), Array#concat(…), Array#{slice,toSpliced}() and String#split('').🔧💡
prefer-string-rawPrefer using the String.raw tag to avoid escaping \.✅ ☑️🔧
prefer-string-replace-allPrefer String#replaceAll() over regex searches with the global flag.✅ ☑️🔧
prefer-string-slicePrefer String#slice() over String#substr() and String#substring().✅ ☑️🔧
prefer-string-starts-ends-withPrefer String#startsWith() & String#endsWith() over RegExp#test().✅ ☑️🔧💡
prefer-string-trim-start-endPrefer String#trimStart() / String#trimEnd() over String#trimLeft() / String#trimRight().✅ ☑️🔧
prefer-structured-clonePrefer using structuredClone to create a deep clone.✅ ☑️💡
prefer-top-level-awaitPrefer top-level await over top-level promises and async function calls.✅ ☑️💡

Code Style & Consistency

Rules that enforce consistent formatting, naming conventions, and structural patterns.
RuleDescriptionConfigFixSuggestion
empty-brace-spacesEnforce no spaces between braces.🔧
escape-caseRequire escape sequences to use uppercase or lowercase values.✅ ☑️🔧
explicit-length-checkEnforce explicitly comparing the length or size property of a value.🔧💡
filename-caseEnforce a case style for filenames.
import-styleEnforce specific import styles per module.✅ ☑️
no-console-spacesDo not use leading/trailing space between console.log parameters.✅ ☑️🔧
no-hex-escapeEnforce the use of Unicode escapes instead of hexadecimal escapes.✅ ☑️🔧
no-lonely-ifDisallow if statements as the only statement in if blocks without else.✅ ☑️🔧
no-named-defaultDisallow named usage of default import and export.✅ ☑️🔧
no-negated-conditionDisallow negated conditions.✅ ☑️🔧
no-negation-in-equality-checkDisallow negated expression in equality check.✅ ☑️💡
no-nested-ternaryDisallow nested ternary expressions.🔧
no-new-arrayDisallow new Array().✅ ☑️🔧💡
no-new-bufferEnforce the use of Buffer.from() and Buffer.alloc() instead of the deprecated new Buffer().✅ ☑️🔧💡
no-nullDisallow the use of the null literal.🔧💡
number-literal-caseEnforce proper case for numeric literals.✅ ☑️🔧
numeric-separators-styleEnforce the style of numeric separators by correctly grouping digits.✅ ☑️🔧
prefer-logical-operator-over-ternaryPrefer using a logical operator over a ternary.✅ ☑️💡
prefer-simple-condition-firstPrefer simple conditions first in logical expressions.✅ ☑️🔧💡
prefer-single-callEnforce combining multiple Array#push(), Element#classList.{add,remove}(), and importScripts() into one call.✅ ☑️🔧💡
prefer-switchPrefer switch over multiple else-if.✅ ☑️🔧
prefer-ternaryPrefer ternary expressions over simple if-else statements.✅ ☑️🔧
relative-url-styleEnforce consistent relative URL style.✅ ☑️🔧💡
require-array-join-separatorEnforce using the separator argument with Array#join().✅ ☑️🔧
require-module-attributesRequire non-empty module attributes for imports and exports.✅ ☑️🔧
require-module-specifiersRequire non-empty specifier list in import and export statements.✅ ☑️🔧💡
require-number-to-fixed-digits-argumentEnforce using the digits argument with Number#toFixed().✅ ☑️🔧
switch-case-bracesEnforce consistent brace style for case clauses.🔧
switch-case-break-positionEnforce consistent break/return/continue/throw position in case clauses.🔧
template-indentFix whitespace-insensitive template indentation.🔧
text-encoding-identifier-caseEnforce consistent case for text encoding identifiers.✅ ☑️🔧💡

Build docs developers (and LLMs) love