1+ import { AST , Linter } from 'eslint'
2+ import { ExpressionStatement } from 'estree'
13import path from 'path'
2-
34import remarkMdx from 'remark-mdx'
45import remarkParse from 'remark-parse'
56import unified from 'unified'
7+ import { Node , Parent } from 'unist'
68
79import {
810 hasProperties ,
911 isJsxNode ,
12+ last ,
1013 normalizeParser ,
1114 normalizePosition ,
1215 restoreNodeLocation ,
13- last ,
1416} from './helper'
15- import { isComment , COMMENT_CONTENT_REGEX } from './regexp'
17+ import { COMMENT_CONTENT_REGEX , isComment } from './regexp'
1618import { 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'
2220
2321export const mdxProcessor = unified ( )
2422 . use ( remarkParse )
@@ -44,6 +42,10 @@ export const DEFAULT_PARSER_OPTIONS: ParserOptions = {
4442 filePath : '__placeholder__.mdx' ,
4543}
4644
45+ const JSX_WRAPPER_START = '<$>'
46+ const JSX_WRAPPER_END = '</$>'
47+ const OFFSET = JSX_WRAPPER_START . length
48+
4749export class Parser {
4850 // @internal
4951 private _parser : ParserFn
@@ -71,9 +73,9 @@ export class Parser {
7173 return node
7274 }
7375
74- const matched = COMMENT_CONTENT_REGEX . exec ( value )
76+ const commentContent = COMMENT_CONTENT_REGEX . exec ( value )
7577
76- if ( matched ) {
78+ if ( commentContent ) {
7779 const comments : Comment [ ] = [ ]
7880 const {
7981 position : {
@@ -176,7 +178,7 @@ export class Parser {
176178
177179 let normalized = this . normalizeJsxNode ( node , parent )
178180 normalized = Array . isArray ( normalized ) ? normalized : [ normalized ]
179- normalized . forEach ( node => this . _nodeToAst ( node , options ) )
181+ normalized . forEach ( _node => this . _nodeToAst ( _node , options ) )
180182 } ,
181183 } )
182184 }
@@ -212,15 +214,18 @@ export class Parser {
212214
213215 try {
214216 // 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
216220 } catch ( e ) {
217221 if ( hasProperties < LocationError > ( e , LOC_ERROR_PROPERTIES ) ) {
218222 const {
219223 position : { start } ,
220224 } = node
221225
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
224229 e . lineNumber += start . line - 1
225230
226231 throw e
@@ -251,21 +256,21 @@ export class Parser {
251256 } = jsNode
252257 const startLine = line + start . line - 1
253258 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
256261 nodes . push ( {
257262 type : 'jsx' ,
258263 data : nodes . length ? null : node . data ,
259264 value : value . slice ( startOffset , endOffset ) ,
260265 position : {
261266 start : {
262267 line : startLine ,
263- column : line === startLine ? start . column - 3 : start . column ,
268+ column : line === startLine ? start . column - OFFSET : start . column ,
264269 offset : offset + startOffset ,
265270 } ,
266271 end : {
267272 line : endLine ,
268- column : line === startLine ? end . column - 3 : end . column ,
273+ column : line === startLine ? end . column - OFFSET : end . column ,
269274 offset : offset + endOffset ,
270275 } ,
271276 } ,
@@ -288,7 +293,7 @@ export class Parser {
288293 }
289294
290295 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
292297
293298 let program : AST . Program
294299
@@ -310,7 +315,6 @@ export class Parser {
310315 AST_PROPS . forEach ( prop =>
311316 this . _ast [ prop ] . push (
312317 // unfortunately, TS complains about incompatible signature
313- // eslint-disable-next-line @typescript-eslint/ban-ts-ignore
314318 // @ts -ignore
315319 ...program [ prop ] . map ( item =>
316320 restoreNodeLocation ( item , startLine , offset ) ,
0 commit comments