@@ -4,46 +4,31 @@ var color = require('./color')
4
4
5
5
module . exports = color ? inspect : /* istanbul ignore next */ noColor
6
6
7
- inspect . color = inspect
8
- noColor . color = inspect
9
- inspect . noColor = noColor
10
- noColor . noColor = noColor
7
+ inspect . color = noColor . color = inspect
8
+ inspect . noColor = noColor . noColor = noColor
11
9
12
10
var bold = ansiColor ( 1 , 22 )
13
11
var dim = ansiColor ( 2 , 22 )
14
12
var yellow = ansiColor ( 33 , 39 )
15
13
var green = ansiColor ( 32 , 39 )
16
14
17
- // Define ANSII color removal functionality .
15
+ // ANSI color regex .
18
16
var colorExpression = / (?: (?: \u001B \[ ) | \u009B ) (?: \d { 1 , 3 } ) ? (?: (?: ; \d { 0 , 3 } ) * ) ? [ A - M | f - m ] | \u001B [ A - M ] / g
19
17
20
- // Standard keys defined by unist (<https://github.com/syntax-tree/unist>) that
21
- // we format differently.
22
- // We don’t ignore `data` though.
23
- // Also includes `name` (from xast) and `tagName` (from `hast`).
24
- var ignore = [ 'type' , 'value' , 'children' , 'position' ]
25
- var ignoreString = [ 'name' , 'tagName' ]
26
-
27
- var dataOnly = [ 'data' , 'attributes' , 'properties' ]
28
-
29
18
// Inspects a node, without using color.
30
19
function noColor ( node ) {
31
- return stripColor ( inspect ( node ) )
20
+ return inspect ( node ) . replace ( colorExpression , '' )
32
21
}
33
22
34
23
// Inspects a node.
35
- function inspect ( node , options ) {
36
- var settings = options || { }
37
- var showPositions = settings . showPositions
38
-
39
- if ( showPositions === null || showPositions === undefined ) {
40
- showPositions = true
41
- }
24
+ function inspect ( tree , options ) {
25
+ var positions =
26
+ ! options || options . showPositions == null ? true : options . showPositions
42
27
43
- return inspectValue ( node )
28
+ return inspectValue ( tree )
44
29
45
30
function inspectValue ( node ) {
46
- if ( node && Boolean ( node . length ) && typeof node === 'object' ) {
31
+ if ( node && typeof node === 'object' && 'length' in node ) {
47
32
return inspectNodes ( node )
48
33
}
49
34
@@ -59,30 +44,25 @@ function inspect(node, options) {
59
44
}
60
45
61
46
function inspectNodes ( nodes ) {
62
- var length = nodes . length
63
- var index = - 1
64
47
var result = [ ]
65
- var node
66
- var tail
67
- var value
68
-
69
- while ( ++ index < length ) {
70
- node = nodes [ index ]
71
- tail = index === length - 1
72
-
73
- value =
74
- dim ( ( tail ? '└' : '├' ) + '─' + index ) +
75
- ' ' +
76
- indent ( inspectValue ( node ) , ( tail ? ' ' : dim ( '│' ) ) + ' ' , true )
48
+ var index = - 1
77
49
78
- result . push ( value )
50
+ while ( ++ index < nodes . length ) {
51
+ result . push (
52
+ dim ( ( index < nodes . length - 1 ? '├' : '└' ) + '─' + index ) +
53
+ ' ' +
54
+ indent (
55
+ inspectValue ( nodes [ index ] ) ,
56
+ ( index < nodes . length - 1 ? dim ( '│' ) : ' ' ) + ' ' ,
57
+ true
58
+ )
59
+ )
79
60
}
80
61
81
62
return result . join ( '\n' )
82
63
}
83
64
84
65
function inspectFields ( object ) {
85
- var nonEmpty = object . children && object . children . length
86
66
var result = [ ]
87
67
var key
88
68
var value
@@ -93,24 +73,36 @@ function inspect(node, options) {
93
73
94
74
if (
95
75
value === undefined ||
96
- ignore . indexOf ( key ) !== - 1 ||
97
- ( ignoreString . indexOf ( key ) !== - 1 && typeof value === 'string' )
76
+ // Standard keys defined by unist that we format differently.
77
+ // <https://github.com/syntax-tree/unist>
78
+ key === 'type' ||
79
+ key === 'value' ||
80
+ key === 'children' ||
81
+ key === 'position' ||
82
+ // Ignore `name` (from xast) and `tagName` (from `hast`) when string.
83
+ ( typeof value === 'string' && ( key === 'name' || key === 'tagName' ) )
98
84
) {
99
85
continue
100
86
}
101
87
88
+ // A single node.
102
89
if (
103
90
value &&
104
91
typeof value === 'object' &&
105
- typeof value . type === 'string' &&
106
- dataOnly . indexOf ( key ) === - 1
92
+ value . type &&
93
+ key !== 'data' &&
94
+ key !== 'attributes' &&
95
+ key !== 'properties'
107
96
) {
108
97
formatted = inspectTree ( value )
109
- } else if (
110
- Array . isArray ( value ) &&
98
+ }
99
+ // A list of nodes.
100
+ else if (
101
+ value &&
102
+ typeof value === 'object' &&
103
+ 'length' in value &&
111
104
value [ 0 ] &&
112
- typeof value [ 0 ] === 'object' &&
113
- typeof value [ 0 ] . type === 'string'
105
+ value [ 0 ] . type
114
106
) {
115
107
formatted = '\n' + inspectNodes ( value )
116
108
} else {
@@ -122,7 +114,10 @@ function inspect(node, options) {
122
114
)
123
115
}
124
116
125
- return indent ( result . join ( '\n' ) , ( nonEmpty ? dim ( '│' ) : ' ' ) + ' ' )
117
+ return indent (
118
+ result . join ( '\n' ) ,
119
+ ( object . children && object . children . length ? dim ( '│' ) : ' ' ) + ' '
120
+ )
126
121
}
127
122
128
123
function inspectTree ( node , pad ) {
@@ -138,10 +133,7 @@ function inspect(node, options) {
138
133
function formatNode ( node ) {
139
134
var result = [ bold ( node . type ) ]
140
135
var kind = node . tagName || node . name
141
- var position = node . position || { }
142
- var location = showPositions
143
- ? stringifyPosition ( position . start , position . end )
144
- : ''
136
+ var position = positions ? stringifyPosition ( node . position ) : ''
145
137
146
138
if ( typeof kind === 'string' ) {
147
139
result . push ( '<' , kind , '>' )
@@ -153,8 +145,8 @@ function inspect(node, options) {
153
145
result . push ( ' ' , green ( inspectNonTree ( node . value , '' ) ) )
154
146
}
155
147
156
- if ( location ) {
157
- result . push ( ' ' , dim ( '(' ) , location , dim ( ')' ) )
148
+ if ( position ) {
149
+ result . push ( ' ' , dim ( '(' ) , position , dim ( ')' ) )
158
150
}
159
151
160
152
return result . join ( '' )
@@ -164,72 +156,42 @@ function inspect(node, options) {
164
156
function indent ( value , indentation , ignoreFirst ) {
165
157
var lines = value . split ( '\n' )
166
158
var index = ignoreFirst ? 0 : - 1
167
- var length = lines . length
168
159
169
- if ( value === '' ) return ''
160
+ if ( ! value ) return value
170
161
171
- while ( ++ index < length ) {
162
+ while ( ++ index < lines . length ) {
172
163
lines [ index ] = indentation + lines [ index ]
173
164
}
174
165
175
166
return lines . join ( '\n' )
176
167
}
177
168
178
169
// Compile a position.
179
- function stringifyPosition ( start , end ) {
170
+ function stringifyPosition ( value ) {
171
+ var position = value || { }
180
172
var result = [ ]
181
173
var positions = [ ]
182
174
var offsets = [ ]
183
175
184
- add ( start )
185
- add ( end )
176
+ point ( position . start )
177
+ point ( position . end )
186
178
187
- if ( positions . length > 0 ) {
188
- result . push ( positions . join ( '-' ) )
189
- }
190
-
191
- if ( offsets . length > 0 ) {
192
- result . push ( offsets . join ( '-' ) )
193
- }
179
+ if ( positions . length ) result . push ( positions . join ( '-' ) )
180
+ if ( offsets . length ) result . push ( offsets . join ( '-' ) )
194
181
195
182
return result . join ( ', ' )
196
183
197
- // Add a position.
198
- function add ( position ) {
199
- var tuple = stringifyPoint ( position )
184
+ function point ( value ) {
185
+ if ( value ) {
186
+ positions . push ( ( value . line || 1 ) + ':' + ( value . column || 1 ) )
200
187
201
- if ( tuple ) {
202
- positions . push ( tuple [ 0 ] )
203
-
204
- if ( tuple [ 1 ] ) {
205
- offsets . push ( tuple [ 1 ] )
188
+ if ( 'offset' in value ) {
189
+ offsets . push ( String ( value . offset || 0 ) )
206
190
}
207
191
}
208
192
}
209
193
}
210
194
211
- // Compile a point.
212
- function stringifyPoint ( value ) {
213
- var result = [ ]
214
-
215
- if ( ! value ) {
216
- return null
217
- }
218
-
219
- result = [ [ value . line || 1 , value . column || 1 ] . join ( ':' ) ]
220
-
221
- if ( 'offset' in value ) {
222
- result . push ( String ( value . offset || 0 ) )
223
- }
224
-
225
- return result
226
- }
227
-
228
- // Remove ANSI color from `value`.
229
- function stripColor ( value ) {
230
- return value . replace ( colorExpression , '' )
231
- }
232
-
233
195
// Factory to wrap values in ANSI colours.
234
196
function ansiColor ( open , close ) {
235
197
return color
0 commit comments