Skip to content

Commit 6be0913

Browse files
committed
Add all non-standard attributes to output
1 parent 4fc9559 commit 6be0913

File tree

2 files changed

+94
-2
lines changed

2 files changed

+94
-2
lines changed

index.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,19 @@ var CHAR_CONTINUE_AND_SPLIT = '├';
7474
var CONTINUE = CHAR_CONTINUE_AND_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
7575
var STOP = CHAR_SPLIT + CHAR_HORIZONTAL_LINE + ' ';
7676

77+
/*
78+
* Standard keys defined by unist:
79+
* https://github.com/wooorm/unist.
80+
* We don‘t include `data` though.
81+
*/
82+
83+
var ignore = [
84+
'type',
85+
'value',
86+
'children',
87+
'position'
88+
];
89+
7790
/**
7891
* Colored nesting formatter.
7992
*
@@ -161,6 +174,9 @@ function formatNode(node) {
161174
var log = node.type;
162175
var location = node.position || {};
163176
var position = stringify(location.start, location.end);
177+
var key;
178+
var values = [];
179+
var value;
164180

165181
if (node.children && node.children.length) {
166182
log += dim('[') + yellow(node.children.length) + dim(']');
@@ -172,8 +188,23 @@ function formatNode(node) {
172188
log += ' (' + position + ')';
173189
}
174190

175-
if (!isEmpty(node.data)) {
176-
log += ' [data=' + JSON.stringify(node.data) + ']';
191+
for (key in node) {
192+
value = node[key];
193+
194+
if (
195+
ignore.indexOf(key) !== -1 ||
196+
value === null ||
197+
value === undefined ||
198+
(typeof value === 'object' && isEmpty(value))
199+
) {
200+
continue;
201+
}
202+
203+
values.push('[' + key + '=' + JSON.stringify(value) + ']');
204+
}
205+
206+
if (values.length) {
207+
log += ' ' + values.join('');
177208
}
178209

179210
return log;

test.js

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -145,6 +145,67 @@ test('inspect()', function (t) {
145145
'should work with data attributes'
146146
);
147147

148+
t.equal(
149+
strip(inspect({
150+
'type': 'table',
151+
'align': ['left', 'center'],
152+
'children': [
153+
{
154+
'type': 'tableRow',
155+
'children': [
156+
{
157+
'type': 'tableCell',
158+
'children': [{
159+
'type': 'text',
160+
'value': 'foo'
161+
}]
162+
},
163+
{
164+
'type': 'tableCell',
165+
'children': [{
166+
'type': 'text',
167+
'value': 'bar'
168+
}]
169+
}
170+
]
171+
},
172+
{
173+
'type': 'tableRow',
174+
'children': [
175+
{
176+
'type': 'tableCell',
177+
'children': [{
178+
'type': 'text',
179+
'value': 'baz'
180+
}]
181+
},
182+
{
183+
'type': 'tableCell',
184+
'children': [{
185+
'type': 'text',
186+
'value': 'qux'
187+
}]
188+
}
189+
]
190+
}
191+
]
192+
})),
193+
[
194+
'table[2] [align=["left","center"]]',
195+
'├─ tableRow[2]',
196+
'│ ├─ tableCell[1]',
197+
'│ │ └─ text: "foo"',
198+
'│ └─ tableCell[1]',
199+
'│ └─ text: "bar"',
200+
'└─ tableRow[2]',
201+
' ├─ tableCell[1]',
202+
' │ └─ text: "baz"',
203+
' └─ tableCell[1]',
204+
' └─ text: "qux"'
205+
].join('\n'),
206+
'should work with other attributes'
207+
);
208+
148209
t.equal(
149210
strip(inspect({
150211
'type': 'foo',

0 commit comments

Comments
 (0)