@@ -3,10 +3,16 @@ import path from 'path'
33import type { Rule } from 'eslint'
44import type { ParserOptions } from 'eslint-mdx'
55import { DEFAULT_EXTENSIONS , MARKDOWN_EXTENSIONS } from 'eslint-mdx'
6+ import { createSyncFn } from 'synckit'
7+ import type { FrozenProcessor } from 'unified'
68import vfile from 'vfile'
79
810import { getPhysicalFilename , getRemarkProcessor } from './helpers'
9- import type { RemarkLintMessage } from './types'
11+ import type { ProcessSync , RemarkLintMessage } from './types'
12+
13+ const processSync = createSyncFn ( require . resolve ( '../worker' ) ) as ProcessSync
14+
15+ const brokenCache = new WeakMap < FrozenProcessor , true > ( )
1016
1117export const remark : Rule . RuleModule = {
1218 meta : {
@@ -36,22 +42,45 @@ export const remark: Rule.RuleModule = {
3642 if ( ! isMdx && ! isMarkdown ) {
3743 return
3844 }
45+
46+ const physicalFilename = getPhysicalFilename ( filename )
47+
3948 const sourceText = sourceCode . getText ( node )
40- const remarkProcessor = getRemarkProcessor (
41- getPhysicalFilename ( filename ) ,
42- isMdx ,
43- )
44- const file = vfile ( {
49+ const remarkProcessor = getRemarkProcessor ( physicalFilename , isMdx )
50+
51+ const fileOptions = {
4552 path : filename ,
4653 contents : sourceText ,
47- } )
54+ }
4855
49- try {
50- remarkProcessor . processSync ( file )
51- } catch ( err ) {
52- /* istanbul ignore next */
53- if ( ! file . messages . includes ( err ) ) {
54- file . message ( err ) . fatal = true
56+ const file = vfile ( fileOptions )
57+
58+ let broken = brokenCache . get ( remarkProcessor )
59+
60+ if ( broken ) {
61+ const { messages } = processSync ( fileOptions , physicalFilename , isMdx )
62+ file . messages = messages
63+ } else {
64+ try {
65+ remarkProcessor . processSync ( file )
66+ } catch ( err ) {
67+ /* istanbul ignore else */
68+ if (
69+ ( err as Error ) . message ===
70+ '`processSync` finished async. Use `process` instead'
71+ ) {
72+ brokenCache . set ( remarkProcessor , ( broken = true ) )
73+ const { messages } = processSync (
74+ fileOptions ,
75+ physicalFilename ,
76+ isMdx ,
77+ )
78+ file . messages = messages
79+ } else {
80+ if ( ! file . messages . includes ( err ) ) {
81+ file . message ( err ) . fatal = true
82+ }
83+ }
5584 }
5685 }
5786
@@ -102,11 +131,14 @@ export const remark: Rule.RuleModule = {
102131 end . offset == null ? start . offset + 1 : end . offset ,
103132 ]
104133 const partialText = sourceText . slice ( ...range )
105- const fixed = remarkProcessor . processSync ( partialText ) . toString ( )
134+ const fixed = broken
135+ ? processSync ( partialText , physicalFilename , isMdx )
136+ : remarkProcessor . processSync ( partialText ) . toString ( )
106137 return fixer . replaceTextRange (
107138 range ,
108- /* istanbul ignore next */
109- partialText . endsWith ( '\n' ) ? fixed : fixed . slice ( 0 , - 1 ) , // remove redundant new line
139+ partialText . endsWith ( '\n' )
140+ ? /* istanbul ignore next */ fixed
141+ : fixed . slice ( 0 , - 1 ) , // remove redundant new line
110142 )
111143 } ,
112144 } )
0 commit comments