-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
recma-stringify.js
45 lines (39 loc) · 1.14 KB
/
recma-stringify.js
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
/**
* @typedef {import('estree-jsx').Program} Program
* @typedef {import('unified').Processor<undefined, undefined, undefined, Program, string>} Processor
* @typedef {import('vfile').VFile} VFile
*
* @typedef {import('../core.js').ProcessorOptions} ProcessorOptions
*/
import {jsx, toJs} from 'estree-util-to-js'
/**
* Serialize an esast (estree) program to JavaScript.
*
* @type {import('unified').Plugin<[Readonly<ProcessorOptions>], Program, string>}
*/
export function recmaStringify(options) {
// @ts-expect-error: TS is wrong about `this`.
// eslint-disable-next-line unicorn/no-this-assignment
const self = /** @type {Processor} */ (this)
const {SourceMapGenerator} = options
self.compiler = compiler
/**
* @param {Program} tree
* Tree.
* @param {VFile} file
* File.
* @returns {string}
* JavaScript.
*/
function compiler(tree, file) {
const result = SourceMapGenerator
? toJs(tree, {
SourceMapGenerator,
filePath: file.path || 'unknown.mdx',
handlers: jsx
})
: toJs(tree, {handlers: jsx})
file.map = result.map
return result.value
}
}