Skip to content

Commit 1130911

Browse files
nd0utJounQin
andauthored
fix: parsing many comments error due to mutable regex (#227)
Co-authored-by: JounQin <[email protected]>
1 parent 334d1fd commit 1130911

File tree

3 files changed

+17
-6
lines changed

3 files changed

+17
-6
lines changed

packages/eslint-mdx/src/parser.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,11 @@ import {
1313
normalizePosition,
1414
restoreNodeLocation,
1515
} from './helper'
16-
import { COMMENT_CONTENT_REGEX, isComment } from './regexp'
16+
import {
17+
COMMENT_CONTENT_REGEX,
18+
COMMENT_CONTENT_REGEX_GLOBAL,
19+
isComment,
20+
} from './regexp'
1721
import { traverse } from './traverse'
1822
import {
1923
Comment,
@@ -101,7 +105,7 @@ export class Parser {
101105
inline: !!parent && parent.type !== 'root',
102106
},
103107
value: value.replace(
104-
COMMENT_CONTENT_REGEX,
108+
COMMENT_CONTENT_REGEX_GLOBAL,
105109
(
106110
matched: string,
107111
$0: string,

packages/eslint-mdx/src/regexp.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ export const selfClosingTag =
2626
export const comment = '<!---->|<!--(?:-?[^>-])(?:-?[^-])*-->'
2727
export const commentOpen = '(<!---*)'
2828
export const commentClose = '(-*-->)'
29+
export const commentContent = `${commentOpen}([\\s\\S]*?)${commentClose}`
2930

3031
export const OPEN_TAG_REGEX = new RegExp(`^(?:${openTag})$`)
3132
export const CLOSE_TAG_REGEX = new RegExp(`^(?:${closeTag})$`)
@@ -34,10 +35,8 @@ export const OPEN_CLOSE_TAG_REGEX = new RegExp(
3435
)
3536
export const SELF_CLOSING_TAG_REGEX = new RegExp(`^(?:${selfClosingTag})$`)
3637
export const COMMENT_REGEX = new RegExp(`^(?:${comment})$`)
37-
export const COMMENT_CONTENT_REGEX = new RegExp(
38-
`${commentOpen}([\\s\\S]*?)${commentClose}`,
39-
'g',
40-
)
38+
export const COMMENT_CONTENT_REGEX = new RegExp(commentContent)
39+
export const COMMENT_CONTENT_REGEX_GLOBAL = new RegExp(commentContent, 'g')
4140

4241
export const isOpenTag = (text: string) => OPEN_TAG_REGEX.test(text)
4342
export const isCloseTag = (text: string) => CLOSE_TAG_REGEX.test(text)

test/parser.test.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -219,4 +219,12 @@ describe('parser', () => {
219219
filePath: 'test.md',
220220
}),
221221
).not.toThrow())
222+
223+
it('should work with multiple comments', () =>
224+
expect(() =>
225+
parser.parse('<!-- * foo -->\n<!-- * bar -->', {
226+
...parserOptions,
227+
filePath: 'test.mdx',
228+
}),
229+
).not.toThrow())
222230
})

0 commit comments

Comments
 (0)