Skip to content

Commit 5386098

Browse files
committed
feat: support other extensions, detect parsers automatically
new overrides config, can be extended on top level
1 parent 48ef13f commit 5386098

File tree

8 files changed

+69
-39
lines changed

8 files changed

+69
-39
lines changed

.eslintrc.js

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ module.exports = {
1111
},
1212
},
1313
},
14-
extends: ['plugin:jest/recommended', '1stg/react'],
14+
extends: [
15+
'plugin:jest/recommended',
16+
'1stg/react',
17+
'plugin:@rxts/mdx/recommended',
18+
],
1519
rules: {
1620
'@typescript-eslint/no-explicit-any': 0,
1721
'@typescript-eslint/array-type': [
@@ -32,7 +36,7 @@ module.exports = {
3236
},
3337
{
3438
files: ['*.mdx'],
35-
extends: ['plugin:@rxts/mdx/recommended'],
39+
extends: ['plugin:@rxts/mdx/overrides'],
3640
},
3741
{
3842
files: ['*.ts', '*.tsx'],

README.md

Lines changed: 16 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -38,10 +38,11 @@ npm i -D @rxts/eslint-plugin-mdx
3838

3939
```json
4040
{
41+
"extends": ["plugin:@rxts/mdx/recommended"],
4142
"overrides": [
4243
{
4344
"files": ["*.mdx"],
44-
"extends": ["plugin:@rxts/mdx/recommended"]
45+
"extends": ["plugin:@rxts/mdx/overrides"]
4546
}
4647
]
4748
}
@@ -51,18 +52,16 @@ npm i -D @rxts/eslint-plugin-mdx
5152

5253
```json
5354
{
55+
"extends": ["plugin:@rxts/mdx/recommended"],
5456
"overrides": [
5557
{
5658
"files": ["*.mdx"],
57-
"parser": "eslint-mdx",
58-
"plugins": ["@rxts/mdx"],
59+
"globals": {
60+
"React": false
61+
},
5962
"rules": {
60-
"@rxts/mdx/no-jsx-html-comments": 2,
61-
"@rxts/mdx/no-unescaped-entities": 1,
62-
"@rxts/mdx/no-unused-expressions": 2,
63-
"no-unused-expressions": 0,
64-
"react/react-in-jsx-scope": 0,
65-
"react/no-unescaped-entities": 0
63+
"lines-between-class-members": 0,
64+
"react/react-in-jsx-scope": 0
6665
}
6766
}
6867
]
@@ -75,27 +74,20 @@ npm i -D @rxts/eslint-plugin-mdx
7574
eslint . --ext js,mdx
7675
```
7776

78-
3. Custom parser for ES syntax is also supported, although `babel-eslint` will be detected automatically what means you actually do not need to do this:
77+
## Parser Options
78+
79+
1. `parser` (`string | Function`): Custom parser for ES syntax is supported, although `@typescript-eslint/parser` or `babel-eslint` will be detected automatically what means you actually do not need do this:
7980

8081
```json
8182
{
82-
"overrides": [
83-
{
84-
"files": ["*.mdx"],
85-
"extends": ["plugin:@rxts/mdx/recommended"],
86-
"parserOptions": {
87-
"parser": "babel-eslint"
88-
}
89-
}
90-
]
83+
"extends": ["plugin:@rxts/mdx/recommended"],
84+
"parserOptions": {
85+
"parser": "babel-eslint"
86+
}
9187
}
9288
```
9389

94-
## FAQ
95-
96-
### Why I need to use `overrides`?
97-
98-
This parser/plugin should only affects `.mdx` files, and `overrides` in `shared configuration` is still [not extendable](https://github.com/eslint/eslint/issues/12032) for now, of course you can manually config it on your own risk.
90+
2. `extensions` (`string | string[]`): `eslint-mdx` will only resolve `.mdx` files by default, files with other extensions will be resolved by the `parser` option. If you want to resolve other extensions as like `.mdx`, you can use this option.
9991

10092
## Rules
10193

package.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "eslint-mdx",
3-
"version": "0.8.1",
3+
"version": "0.9.0",
44
"description": "ESLint Parser/Plugin for MDX",
55
"repository": "[email protected]:rx-ts/eslint-plugin-mdx.git",
66
"author": "JounQin <[email protected]>",

packages/eslint-mdx/src/parser.ts

Lines changed: 31 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
22
/// <reference path="../typings.d.ts" />
33

4+
import path from 'path'
5+
46
import { parse as esParse } from 'espree'
57
import remarkMdx from 'remark-mdx'
68
import remarkParse from 'remark-parse'
@@ -13,6 +15,7 @@ import {
1315
} from './helper'
1416
import { isComment } from './regexp'
1517
import { traverse } from './traverse'
18+
import { ParserOptions } from './types'
1619

1720
import { AST, Linter } from 'eslint'
1821
import { Parent, Node } from 'unist'
@@ -27,11 +30,12 @@ export const mdxProcessor = unified()
2730
.use(remarkMdx)
2831
.freeze()
2932

30-
export const parseForESLint = (
31-
code: string,
32-
options: Linter.ParserOptions = {},
33-
) => {
34-
let { parser } = options
33+
export const DEFAULT_EXTENSIONS = ['.mdx']
34+
35+
export const FALLBACK_PARSERS = ['@typescript-eslint/parser', 'babel-eslint']
36+
37+
export const parseForESLint = (code: string, options: ParserOptions = {}) => {
38+
let { extensions, parser } = options
3539

3640
if (parser) {
3741
if (typeof parser === 'string') {
@@ -48,16 +52,32 @@ export const parseForESLint = (
4852
)
4953
}
5054
} else {
51-
try {
52-
// try to load babel-eslint automatically
53-
// eslint-disable-next-line @typescript-eslint/no-var-requires
54-
const babelEslint = require('babel-eslint')
55-
parser = babelEslint.parseForESLint || babelEslint.parse
56-
} catch (e) {
55+
// try to load FALLBACK_PARSERS automatically
56+
for (const fallback of FALLBACK_PARSERS) {
57+
try {
58+
// eslint-disable-next-line @typescript-eslint/no-var-requires
59+
const fallbackParser = require(fallback)
60+
parser = fallbackParser.parseForESLint || fallbackParser.parse
61+
break
62+
} catch (e) {}
63+
}
64+
65+
if (typeof parser !== 'function') {
5766
parser = esParse
5867
}
5968
}
6069

70+
if (
71+
!DEFAULT_EXTENSIONS.concat(extensions || []).includes(
72+
path.extname(options.filePath),
73+
)
74+
) {
75+
const program = parser(code, options)
76+
return (program.ast
77+
? program
78+
: { ast: program }) as Linter.ESLintParseResult
79+
}
80+
6181
const root = mdxProcessor.parse(code) as Parent
6282

6383
const ast: AST.Program = {

packages/eslint-mdx/src/types.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
1+
import { Linter } from 'eslint'
12
import { Node, Parent, Point } from 'unist'
23

4+
export interface ParserOptions extends Linter.ParserOptions {
5+
extensions?: string | string[]
6+
}
7+
38
export type Traverser = (node: Node, parent?: Parent) => void
49

510
export interface TraverseOptions {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
export * from './base'
2+
export * from './overrides'
23
export * from './recommended'
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
export const overrides = {
2+
globals: {
3+
React: false,
4+
},
5+
rules: {
6+
'lines-between-class-members': 0, // See https://github.com/mdx-js/mdx/issues/195
7+
'react/react-in-jsx-scope': 0,
8+
},
9+
}

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

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,5 @@ export const recommended = {
88
'@rxts/mdx/no-unused-expressions': 2,
99
'no-unused-expressions': 0,
1010
'react/no-unescaped-entities': 0,
11-
'react/react-in-jsx-scope': 0,
1211
},
1312
}

0 commit comments

Comments
 (0)