What is eslint-plugin-unicorn?
The eslint-plugin-unicorn package is a set of ESLint rules that aim to improve code quality by enforcing best practices, preventing common mistakes, and suggesting modern syntax and methods. It includes a variety of rules that are not covered by the core ESLint rules or other plugins, focusing on code enhancements and consistency.
What are eslint-plugin-unicorn's main functionalities?
Filename Case
Enforces a specific case for filenames, which can be configured to be kebab-case, camelCase, or snake_case. This helps maintain consistency in the naming of files across a project.
"unicorn/filename-case": ["error", {"case": "kebabCase"}]
Number Literal Case
Enforces lowercase identifier and uppercase value for number literals, improving readability and consistency in the code.
"unicorn/number-literal-case": "error"
Prevent Abbreviations
Discourages the use of abbreviations in variable names, function names, and other identifiers to improve code clarity and readability.
"unicorn/prevent-abbreviations": ["error", {"replacements": {"temp": false, "err": {"error": true}}}]
Better Regex
Suggests simpler and more optimized regular expressions to improve performance and readability.
"unicorn/better-regex": "error"
Consistent Function Scoping
Enforces that functions are defined at the highest possible level, which can help with readability and reduce the complexity of nested functions.
"unicorn/consistent-function-scoping": "error"
Other packages similar to eslint-plugin-unicorn
eslint-plugin-import
This plugin provides linting rules related to ES2015+ import/export syntax, helping to ensure proper import order, naming, and file path correctness. It is similar to eslint-plugin-unicorn in that it enforces best practices, but it focuses specifically on module import and export issues.
eslint-plugin-promise
This plugin enforces best practices for JavaScript promises. It is similar to eslint-plugin-unicorn in its goal to improve code quality, but it focuses specifically on the use of promises and asynchronous code.
eslint-plugin-jsx-a11y
This plugin checks JSX code for accessibility issues, ensuring that web applications are accessible to users with disabilities. While eslint-plugin-unicorn focuses on general JavaScript code quality, eslint-plugin-jsx-a11y focuses on accessibility concerns in React and JSX.
eslint-plugin-react
This plugin provides React-specific linting rules. It is similar to eslint-plugin-unicorn in that it helps maintain code quality and consistency, but it is tailored specifically for React development.
eslint-plugin-unicorn
Various awesome ESLint rules
You might want to check out XO, which includes this plugin.
Install
$ npm install --save-dev eslint eslint-plugin-unicorn
Usage
Configure it in package.json
.
{
"name": "my-awesome-project",
"eslintConfig": {
"env": {
"es6": true
},
"parserOptions": {
"ecmaVersion": 2017,
"sourceType": "module"
},
"plugins": [
"unicorn"
],
"rules": {
"unicorn/catch-error-name": ["error", {"name": "err"}],
"unicorn/explicit-length-check": "error",
"unicorn/filename-case": ["error", {"case": "kebabCase"}],
"unicorn/no-abusive-eslint-disable": "error",
"unicorn/no-process-exit": "error",
"unicorn/throw-new-error": "error",
"unicorn/number-literal-case": "error",
"unicorn/escape-case": "error",
"unicorn/no-array-instanceof": "error",
"unicorn/no-new-buffer": "error",
"unicorn/no-hex-escape": "error",
"unicorn/custom-error-definition": "error",
"unicorn/prefer-starts-ends-with": "error",
"unicorn/prefer-type-error": "error"
}
}
}
Rules
Recommended config
This plugin exports a recommended
config that enforces good practices.
Enable it in your package.json
with the extends
option:
{
"name": "my-awesome-project",
"eslintConfig": {
"plugins": [
"unicorn"
],
"extends": "plugin:unicorn/recommended"
}
}
See the ESLint docs for more information about extending config files.
Note: This config will also enable the correct parser options and environment.
Created by
License
MIT © Sindre Sorhus