Skip to content

Commit 3e48857

Browse files
jjangga0214ljharb
authored andcommitted
[New] support new config system
- add top-level entry points for recommended configs - add documentation to readme
1 parent 7b3492b commit 3e48857

File tree

5 files changed

+148
-1
lines changed

5 files changed

+148
-1
lines changed

README.md

+120-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ 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+
<a id="usage"></a>
64+
## Usage (legacy: `.eslintrc`)
6465

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

@@ -109,6 +110,124 @@ configuration file by mapping each custom component name to a DOM element type.
109110
}
110111
```
111112

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

114233
<!-- begin auto-generated rules list -->

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)