forked from prettier/prettier
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrequire_standalone.js
55 lines (49 loc) · 1.31 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
50
51
52
53
54
55
"use strict";
const fs = require("fs");
const vm = require("vm");
const globby = require("globby");
const sandbox = vm.createContext();
const source = globby
.sync(["standalone.js", "parser-*.js"], {
cwd: process.env.PRETTIER_DIR,
absolute: true,
})
.map((file) => fs.readFileSync(file, "utf8"))
.join(";");
vm.runInContext(source, sandbox);
// 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 }
);
},
},
};