Skip to content

Commit a6750f7

Browse files
jjangga0214ljharb
authored andcommitted
[New] support new config system
- add top-level entry points for recommended configs - add documentation to readme
1 parent 65be35b commit a6750f7

File tree

5 files changed

+149
-2
lines changed

5 files changed

+149
-2
lines changed

README.md

+121-2
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ yarn add eslint-plugin-jsx-a11y --dev
6060

6161
**Note:** If you installed ESLint globally (using the `-g` flag in npm, or the `global` prefix in yarn) then you must also install `eslint-plugin-jsx-a11y` globally.
6262

63-
## Usage
63+
## Usage (legacy: `.eslintrc`)
6464

6565
Add `jsx-a11y` to the plugins section of your `.eslintrc` configuration file. You can omit the `eslint-plugin-` prefix:
6666

@@ -108,6 +108,125 @@ configuration file by mapping each custom component name to a DOM element type.
108108
}
109109
```
110110

111+
## Usage (new: `eslint.config.js`)
112+
113+
From [`v8.21.0`](https://github.com/eslint/eslint/releases/tag/v8.21.0), eslint announced a new config system.
114+
In the new system, `.eslintrc*` is no longer used. `eslint.config.js` would be the default config file name.
115+
In eslint `v8`, the legacy system (`.eslintrc*`) would still be supported, while in eslint `v9`, only the new system would be supported.
116+
117+
And from [`v8.23.0`](https://github.com/eslint/eslint/releases/tag/v8.23.0), eslint CLI starts to look up `eslint.config.js`.
118+
**So, if your eslint is `>=8.23.0`, you're 100% ready to use the new config system.**
119+
120+
You might want to check out the official blog posts,
121+
122+
- <https://eslint.org/blog/2022/08/new-config-system-part-1/>
123+
- <https://eslint.org/blog/2022/08/new-config-system-part-2/>
124+
- <https://eslint.org/blog/2022/08/new-config-system-part-3/>
125+
126+
and the [official docs](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new).
127+
128+
The default export of `eslint-plugin-jsx-a11y` is a plugin object.
129+
130+
```js
131+
const jsxA11y = require('eslint-plugin-jsx-a11y');
132+
133+
module.exports = [
134+
135+
{
136+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
137+
plugins: {
138+
'jsx-a11y': jsxA11y,
139+
},
140+
languageOptions: {
141+
parserOptions: {
142+
ecmaFeatures: {
143+
jsx: true,
144+
},
145+
},
146+
},
147+
rules: {
148+
// ... any rules you want
149+
'jsx-a11y/alt-text': 'error',
150+
},
151+
// ... others are omitted for brevity
152+
},
153+
154+
];
155+
```
156+
157+
There're also 2 shareable configs.
158+
159+
- `eslint-plugin-jsx-a11y/strict`
160+
- `eslint-plugin-jsx-a11y/recommended`
161+
162+
If your eslint.config.js is ESM, include the `.js` extension (e.g. `eslint-plugin-jsx-a11y/recommended.js`). Note that the next semver-major will require omitting the extension for these imports.
163+
164+
**Note**: These configurations will import `eslint-plugin-jsx-a11y` and enable JSX in [`languageOptions.parserOptions`](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuration-objects).
165+
166+
In the new config system, the `plugin:` protocol(e.g. `plugin:jsx-a11y/recommended`) is no longer valid.
167+
As eslint does not automatically import the preset config (shareable config), you explicitly do it by yourself.
168+
169+
```js
170+
const jsxA11yRecommended = require('eslint-plugin-jsx-a11y/recommended');
171+
172+
export default [
173+
174+
jsxA11yRecommended, // This is not a plugin object, but a shareable config object
175+
176+
];
177+
```
178+
179+
You can of course add/override properties.
180+
181+
**Note**: Our shareable configs does not preconfigure `files` or [`languageOptions.globals`](https://eslint.org/docs/latest/user-guide/configuring/configuration-files-new#configuration-objects).
182+
For most of the cases, you probably want to configure some properties by yourself.
183+
184+
```js
185+
const jsxA11yRecommended = require('eslint-plugin-jsx-a11y/recommended');
186+
const globals = require('globals');
187+
188+
module.exports = [
189+
190+
{
191+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
192+
...jsxA11yRecommended,
193+
languageOptions: {
194+
...jsxA11yRecommended.languageOptions,
195+
globals: {
196+
...globals.serviceworker,
197+
...globals.browser,
198+
},
199+
},
200+
},
201+
202+
];
203+
```
204+
205+
The above example is same as the example below, as the new config system is based on chaining.
206+
207+
```js
208+
const jsxA11yRecommended = require('eslint-plugin-jsx-a11y/recommended');
209+
const globals = require('globals');
210+
211+
module.exports = [
212+
213+
{
214+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
215+
...jsxA11yRecommended,
216+
},
217+
{
218+
files: ['**/*.{js,mjs,cjs,jsx,mjsx,ts,tsx,mtsx}'],
219+
languageOptions: {
220+
globals: {
221+
...globals.serviceworker,
222+
...globals.browser,
223+
},
224+
}
225+
},
226+
227+
];
228+
```
229+
111230
## Supported Rules
112231

113232
<!-- AUTO-GENERATED-CONTENT:START (LIST) -->
@@ -293,7 +412,7 @@ The following rules have extra options when in _recommended_ mode:
293412
If you are developing new rules for this project, you can use the `create-rule`
294413
script to scaffold the new files.
295414

296-
```
415+
```bash
297416
$ ./scripts/create-rule.js my-new-rule
298417
```
299418

recommended.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/no-unresolved, import/extensions
2+
module.exports = require('./lib/configs/recommended');

src/configs/recommended.js

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import jsxAlly from '..';
2+
3+
const { configs, ...plugin } = jsxAlly;
4+
5+
export default {
6+
plugins: {
7+
'jsx-a11y': plugin,
8+
},
9+
languageOptions: {
10+
parserOptions: {
11+
ecmaFeatures: {
12+
jsx: true,
13+
},
14+
},
15+
},
16+
rules: configs.recommended.rules,
17+
};

src/configs/strict.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import jsxAlly from '..';
2+
import recommended from './recommended';
3+
4+
export default {
5+
...recommended,
6+
rules: jsxAlly.configs.strict.rules,
7+
};

strict.js

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
// eslint-disable-next-line import/no-unresolved, import/extensions
2+
module.exports = require('./lib/configs/strict');

0 commit comments

Comments
 (0)