forked from mdx-js/eslint-mdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
worker.ts
782 lines (649 loc) · 22.7 KB
/
worker.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
/* eslint-disable @typescript-eslint/consistent-type-imports */
/* eslint-disable unicorn/no-await-expression-member */
import path from 'node:path'
import { pathToFileURL } from 'node:url'
import type { Token, TokenType, tokTypes as _tokTypes } from 'acorn'
import type { AST } from 'eslint'
import type { EsprimaToken } from 'espree/lib/token-translator'
import type {
BaseExpression,
Comment,
Expression,
ExpressionStatement,
ObjectExpression,
Program,
SpreadElement,
} from 'estree'
import type {
JSXClosingElement,
JSXElement,
JSXExpressionContainer,
JSXFragment,
JSXIdentifier,
JSXMemberExpression,
JSXNamespacedName,
JSXText,
} from 'estree-jsx'
import type { Nodes, Root } from 'mdast'
import type { Options } from 'micromark-extension-mdx-expression'
import { extractProperties, runAsWorker } from 'synckit'
import type { Processor } from 'unified'
import type { Configuration, ConfigResult } from 'unified-engine'
import type { Node } from 'unist'
import { ok as assert } from 'uvu/assert'
import type { VFileMessage } from 'vfile-message'
import {
loadEsmModule,
nextCharOffsetFactory,
normalizePosition,
prevCharOffsetFactory,
} from './helpers'
import { restoreTokens } from './tokens'
import type {
Arrayable,
MDXCode,
MDXHeading,
NormalPosition,
WorkerOptions,
WorkerResult,
} from './types'
let config: Configuration
let acorn: typeof import('acorn')
let acornJsx: {
default: typeof import('acorn-jsx')
}
let acornParser: typeof import('acorn').Parser
let tokTypes: typeof _tokTypes
let jsxTokTypes: Record<string, TokenType>
let tt: Record<string, TokenType> & typeof _tokTypes
let TokenTranslator: (typeof import('espree/lib/token-translator'))['default']
export const processorCache = new Map<
string,
Processor<Root, undefined, undefined, Root, string>
>()
const getRemarkConfig = async (searchFrom: string) => {
if (!config) {
const { Configuration } =
await loadEsmModule<typeof import('unified-engine')>('unified-engine')
config = new Configuration({
cwd: process.cwd(),
packageField: 'remarkConfig',
pluginPrefix: 'remark',
rcName: '.remarkrc',
detectConfig: true,
})
}
return new Promise<ConfigResult>((resolve, reject) =>
config.load(searchFrom, (error, result) => {
error ? reject(error) : resolve(result)
}),
)
}
const getRemarkMdxOptions = (tokens: Token[]): Options => ({
acorn: acornParser,
acornOptions: {
ecmaVersion: 'latest',
sourceType: 'module',
locations: true,
ranges: true,
onToken: tokens,
},
})
const sharedTokens: Token[] = []
export const getRemarkProcessor = async (
searchFrom: string,
isMdx: boolean,
ignoreRemarkConfig?: boolean,
) => {
const initCacheKey = `${String(isMdx)}-${searchFrom}`
let cachedProcessor = processorCache.get(initCacheKey)
if (cachedProcessor) {
return cachedProcessor
}
const result = ignoreRemarkConfig ? null : await getRemarkConfig(searchFrom)
const cacheKey = result?.filePath
? `${String(isMdx)}-${result.filePath}`
: String(isMdx)
cachedProcessor = processorCache.get(cacheKey)
if (cachedProcessor) {
return cachedProcessor
}
const { unified } = await loadEsmModule<typeof import('unified')>('unified')
const remarkParse = (
await loadEsmModule<typeof import('remark-parse')>('remark-parse')
).default
const remarkStringify = (
await loadEsmModule<typeof import('remark-stringify')>('remark-stringify')
).default
const remarkMdx = (
await loadEsmModule<typeof import('remark-mdx')>('remark-mdx')
).default
const remarkProcessor = unified().use(remarkParse).freeze()
if (result?.filePath) {
const { plugins, settings } = result
// disable this rule automatically since we already have a parser option `extensions`
// only disable this plugin if there are at least one plugin enabled
// otherwise it is redundant
/* istanbul ignore else */
if (plugins.length > 0) {
try {
plugins.push([
(
await loadEsmModule<typeof import('remark-lint-file-extension')>(
'remark-lint-file-extension',
)
).default,
false,
])
} catch {
// just ignore if the package does not exist
}
}
const initProcessor = remarkProcessor()
.use({ settings })
.use(remarkStringify)
if (isMdx) {
initProcessor.use(remarkMdx, getRemarkMdxOptions(sharedTokens))
}
cachedProcessor = plugins
.reduce((processor, plugin) => processor.use(...plugin), initProcessor)
.freeze()
} else {
const initProcessor = remarkProcessor().use(remarkStringify)
if (isMdx) {
initProcessor.use(remarkMdx, getRemarkMdxOptions(sharedTokens))
}
cachedProcessor = initProcessor.freeze()
}
processorCache
.set(initCacheKey, cachedProcessor)
.set(cacheKey, cachedProcessor)
return cachedProcessor
}
function isExpressionStatement(
statement: Program['body'][number],
): asserts statement is ExpressionStatement | undefined {
assert(!statement || statement.type === 'ExpressionStatement')
}
runAsWorker(
async ({
fileOptions,
physicalFilename,
isMdx,
process,
ignoreRemarkConfig,
}: // eslint-disable-next-line sonarjs/cognitive-complexity
WorkerOptions): Promise<WorkerResult> => {
sharedTokens.length = 0
/**
* unified plugins are using ESM version of acorn,
* so we have to use the same version as well,
* otherwise the TokenType will be different class
* @see also https://github.com/acornjs/acorn-jsx/issues/133
*/
if (!acorn) {
acorn = await loadEsmModule<typeof import('acorn')>('acorn')
acornJsx = await loadEsmModule<{ default: typeof import('acorn-jsx') }>(
'acorn-jsx',
)
acornParser = acorn.Parser.extend(acornJsx.default())
}
const processor = await getRemarkProcessor(
physicalFilename,
isMdx,
ignoreRemarkConfig,
)
if (process) {
const { VFile } = await loadEsmModule<typeof import('vfile')>('vfile')
const file = new VFile(fileOptions)
try {
await processor.process(file)
} catch (err) {
const error = err as VFileMessage
if (!file.messages.includes(error)) {
file.message(error).fatal = true
}
}
return {
messages: file.messages.map(message => extractProperties(message)),
content: file.toString(),
}
}
if (!tokTypes) {
tokTypes = acorn.tokTypes
}
if (!jsxTokTypes) {
// eslint-disable-next-line @typescript-eslint/no-unsafe-assignment
jsxTokTypes = acornJsx.default({
allowNamespacedObjects: true,
// @ts-expect-error -- no type
// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
})(acorn.Parser).acornJsx.tokTypes
}
if (!TokenTranslator) {
TokenTranslator = (
await loadEsmModule<typeof import('espree/lib/token-translator')>(
pathToFileURL(
path.resolve(
require.resolve('espree/package.json'),
'../lib/token-translator.js',
),
),
)
).default
}
if (!tt) {
tt = {
...tokTypes,
...jsxTokTypes,
}
}
const text = fileOptions.value as string
const tokenTranslator = new TokenTranslator(tt, text)
const root = processor.parse(fileOptions)
const body: Program['body'] = []
const comments: Comment[] = []
const tokens: AST.Token[] = []
const { visit } =
await loadEsmModule<typeof import('unist-util-visit')>('unist-util-visit')
const processed = new WeakSet<Node>()
// TODO: merge with `tokens.ts`
if (isMdx) {
const prevCharOffset = prevCharOffsetFactory(text)
const nextCharOffset = nextCharOffsetFactory(text)
const normalizeNode = (start: number, end: number) => ({
...normalizePosition({
start: { offset: start },
end: { offset: end },
text,
}),
raw: text.slice(start, end),
})
const handleJsxName = (
nodeName: string,
start: number,
): JSXIdentifier | JSXMemberExpression | JSXNamespacedName => {
const name = nodeName.trim()
const nameIndex = nodeName.indexOf(name)
const colonIndex = nodeName.indexOf(':')
if (colonIndex !== -1) {
const [fullNamespace, fullName] = nodeName.split(':')
return {
...normalizeNode(
start + nameIndex,
start + nameIndex + name.length,
),
type: 'JSXNamespacedName',
namespace: handleJsxName(fullNamespace, start) as JSXIdentifier,
name: handleJsxName(
fullName,
start + colonIndex + 1,
) as JSXIdentifier,
}
}
const lastPointIndex = nodeName.lastIndexOf('.')
if (lastPointIndex === -1) {
return {
...normalizeNode(
start + nameIndex,
start + nameIndex + name.length,
),
type: 'JSXIdentifier',
name,
}
}
const objectName = nodeName.slice(0, lastPointIndex)
const propertyName = nodeName.slice(lastPointIndex + 1)
return {
...normalizeNode(start + nameIndex, start + nameIndex + name.length),
type: 'JSXMemberExpression',
object: handleJsxName(objectName, start) as
| JSXIdentifier
| JSXMemberExpression,
property: handleJsxName(
propertyName,
start + lastPointIndex + 1,
) as JSXIdentifier,
}
}
visit(root, node => {
if (
processed.has(node) ||
(node.type !== 'mdxFlowExpression' &&
node.type !== 'mdxJsxFlowElement' &&
node.type !== 'mdxJsxTextElement' &&
node.type !== 'mdxTextExpression' &&
node.type !== 'mdxjsEsm')
) {
return
}
processed.add(node)
function handleChildren(node: Nodes) {
return 'children' in node
? node.children.reduce<JSXElement['children']>((acc, child) => {
processed.add(child)
if (child.data && 'estree' in child.data && child.data.estree) {
const { estree } = child.data
assert(estree.body.length <= 1)
const statement = estree.body[0]
isExpressionStatement(statement)
const expression = statement?.expression
if (child.type === 'mdxTextExpression') {
const {
start: { offset: start },
end: { offset: end },
} = node.position
const expressionContainer: JSXExpressionContainer = {
...normalizeNode(start, end),
type: 'JSXExpressionContainer',
expression: expression || {
...normalizeNode(start + 1, end - 1),
type: 'JSXEmptyExpression',
},
}
acc.push(expressionContainer)
} else if (expression) {
acc.push(expression as JSXElement['children'][number])
}
comments.push(...estree.comments)
} else {
const expression = handleNode(child) as Arrayable<
JSXElement['children']
>
if (Array.isArray(expression)) {
acc.push(...expression)
} else if (expression) {
acc.push(expression)
}
}
return acc
}, [])
: []
}
function handleNode(node: Nodes) {
if (node.type === 'paragraph') {
return handleChildren(node)
}
const {
start: { offset: start },
end: { offset: end },
} = node.position
if (node.type === 'code') {
const { lang, meta, value } = node
const mdxJsxCode: MDXCode = {
...normalizeNode(start, end),
type: 'MDXCode',
lang,
meta,
value,
}
return mdxJsxCode
}
if (node.type === 'heading') {
const { depth } = node
const mdxJsxHeading: MDXHeading = {
...normalizeNode(start, end),
type: 'MDXHeading',
depth,
children: handleChildren(node),
}
return mdxJsxHeading
}
if (node.type === 'text') {
const jsxText: JSXText = {
...normalizeNode(start, end),
type: 'JSXText',
value: node.value,
}
return jsxText
}
if (
node.type !== 'mdxJsxTextElement' &&
node.type !== 'mdxJsxFlowElement'
) {
return
}
const children = handleChildren(node)
// keep for debugging
// if (+1 === 1) {
// throw new SyntaxError(JSON.stringify({ node, children }, null, 2))
// }
const nodePos = node.position
const nodeStart = nodePos.start.offset
const nodeEnd = nodePos.end.offset
const lastCharOffset = prevCharOffset(nodeEnd - 2)
let expression: BaseExpression
if ('name' in node && node.name) {
const nodeNameLength = node.name.length
const nodeNameStart = nextCharOffset(nodeStart + 1)
const selfClosing = text[lastCharOffset] === '/'
let lastAttrOffset = nodeNameStart + nodeNameLength - 1
let closingElement: JSXClosingElement | null = null
if (!selfClosing) {
const prevOffset = prevCharOffset(lastCharOffset)
const slashOffset = prevCharOffset(prevOffset - nodeNameLength)
assert(
text[slashOffset] === '/',
`expect \`${text[slashOffset]}\` to be \`/\`, the node is ${node.name}`,
)
const tagStartOffset = prevCharOffset(slashOffset - 1)
assert(text[tagStartOffset] === '<')
closingElement = {
...normalizeNode(tagStartOffset, nodeEnd),
type: 'JSXClosingElement',
name: handleJsxName(node.name, prevOffset + 1 - nodeNameLength),
}
}
const jsxEl: JSXElement = {
...normalizeNode(nodeStart, nodeEnd),
type: 'JSXElement',
openingElement: {
type: 'JSXOpeningElement',
name: handleJsxName(node.name, nodeNameStart),
attributes: node.attributes.map(attr => {
if (attr.type === 'mdxJsxExpressionAttribute') {
assert(attr.data)
assert(attr.data.estree)
assert(attr.data.estree.range)
let [attrValStart, attrValEnd] = attr.data.estree.range
attrValStart = prevCharOffset(attrValStart - 1)
attrValEnd = nextCharOffset(attrValEnd)
assert(text[attrValStart] === '{')
assert(text[attrValEnd] === '}')
lastAttrOffset = attrValEnd
return {
// mdxJsxExpressionAttribute
...normalizeNode(attrValStart, attrValEnd + 1),
type: 'JSXSpreadAttribute',
// https://github.com/mdx-js/eslint-mdx/pull/394#discussion_r872974843
argument: (
(
(attr.data.estree.body[0] as ExpressionStatement)
.expression as ObjectExpression
).properties[0] as SpreadElement
).argument,
}
}
const attrStart = nextCharOffset(lastAttrOffset + 1)
assert(attrStart != null)
const attrName = attr.name
const attrNameLength = attrName.length
const attrValue = attr.value
lastAttrOffset = attrStart + attrNameLength
const attrNamePos = normalizeNode(attrStart, lastAttrOffset)
if (attrValue == null) {
return {
...normalizeNode(attrStart, lastAttrOffset),
type: 'JSXAttribute',
name: {
...attrNamePos,
type: 'JSXIdentifier',
name: attrName,
},
value: null,
}
}
const attrEqualOffset = nextCharOffset(
attrStart + attrNameLength,
)
assert(text[attrEqualOffset] === '=')
let attrValuePos: NormalPosition
if (typeof attrValue === 'string') {
const attrQuoteOffset = nextCharOffset(attrEqualOffset + 1)
const attrQuote = text[attrQuoteOffset]
assert(attrQuote === '"' || attrQuote === "'")
lastAttrOffset = nextCharOffset(
attrQuoteOffset + attrValue.length + 1,
)
assert(text[lastAttrOffset] === attrQuote)
attrValuePos = normalizeNode(
attrQuoteOffset,
lastAttrOffset + 1,
)
} else {
const data = attrValue.data
let [attrValStart, attrValEnd] = data.estree.range
attrValStart = prevCharOffset(attrValStart - 1)
attrValEnd = nextCharOffset(attrValEnd)
assert(text[attrValStart] === '{')
assert(text[attrValEnd] === '}')
lastAttrOffset = attrValEnd
attrValuePos = normalizeNode(attrValStart, attrValEnd + 1)
}
return {
...normalizeNode(attrStart, lastAttrOffset + 1),
type: 'JSXAttribute',
name: {
...attrNamePos,
type: 'JSXIdentifier',
name: attrName,
},
value:
typeof attr.value === 'string'
? {
...attrValuePos,
type: 'Literal',
value: attr.value,
}
: {
...attrValuePos,
type: 'JSXExpressionContainer',
expression: (
attr.value.data.estree
.body[0] as ExpressionStatement
).expression,
},
}
}),
selfClosing,
},
closingElement,
children,
}
let nextOffset = nextCharOffset(lastAttrOffset + 1)
let nextChar = text[nextOffset]
const expectedNextChar = selfClosing ? '/' : '>'
if (nextChar !== expectedNextChar) {
nextOffset = /** @type {number} */ nextCharOffset(lastAttrOffset)
nextChar = text[nextOffset]
}
assert(
nextChar === expectedNextChar,
`\`nextChar\` must be '${expectedNextChar}' but actually is '${nextChar}'`,
)
Object.assign(
jsxEl.openingElement,
normalizeNode(nodeStart, selfClosing ? nodeEnd : nextOffset + 1),
)
expression = jsxEl
} else {
const openEndOffset = nextCharOffset(nodeStart + 1)
const openPos = normalizeNode(nodeStart, openEndOffset)
const closeStartOffset = prevCharOffset(lastCharOffset - 1)
const jsxFrg: JSXFragment = {
...openPos,
type: 'JSXFragment',
openingFragment: {
...openPos,
type: 'JSXOpeningFragment',
},
closingFragment: {
...normalizeNode(closeStartOffset, nodeEnd),
type: 'JSXClosingFragment',
},
children,
}
expression = jsxFrg
}
return expression
}
const expression = handleNode(node)
// keep for debugging
// if (+1 === 1) {
// throw new SyntaxError(JSON.stringify({ node, expression }, null, 2))
// }
if (expression) {
body.push({
...normalizePosition(node.position),
type: 'ExpressionStatement',
expression: expression as Expression,
})
}
const estree = ((node.data &&
'estree' in node.data &&
node.data.estree) || {
body: [],
comments: [],
}) as Program
body.push(...estree.body)
comments.push(...estree.comments)
})
}
const { visit: visitEstree } =
await loadEsmModule<typeof import('estree-util-visit')>(
'estree-util-visit',
)
visitEstree(
{
type: 'Program',
sourceType: 'module',
body,
},
node => {
if (node.type !== 'TemplateElement') {
return
}
/**
* Copied from @see https://github.com/eslint/espree/blob/1584ddb00f0b4e3ada764ac86ae20e1480003de3/lib/espree.js#L227-L241
*/
const templateElement = node
const startOffset = -1
const endOffset = templateElement.tail ? 1 : 2
// @ts-expect-error - unavailable for typing from estree
templateElement.start += startOffset
// @ts-expect-error - unavailable for typing from estree
templateElement.end += endOffset
if (templateElement.range) {
templateElement.range[0] += startOffset
templateElement.range[1] += endOffset
}
if (templateElement.loc) {
templateElement.loc.start.column += startOffset
templateElement.loc.end.column += endOffset
}
},
)
for (const token of restoreTokens(text, root, sharedTokens, tt, visit)) {
tokenTranslator.onToken(token, {
ecmaVersion: 'latest',
tokens: tokens as EsprimaToken[],
})
}
return {
root,
body,
comments,
tokens,
}
},
)