Skip to content

Commit

Permalink
fix: incompatible with some react rules (#489)
Browse files Browse the repository at this point in the history
close #437
close #488, related #425
  • Loading branch information
JounQin authored Dec 20, 2023
1 parent cd50b12 commit f9ff397
Show file tree
Hide file tree
Showing 17 changed files with 5,715 additions and 1,962 deletions.
5 changes: 5 additions & 0 deletions .changeset/ninety-bugs-dance.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"eslint-mdx": patch
---

fix: incorrect `JSXAttribute` node position info - close #488, related #425
6 changes: 6 additions & 0 deletions .changeset/tasty-actors-protect.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"eslint-mdx": patch
"eslint-plugin-mdx": patch
---

fix: incompatible with some react rules: `jsx-curly-brace-presence`, `jsx-sort-props`, `self-closing-comp`
9 changes: 9 additions & 0 deletions .eslintrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,20 @@ module.exports = {
{
files: '*.{md,mdx}',
rules: {
'react/jsx-curly-brace-presence': 'error',
'react/jsx-sort-props': 'error',
'react/self-closing-comp': 'error',
'react/no-unescaped-entities': 'warn',
},
settings: {
'mdx/code-blocks': true,
},
},
{
files: '**/*.{md,mdx}/**/*.ts',
rules: {
'no-magic-numbers': 'off',
},
},
],
}
4 changes: 2 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@
*.tsbuildinfo
.*cache
.changelog
.idea
.type-coverage
coverage
lib
node_modules
/test.mdx
.idea/
/test.*
56 changes: 55 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- [Classic](#classic)
- [Flat Config](#flat-config)
- [Parser Options](#parser-options)
- [Parser API](#parser-api)
- [`MDXCode`](#mdxcode)
- [`MDXHeading`](#mdxheading)
- [Typings](#typings)
- [Rules](#rules)
- [mdx/remark](#mdxremark)
- [Prettier Integration](#prettier-integration)
Expand All @@ -47,7 +51,7 @@

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

[VSCode MDX][]\: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
[VSCode MDX][]: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.

## Packages

Expand Down Expand Up @@ -137,6 +141,56 @@ eslint . --ext js,md,mdx

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

## Parser API

### `MDXCode`

A new `MDXCode` estree node type is exported from `eslint-mdx` which represents code blocks in `mdx` like the following:

````mdx
<div>
```js
export function foo() {
return 'bar'
}
```
</div>
````

See also <https://github.com/syntax-tree/mdast#code>

### `MDXHeading`

A new `MDXHeading` estree node type is exported from `eslint-mdx` which represents markdown heading in `mdx` like the following:

```mdx
<div># Here's a text gradient short code!</div>
```

See also <https://github.com/syntax-tree/mdast#heading>

### Typings

```ts
import type { BaseNode } from 'estree'
import type { JSXElement } from 'estree-jsx'

export interface MDXCode extends BaseNode {
type: 'MDXCode'
value: string
lang?: string | null
meta?: string | null
}

export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6

export interface MDXHeading extends BaseNode {
type: 'MDXHeading'
depth: HeadingDepth
children: JSXElement['children']
}
```

## Rules

### mdx/remark
Expand Down
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
"typecov": "type-coverage"
},
"devDependencies": {
"@1stg/lib-config": "^12.0.0",
"@1stg/lib-config": "^12.0.1",
"@changesets/changelog-github": "^0.4.8",
"@changesets/cli": "^2.26.2",
"@types/eslint": "^8.44.1",
Expand Down Expand Up @@ -58,6 +58,7 @@
"fixtures",
"lib",
"CHANGELOG.md",
"/test.*",
"!/.*.js"
],
"jest": {
Expand Down
56 changes: 55 additions & 1 deletion packages/eslint-mdx/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,10 @@
- [Classic](#classic)
- [Flat Config](#flat-config)
- [Parser Options](#parser-options)
- [Parser API](#parser-api)
- [`MDXCode`](#mdxcode)
- [`MDXHeading`](#mdxheading)
- [Typings](#typings)
- [Rules](#rules)
- [mdx/remark](#mdxremark)
- [Prettier Integration](#prettier-integration)
Expand All @@ -47,7 +51,7 @@

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

[VSCode MDX][]\: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.
[VSCode MDX][]: Integrates with [VSCode ESLint][], syntaxes highlighting and error reporting.

## Packages

Expand Down Expand Up @@ -137,6 +141,56 @@ eslint . --ext js,md,mdx

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

## Parser API

### `MDXCode`

A new `MDXCode` estree node type is exported from `eslint-mdx` which represents code blocks in `mdx` like the following:

````mdx
<div>
```js
export function foo() {
return 'bar'
}
```
</div>
````

See also <https://github.com/syntax-tree/mdast#code>

### `MDXHeading`

A new `MDXHeading` estree node type is exported from `eslint-mdx` which represents markdown heading in `mdx` like the following:

```mdx
<div># Here's a text gradient short code!</div>
```

See also <https://github.com/syntax-tree/mdast#heading>

### Typings

```ts
import type { BaseNode } from 'estree'
import type { JSXElement } from 'estree-jsx'

export interface MDXCode extends BaseNode {
type: 'MDXCode'
value: string
lang?: string | null
meta?: string | null
}

export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6

export interface MDXHeading extends BaseNode {
type: 'MDXHeading'
depth: HeadingDepth
children: JSXElement['children']
}
```

## Rules

### mdx/remark
Expand Down
15 changes: 10 additions & 5 deletions packages/eslint-mdx/src/parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,11 +63,16 @@ export class Parser {
})
} catch (err: unknown) {
const error = err as VFileMessage
throw Object.assign(new SyntaxError(error.message), {
lineNumber: error.line,
column: error.column,
index: /* istanbul ignore next */ error.position?.start.offset,
})
throw Object.assign(
new SyntaxError(error.message, {
cause: error,
}),
{
lineNumber: error.line,
column: error.column,
index: /* istanbul ignore next */ error.position?.start.offset,
},
)
}

const { root, body, comments, tokens } = result
Expand Down
22 changes: 21 additions & 1 deletion packages/eslint-mdx/src/types.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { Position } from 'acorn'
import type { AST, Linter } from 'eslint'
import type { Program } from 'estree'
import type { BaseNode, Program } from 'estree'
import type { JSXElement } from 'estree-jsx'
import type { Root } from 'mdast'
import type { VFileOptions } from 'vfile'
import type { VFileMessage } from 'vfile-message'
Expand Down Expand Up @@ -43,3 +44,22 @@ export interface WorkerProcessResult {
}

export type WorkerResult = WorkerParseResult | WorkerProcessResult

type _Arrayable<T, R = T extends Array<infer U> ? U : T> = R | R[]

export type Arrayable<T> = _Arrayable<T>

export interface MDXCode extends BaseNode {
type: 'MDXCode'
value: string
lang?: string | null
meta?: string | null
}

export type HeadingDepth = 1 | 2 | 3 | 4 | 5 | 6

export interface MDXHeading extends BaseNode {
type: 'MDXHeading'
depth: HeadingDepth
children: JSXElement['children']
}
Loading

0 comments on commit f9ff397

Please sign in to comment.