@@ -15,110 +15,133 @@ var yellow = ansiColor(33, 39)
15
15
var green = ansiColor ( 32 , 39 )
16
16
17
17
// Define ANSII color removal functionality.
18
- var colorExpression = / (?: (?: \u001B \[ ) | \u009b ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001b [ A - M ] / g
18
+ var colorExpression = / (?: (?: \u001B \[ ) | \u009B ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001B [ A - M ] / g
19
19
20
20
// Standard keys defined by unist: https://github.com/syntax-tree/unist.
21
21
// We don’t ignore `data` though.
22
22
var ignore = [ 'type' , 'value' , 'children' , 'position' ]
23
23
24
24
// Inspects a node, without using color.
25
- function noColor ( node , pad ) {
26
- return stripColor ( inspect ( node , pad ) )
25
+ function noColor ( node ) {
26
+ return stripColor ( inspect ( node ) )
27
27
}
28
28
29
29
// Inspects a node.
30
- function inspect ( node , pad ) {
31
- var result
32
- var children
33
- var index
34
- var length
30
+ function inspect ( node ) {
31
+ return inspectValue ( node , '' )
35
32
36
- if ( node && Boolean ( node . length ) && typeof node !== 'string' ) {
37
- length = node . length
38
- index = - 1
39
- result = [ ]
33
+ function inspectValue ( node , pad ) {
34
+ if ( node && Boolean ( node . length ) && typeof node === 'object' ) {
35
+ return inspectAll ( node , pad )
36
+ }
40
37
41
- while ( ++ index < length ) {
42
- result [ index ] = inspect ( node [ index ] )
38
+ if ( node && node . type ) {
39
+ return inspectTree ( node , pad )
43
40
}
44
41
45
- return result . join ( '\n' )
42
+ return inspectNonTree ( node , pad )
46
43
}
47
44
48
- if ( ! node || ! node . type ) {
49
- return String ( node )
45
+ function inspectNonTree ( value , pad ) {
46
+ return formatNesting ( pad ) + String ( value )
50
47
}
51
48
52
- result = [ formatNode ( node ) ]
53
- children = node . children
54
- length = children && children . length
55
- index = - 1
49
+ function inspectAll ( nodes , pad ) {
50
+ var length = nodes . length
51
+ var index = - 1
52
+ var result = [ ]
53
+ var node
54
+ var tail
55
+
56
+ while ( ++ index < length ) {
57
+ node = nodes [ index ]
58
+ tail = index === length - 1
59
+
60
+ result . push (
61
+ formatNesting ( pad + ( tail ? '└' : '├' ) + '─ ' ) ,
62
+ inspectValue ( node , pad + ( tail ? ' ' : '│' ) + ' ' ) ,
63
+ tail ? '' : '\n'
64
+ )
65
+ }
56
66
57
- if ( ! length ) {
58
- return result [ 0 ]
67
+ return result . join ( '' )
59
68
}
60
69
61
- if ( ! pad || typeof pad === 'number' ) {
62
- pad = ''
70
+ function inspectTree ( node , pad ) {
71
+ var result = formatNode ( node , pad )
72
+ var content = inspectAll ( node . children || [ ] , pad )
73
+ return content ? result + '\n' + content : result
63
74
}
64
75
65
- while ( ++ index < length ) {
66
- node = children [ index ]
76
+ // Colored nesting formatter.
77
+ function formatNesting ( value ) {
78
+ return value ? dim ( value ) : ''
79
+ }
67
80
68
- if ( index === length - 1 ) {
69
- result . push ( formatNesting ( pad + '└─ ' ) + inspect ( node , pad + ' ' ) )
70
- } else {
71
- result . push ( formatNesting ( pad + '├─ ' ) + inspect ( node , pad + '│ ' ) )
81
+ // Colored node formatter.
82
+ function formatNode ( node ) {
83
+ var result = [ node . type ]
84
+ var position = node . position || { }
85
+ var location = stringifyPosition ( position . start , position . end )
86
+ var attributes = [ ]
87
+ var key
88
+ var value
89
+
90
+ if ( node . children ) {
91
+ result . push ( dim ( '[' ) , yellow ( node . children . length ) , dim ( ']' ) )
92
+ } else if ( typeof node . value === 'string' ) {
93
+ result . push ( dim ( ': ' ) , green ( JSON . stringify ( node . value ) ) )
72
94
}
73
- }
74
95
75
- return result . join ( '\n' )
76
- }
96
+ if ( location ) {
97
+ result . push ( ' (' , location , ')' )
98
+ }
77
99
78
- // Colored nesting formatter.
79
- function formatNesting ( value ) {
80
- return dim ( value )
81
- }
100
+ for ( key in node ) {
101
+ value = node [ key ]
82
102
83
- // Compile a single position.
84
- function compile ( pos ) {
85
- var values = [ ]
103
+ if (
104
+ ignore . indexOf ( key ) !== - 1 ||
105
+ value === null ||
106
+ value === undefined ||
107
+ ( typeof value === 'object' && isEmpty ( value ) )
108
+ ) {
109
+ continue
110
+ }
86
111
87
- if ( ! pos ) {
88
- return null
89
- }
112
+ attributes . push ( '[' + key + '=' + JSON . stringify ( value ) + ']' )
113
+ }
90
114
91
- values = [ [ pos . line || 1 , pos . column || 1 ] . join ( ':' ) ]
115
+ if ( attributes . length !== 0 ) {
116
+ result = result . concat ( ' ' , attributes )
117
+ }
92
118
93
- if ( 'offset' in pos ) {
94
- values . push ( String ( pos . offset || 0 ) )
119
+ return result . join ( '' )
95
120
}
96
-
97
- return values
98
121
}
99
122
100
- // Compile a location .
101
- function stringify ( start , end ) {
102
- var values = [ ]
123
+ // Compile a position .
124
+ function stringifyPosition ( start , end ) {
125
+ var result = [ ]
103
126
var positions = [ ]
104
127
var offsets = [ ]
105
128
106
129
add ( start )
107
130
add ( end )
108
131
109
132
if ( positions . length !== 0 ) {
110
- values . push ( positions . join ( '-' ) )
133
+ result . push ( positions . join ( '-' ) )
111
134
}
112
135
113
136
if ( offsets . length !== 0 ) {
114
- values . push ( offsets . join ( '-' ) )
137
+ result . push ( offsets . join ( '-' ) )
115
138
}
116
139
117
- return values . join ( ', ' )
140
+ return result . join ( ', ' )
118
141
119
142
// Add a position.
120
143
function add ( position ) {
121
- var tuple = compile ( position )
144
+ var tuple = stringifyPoint ( position )
122
145
123
146
if ( tuple ) {
124
147
positions . push ( tuple [ 0 ] )
@@ -130,48 +153,24 @@ function stringify(start, end) {
130
153
}
131
154
}
132
155
133
- // Colored node formatter.
134
- function formatNode ( node ) {
135
- var log = node . type
136
- var location = node . position || { }
137
- var position = stringify ( location . start , location . end )
138
- var key
139
- var values = [ ]
140
- var value
141
-
142
- if ( node . children ) {
143
- log += dim ( '[' ) + yellow ( node . children . length ) + dim ( ']' )
144
- } else if ( typeof node . value === 'string' ) {
145
- log += dim ( ': ' ) + green ( JSON . stringify ( node . value ) )
146
- }
156
+ // Compile a point.
157
+ function stringifyPoint ( value ) {
158
+ var result = [ ]
147
159
148
- if ( position ) {
149
- log += ' (' + position + ')'
160
+ if ( ! value ) {
161
+ return null
150
162
}
151
163
152
- for ( key in node ) {
153
- value = node [ key ]
154
-
155
- if (
156
- ignore . indexOf ( key ) !== - 1 ||
157
- value === null ||
158
- value === undefined ||
159
- ( typeof value === 'object' && isEmpty ( value ) )
160
- ) {
161
- continue
162
- }
163
-
164
- values . push ( '[' + key + '=' + JSON . stringify ( value ) + ']' )
165
- }
164
+ result = [ [ value . line || 1 , value . column || 1 ] . join ( ':' ) ]
166
165
167
- if ( values . length !== 0 ) {
168
- log += ' ' + values . join ( '' )
166
+ if ( 'offset' in value ) {
167
+ result . push ( String ( value . offset || 0 ) )
169
168
}
170
169
171
- return log
170
+ return result
172
171
}
173
172
174
- // Remove ANSI colour from `value`.
173
+ // Remove ANSI color from `value`.
175
174
function stripColor ( value ) {
176
175
return value . replace ( colorExpression , '' )
177
176
}
0 commit comments