-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpretty.js
More file actions
37 lines (33 loc) · 691 Bytes
/
pretty.js
File metadata and controls
37 lines (33 loc) · 691 Bytes
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
function escape
(name) {
return name
.replace('\\', '\\\\')
.replace('(', '\\(')
.replace(')', '\\)')
.replace(',', '\\,')
}
export
function pretty
(node, offset = 0, indent = 0) {
if (node) {
let ret, child, prefix
ret = ''
prefix = ''
if (indent)
prefix = ' '.repeat(offset)
offset += (node.name.length + 1)
child = node.firstChild
while (child) {
let str
str = pretty(child, offset, ret.length)
if (str) {
if (ret.length)
ret += ',\n'
ret += str
}
child = child.nextSibling
}
return prefix + escape(node.name) + (ret.length ? '(' + ret + ')' : '')
}
return ''
}