@@ -3,10 +3,16 @@ import path from 'path'
3
3
import type { Rule } from 'eslint'
4
4
import type { ParserOptions } from 'eslint-mdx'
5
5
import { DEFAULT_EXTENSIONS , MARKDOWN_EXTENSIONS } from 'eslint-mdx'
6
+ import { createSyncFn } from 'synckit'
7
+ import type { FrozenProcessor } from 'unified'
6
8
import vfile from 'vfile'
7
9
8
10
import { 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 > ( )
10
16
11
17
export const remark : Rule . RuleModule = {
12
18
meta : {
@@ -36,22 +42,45 @@ export const remark: Rule.RuleModule = {
36
42
if ( ! isMdx && ! isMarkdown ) {
37
43
return
38
44
}
45
+
46
+ const physicalFilename = getPhysicalFilename ( filename )
47
+
39
48
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 = {
45
52
path : filename ,
46
53
contents : sourceText ,
47
- } )
54
+ }
48
55
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
+ }
55
84
}
56
85
}
57
86
@@ -102,11 +131,14 @@ export const remark: Rule.RuleModule = {
102
131
end . offset == null ? start . offset + 1 : end . offset ,
103
132
]
104
133
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 ( )
106
137
return fixer . replaceTextRange (
107
138
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
110
142
)
111
143
} ,
112
144
} )
0 commit comments