Skip to content

Commit e3c225b

Browse files
authored
fix: incompatible with some react rules (#493)
1 parent 8f62574 commit e3c225b

File tree

17 files changed

+4801
-1335
lines changed

17 files changed

+4801
-1335
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: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
"typecov": "type-coverage"
2828
},
2929
"devDependencies": {
30-
"@1stg/lib-config": "^12.0.0",
30+
"@1stg/lib-config": "^12.0.1",
3131
"@changesets/changelog-github": "^0.5.0",
3232
"@changesets/cli": "^2.27.1",
3333
"@types/eslint": "^8.44.8",
@@ -50,12 +50,15 @@
5050
"@types/mdast": "^4.0.3",
5151
"acorn": "^8.11.2",
5252
"cliui": "npm:@isaacs/cliui@^8.0.2",
53+
"eslint-mdx": "link:packages/eslint-mdx",
5354
"eslint-plugin-markdown": "JounQin/eslint-plugin-markdown#feat/bump",
55+
"eslint-plugin-mdx": "link:packages/eslint-plugin-mdx",
5456
"mdast-util-frontmatter": "^2.0.1",
5557
"mdast-util-gfm": "^3.0.0",
5658
"prettier": "^2.8.8",
5759
"unified": "^11.0.4",
58-
"unified-engine": "^11.2.0"
60+
"unified-engine": "^11.2.0",
61+
"unist-util-visit": "^5.0.0"
5962
},
6063
"commitlint": {
6164
"extends": [
@@ -68,6 +71,7 @@
6871
"fixtures",
6972
"lib",
7073
"CHANGELOG.md",
74+
"/test.*",
7175
"!/.*.js"
7276
],
7377
"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
@@ -64,11 +64,16 @@ export class Parser {
6464
} catch (err: unknown) {
6565
const { message, line, column, place } = err as VFileMessage
6666
const point = place && ('start' in place ? place.start : place)
67-
throw Object.assign(new SyntaxError(message), {
68-
lineNumber: line,
69-
column,
70-
index: /* istanbul ignore next */ point?.offset,
71-
})
67+
throw Object.assign(
68+
new SyntaxError(message, {
69+
cause: err,
70+
}),
71+
{
72+
lineNumber: line,
73+
column,
74+
index: /* istanbul ignore next */ point?.offset,
75+
},
76+
)
7277
}
7378

7479
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)