@@ -3,58 +3,17 @@ import fs from 'fs'
33import path from 'path'
44import { pathToFileURL } from 'url'
55
6- import type { SourceLocation } from 'estree '
6+ import type { Position } from 'acorn '
77import { createSyncFn } from 'synckit'
8- import type { Node , Position } from 'unist'
8+ import type { Point } from 'unist'
99
1010import type {
11- MdxNode ,
12- ValueOf ,
11+ NormalPosition ,
1312 WorkerOptions ,
1413 WorkerParseResult ,
1514 WorkerProcessResult ,
1615} from './types'
1716
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-
5817export const last = < T > ( items : T [ ] | readonly T [ ] ) =>
5918 items && items [ items . length - 1 ]
6019
@@ -163,6 +122,7 @@ export const requirePkg = async <T>(
163122 prefix = prefix . endsWith ( '-' ) ? prefix : prefix + '-'
164123 packages = [
165124 plugin ,
125+ /* istanbul ignore next */
166126 plugin . startsWith ( '@' )
167127 ? plugin . replace ( '/' , '/' + prefix )
168128 : prefix + plugin ,
@@ -181,6 +141,87 @@ export const requirePkg = async <T>(
181141 throw error
182142}
183143
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+
184225const workerPath = require . resolve ( './worker' )
185226
186227export const performSyncWork = createSyncFn ( workerPath ) as ( (
0 commit comments