]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/user-guide/configuring/plugins.md
import 8.4.0 source
[pve-eslint.git] / eslint / docs / user-guide / configuring / plugins.md
CommitLineData
5422a9cc
TL
1# Plugins
2
5422a9cc
TL
3* [Specifying Parser](#specifying-parser)
4* [Specifying Processor](#specifying-processor)
5* [Configuring Plugins](#configuring-plugins)
6
7## Specifying Parser
8
9By default, ESLint uses [Espree](https://github.com/eslint/espree) as its parser. You can optionally specify that a different parser should be used in your configuration file so long as the parser meets the following requirements:
10
111. It must be a Node module loadable from the config file where the parser is used. Usually, this means you should install the parser package separately using npm.
121. It must conform to the [parser interface](https://eslint.org/docs/developer-guide/working-with-custom-parsers).
13
14Note that even with these compatibilities, there are no guarantees that an external parser will work correctly with ESLint and ESLint will not fix bugs related to incompatibilities with other parsers.
15
16To indicate the npm module to use as your parser, specify it using the `parser` option in your `.eslintrc` file. For example, the following specifies to use Esprima instead of Espree:
17
18```json
19{
20 "parser": "esprima",
21 "rules": {
22 "semi": "error"
23 }
24}
25```
26
27The following parsers are compatible with ESLint:
28
29* [Esprima](https://www.npmjs.com/package/esprima)
30* [@babel/eslint-parser](https://www.npmjs.com/package/@babel/eslint-parser) - A wrapper around the [Babel](https://babeljs.io) parser that makes it compatible with ESLint.
31* [@typescript-eslint/parser](https://www.npmjs.com/package/@typescript-eslint/parser) - A parser that converts TypeScript into an ESTree-compatible form so it can be used in ESLint.
32
33Note when using a custom parser, the `parserOptions` configuration property is still required for ESLint to work properly with features not in ECMAScript 5 by default. Parsers are all passed `parserOptions` and may or may not use them to determine which features to enable.
34
35## Specifying Processor
36
37Plugins may provide processors. Processors can extract JavaScript code from other kinds of files, then let ESLint lint the JavaScript code or processors can convert JavaScript code in preprocessing for some purpose.
38
39To specify processors in a configuration file, use the `processor` key with the concatenated string of a plugin name and a processor name by a slash. For example, the following enables the processor `a-processor` that the plugin `a-plugin` provided:
40
41```json
42{
43 "plugins": ["a-plugin"],
44 "processor": "a-plugin/a-processor"
45}
46```
47
48To specify processors for specific kinds of files, use the combination of the `overrides` key and the `processor` key. For example, the following uses the processor `a-plugin/markdown` for `*.md` files.
49
50```json
51{
52 "plugins": ["a-plugin"],
53 "overrides": [
54 {
55 "files": ["*.md"],
56 "processor": "a-plugin/markdown"
57 }
58 ]
59}
60```
61
62Processors may make named code blocks such as `0.js` and `1.js`. 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.
63
64```json
65{
66 "plugins": ["a-plugin"],
67 "overrides": [
68 {
69 "files": ["*.md"],
70 "processor": "a-plugin/markdown"
71 },
72 {
73 "files": ["**/*.md/*.js"],
74 "rules": {
75 "strict": "off"
76 }
77 }
78 ]
79}
80```
81
82ESLint checks the file path of named code blocks then ignores those if any `overrides` entry didn't match the file path. Be sure to add an `overrides` entry if you want to lint named code blocks other than `*.js`.
83
84## Configuring Plugins
85
86ESLint supports the use of third-party plugins. Before using the plugin, you have to install it using npm.
87
88To configure plugins inside of a configuration file, use the `plugins` key, which contains a list of plugin names. The `eslint-plugin-` prefix can be omitted from the plugin name.
89
90```json
91{
92 "plugins": [
93 "plugin1",
94 "eslint-plugin-plugin2"
95 ]
96}
97```
98
99And in YAML:
100
101```yaml
102---
103 plugins:
104 - plugin1
105 - eslint-plugin-plugin2
106```
107
108**Notes:**
109
1101. Plugins are resolved relative to the config file. In other words, ESLint will load the plugin as a user would obtain by running `require('eslint-plugin-pluginname')` in the config file.
1112. Plugins in the base configuration (loaded by `extends` setting) are relative to the derived config file. For example, if `./.eslintrc` has `extends: ["foo"]` and the `eslint-config-foo` has `plugins: ["bar"]`, ESLint finds the `eslint-plugin-bar` from `./node_modules/` (rather than `./node_modules/eslint-config-foo/node_modules/`) or ancestor directories. Thus every plugin in the config file and base configurations is resolved uniquely.
112
113### Naming convention
114
115#### Include a plugin
116
117The `eslint-plugin-` prefix can be omitted for non-scoped packages
118
119```js
120{
121 // ...
122 "plugins": [
123 "jquery", // means eslint-plugin-jquery
124 ]
125 // ...
126}
127```
128
129The same rule does apply to scoped packages:
130
131```js
132{
133 // ...
134 "plugins": [
135 "@jquery/jquery", // means @jquery/eslint-plugin-jquery
136 "@foobar" // means @foobar/eslint-plugin
137 ]
138 // ...
139}
140```
141
142#### Use a plugin
143
144When using rules, environments or configs defined by plugins, they must be referenced following the convention:
145
146* `eslint-plugin-foo` → `foo/a-rule`
147* `@foo/eslint-plugin` → `@foo/a-config`
148* `@foo/eslint-plugin-bar` → `@foo/bar/a-environment`
149
150For example:
151
152```js
153{
154 // ...
155 "plugins": [
156 "jquery", // eslint-plugin-jquery
157 "@foo/foo", // @foo/eslint-plugin-foo
158 "@bar" // @bar/eslint-plugin
159 ],
160 "extends": [
161 "plugin:@foo/foo/recommended",
162 "plugin:@bar/recommended"
163 ],
164 "rules": {
165 "jquery/a-rule": "error",
166 "@foo/foo/some-rule": "error",
167 "@bar/another-rule": "error"
168 },
169 "env": {
170 "jquery/jquery": true,
171 "@foo/foo/env-foo": true,
172 "@bar/env-bar": true,
173 }
174 // ...
175}
176```