Skip to content

Commit f9ff397

Browse files
authored
fix: incompatible with some react rules (#489)
close #437 close #488, related #425
1 parent cd50b12 commit f9ff397

File tree

17 files changed

+5715
-1962
lines changed

17 files changed

+5715
-1962
lines changed

.changeset/ninety-bugs-dance.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"eslint-mdx": patch
3+
---
4+
5+
fix: incorrect `JSXAttribute` node position info - close #488, related #425

.changeset/tasty-actors-protect.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
"eslint-mdx": patch
3+
"eslint-plugin-mdx": patch
4+
---
5+
6+
fix: incompatible with some react rules: `jsx-curly-brace-presence`, `jsx-sort-props`, `self-closing-comp`

.eslintrc.js

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,20 @@ module.exports = {
2121
{
2222
files: '*.{md,mdx}',
2323
rules: {
24+
'react/jsx-curly-brace-presence': 'error',
25+
'react/jsx-sort-props': 'error',
26+
'react/self-closing-comp': 'error',
2427
'react/no-unescaped-entities': 'warn',
2528
},
2629
settings: {
2730
'mdx/code-blocks': true,
2831
},
2932
},
33+
{
34+
files: '**/*.{md,mdx}/**/*.ts',
35+
rules: {
36+
'no-magic-numbers': 'off',
37+
},
38+
},
3039
],
3140
}

.gitignore

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
*.tsbuildinfo
33
.*cache
44
.changelog
5+
.idea
56
.type-coverage
67
coverage
78
lib
89
node_modules
9-
/test.mdx
10-
.idea/
10+
/test.*

README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
- [Classic](#classic)
3636
- [Flat Config](#flat-config)
3737
- [Parser Options](#parser-options)
38+
- [Parser API](#parser-api)
39+
- [`MDXCode`](#mdxcode)
40+
- [`MDXHeading`](#mdxheading)
41+
- [Typings](#typings)
3842
- [Rules](#rules)
3943
- [mdx/remark](#mdxremark)
4044
- [Prettier Integration](#prettier-integration)
@@ -47,7 +51,7 @@
4751

4852
[![Visual Studio Marketplace Version](https://img.shields.io/visual-studio-marketplace/v/unifiedjs.vscode-mdx)](https://marketplace.visualstudio.com/items?itemName=unifiedjs.vscode-mdx)
4953

50-
[VSCode MDX][]\: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
54+
[VSCode MDX][]: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
5155

5256
## Packages
5357

@@ -137,6 +141,56 @@ eslint . --ext js,md,mdx
137141

138142
3. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.
139143

144+
## Parser API
145+
146+
### `MDXCode`
147+
148+
A new `MDXCode` estree node type is exported from `eslint-mdx` which represents code blocks in `mdx` like the following:
149+
150+
````mdx
151+
<div>
152+
```js
153+
export function foo() {
154+
return 'bar'
155+
}
156+
```
157+
</div>
158+
````
159+
160+
See also <https://github.com/syntax-tree/mdast#code>
161+
162+
### `MDXHeading`
163+
164+
A new `MDXHeading` estree node type is exported from `eslint-mdx` which represents markdown heading in `mdx` like the following:
165+
166+
```mdx
167+
<div># Here's a text gradient short code!</div>
168+
```
169+
170+
See also <https://github.com/syntax-tree/mdast#heading>
171+
172+
### Typings
173+
174+
```ts
175+
import type { BaseNode } from 'estree'
176+
import type { JSXElement } from 'estree-jsx'
177+
178+
export interface MDXCode extends BaseNode {
179+
type: 'MDXCode'
180+
value: string
181+
lang?: string | null
182+
meta?: string | null
183+
}
184+
185+
export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6
186+
187+
export interface MDXHeading extends BaseNode {
188+
type: 'MDXHeading'
189+
depth: HeadingDepth
190+
children: JSXElement['children']
191+
}
192+
```
193+
140194
## Rules
141195

142196
### mdx/remark

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
"typecov": "type-coverage"
2727
},
2828
"devDependencies": {
29-
"@1stg/lib-config": "^12.0.0",
29+
"@1stg/lib-config": "^12.0.1",
3030
"@changesets/changelog-github": "^0.4.8",
3131
"@changesets/cli": "^2.26.2",
3232
"@types/eslint": "^8.44.1",
@@ -58,6 +58,7 @@
5858
"fixtures",
5959
"lib",
6060
"CHANGELOG.md",
61+
"/test.*",
6162
"!/.*.js"
6263
],
6364
"jest": {

packages/eslint-mdx/README.md

Lines changed: 55 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,10 @@
3535
- [Classic](#classic)
3636
- [Flat Config](#flat-config)
3737
- [Parser Options](#parser-options)
38+
- [Parser API](#parser-api)
39+
- [`MDXCode`](#mdxcode)
40+
- [`MDXHeading`](#mdxheading)
41+
- [Typings](#typings)
3842
- [Rules](#rules)
3943
- [mdx/remark](#mdxremark)
4044
- [Prettier Integration](#prettier-integration)
@@ -47,7 +51,7 @@
4751

4852
[![Visual Studio Marketplace Version](https://img.shields.io/visual-studio-marketplace/v/unifiedjs.vscode-mdx)](https://marketplace.visualstudio.com/items?itemName=unifiedjs.vscode-mdx)
4953

50-
[VSCode MDX][]\: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
54+
[VSCode MDX][]: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
5155

5256
## Packages
5357

@@ -137,6 +141,56 @@ eslint . --ext js,md,mdx
137141

138142
3. `ignoreRemarkConfig` (`boolean`): Ignore the `remark` configuration defined in the project.
139143

144+
## Parser API
145+
146+
### `MDXCode`
147+
148+
A new `MDXCode` estree node type is exported from `eslint-mdx` which represents code blocks in `mdx` like the following:
149+
150+
````mdx
151+
<div>
152+
```js
153+
export function foo() {
154+
return 'bar'
155+
}
156+
```
157+
</div>
158+
````
159+
160+
See also <https://github.com/syntax-tree/mdast#code>
161+
162+
### `MDXHeading`
163+
164+
A new `MDXHeading` estree node type is exported from `eslint-mdx` which represents markdown heading in `mdx` like the following:
165+
166+
```mdx
167+
<div># Here's a text gradient short code!</div>
168+
```
169+
170+
See also <https://github.com/syntax-tree/mdast#heading>
171+
172+
### Typings
173+
174+
```ts
175+
import type { BaseNode } from 'estree'
176+
import type { JSXElement } from 'estree-jsx'
177+
178+
export interface MDXCode extends BaseNode {
179+
type: 'MDXCode'
180+
value: string
181+
lang?: string | null
182+
meta?: string | null
183+
}
184+
185+
export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6
186+
187+
export interface MDXHeading extends BaseNode {
188+
type: 'MDXHeading'
189+
depth: HeadingDepth
190+
children: JSXElement['children']
191+
}
192+
```
193+
140194
## Rules
141195

142196
### mdx/remark

packages/eslint-mdx/src/parser.ts

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,11 +63,16 @@ export class Parser {
6363
})
6464
} catch (err: unknown) {
6565
const error = err as VFileMessage
66-
throw Object.assign(new SyntaxError(error.message), {
67-
lineNumber: error.line,
68-
column: error.column,
69-
index: /* istanbul ignore next */ error.position?.start.offset,
70-
})
66+
throw Object.assign(
67+
new SyntaxError(error.message, {
68+
cause: error,
69+
}),
70+
{
71+
lineNumber: error.line,
72+
column: error.column,
73+
index: /* istanbul ignore next */ error.position?.start.offset,
74+
},
75+
)
7176
}
7277

7378
const { root, body, comments, tokens } = result

packages/eslint-mdx/src/types.ts

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { Position } from 'acorn'
22
import type { AST, Linter } from 'eslint'
3-
import type { Program } from 'estree'
3+
import type { BaseNode, Program } from 'estree'
4+
import type { JSXElement } from 'estree-jsx'
45
import type { Root } from 'mdast'
56
import type { VFileOptions } from 'vfile'
67
import type { VFileMessage } from 'vfile-message'
@@ -43,3 +44,22 @@ export interface WorkerProcessResult {
4344
}
4445

4546
export type WorkerResult = WorkerParseResult | WorkerProcessResult
47+
48+
type _Arrayable<T, R = T extends Array<infer U> ? U : T> = R | R[]
49+
50+
export type Arrayable<T> = _Arrayable<T>
51+
52+
export interface MDXCode extends BaseNode {
53+
type: 'MDXCode'
54+
value: string
55+
lang?: string | null
56+
meta?: string | null
57+
}
58+
59+
export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6
60+
61+
export interface MDXHeading extends BaseNode {
62+
type: 'MDXHeading'
63+
depth: HeadingDepth
64+
children: JSXElement['children']
65+
}

0 commit comments

Comments
 (0)