/
require-standalone.js
49 lines (46 loc) · 1.24 KB
/
require-standalone.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
46
47
48
49
"use strict";
const vm = require("vm");
const fastGlob = require("fast-glob");
const createSandBox = require("./utils/create-sandbox.js");
const sandbox = createSandBox({
files: fastGlob.sync(["standalone.js", "parser-*.js"], {
cwd: process.env.PRETTIER_DIR,
absolute: true,
}),
});
// TODO: maybe expose (and write tests) for `format`, `utils`, and
// `__debug` methods
module.exports = {
formatWithCursor(input, options) {
return vm.runInNewContext(
`
const options = {
...$$$options,
plugins: [
...Object.values(prettierPlugins),
...($$$options.plugins || []),
],
};
prettier.formatWithCursor($$$input, options);
`,
{ $$$input: input, $$$options: options, ...sandbox }
);
},
__debug: {
parse(input, options, massage) {
return vm.runInNewContext(
`
const options = {
...$$$options,
plugins: [
...Object.values(prettierPlugins),
...($$$options.plugins || []),
],
};
prettier.__debug.parse($$$input, options, ${JSON.stringify(massage)});
`,
{ $$$input: input, $$$options: options, ...sandbox }
);
},
},
};