Skip to content

Commit 5b04837

Browse files
authored
feat: support new flat config (#468)
close #455
1 parent 223ed69 commit 5b04837

File tree

20 files changed

+423
-84
lines changed

20 files changed

+423
-84
lines changed

.changeset/fifty-vans-vanish.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"eslint-mdx": minor
3+
"eslint-plugin-mdx": minor
4+
---
5+
6+
feat: add metadata for parser, processor and plugin

.changeset/shy-mayflies-dress.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-plugin-mdx": minor
3+
---
4+
5+
feat: support new flat config - close #455

README.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- [Install](#install)
3333
- [Notice](#notice)
3434
- [Usage](#usage)
35+
- [Classic](#classic)
36+
- [Flat Config](#flat-config)
3537
- [Parser Options](#parser-options)
3638
- [Rules](#rules)
3739
- [mdx/remark](#mdxremark)
@@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922
7476

7577
## Usage
7678

77-
1. In your ESLint 8+ config file, just add:
78-
79-
```jsonc
80-
{
81-
"extends": ["plugin:mdx/recommended"],
82-
// optional, if you want to lint code blocks at the same time
83-
"settings": {
84-
"mdx/code-blocks": true,
85-
// optional, if you want to disable language mapper, set it to `false`
86-
// if you want to override the default language mapper inside, you can provide your own
87-
"mdx/language-mapper": {}
88-
}
89-
}
90-
```
91-
92-
2. Make sure ESLint knows to run on `.md` or `.mdx` files:
93-
94-
```sh
95-
eslint . --ext js,md,mdx
96-
```
79+
### Classic
80+
81+
`.eslintrc` file:
82+
83+
```jsonc
84+
{
85+
"extends": ["plugin:mdx/recommended"],
86+
// optional, if you want to lint code blocks at the same time
87+
"settings": {
88+
"mdx/code-blocks": true,
89+
// optional, if you want to disable language mapper, set it to `false`
90+
// if you want to override the default language mapper inside, you can provide your own
91+
"mdx/language-mapper": {}
92+
}
93+
}
94+
```
95+
96+
### Flat Config
97+
98+
`eslint.config.js` file:
99+
100+
```js
101+
import * as mdx from 'eslint-plugin-mdx'
102+
103+
export default [
104+
{
105+
...mdx.flat,
106+
// optional, if you want to lint code blocks at the same
107+
processor: mdx.createRemarkProcessor({
108+
lintCodeBlocks: true,
109+
// optional, if you want to disable language mapper, set it to `false`
110+
// if you want to override the default language mapper inside, you can provide your own
111+
languageMapper: {},
112+
}),
113+
},
114+
{
115+
...mdx.flatCodeBlocks,
116+
rules: {
117+
...mdx.flatCodeBlocks.rules,
118+
// if you want to override some rules for code blocks
119+
'no-var': 'error',
120+
'prefer-const': 'error',
121+
},
122+
},
123+
]
124+
```
125+
126+
Then, make sure ESLint knows to run on `.md` or `.mdx` files:
127+
128+
```sh
129+
eslint . --ext js,md,mdx
130+
```
97131

98132
## Parser Options
99133

packages/eslint-mdx/README.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- [Install](#install)
3333
- [Notice](#notice)
3434
- [Usage](#usage)
35+
- [Classic](#classic)
36+
- [Flat Config](#flat-config)
3537
- [Parser Options](#parser-options)
3638
- [Rules](#rules)
3739
- [mdx/remark](#mdxremark)
@@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922
7476

7577
## Usage
7678

77-
1. In your ESLint 8+ config file, just add:
78-
79-
```jsonc
80-
{
81-
"extends": ["plugin:mdx/recommended"],
82-
// optional, if you want to lint code blocks at the same time
83-
"settings": {
84-
"mdx/code-blocks": true,
85-
// optional, if you want to disable language mapper, set it to `false`
86-
// if you want to override the default language mapper inside, you can provide your own
87-
"mdx/language-mapper": {}
88-
}
89-
}
90-
```
91-
92-
2. Make sure ESLint knows to run on `.md` or `.mdx` files:
93-
94-
```sh
95-
eslint . --ext js,md,mdx
96-
```
79+
### Classic
80+
81+
`.eslintrc` file:
82+
83+
```jsonc
84+
{
85+
"extends": ["plugin:mdx/recommended"],
86+
// optional, if you want to lint code blocks at the same time
87+
"settings": {
88+
"mdx/code-blocks": true,
89+
// optional, if you want to disable language mapper, set it to `false`
90+
// if you want to override the default language mapper inside, you can provide your own
91+
"mdx/language-mapper": {}
92+
}
93+
}
94+
```
95+
96+
### Flat Config
97+
98+
`eslint.config.js` file:
99+
100+
```js
101+
import * as mdx from 'eslint-plugin-mdx'
102+
103+
export default [
104+
{
105+
...mdx.flat,
106+
// optional, if you want to lint code blocks at the same
107+
processor: mdx.createRemarkProcessor({
108+
lintCodeBlocks: true,
109+
// optional, if you want to disable language mapper, set it to `false`
110+
// if you want to override the default language mapper inside, you can provide your own
111+
languageMapper: {},
112+
}),
113+
},
114+
{
115+
...mdx.flatCodeBlocks,
116+
rules: {
117+
...mdx.flatCodeBlocks.rules,
118+
// if you want to override some rules for code blocks
119+
'no-var': 'error',
120+
'prefer-const': 'error',
121+
},
122+
},
123+
]
124+
```
125+
126+
Then, make sure ESLint knows to run on `.md` or `.mdx` files:
127+
128+
```sh
129+
eslint . --ext js,md,mdx
130+
```
97131

98132
## Parser Options
99133

packages/eslint-mdx/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
export * from './helpers'
2+
export * from './meta'
23
export * from './parser'
34
export * from './sync'
45
export * from './types'

packages/eslint-mdx/src/meta.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -- no idea
2+
// @ts-ignore
3+
import { name, version } from '../package.json'
4+
5+
export const meta = { name, version }

packages/eslint-plugin-mdx/README.md

Lines changed: 54 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@
3232
- [Install](#install)
3333
- [Notice](#notice)
3434
- [Usage](#usage)
35+
- [Classic](#classic)
36+
- [Flat Config](#flat-config)
3537
- [Parser Options](#parser-options)
3638
- [Rules](#rules)
3739
- [mdx/remark](#mdxremark)
@@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922
7476

7577
## Usage
7678

77-
1. In your ESLint 8+ config file, just add:
78-
79-
```jsonc
80-
{
81-
"extends": ["plugin:mdx/recommended"],
82-
// optional, if you want to lint code blocks at the same time
83-
"settings": {
84-
"mdx/code-blocks": true,
85-
// optional, if you want to disable language mapper, set it to `false`
86-
// if you want to override the default language mapper inside, you can provide your own
87-
"mdx/language-mapper": {}
88-
}
89-
}
90-
```
91-
92-
2. Make sure ESLint knows to run on `.md` or `.mdx` files:
93-
94-
```sh
95-
eslint . --ext js,md,mdx
96-
```
79+
### Classic
80+
81+
`.eslintrc` file:
82+
83+
```jsonc
84+
{
85+
"extends": ["plugin:mdx/recommended"],
86+
// optional, if you want to lint code blocks at the same time
87+
"settings": {
88+
"mdx/code-blocks": true,
89+
// optional, if you want to disable language mapper, set it to `false`
90+
// if you want to override the default language mapper inside, you can provide your own
91+
"mdx/language-mapper": {}
92+
}
93+
}
94+
```
95+
96+
### Flat Config
97+
98+
`eslint.config.js` file:
99+
100+
```js
101+
import * as mdx from 'eslint-plugin-mdx'
102+
103+
export default [
104+
{
105+
...mdx.flat,
106+
// optional, if you want to lint code blocks at the same
107+
processor: mdx.createRemarkProcessor({
108+
lintCodeBlocks: true,
109+
// optional, if you want to disable language mapper, set it to `false`
110+
// if you want to override the default language mapper inside, you can provide your own
111+
languageMapper: {},
112+
}),
113+
},
114+
{
115+
...mdx.flatCodeBlocks,
116+
rules: {
117+
...mdx.flatCodeBlocks.rules,
118+
// if you want to override some rules for code blocks
119+
'no-var': 'error',
120+
'prefer-const': 'error',
121+
},
122+
},
123+
]
124+
```
125+
126+
Then, make sure ESLint knows to run on `.md` or `.mdx` files:
127+
128+
```sh
129+
eslint . --ext js,md,mdx
130+
```
97131

98132
## Parser Options
99133

packages/eslint-plugin-mdx/src/configs/code-blocks.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import type { Linter } from 'eslint'
22

3-
export const codeBlocks: Linter.Config = {
3+
export const codeBlocks = {
44
parserOptions: {
55
ecmaFeatures: {
66
// Adding a "use strict" directive at the top of
@@ -31,4 +31,4 @@ export const codeBlocks: Linter.Config = {
3131
// Mark from the Markdown parser.
3232
'unicode-bom': 'off',
3333
},
34-
}
34+
} satisfies Linter.Config
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import type { Linter } from 'eslint'
2+
import * as eslintMdx from 'eslint-mdx'
3+
4+
import * as mdx from '..'
5+
6+
import { codeBlocks } from './code-blocks'
7+
8+
export const flat: Linter.FlatConfig = {
9+
files: ['**/*.{md,mdx}'],
10+
languageOptions: {
11+
sourceType: 'module',
12+
ecmaVersion: 'latest',
13+
parser: eslintMdx,
14+
globals: {
15+
React: false,
16+
},
17+
},
18+
plugins: {
19+
mdx,
20+
},
21+
rules: {
22+
'mdx/remark': 'warn',
23+
'no-unused-expressions': 'error',
24+
'react/react-in-jsx-scope': 0,
25+
},
26+
}
27+
28+
const { parserOptions, ...restConfig } = codeBlocks
29+
30+
export const flatCodeBlocks: Linter.FlatConfig = {
31+
files: ['**/*.{md,mdx}/*'],
32+
languageOptions: {
33+
parserOptions,
34+
},
35+
...restConfig,
36+
}

packages/eslint-plugin-mdx/src/configs/index.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,18 @@
22

33
import { base } from './base'
44
import { codeBlocks } from './code-blocks'
5+
import { flat, flatCodeBlocks } from './flat'
56
import { overrides } from './overrides'
67
import { recommended } from './recommended'
78

8-
export { base, codeBlocks, overrides, recommended }
9+
export { base, codeBlocks, flat, flatCodeBlocks, overrides, recommended }
910

1011
export const configs = {
1112
base,
1213
'code-blocks': codeBlocks,
1314
codeBlocks,
15+
flat,
16+
flatCodeBlocks,
1417
overrides,
1518
recommended,
1619
}

0 commit comments

Comments
 (0)