@@ -5,12 +5,20 @@ import type { ParserOptions } from 'eslint-mdx'
5
5
import { DEFAULT_EXTENSIONS , MARKDOWN_EXTENSIONS } from 'eslint-mdx'
6
6
import { createSyncFn } from 'synckit'
7
7
import type { FrozenProcessor } from 'unified'
8
+ import type { VFile , VFileOptions } from 'vfile'
8
9
import vfile from 'vfile'
9
10
10
11
import { getPhysicalFilename , getRemarkProcessor } from './helpers'
11
- import type { ProcessSync , RemarkLintMessage } from './types'
12
+ import type { RemarkLintMessage } from './types'
12
13
13
- const processSync = createSyncFn ( require . resolve ( '../worker' ) ) as ProcessSync
14
+ const processSync = createSyncFn ( require . resolve ( '../worker' ) ) as (
15
+ fileOptions : VFileOptions ,
16
+ physicalFilename : string ,
17
+ isFile : boolean ,
18
+ ) => {
19
+ messages : VFile [ 'messages' ]
20
+ content : string
21
+ }
14
22
15
23
const brokenCache = new WeakMap < FrozenProcessor , true > ( )
16
24
@@ -54,12 +62,18 @@ export const remark: Rule.RuleModule = {
54
62
}
55
63
56
64
const file = vfile ( fileOptions )
65
+ let fixedText : string
57
66
58
67
let broken = brokenCache . get ( remarkProcessor )
59
68
60
69
if ( broken ) {
61
- const { messages } = processSync ( fileOptions , physicalFilename , isMdx )
70
+ const { messages, content } = processSync (
71
+ fileOptions ,
72
+ physicalFilename ,
73
+ isMdx ,
74
+ )
62
75
file . messages = messages
76
+ fixedText = content
63
77
} else {
64
78
try {
65
79
remarkProcessor . processSync ( file )
@@ -70,20 +84,30 @@ export const remark: Rule.RuleModule = {
70
84
'`processSync` finished async. Use `process` instead'
71
85
) {
72
86
brokenCache . set ( remarkProcessor , ( broken = true ) )
73
- const { messages } = processSync (
87
+ const { messages, content } = processSync (
74
88
fileOptions ,
75
89
physicalFilename ,
76
90
isMdx ,
77
91
)
78
92
file . messages = messages
79
- } else {
80
- if ( ! file . messages . includes ( err ) ) {
81
- file . message ( err ) . fatal = true
82
- }
93
+ fixedText = content
94
+ } else if ( ! file . messages . includes ( err ) ) {
95
+ file . message ( err ) . fatal = true
83
96
}
84
97
}
85
98
}
86
99
100
+ if ( ! broken ) {
101
+ fixedText = file . toString ( )
102
+ }
103
+
104
+ fixedText =
105
+ filename === physicalFilename || sourceText . endsWith ( '\n' )
106
+ ? fixedText
107
+ : fixedText . slice ( 0 , - 1 )
108
+
109
+ let fixed = 0
110
+
87
111
for ( const {
88
112
source,
89
113
reason,
@@ -120,27 +144,16 @@ export const remark: Rule.RuleModule = {
120
144
} ,
121
145
} ,
122
146
node,
123
- fix ( fixer ) {
124
- /* istanbul ignore if */
125
- if ( start . offset == null ) {
126
- return null
127
- }
128
- const range : [ number , number ] = [
129
- start . offset ,
130
- /* istanbul ignore next */
131
- end . offset == null ? start . offset + 1 : end . offset ,
132
- ]
133
- const partialText = sourceText . slice ( ...range )
134
- const fixed = broken
135
- ? processSync ( partialText , physicalFilename , isMdx )
136
- : remarkProcessor . processSync ( partialText ) . toString ( )
137
- return fixer . replaceTextRange (
138
- range ,
139
- partialText . endsWith ( '\n' )
140
- ? /* istanbul ignore next */ fixed
141
- : fixed . slice ( 0 , - 1 ) , // remove redundant new line
142
- )
143
- } ,
147
+ fix :
148
+ fixedText === sourceText
149
+ ? null
150
+ : ( ) =>
151
+ fixed ++
152
+ ? null
153
+ : {
154
+ range : [ 0 , sourceText . length ] ,
155
+ text : fixedText ,
156
+ } ,
144
157
} )
145
158
}
146
159
} ,
0 commit comments