What's the difference between plugins and extends in eslint?

前端 未结 3 526
醉话见心
醉话见心 2020-12-28 11:43

I don\'t understand why we have plugins and extends. What is the difference between them and do I need one or the other?

相关标签:
3条回答
  • 2020-12-28 12:32

    So found out that plugins add extra capabilities and extends gives you a baseline on which to add your own custom rules. Thanks to my friend Oliver for helping me answer this question!

    0 讨论(0)
  • 2020-12-28 12:37

    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. A plugin may provide you with zero, one, or more configuration files. If the plugin provides configuration file, then you can load that in your extends section after adding the plugin in the plugins section.

    So essentially, plugins given you some rules that have been coded and you can choose which ones are relevant. It may also provide config files to apply rules that the authors think are logically grouped/relevant but providing a config file is not mandatory for a plugin. extends, on the other hand, provides you the ability to apply rules in bulk based on config file specifications.

    Example Plugin - eslint-plugin-react:

    "plugins": [
      "react"
    ],
    "extends": [
      "eslint:recommended",
      "plugin:react/recommended"
    ]
    

    Example Config - eslint-config-google:

    "extends": [
      "google"
    ]
    

    Good Luck...

    0 讨论(0)
  • 2020-12-28 12:44

    In addition to shmit's good answer:

    extends

    is about extending configurations in general, not only plugins. Potential values are:

    • "eslint:recommended"
    • "eslint:all"
    • Shareable configuration from npm package (eslint-config-xxx or scoped name)
    • Plugin configuration from npm package (eslint-plugin-xxx or scoped name)
    • Another configuration file, like "./my/path/.eslintrc.js"

    Plugin notation: plugin:<package name>/<configuration name>, e.g. for eslint-plugin-react:

     "extends": ["plugin:react/recommended"]
    

    By extending from a plugin config, we can get recommended rules without adding them manually.

    plugins

    A plugin is a special eslint npm package, that provides additional rule definitions (rules), environments, processors and configs for different configurations of recommended / default rule values.

    The plugins property in .eslintrc.js is merely a flag to enable a given plugin after installation with npm i. We now can refer to the plugin's rules, but have to set all rules values manually.

    0 讨论(0)
提交回复
热议问题