Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ESLint extends vs plugins v2020

There's answered question which in my opinion doesn't actually answers the question, on the difference between extends: [] vs plugins: [] in ESLint.

In my case, i just used extends section:

extends: [
  'plugin:@typescript-eslint/recommended',
],
plugins: [],
rules: {
  '@typescript-eslint/explicit-function-return-type': [
    'error',
    {
      allowExpressions: true,
    },
  ],
}

As you can see, i just used predefined config from plugin:@typescript-eslint/recommended and also overwritten @typescript-eslint/explicit-function-return-type rule in rules: {} section. But why do we need this PLUGINS section then? If everything works without it? What do i miss?

like image 420
Alexander Kim Avatar asked Apr 30 '20 16:04

Alexander Kim


People also ask

What is the difference between ESLint sections extends and plugin?

extends uses a config file which applies set of rules when you add that to the extends options. A plugin on the other hand provides you with a set of rules that you can individually apply depending on your need. Just having a plugin does not enforce any rule. You have to choose which rules you need.

What is the use of ESLint plugin?

ESLint handles such a named code block as a child file of the original file. You can specify additional configurations for named code blocks in the overrides section of the config. For example, the following disables the strict rule for the named code blocks which end with . js in markdown files.

How do I configure Eslintrc?

There are two primary ways to configure ESLint: Configuration Comments - use JavaScript comments to embed configuration information directly into a file. Configuration Files - use a JavaScript, JSON, or YAML file to specify configuration information for an entire directory and all of its subdirectories.

Where is the ESLint config file?

The ESLint configuration is in the ./node_modules/@spm/eslint-config/index. js file. To check the code for ESLint violations, run the following command in the application root directory.


1 Answers

You have done it correctly.

For your example, there are 2 ways to do add typescript-eslint...

  • 1st way:
{
  parser: "@typescript-eslint/parser",
  parserOptions: { sourceType: "module" },
  plugins: ["@typescript-eslint"],
  extends: [],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}
  • 2nd way:
{
  plugins: [],
  extends: ["plugin:@typescript-eslint/recommended"],
  rules: {
    "@typescript-eslint/explicit-function-return-type": [
      "error",
      {
        allowExpressions: true
      }
    ]
  }
}

The difference is...

  • 1st way:
    • parser, parserOptions and plugins are manually added,
    • Only @typescript-eslint/explicit-function-return-type is enforced.
  • 2nd way:
    • plugin:@typescript-eslint/recommended has automatically added parser, parserOptions and plugins.
    • Recommended typescript rules are added and enforced.
    • @typescript-eslint/explicit-function-return-type is augmented and enforced.

This is why when you use plugin:@typescript-eslint/recommended, things work normally even if plugins is empty. A well-written plugins/configs allows that to happen.

like image 181
Emerzonik Avatar answered Sep 17 '22 17:09

Emerzonik