1
+ import { AST , Linter } from 'eslint'
2
+ import { ExpressionStatement } from 'estree'
1
3
import path from 'path'
2
-
3
4
import remarkMdx from 'remark-mdx'
4
5
import remarkParse from 'remark-parse'
5
6
import unified from 'unified'
7
+ import { Node , Parent } from 'unist'
6
8
7
9
import {
8
10
hasProperties ,
9
11
isJsxNode ,
12
+ last ,
10
13
normalizeParser ,
11
14
normalizePosition ,
12
15
restoreNodeLocation ,
13
- last ,
14
16
} from './helper'
15
- import { isComment , COMMENT_CONTENT_REGEX } from './regexp'
17
+ import { COMMENT_CONTENT_REGEX , isComment } from './regexp'
16
18
import { traverse } from './traverse'
17
- import { ParserOptions , LocationError , Comment , ParserFn } from './types'
18
-
19
- import { AST , Linter } from 'eslint'
20
- import { Parent , Node } from 'unist'
21
- import { ExpressionStatement } from 'estree'
19
+ import { Comment , LocationError , ParserFn , ParserOptions } from './types'
22
20
23
21
export const mdxProcessor = unified ( )
24
22
. use ( remarkParse )
@@ -44,6 +42,10 @@ export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
44
42
filePath : '__placeholder__.mdx' ,
45
43
}
46
44
45
+ const JSX_WRAPPER_START = '<$>'
46
+ const JSX_WRAPPER_END = '</$>'
47
+ const OFFSET = JSX_WRAPPER_START . length
48
+
47
49
export class Parser {
48
50
// @internal
49
51
private _parser : ParserFn
@@ -71,9 +73,9 @@ export class Parser {
71
73
return node
72
74
}
73
75
74
- const matched = COMMENT_CONTENT_REGEX . exec ( value )
76
+ const commentContent = COMMENT_CONTENT_REGEX . exec ( value )
75
77
76
- if ( matched ) {
78
+ if ( commentContent ) {
77
79
const comments : Comment [ ] = [ ]
78
80
const {
79
81
position : {
@@ -176,7 +178,7 @@ export class Parser {
176
178
177
179
let normalized = this . normalizeJsxNode ( node , parent )
178
180
normalized = Array . isArray ( normalized ) ? normalized : [ normalized ]
179
- normalized . forEach ( node => this . _nodeToAst ( node , options ) )
181
+ normalized . forEach ( _node => this . _nodeToAst ( _node , options ) )
180
182
} ,
181
183
} )
182
184
}
@@ -212,15 +214,18 @@ export class Parser {
212
214
213
215
try {
214
216
// wrap into single element which is valid jsx but not valid jsx in mdx, so that it won't break on adjacent JSX nodes
215
- program = this . _eslintParse ( `<$>${ value } </$>` ) . ast
217
+ program = this . _eslintParse (
218
+ `${ JSX_WRAPPER_START } ${ value } ${ JSX_WRAPPER_END } ` ,
219
+ ) . ast
216
220
} catch ( e ) {
217
221
if ( hasProperties < LocationError > ( e , LOC_ERROR_PROPERTIES ) ) {
218
222
const {
219
223
position : { start } ,
220
224
} = node
221
225
222
- e . index += start . offset - 3
223
- e . column = e . lineNumber > 1 ? e . column : e . column + start . column - 3
226
+ e . index += start . offset - OFFSET
227
+ e . column =
228
+ e . lineNumber > 1 ? e . column : e . column + start . column - OFFSET
224
229
e . lineNumber += start . line - 1
225
230
226
231
throw e
@@ -251,21 +256,21 @@ export class Parser {
251
256
} = jsNode
252
257
const startLine = line + start . line - 1
253
258
const endLine = line + end . line - 1
254
- const startOffset = range [ 0 ] - 3
255
- const endOffset = range [ 1 ] - 3
259
+ const startOffset = range [ 0 ] - OFFSET
260
+ const endOffset = range [ 1 ] - OFFSET
256
261
nodes . push ( {
257
262
type : 'jsx' ,
258
263
data : nodes . length ? null : node . data ,
259
264
value : value . slice ( startOffset , endOffset ) ,
260
265
position : {
261
266
start : {
262
267
line : startLine ,
263
- column : line === startLine ? start . column - 3 : start . column ,
268
+ column : line === startLine ? start . column - OFFSET : start . column ,
264
269
offset : offset + startOffset ,
265
270
} ,
266
271
end : {
267
272
line : endLine ,
268
- column : line === startLine ? end . column - 3 : end . column ,
273
+ column : line === startLine ? end . column - OFFSET : end . column ,
269
274
offset : offset + endOffset ,
270
275
} ,
271
276
} ,
@@ -288,7 +293,7 @@ export class Parser {
288
293
}
289
294
290
295
const { loc, start } = normalizePosition ( node . position )
291
- const startLine = loc . start . line - 1 //! line is 1-indexed, change to 0-indexed to simplify usage
296
+ const startLine = loc . start . line - 1 // ! line is 1-indexed, change to 0-indexed to simplify usage
292
297
293
298
let program : AST . Program
294
299
@@ -310,7 +315,6 @@ export class Parser {
310
315
AST_PROPS . forEach ( prop =>
311
316
this . _ast [ prop ] . push (
312
317
// unfortunately, TS complains about incompatible signature
313
- // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
314
318
// @ts -ignore
315
319
...program [ prop ] . map ( item =>
316
320
restoreNodeLocation ( item , startLine , offset ) ,
0 commit comments