Skip to content

Automatically configure ESLint based on project dependencies

Notifications You must be signed in to change notification settings

alecmev/eslint-config-auto

 
 

Repository files navigation

eslint-config-auto  NPM version License: MIT

Automatically configure ESLint based on project dependencies

This project got created because I got tired of managing eslint in multiple different projects and trying to keep them all vaguely in sync. It will automatically configure the most appropriate version of the airbnb eslint rules and a curated selection of plugins based on the contents of your project's package.json file each time you run eslint. When you add a new library to your project, the associated plugin will get automatically included.

The aim here is to include a range of mostly reasonable plugins, whilst not being overly restrictive. The recommended configs for each plugin are used with a few exceptions for rules that have been disabled for appearing unduely restrictive or conflicting with other plugins. If you use Prettier then all the formating rules are also omitted by including eslint-config-prettier.

In addition to JavaScript, suport is included for linting TypeScript, HTML, Json, and MarkDown files.

Alternative

If you like the ideas behind this config, but not the AirBnB styleguide, then checkout eslint-config-adjunt. It includes most of the extra plugins used here, but lets you choose your prefered main eslint config.

Contibuting

Suggestions for adding new plugings will be carefully considered against the mostly reasonable standard and usefulness. If you would like to help out, then I would like to add support for Vue and other popular frameworks. Thanks goes to awesome-eslint for having collated the packages this project utalises.

Install

To install this config, run the following command.

npm install eslint-config-auto --save-dev

Configure

Create an .eslintrc file with the following contents.

{
  "extends": ["auto"]
}

You can now include html, json and markdown in the list of files passed to eslint to lint any JavaScript contained.

{
  "scripts": {
    "eslint": "eslint --color --ext .html,.js,.json,.jsx,.md,.ts,.tsx *.* src",
    "eslint:fix": "npm run eslint -- --fix"
  }
}

Install Dependencies

After you have configured eslint to include this package, the first time you run eslint it will output the npm command to install the dependencies required for your project. Copy and paste this command into the console, and you are then ready to start linting.

Rules

AirBNB

The most appropreate version of the AirBNB eslint config will be automatically selected.

Babel

If the project includes Babel in it's devDependencies, then eslint-plugin-babel and eslint-config-airbnb-babel will be loaded and the parser will be set to babel-eslint.

Some project setup utils, such as Create React App, use Babel without including it as a project dependancies. In such cases you can turn Babel support on by adding settings: { babel: true } to your .eslintrc config file.

Code Quality

These two plugins provide a range of code quality rules:

Compat

If settings.compat = true in your .eslintrc, then eslint-plugin-compat is loaded.

Langauges

The following plugins expand esLint to support TypeScript, JSON, and lint code contiained in HTML and MarkDown files:

If the project includes TypeScript, then the rules will adapt to lint typescript files and the parser will be set to @typescript-eslint/parser for ts and tsx filetypes.

When linting code snippets in Markdown files, a few rules relating to globals and unused vars are disabled.

Library Plugins

These plugins will be loaded in based on your project dependencies in package.json. If a supported library is part of your project then it's related esLint plugins will be loaded. The following packages are supported:

Node

If env.node = true in your .eslintrc file, then eslint-plugin-node is loaded.

React

If a project contains React, then a React version of the AirBNB config will be used and if Redux is installed the following extra plugins will be loaded.

Practices

The following esLint plugins enforce good coding practices:

Prettier

If prettier is installed, any rules that may conflict with Prettier will be disabled. The plugin should read you Prettier config from your project's root.

The prettier configs for different eslint plugins are also automatically included based on which eslint plugins have been installed into your project.

Security

These plugins add code security rules to esLint:

Test Libraries

Test plugins are loaded based on which testing tools you have listed in devDependencies of package.json. The following test plugins are supported:

For test files a few rules are turned off, to better to support normal unit test coding styles.

Rules

In the most part the default rules are used for the plugins listed above, with the following exceptions.

Switch-Case

Adds the fallthrough: 'never' option to the newline-between-switch-case rule.

// Good

switch (foo) {
  case 1:
    something()
    break

  case 2:
  case 3:
    somethingElse()
    break

  default:
    defaultThing()
}

Disabled rules

The following rules are disabled due to them being considered unduly restrictive or unhelpful.

  • jest/no-disabled-tests
  • react-redux/prefer-separate-component-file
  • redux-saga/no-unhandled-errors
  • lodash/prefer over native rules
  • lodash-fp/use-fp
  • unicorn/no-array-for-each
  • unicorn/no-fn-reference-in-iterator
  • unicorn/no-array-for-each
  • unicorn/no-reduce
  • unicorn/no-null
  • unicorn/prefer-number-properties
  • unicorn/prefer-optional-catch-binding
  • unicorn/prevent-abbreviations

The following rules are disabled due to clashing with other plugins

  • array-func/prefer-array-from
  • import/order
  • sort-imports

License

Copyright © 2020-21 David J. Bradshaw. Licensed under the MIT License.

About

Automatically configure ESLint based on project dependencies

Resources

Security policy

Stars

Watchers

Forks

Packages

No packages published

Languages

  • JavaScript 100.0%