readme.md 40 KB

eslint-plugin-unicorn Coverage Status npm version

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 rule in package.json.

If you don't use the preset, ensure you use the same env and parserOptions config as below.

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

Rules

๐Ÿ’ผ Configurations enabled in.\ ๐Ÿšซ Configurations disabled in.\ โœ… Set in the recommended configuration.\ ๐Ÿ”ง Automatically fixable by the --fix 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-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-negated-condition | Disallow negated conditions. | โœ… | | ๐Ÿ”ง | | | 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-typeof-undefined | Disallow comparing undefined using typeof. | โœ… | | ๐Ÿ”ง | ๐Ÿ’ก | | no-unnecessary-await | Disallow awaiting non-promise values. | โœ… | | ๐Ÿ”ง | | | 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(โ€ฆ) 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-index-of | Prefer Array#{indexOf,lastIndexOf}() over Array#{findIndex,findLastIndex}() when looking for the index of an item. | โœ… | | ๐Ÿ”ง | ๐Ÿ’ก | | prefer-array-some | Prefer .some(โ€ฆ) over .filter(โ€ฆ).length check and .{find,findLast}(โ€ฆ). | โœ… | | ๐Ÿ”ง | ๐Ÿ’ก | | 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,at}() and Array#splice(). | โœ… | | ๐Ÿ”ง | | | 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-set-size | Prefer using Set#size instead of Array#length. | โœ… | | ๐Ÿ”ง | | | 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. | | โœ… | ๐Ÿ”ง | ๐Ÿ’ก | | switch-case-braces | Enforce consistent brace style for 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 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.

Recommended config

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