@@ -3,58 +3,17 @@ import fs from 'fs'
3
3
import path from 'path'
4
4
import { pathToFileURL } from 'url'
5
5
6
- import type { SourceLocation } from 'estree '
6
+ import type { Position } from 'acorn '
7
7
import { createSyncFn } from 'synckit'
8
- import type { Node , Position } from 'unist'
8
+ import type { Point } from 'unist'
9
9
10
10
import type {
11
- MdxNode ,
12
- ValueOf ,
11
+ NormalPosition ,
13
12
WorkerOptions ,
14
13
WorkerParseResult ,
15
14
WorkerProcessResult ,
16
15
} from './types'
17
16
18
- export const MdxNodeType = {
19
- FLOW_EXPRESSION : 'mdxFlowExpression' ,
20
- JSX_FLOW_ELEMENT : 'mdxJsxFlowElement' ,
21
- JSX_TEXT_ELEMENT : 'mdxJsxTextElement' ,
22
- TEXT_EXPRESSION : 'mdxTextExpression' ,
23
- JS_ESM : 'mdxjsEsm' ,
24
- } as const
25
-
26
- export type MdxNodeType = ValueOf < typeof MdxNodeType >
27
-
28
- export const MDX_NODE_TYPES = [
29
- MdxNodeType . FLOW_EXPRESSION ,
30
- MdxNodeType . JSX_FLOW_ELEMENT ,
31
- MdxNodeType . JSX_TEXT_ELEMENT ,
32
- MdxNodeType . TEXT_EXPRESSION ,
33
- MdxNodeType . JS_ESM ,
34
- ] as const
35
-
36
- export const isMdxNode = ( node : Node ) : node is MdxNode =>
37
- MDX_NODE_TYPES . includes ( node . type as MdxNodeType )
38
-
39
- export interface BaseNode {
40
- type : string
41
- loc : SourceLocation
42
- range : [ number , number ]
43
- start ?: number
44
- end ?: number
45
- }
46
-
47
- export const normalizePosition = ( loc : Position ) : Omit < BaseNode , 'type' > => {
48
- const start = loc . start . offset
49
- const end = loc . end . offset
50
- return {
51
- range : [ start , end ] ,
52
- loc,
53
- start,
54
- end,
55
- }
56
- }
57
-
58
17
export const last = < T > ( items : T [ ] | readonly T [ ] ) =>
59
18
items && items [ items . length - 1 ]
60
19
@@ -163,6 +122,7 @@ export const requirePkg = async <T>(
163
122
prefix = prefix . endsWith ( '-' ) ? prefix : prefix + '-'
164
123
packages = [
165
124
plugin ,
125
+ /* istanbul ignore next */
166
126
plugin . startsWith ( '@' )
167
127
? plugin . replace ( '/' , '/' + prefix )
168
128
: prefix + plugin ,
@@ -181,6 +141,87 @@ export const requirePkg = async <T>(
181
141
throw error
182
142
}
183
143
144
+ /* istanbul ignore next -- used in worker */
145
+ export const getPositionAtFactory = ( text : string ) => {
146
+ const lines = text . split ( '\n' )
147
+ return ( offset : number ) : Position => {
148
+ let currOffset = 0
149
+
150
+ for ( const [ index , line_ ] of lines . entries ( ) ) {
151
+ const line = index + 1
152
+ const nextOffset = currOffset + line_ . length
153
+
154
+ if ( nextOffset >= offset ) {
155
+ return {
156
+ line,
157
+ column : offset - currOffset ,
158
+ offset,
159
+ }
160
+ }
161
+
162
+ currOffset = nextOffset + 1 // add a line break `\n` offset
163
+ }
164
+ }
165
+ }
166
+
167
+ export const normalizePosition = ( {
168
+ start,
169
+ end,
170
+ text,
171
+ } : {
172
+ start : Point | { offset : number }
173
+ end : Point | { offset : number }
174
+ text ?: string
175
+ } ) : NormalPosition => {
176
+ const startOffset = start . offset
177
+ const endOffset = end . offset
178
+ const range : [ number , number ] = [ startOffset , endOffset ]
179
+ const getPositionAt =
180
+ text == null
181
+ ? null
182
+ : /* istanbul ignore next -- used in worker */ getPositionAtFactory ( text )
183
+ return {
184
+ start : startOffset ,
185
+ end : endOffset ,
186
+ loc : {
187
+ start :
188
+ /* istanbul ignore next -- used in worker */ 'line' in start
189
+ ? ( start as Position )
190
+ : getPositionAt ( startOffset ) ,
191
+ end :
192
+ /* istanbul ignore next -- used in worker */ 'line' in end
193
+ ? ( end as Position )
194
+ : getPositionAt ( endOffset ) ,
195
+ } ,
196
+ range,
197
+ }
198
+ }
199
+
200
+ /* istanbul ignore next -- used in worker */
201
+ export const prevCharOffsetFactory =
202
+ ( text : string ) =>
203
+ ( offset : number ) : number => {
204
+ for ( let i = offset ; i >= 0 ; i -- ) {
205
+ const char = text [ i ]
206
+ if ( / ^ \S $ / . test ( char ) ) {
207
+ return i
208
+ }
209
+ }
210
+ }
211
+
212
+ /* istanbul ignore next -- used in worker */
213
+ export const nextCharOffsetFactory = ( text : string ) => {
214
+ const total = text . length
215
+ return ( offset : number ) : number => {
216
+ for ( let i = offset ; i <= total ; i ++ ) {
217
+ const char = text [ i ]
218
+ if ( / ^ \S $ / . test ( char ) ) {
219
+ return i
220
+ }
221
+ }
222
+ }
223
+ }
224
+
184
225
const workerPath = require . resolve ( './worker' )
185
226
186
227
export const performSyncWork = createSyncFn ( workerPath ) as ( (
0 commit comments