Skip to content

sindresorhus/eslint-plugin-unicorn

Folders and files

NameName
Last commit message
Last commit date

Latest commit

Β 

History

1,877 Commits
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 
Β 

eslint-plugin-unicorn Coverage Status npm version

Unicorn

More than 100 powerful ESLint rules

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

Most rules target JavaScript and TypeScript, but some also lint CSS, HTML, JSON, and Markdown when used with the matching ESLint language plugin.

Propose or contribute a new rule ➑

Fully AI-generated pull requests are not accepted. You can use AI, but should be verified and cleaned up by a human. Only Opus 4.6+ (high-effort) and Codex 5.4+ (extra high) are accepted models. Preferably created with Opus and verified by Codex.

Install

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

Requires ESLint >=10.4, flat config, and ESM.

Usage

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.

import eslintPluginUnicorn from 'eslint-plugin-unicorn';
import globals from 'globals';

export default [
	{
		languageOptions: {
			globals: globals.builtin,
		},
		plugins: {
			unicorn: eslintPluginUnicorn,
		},
		rules: {
			'unicorn/prefer-module': 'error',
			'unicorn/…': 'error',
		},
	},
	// …
];

Rules

πŸ’Ό Configurations enabled in.
βœ… Set in the recommended configuration.
β˜‘οΈ Set in the unopinionated configuration.
πŸ”§ Automatically fixable by the --fix CLI option.
πŸ’‘ Manually fixable by editor suggestions.

NameΒ Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β Β  Description πŸ’Ό πŸ”§ πŸ’‘
better-dom-traversing Prefer better DOM traversal APIs. βœ… β˜‘οΈ πŸ’‘
catch-error-name Enforce a specific parameter name in catch clauses. βœ… πŸ”§
class-reference-in-static-methods Enforce consistent class references in static methods. βœ… πŸ’‘
comment-content Enforce better comment content. βœ… πŸ”§
consistent-assert Enforce consistent assertion style with node:assert. βœ… πŸ”§
consistent-compound-words Enforce consistent spelling of compound words in identifiers. βœ… β˜‘οΈ πŸ’‘
consistent-date-clone Prefer passing Date directly to the constructor when cloning. βœ… β˜‘οΈ πŸ”§
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 indexOf(), lastIndexOf(), findIndex(), and findLastIndex(). βœ… β˜‘οΈ πŸ”§
consistent-function-scoping Move function definitions to the highest possible scope. βœ…
consistent-function-style Enforce function syntax by role. πŸ’‘
consistent-json-file-read Enforce consistent JSON file reads before JSON.parse(). βœ… πŸ”§
consistent-template-literal-escape Enforce consistent style for escaping ${ in template literals. βœ… πŸ”§
custom-error-definition Enforce correct Error subclassing. πŸ”§
dom-node-dataset Enforce consistent style for DOM element dataset access. βœ… β˜‘οΈ πŸ”§
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 or lowercase 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 and directory names. βœ…
id-match Require identifiers to match a specified regular expression.
import-style Enforce specific import styles per module. βœ… β˜‘οΈ
isolated-functions Prevent usage of variables from outside the scope of isolated functions. βœ…
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-accessor-recursion Disallow recursive access to this within getters and setters. βœ… β˜‘οΈ
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-fill-with-reference-type Disallow using reference values as Array#fill() values. βœ… β˜‘οΈ
no-array-for-each Prefer for…of over the forEach method. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-array-from-fill Disallow .fill() after Array.from({length: …}). βœ… β˜‘οΈ
no-array-method-this-argument Disallow using the this argument in array methods. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-array-reduce Disallow Array#reduce() and Array#reduceRight(). βœ… πŸ”§
no-array-reverse Prefer Array#toReversed() over Array#reverse(). βœ… β˜‘οΈ πŸ’‘
no-array-sort Prefer Array#toSorted() over Array#sort(). βœ… β˜‘οΈ πŸ’‘
no-await-expression-member Disallow member access from await expression. βœ… πŸ”§
no-await-in-promise-methods Disallow using await in Promise method parameters. βœ… β˜‘οΈ πŸ’‘
no-blob-to-file Disallow unnecessary Blob to File conversion. βœ… β˜‘οΈ πŸ’‘
no-break-in-nested-loop Disallow break and continue in nested loops and switches inside loops. βœ…
no-canvas-to-image Prefer drawing canvases directly instead of converting them to images. βœ… β˜‘οΈ
no-computed-property-existence-check Disallow dynamic object property existence checks. βœ… πŸ’‘
no-confusing-array-splice Disallow confusing uses of Array#{splice,toSpliced}(). βœ… πŸ’‘
no-console-spaces Do not use leading/trailing space between console.log parameters. βœ… β˜‘οΈ πŸ”§
no-declarations-before-early-exit Disallow declarations before conditional early exits when they are only used after the exit. βœ… β˜‘οΈ πŸ”§
no-document-cookie Do not use document.cookie directly. βœ… β˜‘οΈ
no-duplicate-loops Disallow .map() and .filter() in for…of and for await…of loop headers. βœ…
no-duplicate-set-values Disallow duplicate values in Set constructor array literals. βœ…
no-empty-file Disallow empty files. βœ… β˜‘οΈ
no-error-property-assignment Disallow assigning to built-in error properties. βœ… β˜‘οΈ
no-exports-in-scripts Disallow exports in scripts. βœ… β˜‘οΈ
no-for-loop Do not use a for loop that can be replaced with a for-of loop. βœ… πŸ”§ πŸ’‘
no-global-object-property-assignment Disallow assigning properties on the global object. βœ… β˜‘οΈ
no-hex-escape Enforce the use of Unicode escapes instead of hexadecimal escapes. βœ… β˜‘οΈ πŸ”§
no-immediate-mutation Disallow immediate mutation after variable assignment. βœ… πŸ”§ πŸ’‘
no-incorrect-query-selector Disallow incorrect querySelector() and querySelectorAll() usage. βœ… πŸ”§
no-incorrect-template-string-interpolation Disallow incorrect template literal interpolation syntax. βœ… β˜‘οΈ πŸ’‘
no-instanceof-builtins Disallow instanceof with built-in objects βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-invalid-fetch-options Disallow invalid options in fetch() and new Request(). βœ… β˜‘οΈ
no-invalid-file-input-accept Disallow invalid accept values on file inputs. πŸ”§
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-late-current-target-access Disallow accessing event.currentTarget after the synchronous event dispatch has finished. βœ…
no-lonely-if Disallow if statements as the only statement in if blocks without else. βœ… β˜‘οΈ πŸ”§
no-magic-array-flat-depth Disallow a magic number as the depth argument in Array#flat(…). βœ… β˜‘οΈ
no-manually-wrapped-comments Disallow manually wrapped comments. πŸ”§
no-named-default Disallow named usage of default import and export. βœ… β˜‘οΈ πŸ”§
no-negated-comparison Disallow negated comparisons. βœ… β˜‘οΈ πŸ”§ πŸ’‘
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 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-redundant-comparison Disallow comparisons made redundant by an equality check in the same logical AND. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-return-array-push Disallow returning the result of Array#push() with arguments. βœ… πŸ’‘
no-single-promise-in-promise-methods Disallow passing single-element arrays to Promise methods. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-static-only-class Disallow classes that only have static members. βœ… β˜‘οΈ πŸ”§
no-subtraction-comparison Prefer comparing values directly over subtracting and comparing to 0. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-thenable Disallow then property. βœ… β˜‘οΈ
no-this-assignment Disallow assigning this to a variable. βœ… β˜‘οΈ
no-this-outside-of-class Disallow this outside of classes. βœ…
no-typeof-undefined Disallow comparing undefined using typeof. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-undeclared-class-members Require class members to be declared. βœ… πŸ’‘
no-unnecessary-array-flat-depth Disallow using 1 as the depth argument of Array#flat(). βœ… β˜‘οΈ πŸ”§
no-unnecessary-array-splice-count Disallow using .length or Infinity as the deleteCount or skipCount argument of Array#{splice,toSpliced}(). βœ… β˜‘οΈ πŸ”§
no-unnecessary-await Disallow awaiting non-promise values. βœ… β˜‘οΈ πŸ”§
no-unnecessary-global-this Disallow unnecessary globalThis references. βœ… β˜‘οΈ πŸ”§
no-unnecessary-nested-ternary Disallow unnecessary nested ternary expressions. βœ… β˜‘οΈ πŸ”§
no-unnecessary-polyfills Enforce the use of built-in methods instead of unnecessary polyfills. βœ… β˜‘οΈ
no-unnecessary-slice-end Disallow using .length or Infinity as the end argument of {Array,String,TypedArray}#slice(). βœ… β˜‘οΈ πŸ”§
no-unnecessary-splice Disallow Array#splice() when simpler alternatives exist. βœ… πŸ”§
no-unreadable-array-destructuring Disallow unreadable array destructuring. βœ… β˜‘οΈ πŸ”§
no-unreadable-iife Disallow unreadable IIFEs. βœ… β˜‘οΈ πŸ’‘
no-unreadable-new-expression Disallow unreadable new expressions. βœ…
no-unreadable-object-destructuring Disallow unreadable object destructuring. βœ… β˜‘οΈ
no-unsafe-string-replacement Disallow non-literal replacement values in String#replace() and String#replaceAll(). βœ…
no-unused-array-method-return Disallow ignoring the return value of selected array methods. βœ… β˜‘οΈ
no-unused-properties Disallow unused object properties.
no-useless-boolean-cast Disallow unnecessary Boolean() casts in array predicate callbacks. βœ… β˜‘οΈ πŸ”§
no-useless-collection-argument Disallow useless values or fallbacks in Set, Map, WeakSet, or WeakMap. βœ… β˜‘οΈ πŸ”§ πŸ’‘
no-useless-concat Disallow useless concatenation of literals. βœ… β˜‘οΈ πŸ”§
no-useless-else Disallow else after a statement that exits. βœ… πŸ”§
no-useless-error-capture-stack-trace Disallow unnecessary Error.captureStackTrace(…). βœ… β˜‘οΈ πŸ”§
no-useless-fallback-in-spread Disallow useless fallback when spreading in object literals. βœ… β˜‘οΈ πŸ”§
no-useless-iterator-to-array Disallow unnecessary .toArray() on iterators. βœ… β˜‘οΈ πŸ”§ πŸ’‘
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-recursion Disallow simple recursive function calls that can be replaced with a loop. βœ…
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-add-event-listener-options Prefer an options object over a boolean in .addEventListener(). βœ… β˜‘οΈ πŸ”§
prefer-array-find Prefer .find(…) and .findLast(…) over the first or last 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-from-map Prefer using the Array.from() mapping function argument. βœ… β˜‘οΈ πŸ’‘
prefer-array-index-of Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-array-last-methods Prefer last-oriented array methods over Array#reverse() or Array#toReversed() followed by a method. βœ… β˜‘οΈ πŸ’‘
prefer-array-some Prefer .some(…) over .filter(…).length check and .{find,findLast,findIndex,findLastIndex}(…). βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-at Prefer .at() method for index access and String#charAt(). βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-await Prefer await over promise chaining. βœ… β˜‘οΈ
prefer-bigint-literals Prefer BigInt literals over the constructor. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-blob-reading-methods Prefer Blob#arrayBuffer() over FileReader#readAsArrayBuffer(…) and Blob#text() over FileReader#readAsText(…). βœ… β˜‘οΈ
prefer-class-fields Prefer class field declarations over this assignments in constructors. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-classlist-toggle Prefer using Element#classList.toggle() to toggle class names. βœ… β˜‘οΈ πŸ”§ πŸ’‘
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-direct-iteration Prefer direct iteration over default iterator method calls. βœ… β˜‘οΈ πŸ”§
prefer-dispose Prefer using using/await using over manual try/finally resource disposal. πŸ’‘
prefer-dom-node-append Prefer Element#append() over Node#appendChild(). βœ… β˜‘οΈ πŸ”§
prefer-dom-node-html-methods Prefer .getHTML() and .setHTML() over .innerHTML. βœ… πŸ”§ πŸ’‘
prefer-dom-node-remove Prefer childNode.remove() over parentNode.removeChild(childNode). βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-dom-node-text-content Prefer .textContent over .innerText. βœ… β˜‘οΈ πŸ’‘
prefer-early-return Prefer early returns over full-function conditional wrapping. βœ… β˜‘οΈ
prefer-event-target Prefer EventTarget over EventEmitter. βœ… β˜‘οΈ
prefer-export-from Prefer export…from when re-exporting. βœ… πŸ”§ πŸ’‘
prefer-get-or-insert-computed Prefer .getOrInsertComputed() when the default value has side effects. βœ… πŸ”§
prefer-global-number-constants Prefer global numeric constants over Number static properties. βœ… β˜‘οΈ πŸ”§
prefer-global-this Prefer globalThis over window, self, and global. βœ… β˜‘οΈ πŸ”§
prefer-https Prefer HTTPS over HTTP. βœ… πŸ”§
prefer-identifier-import-export-specifiers Prefer identifiers over string literals in import and export specifiers. βœ… β˜‘οΈ πŸ”§
prefer-import-meta-properties Prefer import.meta.{dirname,filename} over legacy techniques for getting file paths. πŸ”§
prefer-includes Prefer .includes() over .indexOf(), .lastIndexOf(), and Array#some() when checking for existence or non-existence. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-includes-over-repeated-comparisons Prefer .includes() over repeated equality comparisons. βœ…
prefer-iterable-in-constructor Prefer passing iterables directly to constructors instead of filling empty collections. βœ… β˜‘οΈ πŸ”§
prefer-iterator-concat Prefer Iterator.concat(…) over temporary spread arrays. πŸ”§ πŸ’‘
prefer-iterator-to-array Prefer Iterator#toArray() over temporary arrays from iterator spreads. βœ… πŸ”§ πŸ’‘
prefer-iterator-to-array-at-end Prefer moving .toArray() to the end of iterator helper chains. βœ… β˜‘οΈ πŸ’‘
prefer-keyboard-event-key Prefer KeyboardEvent#key over deprecated keyboard event properties. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-location-assign Prefer location.assign() over assigning to location.href. βœ… πŸ”§
prefer-logical-operator-over-ternary Prefer using a logical operator over a ternary. βœ… β˜‘οΈ πŸ’‘
prefer-math-abs Prefer Math.abs() over manual absolute value expressions and symmetric range checks. βœ… β˜‘οΈ πŸ”§
prefer-math-min-max Prefer Math.min() and Math.max() over ternaries for simple comparisons. βœ… β˜‘οΈ πŸ”§
prefer-math-trunc Prefer Math.trunc() for truncating numbers. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-modern-dom-apis Prefer modern DOM APIs. βœ… β˜‘οΈ πŸ”§
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 when possible. βœ… β˜‘οΈ πŸ”§
prefer-node-protocol Prefer using the node: protocol when importing Node.js builtin modules. βœ… β˜‘οΈ πŸ”§
prefer-number-coercion Prefer Number() over parseFloat() and base-10 parseInt(). βœ… β˜‘οΈ πŸ’‘
prefer-number-is-safe-integer Prefer Number.isSafeInteger() over Number.isInteger(). βœ… πŸ’‘
prefer-number-properties Prefer Number static methods over global functions and optionally static properties over global constants. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-object-from-entries Prefer using Object.fromEntries(…) to transform a list of key-value pairs into an object. βœ… β˜‘οΈ πŸ”§
prefer-object-iterable-methods Prefer the most specific Object iterable method. βœ… β˜‘οΈ πŸ”§
prefer-optional-catch-binding Prefer omitting the catch binding parameter. βœ… β˜‘οΈ πŸ”§
prefer-private-class-fields Prefer private class fields over the underscore-prefix convention. βœ… πŸ”§
prefer-prototype-methods Prefer borrowing methods from the prototype instead of the instance. βœ… β˜‘οΈ πŸ”§
prefer-query-selector Prefer .querySelector() and .querySelectorAll() over older DOM query methods. βœ… πŸ”§
prefer-queue-microtask Prefer queueMicrotask() over process.nextTick(), setImmediate(), and setTimeout(…, 0). βœ… β˜‘οΈ πŸ”§
prefer-reflect-apply Prefer Reflect.apply() over Function#apply(). βœ… β˜‘οΈ πŸ”§
prefer-regexp-test Prefer RegExp#test() over String#match(), String#search(), and RegExp#exec(). βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-response-static-json Prefer Response.json() over new Response(JSON.stringify()). βœ… β˜‘οΈ πŸ”§
prefer-scoped-selector Prefer :scope when using element query selector methods. βœ… πŸ’‘
prefer-set-has Prefer Set#has() over Array#includes() when checking for existence or non-existence. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-set-size Prefer using Set#size instead of Array#length. βœ… β˜‘οΈ πŸ”§
prefer-short-arrow-method Prefer arrow function properties over methods with a single return. βœ… πŸ”§
prefer-simple-condition-first Prefer simple conditions first in logical expressions. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-simple-sort-comparator Prefer a simple comparison function for Array#sort(). βœ… β˜‘οΈ πŸ’‘
prefer-single-array-predicate Prefer a single Array#some() or Array#every() with a combined predicate. βœ… β˜‘οΈ πŸ’‘
prefer-single-call Enforce combining multiple Array#{push,unshift}(), Element#classList.{add,remove}(), and importScripts() into one call. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-single-object-destructuring Prefer a single object destructuring declaration per local const source. βœ… πŸ”§
prefer-split-limit Prefer String#split() with a limit. βœ… β˜‘οΈ πŸ”§
prefer-spread Prefer the spread operator over Array.from(…), Array#concat(…), and Array#{slice,toSpliced}(). βœ… πŸ”§ πŸ’‘
prefer-string-match-all Prefer String#matchAll() over RegExp#exec() loops. βœ… β˜‘οΈ πŸ”§
prefer-string-pad-start-end Prefer String#padStart() and String#padEnd() over manual string padding. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-string-raw Prefer using the String.raw tag to avoid escaping \. βœ… β˜‘οΈ πŸ”§
prefer-string-repeat Prefer String#repeat() for repeated whitespace. βœ… β˜‘οΈ πŸ”§
prefer-string-replace-all Prefer String#replaceAll() over regex searches with the global flag and String#split().join(). βœ… β˜‘οΈ πŸ”§
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() and String#indexOf() === 0. βœ… β˜‘οΈ πŸ”§ πŸ’‘
prefer-string-trim-start-end Prefer String#trimStart() / String#trimEnd() over String#trimLeft() / String#trimRight(). βœ… β˜‘οΈ πŸ”§
prefer-structured-clone Prefer using structuredClone to create a deep clone. βœ… β˜‘οΈ πŸ’‘
prefer-switch Prefer switch over multiple else-if. βœ… β˜‘οΈ πŸ”§
prefer-temporal Prefer Temporal over Date. πŸ”§ πŸ’‘
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. βœ… β˜‘οΈ πŸ”§
prefer-type-literal-last Require type literals to be last in union and intersection types. βœ… β˜‘οΈ πŸ”§
prefer-uint8array-base64 Prefer Uint8Array#toBase64() and Uint8Array.fromBase64() over atob(), btoa(), and Buffer base64 conversions. βœ… β˜‘οΈ πŸ’‘
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-array-sort-compare Require a compare function when calling Array#sort() or Array#toSorted(). βœ… β˜‘οΈ πŸ’‘
require-css-escape Require CSS.escape() for interpolated values in CSS selectors. βœ… β˜‘οΈ πŸ”§
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 Number#toFixed(). βœ… β˜‘οΈ πŸ”§
require-passive-events Require passive event listeners for high-frequency events. βœ… β˜‘οΈ πŸ”§
require-post-message-target-origin Enforce using the targetOrigin argument with window.postMessage(). πŸ’‘
require-proxy-trap-boolean-return Require boolean-returning Proxy traps to return booleans. βœ… β˜‘οΈ πŸ”§
string-content Enforce better string content. πŸ”§ πŸ’‘
switch-case-braces Enforce consistent brace style for case clauses. βœ… πŸ”§
switch-case-break-position Enforce consistent break/return/continue/throw position in case clauses. βœ… πŸ”§
template-indent Fix whitespace-insensitive template indentation. βœ… πŸ”§
text-encoding-identifier-case Enforce consistent case for text encoding identifiers. βœ… β˜‘οΈ πŸ”§ πŸ’‘
throw-new-error Require new when creating an error. βœ… β˜‘οΈ πŸ”§
try-complexity Limit the complexity of try blocks.

Non-JavaScript files

While most rules target JavaScript and TypeScript, some also lint other file types when used with the corresponding ESLint language plugin such as @eslint/css, @eslint/json, @eslint/markdown, or @html-eslint/eslint-plugin. Each such rule declares this with the meta.languages field.

These rules work on any file type:

These rules also work on specific non-JavaScript languages:

Name CSS HTML JSON Markdown
expiring-todo-comments βœ… βœ…
no-empty-file βœ… βœ… βœ… βœ…
no-invalid-file-input-accept βœ…

Deleted and deprecated rules

See the list.

Preset configs

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

Note: Preset configs will also enable the correct language options.

Recommended config

This plugin exports a recommended config that enforces good practices.

import eslintPluginUnicorn from 'eslint-plugin-unicorn';

export default [
		// …
		eslintPluginUnicorn.configs.recommended,
		{
			rules: {
				'unicorn/prefer-module': 'warn',
			},
		},
];

All config

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

import eslintPluginUnicorn from 'eslint-plugin-unicorn';

export default [
		// …
		eslintPluginUnicorn.configs.all,
		{
			rules: {
				'unicorn/prefer-module': 'warn',
			},
		},
];

Maintainers

Former

Packages

 
 
 

Contributors