Skip to content

Commit

Permalink
feat: support new flat config (#468)
Browse files Browse the repository at this point in the history
close #455
  • Loading branch information
JounQin authored Aug 3, 2023
1 parent 223ed69 commit 5b04837
Show file tree
Hide file tree
Showing 20 changed files with 423 additions and 84 deletions.
6 changes: 6 additions & 0 deletions .changeset/fifty-vans-vanish.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"eslint-mdx": minor
"eslint-plugin-mdx": minor
---

feat: add metadata for parser, processor and plugin
5 changes: 5 additions & 0 deletions .changeset/shy-mayflies-dress.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-plugin-mdx": minor
---

feat: support new flat config - close #455
74 changes: 54 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- [Install](#install)
- [Notice](#notice)
- [Usage](#usage)
- [Classic](#classic)
- [Flat Config](#flat-config)
- [Parser Options](#parser-options)
- [Rules](#rules)
- [mdx/remark](#mdxremark)
Expand Down Expand Up @@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922

## Usage

1. In your ESLint 8+ config file, just add:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

2. Make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```
### Classic

`.eslintrc` file:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

### Flat Config

`eslint.config.js` file:

```js
import * as mdx from 'eslint-plugin-mdx'

export default [
{
...mdx.flat,
// optional, if you want to lint code blocks at the same
processor: mdx.createRemarkProcessor({
lintCodeBlocks: true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
languageMapper: {},
}),
},
{
...mdx.flatCodeBlocks,
rules: {
...mdx.flatCodeBlocks.rules,
// if you want to override some rules for code blocks
'no-var': 'error',
'prefer-const': 'error',
},
},
]
```

Then, make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```

## Parser Options

Expand Down
74 changes: 54 additions & 20 deletions packages/eslint-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- [Install](#install)
- [Notice](#notice)
- [Usage](#usage)
- [Classic](#classic)
- [Flat Config](#flat-config)
- [Parser Options](#parser-options)
- [Rules](#rules)
- [mdx/remark](#mdxremark)
Expand Down Expand Up @@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922

## Usage

1. In your ESLint 8+ config file, just add:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

2. Make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```
### Classic

`.eslintrc` file:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

### Flat Config

`eslint.config.js` file:

```js
import * as mdx from 'eslint-plugin-mdx'

export default [
{
...mdx.flat,
// optional, if you want to lint code blocks at the same
processor: mdx.createRemarkProcessor({
lintCodeBlocks: true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
languageMapper: {},
}),
},
{
...mdx.flatCodeBlocks,
rules: {
...mdx.flatCodeBlocks.rules,
// if you want to override some rules for code blocks
'no-var': 'error',
'prefer-const': 'error',
},
},
]
```

Then, make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```

## Parser Options

Expand Down
1 change: 1 addition & 0 deletions packages/eslint-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './helpers'
export * from './meta'
export * from './parser'
export * from './sync'
export * from './types'
5 changes: 5 additions & 0 deletions packages/eslint-mdx/src/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -- no idea
// @ts-ignore
import { name, version } from '../package.json'

export const meta = { name, version }
74 changes: 54 additions & 20 deletions packages/eslint-plugin-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
- [Install](#install)
- [Notice](#notice)
- [Usage](#usage)
- [Classic](#classic)
- [Flat Config](#flat-config)
- [Parser Options](#parser-options)
- [Rules](#rules)
- [mdx/remark](#mdxremark)
Expand Down Expand Up @@ -74,26 +76,58 @@ See [#251](https://github.com/mdx-js/eslint-mdx/issues/251#issuecomment-73613922

## Usage

1. In your ESLint 8+ config file, just add:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

2. Make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```
### Classic

`.eslintrc` file:

```jsonc
{
"extends": ["plugin:mdx/recommended"],
// optional, if you want to lint code blocks at the same time
"settings": {
"mdx/code-blocks": true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
"mdx/language-mapper": {}
}
}
```

### Flat Config

`eslint.config.js` file:

```js
import * as mdx from 'eslint-plugin-mdx'

export default [
{
...mdx.flat,
// optional, if you want to lint code blocks at the same
processor: mdx.createRemarkProcessor({
lintCodeBlocks: true,
// optional, if you want to disable language mapper, set it to `false`
// if you want to override the default language mapper inside, you can provide your own
languageMapper: {},
}),
},
{
...mdx.flatCodeBlocks,
rules: {
...mdx.flatCodeBlocks.rules,
// if you want to override some rules for code blocks
'no-var': 'error',
'prefer-const': 'error',
},
},
]
```

Then, make sure ESLint knows to run on `.md` or `.mdx` files:

```sh
eslint . --ext js,md,mdx
```

## Parser Options

Expand Down
4 changes: 2 additions & 2 deletions packages/eslint-plugin-mdx/src/configs/code-blocks.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import type { Linter } from 'eslint'

export const codeBlocks: Linter.Config = {
export const codeBlocks = {
parserOptions: {
ecmaFeatures: {
// Adding a "use strict" directive at the top of
Expand Down Expand Up @@ -31,4 +31,4 @@ export const codeBlocks: Linter.Config = {
// Mark from the Markdown parser.
'unicode-bom': 'off',
},
}
} satisfies Linter.Config
36 changes: 36 additions & 0 deletions packages/eslint-plugin-mdx/src/configs/flat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import type { Linter } from 'eslint'
import * as eslintMdx from 'eslint-mdx'

import * as mdx from '..'

import { codeBlocks } from './code-blocks'

export const flat: Linter.FlatConfig = {
files: ['**/*.{md,mdx}'],
languageOptions: {
sourceType: 'module',
ecmaVersion: 'latest',
parser: eslintMdx,
globals: {
React: false,
},
},
plugins: {
mdx,
},
rules: {
'mdx/remark': 'warn',
'no-unused-expressions': 'error',
'react/react-in-jsx-scope': 0,
},
}

const { parserOptions, ...restConfig } = codeBlocks

export const flatCodeBlocks: Linter.FlatConfig = {
files: ['**/*.{md,mdx}/*'],
languageOptions: {
parserOptions,
},
...restConfig,
}
5 changes: 4 additions & 1 deletion packages/eslint-plugin-mdx/src/configs/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,18 @@

import { base } from './base'
import { codeBlocks } from './code-blocks'
import { flat, flatCodeBlocks } from './flat'
import { overrides } from './overrides'
import { recommended } from './recommended'

export { base, codeBlocks, overrides, recommended }
export { base, codeBlocks, flat, flatCodeBlocks, overrides, recommended }

export const configs = {
base,
'code-blocks': codeBlocks,
codeBlocks,
flat,
flatCodeBlocks,
overrides,
recommended,
}
1 change: 1 addition & 0 deletions packages/eslint-plugin-mdx/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export * from './configs'
export * from './helpers'
export * from './meta'
export * from './processors'
export * from './rules'
5 changes: 5 additions & 0 deletions packages/eslint-plugin-mdx/src/meta.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
// eslint-disable-next-line @typescript-eslint/prefer-ts-expect-error -- no idea
// @ts-ignore
import { name, version } from '../package.json'

export const meta = { name, version }
5 changes: 1 addition & 4 deletions packages/eslint-plugin-mdx/src/processors/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
/**
* workaround for @link https://github.com/benmosher/eslint-plugin-import/issues/2002
*/

import { remark } from './remark'

export * from './helpers'
export * from './options'
export { createRemarkProcessor } from './remark'
export * from './types'

export const processors = { remark }
Loading

0 comments on commit 5b04837

Please sign in to comment.