1 |
|
2 |
|
3 |
|
4 |
|
5 |
|
6 |
|
7 |
|
8 |
|
9 |
|
10 |
|
11 |
|
12 |
|
13 |
|
14 |
|
15 |
|
16 |
|
17 | import {color} from 'unist-util-inspect/do-not-use-conditional-color'
|
18 |
|
19 |
|
20 |
|
21 |
|
22 |
|
23 |
|
24 |
|
25 |
|
26 |
|
27 |
|
28 |
|
29 |
|
30 | export const inspect = color ? inspectColor : inspectNoColor
|
31 |
|
32 | const own = {}.hasOwnProperty
|
33 |
|
34 | const bold = ansiColor(1, 22)
|
35 | const dim = ansiColor(2, 22)
|
36 | const yellow = ansiColor(33, 39)
|
37 | const green = ansiColor(32, 39)
|
38 |
|
39 |
|
40 |
|
41 | const colorExpression =
|
42 | /(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g
|
43 |
|
44 |
|
45 |
|
46 |
|
47 |
|
48 |
|
49 |
|
50 |
|
51 |
|
52 |
|
53 |
|
54 |
|
55 | export function inspectNoColor(tree, options) {
|
56 | return inspectColor(tree, options).replace(colorExpression, '')
|
57 | }
|
58 |
|
59 |
|
60 |
|
61 |
|
62 |
|
63 |
|
64 |
|
65 |
|
66 |
|
67 |
|
68 |
|
69 | export function inspectColor(tree, options) {
|
70 |
|
71 | const state = {
|
72 | showPositions:
|
73 | !options ||
|
74 | options.showPositions === null ||
|
75 | options.showPositions === undefined
|
76 | ? true
|
77 | : options.showPositions
|
78 | }
|
79 |
|
80 | return inspectValue(tree, state)
|
81 | }
|
82 |
|
83 |
|
84 |
|
85 |
|
86 |
|
87 |
|
88 |
|
89 |
|
90 |
|
91 |
|
92 |
|
93 | function inspectValue(node, state) {
|
94 | if (isArrayUnknown(node)) {
|
95 | return inspectNodes(node, state)
|
96 | }
|
97 |
|
98 | if (isNode(node)) {
|
99 | return inspectTree(node, state)
|
100 | }
|
101 |
|
102 | return inspectNonTree(node)
|
103 | }
|
104 |
|
105 |
|
106 |
|
107 |
|
108 |
|
109 |
|
110 |
|
111 |
|
112 |
|
113 | function inspectNonTree(value) {
|
114 | return JSON.stringify(value)
|
115 | }
|
116 |
|
117 |
|
118 |
|
119 |
|
120 |
|
121 |
|
122 |
|
123 |
|
124 |
|
125 |
|
126 |
|
127 | function inspectNodes(nodes, state) {
|
128 | const size = String(nodes.length - 1).length
|
129 |
|
130 | const result = []
|
131 | let index = -1
|
132 |
|
133 | while (++index < nodes.length) {
|
134 | result.push(
|
135 | dim(
|
136 | (index < nodes.length - 1 ? '├' : '└') +
|
137 | '─' +
|
138 | String(index).padEnd(size)
|
139 | ) +
|
140 | ' ' +
|
141 | indent(
|
142 | inspectValue(nodes[index], state),
|
143 | (index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2),
|
144 | true
|
145 | )
|
146 | )
|
147 | }
|
148 |
|
149 | return result.join('\n')
|
150 | }
|
151 |
|
152 |
|
153 |
|
154 |
|
155 |
|
156 |
|
157 |
|
158 |
|
159 |
|
160 |
|
161 |
|
162 |
|
163 | function inspectFields(object, state) {
|
164 |
|
165 | const result = []
|
166 |
|
167 | let key
|
168 |
|
169 | for (key in object) {
|
170 |
|
171 | if (!own.call(object, key)) continue
|
172 |
|
173 | const value = object[key]
|
174 |
|
175 | let formatted
|
176 |
|
177 | if (
|
178 | value === undefined ||
|
179 |
|
180 |
|
181 | key === 'type' ||
|
182 | key === 'value' ||
|
183 | key === 'children' ||
|
184 | key === 'position' ||
|
185 |
|
186 | (typeof value === 'string' && (key === 'name' || key === 'tagName'))
|
187 | ) {
|
188 | continue
|
189 | }
|
190 |
|
191 |
|
192 | if (
|
193 | isNode(value) &&
|
194 | key !== 'data' &&
|
195 | key !== 'attributes' &&
|
196 | key !== 'properties'
|
197 | ) {
|
198 | formatted = inspectTree(value, state)
|
199 | }
|
200 |
|
201 | else if (value && isArrayUnknown(value) && isNode(value[0])) {
|
202 | formatted = '\n' + inspectNodes(value, state)
|
203 | } else {
|
204 | formatted = inspectNonTree(value)
|
205 | }
|
206 |
|
207 | result.push(
|
208 | key + dim(':') + (/\s/.test(formatted.charAt(0)) ? '' : ' ') + formatted
|
209 | )
|
210 | }
|
211 |
|
212 | return indent(
|
213 | result.join('\n'),
|
214 | (isArrayUnknown(object.children) && object.children.length > 0
|
215 | ? dim('│')
|
216 | : ' ') + ' '
|
217 | )
|
218 | }
|
219 |
|
220 |
|
221 |
|
222 |
|
223 |
|
224 |
|
225 |
|
226 |
|
227 |
|
228 |
|
229 |
|
230 | function inspectTree(node, state) {
|
231 | const result = [formatNode(node, state)]
|
232 |
|
233 | const map = (
|
234 | (node)
|
235 | )
|
236 | const fields = inspectFields(map, state)
|
237 | const content = isArrayUnknown(map.children)
|
238 | ? inspectNodes(map.children, state)
|
239 | : ''
|
240 | if (fields) result.push(fields)
|
241 | if (content) result.push(content)
|
242 | return result.join('\n')
|
243 | }
|
244 |
|
245 |
|
246 |
|
247 |
|
248 |
|
249 |
|
250 |
|
251 |
|
252 |
|
253 |
|
254 |
|
255 | function formatNode(node, state) {
|
256 | const result = [bold(node.type)]
|
257 |
|
258 | const map = (
|
259 | (node)
|
260 | )
|
261 | const kind = map.tagName || map.name
|
262 | const position = state.showPositions ? stringifyPosition(node.position) : ''
|
263 |
|
264 | if (typeof kind === 'string') {
|
265 | result.push('<', kind, '>')
|
266 | }
|
267 |
|
268 | if (isArrayUnknown(map.children)) {
|
269 | result.push(dim('['), yellow(String(map.children.length)), dim(']'))
|
270 | } else if (typeof map.value === 'string') {
|
271 | result.push(' ', green(inspectNonTree(map.value)))
|
272 | }
|
273 |
|
274 | if (position) {
|
275 | result.push(' ', dim('('), position, dim(')'))
|
276 | }
|
277 |
|
278 | return result.join('')
|
279 | }
|
280 |
|
281 |
|
282 |
|
283 |
|
284 |
|
285 |
|
286 |
|
287 |
|
288 |
|
289 |
|
290 |
|
291 |
|
292 |
|
293 | function indent(value, indentation, ignoreFirst) {
|
294 | if (!value) return value
|
295 |
|
296 | const lines = value.split('\n')
|
297 | let index = ignoreFirst ? 0 : -1
|
298 |
|
299 | while (++index < lines.length) {
|
300 | lines[index] = indentation + lines[index]
|
301 | }
|
302 |
|
303 | return lines.join('\n')
|
304 | }
|
305 |
|
306 |
|
307 |
|
308 |
|
309 |
|
310 |
|
311 |
|
312 |
|
313 |
|
314 | function stringifyPosition(value) {
|
315 |
|
316 | const result = []
|
317 |
|
318 | const positions = []
|
319 |
|
320 | const offsets = []
|
321 |
|
322 | if (value && typeof value === 'object') {
|
323 | point('start' in value ? value.start : undefined)
|
324 | point('end' in value ? value.end : undefined)
|
325 | }
|
326 |
|
327 | if (positions.length > 0) result.push(positions.join('-'))
|
328 | if (offsets.length > 0) result.push(offsets.join('-'))
|
329 |
|
330 | return result.join(', ')
|
331 |
|
332 | |
333 |
|
334 |
|
335 |
|
336 |
|
337 |
|
338 | function point(value) {
|
339 | if (value && typeof value === 'object') {
|
340 | const line =
|
341 | 'line' in value && typeof value.line === 'number' ? value.line : 1
|
342 | const column =
|
343 | 'column' in value && typeof value.column === 'number' ? value.column : 1
|
344 |
|
345 | positions.push(line + ':' + column)
|
346 |
|
347 | if ('offset' in value && typeof value.offset === 'number') {
|
348 | offsets.push(String(value.offset || 0))
|
349 | }
|
350 | }
|
351 | }
|
352 | }
|
353 |
|
354 |
|
355 |
|
356 |
|
357 |
|
358 |
|
359 |
|
360 |
|
361 |
|
362 |
|
363 |
|
364 | function ansiColor(open, close) {
|
365 | return color
|
366 |
|
367 | |
368 |
|
369 |
|
370 |
|
371 |
|
372 |
|
373 |
|
374 |
|
375 | function color(value) {
|
376 | return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm'
|
377 | }
|
378 | }
|
379 |
|
380 |
|
381 |
|
382 |
|
383 |
|
384 | function isNode(value) {
|
385 | return Boolean(
|
386 | value &&
|
387 | typeof value === 'object' &&
|
388 | 'type' in value &&
|
389 | typeof value.type === 'string'
|
390 | )
|
391 | }
|
392 |
|
393 |
|
394 |
|
395 |
|
396 |
|
397 | function isArrayUnknown(node) {
|
398 | return Array.isArray(node)
|
399 | }
|