Skip to content

Commit 3c6bd4b

Browse files
committed
Refactor code
1 parent 229ae47 commit 3c6bd4b

File tree

2 files changed

+90
-91
lines changed

2 files changed

+90
-91
lines changed

index.js

Lines changed: 89 additions & 90 deletions
Original file line numberDiff line numberDiff line change
@@ -15,110 +15,133 @@ var yellow = ansiColor(33, 39)
1515
var green = ansiColor(32, 39)
1616

1717
// 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
1919

2020
// Standard keys defined by unist: https://github.com/syntax-tree/unist.
2121
// We don’t ignore `data` though.
2222
var ignore = ['type', 'value', 'children', 'position']
2323

2424
// 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))
2727
}
2828

2929
// 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, '')
3532

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+
}
4037

41-
while (++index < length) {
42-
result[index] = inspect(node[index])
38+
if (node && node.type) {
39+
return inspectTree(node, pad)
4340
}
4441

45-
return result.join('\n')
42+
return inspectNonTree(node, pad)
4643
}
4744

48-
if (!node || !node.type) {
49-
return String(node)
45+
function inspectNonTree(value, pad) {
46+
return formatNesting(pad) + String(value)
5047
}
5148

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+
}
5666

57-
if (!length) {
58-
return result[0]
67+
return result.join('')
5968
}
6069

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
6374
}
6475

65-
while (++index < length) {
66-
node = children[index]
76+
// Colored nesting formatter.
77+
function formatNesting(value) {
78+
return value ? dim(value) : ''
79+
}
6780

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)))
7294
}
73-
}
7495

75-
return result.join('\n')
76-
}
96+
if (location) {
97+
result.push(' (', location, ')')
98+
}
7799

78-
// Colored nesting formatter.
79-
function formatNesting(value) {
80-
return dim(value)
81-
}
100+
for (key in node) {
101+
value = node[key]
82102

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+
}
86111

87-
if (!pos) {
88-
return null
89-
}
112+
attributes.push('[' + key + '=' + JSON.stringify(value) + ']')
113+
}
90114

91-
values = [[pos.line || 1, pos.column || 1].join(':')]
115+
if (attributes.length !== 0) {
116+
result = result.concat(' ', attributes)
117+
}
92118

93-
if ('offset' in pos) {
94-
values.push(String(pos.offset || 0))
119+
return result.join('')
95120
}
96-
97-
return values
98121
}
99122

100-
// Compile a location.
101-
function stringify(start, end) {
102-
var values = []
123+
// Compile a position.
124+
function stringifyPosition(start, end) {
125+
var result = []
103126
var positions = []
104127
var offsets = []
105128

106129
add(start)
107130
add(end)
108131

109132
if (positions.length !== 0) {
110-
values.push(positions.join('-'))
133+
result.push(positions.join('-'))
111134
}
112135

113136
if (offsets.length !== 0) {
114-
values.push(offsets.join('-'))
137+
result.push(offsets.join('-'))
115138
}
116139

117-
return values.join(', ')
140+
return result.join(', ')
118141

119142
// Add a position.
120143
function add(position) {
121-
var tuple = compile(position)
144+
var tuple = stringifyPoint(position)
122145

123146
if (tuple) {
124147
positions.push(tuple[0])
@@ -130,48 +153,24 @@ function stringify(start, end) {
130153
}
131154
}
132155

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 = []
147159

148-
if (position) {
149-
log += ' (' + position + ')'
160+
if (!value) {
161+
return null
150162
}
151163

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(':')]
166165

167-
if (values.length !== 0) {
168-
log += ' ' + values.join('')
166+
if ('offset' in value) {
167+
result.push(String(value.offset || 0))
169168
}
170169

171-
return log
170+
return result
172171
}
173172

174-
// Remove ANSI colour from `value`.
173+
// Remove ANSI color from `value`.
175174
function stripColor(value) {
176175
return value.replace(colorExpression, '')
177176
}

test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ test('inspect()', function (t) {
8181
}
8282
])
8383
),
84-
['SymbolNode: "$"', 'WordNode[1]', '└─ text: "5,00"'].join('\n'),
84+
['├─ SymbolNode: "$"', '└─ WordNode[1]', ' └─ text: "5,00"'].join('\n'),
8585
'should work with a list of nodes'
8686
)
8787

0 commit comments

Comments
 (0)