-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
84 lines (76 loc) · 1.78 KB
/
index.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
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
const fs = require('fs');
const YAML = require('json-to-pretty-yaml');
const espree = require("espree");
const babel = require("@babel/core");
let omit = new Set([
"loc",
"range",
"start",
"end",
"computed",
"optional",
"sourceType",
"tokens",
"comments",
"leadingComments",
"trailingComments",
"innerComments",
"extra",
"raw",
"rawValue",
"errors",
"error"]);
const location = new Set([
"loc",
"range",
"start",
"end",
]);
function replace(key, value) {
if (omit.has(key)) return undefined;
return value;
}
module.exports = function (code, options, filename) {
if (options.all) {
omit.clear();
}
if (options.location) {
omit = location
}
options?.hide?.forEach(element => {
omit.add(element);
});
if (options.hideFile) {
try {
let fields = fs.readFileSync(options.hideFile, 'utf8').split('\n');
fields.forEach(element => {
omit.add(element);
});
} catch (e) {
console.warn(e.message);
}
}
if (filename) options.parse = !/\.json$/.test(filename);
let ast, parse, newOptions;
if (options.babel) {
parse = babel.parse;
newOptions = {}
} else {
parse = espree.parse;
newOptions = { ecmaVersion: espree.latestEcmaVersion, sourceType: "module" }
}
if (options.parse) {
ast = parse(code, newOptions);
} else if (typeof code === "string"){
ast = JSON.parse(code);
} else {
ast = code;
}
let result = JSON.stringify(ast, replace, options.whites);
ast = JSON.parse(result);
if (options.json) {
return result;
} else {
return YAML.stringify(ast);
}
}