-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathindex.js
192 lines (149 loc) · 4.11 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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
'use strict'
var isEmpty = require('is-empty')
var color = require('./color')
module.exports = color ? inspect : /* istanbul ignore next */ noColor
inspect.color = inspect
noColor.color = inspect
inspect.noColor = noColor
noColor.noColor = noColor
var dim = ansiColor(2, 22)
var yellow = ansiColor(33, 39)
var green = ansiColor(32, 39)
// Define ANSII color removal functionality.
var colorExpression = /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
// Standard keys defined by unist (<https://github.com/syntax-tree/unist>) that
// we format differently.
// We don’t ignore `data` though.
// Also includes `name` (from xast) and `tagName` (from `hast`).
var ignore = ['type', 'value', 'children', 'position', 'name', 'tagName']
// Inspects a node, without using color.
function noColor(node) {
return stripColor(inspect(node))
}
// Inspects a node.
function inspect(node) {
return inspectValue(node, '')
function inspectValue(node, pad) {
if (node && Boolean(node.length) && typeof node === 'object') {
return inspectAll(node, pad)
}
if (node && node.type) {
return inspectTree(node, pad)
}
return inspectNonTree(node, pad)
}
function inspectNonTree(value, pad) {
return formatNesting(pad) + String(value)
}
function inspectAll(nodes, pad) {
var length = nodes.length
var index = -1
var result = []
var node
var tail
while (++index < length) {
node = nodes[index]
tail = index === length - 1
result.push(
formatNesting(pad + (tail ? '└' : '├') + '─ '),
inspectValue(node, pad + (tail ? ' ' : '│') + ' '),
tail ? '' : '\n'
)
}
return result.join('')
}
function inspectTree(node, pad) {
var result = formatNode(node, pad)
var content = inspectAll(node.children || [], pad)
return content ? result + '\n' + content : result
}
// Colored nesting formatter.
function formatNesting(value) {
return value ? dim(value) : ''
}
// Colored node formatter.
function formatNode(node) {
var result = [node.type]
var kind = node.tagName || node.name
var position = node.position || {}
var location = stringifyPosition(position.start, position.end)
var attributes = []
var key
var value
if (kind) {
result.push('<', kind, '>')
}
if (node.children) {
result.push(dim('['), yellow(node.children.length), dim(']'))
} else if (typeof node.value === 'string') {
result.push(dim(': '), green(JSON.stringify(node.value)))
}
if (location) {
result.push(' (', location, ')')
}
for (key in node) {
value = node[key]
if (
ignore.indexOf(key) !== -1 ||
value === null ||
value === undefined ||
(typeof value === 'object' && isEmpty(value))
) {
continue
}
attributes.push('[' + key + '=' + JSON.stringify(value) + ']')
}
if (attributes.length !== 0) {
result = result.concat(' ', attributes)
}
return result.join('')
}
}
// Compile a position.
function stringifyPosition(start, end) {
var result = []
var positions = []
var offsets = []
add(start)
add(end)
if (positions.length !== 0) {
result.push(positions.join('-'))
}
if (offsets.length !== 0) {
result.push(offsets.join('-'))
}
return result.join(', ')
// Add a position.
function add(position) {
var tuple = stringifyPoint(position)
if (tuple) {
positions.push(tuple[0])
if (tuple[1]) {
offsets.push(tuple[1])
}
}
}
}
// Compile a point.
function stringifyPoint(value) {
var result = []
if (!value) {
return null
}
result = [[value.line || 1, value.column || 1].join(':')]
if ('offset' in value) {
result.push(String(value.offset || 0))
}
return result
}
// Remove ANSI color from `value`.
function stripColor(value) {
return value.replace(colorExpression, '')
}
// Factory to wrap values in ANSI colours.
function ansiColor(open, close) {
return color
function color(value) {
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
}
}