|
1 | | -'use strict'; |
| 1 | +'use strict' |
2 | 2 |
|
3 | | -var isEmpty = require('is-empty'); |
| 3 | +var isEmpty = require('is-empty') |
4 | 4 |
|
5 | 5 | /* Detect color support. */ |
6 | 6 |
|
7 | | -var color = true; |
| 7 | +var color = true |
8 | 8 |
|
9 | 9 | try { |
10 | | - color = 'inspect' in require('util'); |
| 10 | + color = 'inspect' in require('util') |
11 | 11 | } catch (err) { |
12 | 12 | /* istanbul ignore next - browser */ |
13 | | - color = false; |
| 13 | + color = false |
14 | 14 | } |
15 | 15 |
|
16 | | -module.exports = color ? inspect : /* istanbul ignore next */ noColor; |
| 16 | +module.exports = color ? inspect : /* istanbul ignore next */ noColor |
17 | 17 |
|
18 | | -inspect.color = inspect; |
19 | | -noColor.color = inspect; |
20 | | -inspect.noColor = noColor; |
21 | | -noColor.noColor = noColor; |
| 18 | +inspect.color = inspect |
| 19 | +noColor.color = inspect |
| 20 | +inspect.noColor = noColor |
| 21 | +noColor.noColor = noColor |
22 | 22 |
|
23 | | -var dim = ansiColor(2, 22); |
24 | | -var yellow = ansiColor(33, 39); |
25 | | -var green = ansiColor(32, 39); |
| 23 | +var dim = ansiColor(2, 22) |
| 24 | +var yellow = ansiColor(33, 39) |
| 25 | +var green = ansiColor(32, 39) |
26 | 26 |
|
27 | 27 | /* Define ANSII color removal functionality. */ |
28 | 28 | var COLOR_EXPRESSION = new RegExp( |
29 | 29 | '(?:' + |
30 | 30 | '(?:\\u001b\\[)|' + |
31 | 31 | '\\u009b' + |
32 | | - ')' + |
33 | | - '(?:' + |
| 32 | + ')' + |
| 33 | + '(?:' + |
34 | 34 | '(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m]' + |
35 | | - ')|' + |
36 | | - '\\u001b[A-M]', |
| 35 | + ')|' + |
| 36 | + '\\u001b[A-M]', |
37 | 37 | 'g' |
38 | | -); |
39 | | - |
40 | | -/* Constants. */ |
41 | | -var CHAR_HORIZONTAL_LINE = '─'; |
42 | | -var CHAR_VERTICAL_LINE = '│'; |
43 | | -var CHAR_SPLIT = '└'; |
44 | | -var CHAR_CONTINUE_AND_SPLIT = '├'; |
45 | | -var CONTINUE = CHAR_CONTINUE_AND_SPLIT + CHAR_HORIZONTAL_LINE + ' '; |
46 | | -var STOP = CHAR_SPLIT + CHAR_HORIZONTAL_LINE + ' '; |
| 38 | +) |
47 | 39 |
|
48 | 40 | /* Standard keys defined by unist: |
49 | 41 | * https://github.com/syntax-tree/unist. |
50 | | - * We don‘t include `data` though. */ |
51 | | -var ignore = [ |
52 | | - 'type', |
53 | | - 'value', |
54 | | - 'children', |
55 | | - 'position' |
56 | | -]; |
| 42 | + * We don’t ignore `data` though. */ |
| 43 | +var ignore = ['type', 'value', 'children', 'position'] |
57 | 44 |
|
58 | 45 | /* Inspects a node, without using color. */ |
59 | 46 | function noColor(node, pad) { |
60 | | - return stripColor(inspect(node, pad)); |
| 47 | + return stripColor(inspect(node, pad)) |
61 | 48 | } |
62 | 49 |
|
63 | 50 | /* Inspects a node. */ |
64 | 51 | function inspect(node, pad) { |
65 | | - var result; |
66 | | - var children; |
67 | | - var index; |
68 | | - var length; |
| 52 | + var result |
| 53 | + var children |
| 54 | + var index |
| 55 | + var length |
69 | 56 |
|
70 | 57 | if (node && Boolean(node.length) && typeof node !== 'string') { |
71 | | - length = node.length; |
72 | | - index = -1; |
73 | | - result = []; |
| 58 | + length = node.length |
| 59 | + index = -1 |
| 60 | + result = [] |
74 | 61 |
|
75 | 62 | while (++index < length) { |
76 | | - result[index] = inspect(node[index]); |
| 63 | + result[index] = inspect(node[index]) |
77 | 64 | } |
78 | 65 |
|
79 | | - return result.join('\n'); |
| 66 | + return result.join('\n') |
80 | 67 | } |
81 | 68 |
|
82 | 69 | if (!node || !node.type) { |
83 | | - return String(node); |
| 70 | + return String(node) |
84 | 71 | } |
85 | 72 |
|
86 | | - result = [formatNode(node)]; |
87 | | - children = node.children; |
88 | | - length = children && children.length; |
89 | | - index = -1; |
| 73 | + result = [formatNode(node)] |
| 74 | + children = node.children |
| 75 | + length = children && children.length |
| 76 | + index = -1 |
90 | 77 |
|
91 | 78 | if (!length) { |
92 | | - return result[0]; |
| 79 | + return result[0] |
93 | 80 | } |
94 | 81 |
|
95 | 82 | if (!pad || typeof pad === 'number') { |
96 | | - pad = ''; |
| 83 | + pad = '' |
97 | 84 | } |
98 | 85 |
|
99 | 86 | while (++index < length) { |
100 | | - node = children[index]; |
| 87 | + node = children[index] |
101 | 88 |
|
102 | 89 | if (index === length - 1) { |
103 | | - result.push(formatNesting(pad + STOP) + inspect(node, pad + ' ')); |
| 90 | + result.push(formatNesting(pad + '└─ ') + inspect(node, pad + ' ')) |
104 | 91 | } else { |
105 | | - result.push(formatNesting(pad + CONTINUE) + inspect(node, pad + CHAR_VERTICAL_LINE + ' ')); |
| 92 | + result.push(formatNesting(pad + '├─ ') + inspect(node, pad + '│ ')) |
106 | 93 | } |
107 | 94 | } |
108 | 95 |
|
109 | | - return result.join('\n'); |
| 96 | + return result.join('\n') |
110 | 97 | } |
111 | 98 |
|
112 | 99 | /* Colored nesting formatter. */ |
113 | 100 | function formatNesting(value) { |
114 | | - return dim(value); |
| 101 | + return dim(value) |
115 | 102 | } |
116 | 103 |
|
117 | 104 | /* Compile a single position. */ |
118 | 105 | function compile(pos) { |
119 | | - var values = []; |
| 106 | + var values = [] |
120 | 107 |
|
121 | 108 | if (!pos) { |
122 | | - return null; |
| 109 | + return null |
123 | 110 | } |
124 | 111 |
|
125 | | - values = [ |
126 | | - [pos.line || 1, pos.column || 1].join(':') |
127 | | - ]; |
| 112 | + values = [[pos.line || 1, pos.column || 1].join(':')] |
128 | 113 |
|
129 | 114 | if ('offset' in pos) { |
130 | | - values.push(String(pos.offset || 0)); |
| 115 | + values.push(String(pos.offset || 0)) |
131 | 116 | } |
132 | 117 |
|
133 | | - return values; |
| 118 | + return values |
134 | 119 | } |
135 | 120 |
|
136 | 121 | /* Compile a location. */ |
137 | 122 | function stringify(start, end) { |
138 | | - var values = []; |
139 | | - var positions = []; |
140 | | - var offsets = []; |
| 123 | + var values = [] |
| 124 | + var positions = [] |
| 125 | + var offsets = [] |
141 | 126 |
|
142 | | - add(start); |
143 | | - add(end); |
| 127 | + add(start) |
| 128 | + add(end) |
144 | 129 |
|
145 | 130 | if (positions.length !== 0) { |
146 | | - values.push(positions.join('-')); |
| 131 | + values.push(positions.join('-')) |
147 | 132 | } |
148 | 133 |
|
149 | 134 | if (offsets.length !== 0) { |
150 | | - values.push(offsets.join('-')); |
| 135 | + values.push(offsets.join('-')) |
151 | 136 | } |
152 | 137 |
|
153 | | - return values.join(', '); |
| 138 | + return values.join(', ') |
154 | 139 |
|
155 | 140 | /* Add a position. */ |
156 | 141 | function add(position) { |
157 | | - var tuple = compile(position); |
| 142 | + var tuple = compile(position) |
158 | 143 |
|
159 | 144 | if (tuple) { |
160 | | - positions.push(tuple[0]); |
| 145 | + positions.push(tuple[0]) |
161 | 146 |
|
162 | 147 | if (tuple[1]) { |
163 | | - offsets.push(tuple[1]); |
| 148 | + offsets.push(tuple[1]) |
164 | 149 | } |
165 | 150 | } |
166 | 151 | } |
167 | 152 | } |
168 | 153 |
|
169 | 154 | /* Colored node formatter. */ |
170 | 155 | function formatNode(node) { |
171 | | - var log = node.type; |
172 | | - var location = node.position || {}; |
173 | | - var position = stringify(location.start, location.end); |
174 | | - var key; |
175 | | - var values = []; |
176 | | - var value; |
| 156 | + var log = node.type |
| 157 | + var location = node.position || {} |
| 158 | + var position = stringify(location.start, location.end) |
| 159 | + var key |
| 160 | + var values = [] |
| 161 | + var value |
177 | 162 |
|
178 | 163 | if (node.children) { |
179 | | - log += dim('[') + yellow(node.children.length) + dim(']'); |
| 164 | + log += dim('[') + yellow(node.children.length) + dim(']') |
180 | 165 | } else if (typeof node.value === 'string') { |
181 | | - log += dim(': ') + green(JSON.stringify(node.value)); |
| 166 | + log += dim(': ') + green(JSON.stringify(node.value)) |
182 | 167 | } |
183 | 168 |
|
184 | 169 | if (position) { |
185 | | - log += ' (' + position + ')'; |
| 170 | + log += ' (' + position + ')' |
186 | 171 | } |
187 | 172 |
|
188 | 173 | for (key in node) { |
189 | | - value = node[key]; |
| 174 | + value = node[key] |
190 | 175 |
|
191 | 176 | if ( |
192 | 177 | ignore.indexOf(key) !== -1 || |
193 | 178 | value === null || |
194 | 179 | value === undefined || |
195 | 180 | (typeof value === 'object' && isEmpty(value)) |
196 | 181 | ) { |
197 | | - continue; |
| 182 | + continue |
198 | 183 | } |
199 | 184 |
|
200 | | - values.push('[' + key + '=' + JSON.stringify(value) + ']'); |
| 185 | + values.push('[' + key + '=' + JSON.stringify(value) + ']') |
201 | 186 | } |
202 | 187 |
|
203 | 188 | if (values.length !== 0) { |
204 | | - log += ' ' + values.join(''); |
| 189 | + log += ' ' + values.join('') |
205 | 190 | } |
206 | 191 |
|
207 | | - return log; |
| 192 | + return log |
208 | 193 | } |
209 | 194 |
|
210 | 195 | /* Remove ANSI colour from `value`. */ |
211 | 196 | function stripColor(value) { |
212 | | - return value.replace(COLOR_EXPRESSION, ''); |
| 197 | + return value.replace(COLOR_EXPRESSION, '') |
213 | 198 | } |
214 | 199 |
|
215 | 200 | /* Factory to wrap values in ANSI colours. */ |
216 | 201 | function ansiColor(open, close) { |
217 | | - return color; |
| 202 | + return color |
218 | 203 |
|
219 | 204 | function color(value) { |
220 | | - return '\u001b[' + open + 'm' + value + '\u001b[' + close + 'm'; |
| 205 | + return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm' |
221 | 206 | } |
222 | 207 | } |
0 commit comments