Skip to content

Commit 90583f7

Browse files
committed
fix: skip combine jsx nodes for root/jsx node
refactor parser, make the API simple
1 parent 2e9d4c1 commit 90583f7

File tree

8 files changed

+267
-260
lines changed

8 files changed

+267
-260
lines changed

CHANGELOG.md

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,136 +2,123 @@
22

33
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
44

5-
### [0.9.3](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.1...v0.9.3) (2019-08-04)
5+
### [0.9.4](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.3...v0.9.4) (2019-08-04)
6+
7+
### Bug Fixes
68

9+
- adjacent jsx nodes should be allowed in mdx ([8456381](https://github.com/rx-ts/eslint-plugin-mdx/commit/8456381))
10+
11+
### [0.9.3](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.1...v0.9.3) (2019-08-04)
712

813
### Bug Fixes
914

10-
* error from typescript parser in plain object ([f634e40](https://github.com/rx-ts/eslint-plugin-mdx/commit/f634e40))
15+
- error from typescript parser in plain object ([f634e40](https://github.com/rx-ts/eslint-plugin-mdx/commit/f634e40))
1116

1217
### [0.9.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.9.0...v0.9.1) (2019-08-03)
1318

14-
1519
### Bug Fixes
1620

17-
* always extends base config ([530160a](https://github.com/rx-ts/eslint-plugin-mdx/commit/530160a))
21+
- always extends base config ([530160a](https://github.com/rx-ts/eslint-plugin-mdx/commit/530160a))
1822

1923
## [0.9.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.8.1...v0.9.0) (2019-08-03)
2024

21-
2225
### Features
2326

24-
* support other extensions, detect parsers automatically ([5386098](https://github.com/rx-ts/eslint-plugin-mdx/commit/5386098))
27+
- support other extensions, detect parsers automatically ([5386098](https://github.com/rx-ts/eslint-plugin-mdx/commit/5386098))
2528

2629
### [0.8.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.1...v0.8.1) (2019-08-03)
2730

28-
2931
### Features
3032

31-
* split into two packages, use lerna as publisher ([#22](https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
33+
- split into two packages, use lerna as publisher ([#22](https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
3234

3335
## [0.8.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.1...v0.8.0) (2019-08-03)
3436

35-
3637
### Features
3738

38-
* split into two packages, use lerna as publisher ([#22](https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
39+
- split into two packages, use lerna as publisher ([#22](https://github.com/rx-ts/eslint-plugin-mdx/issues/22)) ([349b1ff](https://github.com/rx-ts/eslint-plugin-mdx/commit/349b1ff)), closes [#21](https://github.com/rx-ts/eslint-plugin-mdx/issues/21)
3940

4041
### [0.7.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.7.0...v0.7.1) (2019-08-03)
4142

42-
4343
### Features
4444

45-
* export a parse function for compatibility usage ([0fdaf9f](https://github.com/rx-ts/eslint-plugin-mdx/commit/0fdaf9f))
45+
- export a parse function for compatibility usage ([0fdaf9f](https://github.com/rx-ts/eslint-plugin-mdx/commit/0fdaf9f))
4646

4747
## [0.7.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.6.0...v0.7.0) (2019-08-02)
4848

49-
5049
### Features
5150

52-
* add new rule no-unescaped-entities ([dca8633](https://github.com/rx-ts/eslint-plugin-mdx/commit/dca8633))
51+
- add new rule no-unescaped-entities ([dca8633](https://github.com/rx-ts/eslint-plugin-mdx/commit/dca8633))
5352

5453
## [0.6.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.5.0...v0.6.0) (2019-08-02)
5554

56-
5755
### Features
5856

59-
* add new rule `no-jsx-html-comments`, close [#13](https://github.com/rx-ts/eslint-plugin-mdx/issues/13) ([67ba91e](https://github.com/rx-ts/eslint-plugin-mdx/commit/67ba91e))
60-
* processor support for normalize comments in jsx [ci skip] ([114831c](https://github.com/rx-ts/eslint-plugin-mdx/commit/114831c))
57+
- add new rule `no-jsx-html-comments`, close [#13](https://github.com/rx-ts/eslint-plugin-mdx/issues/13) ([67ba91e](https://github.com/rx-ts/eslint-plugin-mdx/commit/67ba91e))
58+
- processor support for normalize comments in jsx [ci skip](<[114831c](https://github.com/rx-ts/eslint-plugin-mdx/commit/114831c)>)
6159

6260
## [0.5.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.4.1...v0.5.0) (2019-07-31)
6361

64-
6562
### Features
6663

67-
* add `@rxts/mdx/no-unused-expressions` rule to replace eslint's ([5d62b55](https://github.com/rx-ts/eslint-plugin-mdx/commit/5d62b55))
64+
- add `@rxts/mdx/no-unused-expressions` rule to replace eslint's ([5d62b55](https://github.com/rx-ts/eslint-plugin-mdx/commit/5d62b55))
6865

6966
## [0.4.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.4.0...v0.4.1) (2019-07-31)
7067

71-
7268
### Features
7369

74-
* perf: extract `parseMdx`, exports everything in package ([1f965a1](https://github.com/rx-ts/eslint-plugin-mdx/commit/1f965a1))
70+
- perf: extract `parseMdx`, exports everything in package ([1f965a1](https://github.com/rx-ts/eslint-plugin-mdx/commit/1f965a1))
7571

7672
## [0.4.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.3.1...v0.4.0) (2019-07-31)
7773

78-
7974
### Features
8075

81-
* add body and comments into ast, fix [#12](https://github.com/rx-ts/eslint-plugin-mdx/issues/12) ([5b28bd5](https://github.com/rx-ts/eslint-plugin-mdx/commit/5b28bd5))
76+
- add body and comments into ast, fix [#12](https://github.com/rx-ts/eslint-plugin-mdx/issues/12) ([5b28bd5](https://github.com/rx-ts/eslint-plugin-mdx/commit/5b28bd5))
8277

8378
### [0.3.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.3.0...v0.3.1) (2019-07-30)
8479

85-
8680
### Bug Fixes
8781

88-
* `.*` could not match multi lines, use `[\s\S]*` instead, close [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4) ([f7e7efe](https://github.com/rx-ts/eslint-plugin-mdx/commit/f7e7efe))
82+
- `.*` could not match multi lines, use `[\s\S]*` instead, close [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4) ([f7e7efe](https://github.com/rx-ts/eslint-plugin-mdx/commit/f7e7efe))
8983

9084
## [0.3.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.2.1...v0.3.0) (2019-07-30)
9185

92-
9386
### Bug Fixes
9487

95-
* inline jsx and comment parsing error, close [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4), [#7](https://github.com/rx-ts/eslint-plugin-mdx/issues/7) ([5297a0b](https://github.com/rx-ts/eslint-plugin-mdx/commit/5297a0b))
88+
- inline jsx and comment parsing error, close [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4), [#7](https://github.com/rx-ts/eslint-plugin-mdx/issues/7) ([5297a0b](https://github.com/rx-ts/eslint-plugin-mdx/commit/5297a0b))
9689

9790
### [0.2.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.2.0...v0.2.1) (2019-07-30)
9891

99-
10092
### Bug Fixes
10193

102-
* eslint@5 is actually supported ([e470ddc](https://github.com/rx-ts/eslint-plugin-mdx/commit/e470ddc))
94+
- eslint@5 is actually supported ([e470ddc](https://github.com/rx-ts/eslint-plugin-mdx/commit/e470ddc))
10395

10496
## [0.2.0](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.3...v0.2.0) (2019-07-30)
10597

106-
10798
### Features
10899

109-
* support custom parser like babel-eslint ([b718574](https://github.com/rx-ts/eslint-plugin-mdx/commit/b718574))
100+
- support custom parser like babel-eslint ([b718574](https://github.com/rx-ts/eslint-plugin-mdx/commit/b718574))
110101

111102
### [0.1.3](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.2...v0.1.3) (2019-07-29)
112103

113-
114104
### Bug Fixes
115105

116-
* upgrade peer dependency eslint to >= 6.0.0 ([#3](https://github.com/rx-ts/eslint-plugin-mdx/issues/3) [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4)) ([f0ab288](https://github.com/rx-ts/eslint-plugin-mdx/commit/f0ab288))
106+
- upgrade peer dependency eslint to >= 6.0.0 ([#3](https://github.com/rx-ts/eslint-plugin-mdx/issues/3) [#4](https://github.com/rx-ts/eslint-plugin-mdx/issues/4)) ([f0ab288](https://github.com/rx-ts/eslint-plugin-mdx/commit/f0ab288))
117107

118108
### [0.1.2](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.1...v0.1.2) (2019-07-29)
119109

120-
121110
### Bug Fixes
122111

123-
* show correct column and line on lint error ([90c5390](https://github.com/rx-ts/eslint-plugin-mdx/commit/90c5390))
112+
- show correct column and line on lint error ([90c5390](https://github.com/rx-ts/eslint-plugin-mdx/commit/90c5390))
124113

125114
### [0.1.1](https://github.com/rx-ts/eslint-plugin-mdx/compare/v0.1.0...v0.1.1) (2019-07-29)
126115

127-
128116
### Bug Fixes
129117

130-
* overrides in node_modules seems not working ([d2f1535](https://github.com/rx-ts/eslint-plugin-mdx/commit/d2f1535))
118+
- overrides in node_modules seems not working ([d2f1535](https://github.com/rx-ts/eslint-plugin-mdx/commit/d2f1535))
131119

132120
## 0.1.0 (2019-07-29)
133121

134-
135122
### Features
136123

137-
* traverse ast nodes ([6a71e25](https://github.com/rx-ts/eslint-plugin-mdx/commit/6a71e25))
124+
- traverse ast nodes ([6a71e25](https://github.com/rx-ts/eslint-plugin-mdx/commit/6a71e25))

packages/eslint-mdx/src/helper.ts

Lines changed: 52 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,67 @@
1-
import { Arrayable, JsxNode, JsxType, JsxTypes } from './types'
1+
// eslint-disable-next-line @typescript-eslint/no-triple-slash-reference
2+
/// <reference path="../typings.d.ts" />
3+
4+
import { parse as esParse } from 'espree'
5+
6+
import {
7+
Arrayable,
8+
JsxNode,
9+
JsxType,
10+
JsxTypes,
11+
ParserOptions,
12+
ParserFn,
13+
} from './types'
214

315
import { Position } from 'unist'
416
import { AST } from 'eslint'
517
// `SourceLocation` is not exported from estree, but it is actually working
618
// eslint-disable-next-line import/named
719
import { SourceLocation } from 'estree'
820

21+
export const FALLBACK_PARSERS = [
22+
'@typescript-eslint/parser',
23+
'babel-eslint',
24+
] as const
25+
926
export const JSX_TYPES: JsxTypes = ['JSXElement', 'JSXFragment']
1027

1128
export const isJsxNode = (node: { type: string }): node is JsxNode =>
1229
JSX_TYPES.includes(node.type as JsxType)
1330

31+
export const normalizeParser = (parser: ParserOptions['parser']) => {
32+
if (parser) {
33+
if (typeof parser === 'string') {
34+
parser = require(parser)
35+
}
36+
37+
if (typeof parser === 'object') {
38+
parser = parser.parseForESLint || parser.parse
39+
}
40+
41+
if (typeof parser !== 'function') {
42+
throw new TypeError(
43+
`Invalid custom parser for \`eslint-plugin-mdx\`: ${parser}`,
44+
)
45+
}
46+
} else {
47+
// try to load FALLBACK_PARSERS automatically
48+
for (const fallback of FALLBACK_PARSERS) {
49+
try {
50+
// eslint-disable-next-line @typescript-eslint/no-var-requires
51+
const fallbackParser = require(fallback)
52+
parser = fallbackParser.parseForESLint || fallbackParser.parse
53+
break
54+
} catch (e) {}
55+
}
56+
57+
if (typeof parser !== 'function') {
58+
parser = esParse as ParserFn
59+
}
60+
61+
return parser
62+
}
63+
}
64+
1465
export const normalizePosition = (position: Position) => {
1566
const start = position.start.offset
1667
const end = position.end.offset

0 commit comments

Comments
 (0)