-
-
Notifications
You must be signed in to change notification settings - Fork 5
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
283 additions
and
232 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
coverage/ | ||
unist-util-inspect.js | ||
unist-util-inspect.min.js |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,222 +1,207 @@ | ||
'use strict'; | ||
'use strict' | ||
|
||
var isEmpty = require('is-empty'); | ||
var isEmpty = require('is-empty') | ||
|
||
/* Detect color support. */ | ||
|
||
var color = true; | ||
var color = true | ||
|
||
try { | ||
color = 'inspect' in require('util'); | ||
color = 'inspect' in require('util') | ||
} catch (err) { | ||
/* istanbul ignore next - browser */ | ||
color = false; | ||
color = false | ||
} | ||
|
||
module.exports = color ? inspect : /* istanbul ignore next */ noColor; | ||
module.exports = color ? inspect : /* istanbul ignore next */ noColor | ||
|
||
inspect.color = inspect; | ||
noColor.color = inspect; | ||
inspect.noColor = noColor; | ||
noColor.noColor = noColor; | ||
inspect.color = inspect | ||
noColor.color = inspect | ||
inspect.noColor = noColor | ||
noColor.noColor = noColor | ||
|
||
var dim = ansiColor(2, 22); | ||
var yellow = ansiColor(33, 39); | ||
var green = ansiColor(32, 39); | ||
var dim = ansiColor(2, 22) | ||
var yellow = ansiColor(33, 39) | ||
var green = ansiColor(32, 39) | ||
|
||
/* Define ANSII color removal functionality. */ | ||
var COLOR_EXPRESSION = new RegExp( | ||
'(?:' + | ||
'(?:\\u001b\\[)|' + | ||
'\\u009b' + | ||
')' + | ||
'(?:' + | ||
')' + | ||
'(?:' + | ||
'(?:[0-9]{1,3})?(?:(?:;[0-9]{0,3})*)?[A-M|f-m]' + | ||
')|' + | ||
'\\u001b[A-M]', | ||
')|' + | ||
'\\u001b[A-M]', | ||
'g' | ||
); | ||
|
||
/* Constants. */ | ||
var CHAR_HORIZONTAL_LINE = '─'; | ||
var CHAR_VERTICAL_LINE = '│'; | ||
var CHAR_SPLIT = '└'; | ||
var CHAR_CONTINUE_AND_SPLIT = '├'; | ||
var CONTINUE = CHAR_CONTINUE_AND_SPLIT + CHAR_HORIZONTAL_LINE + ' '; | ||
var STOP = CHAR_SPLIT + CHAR_HORIZONTAL_LINE + ' '; | ||
) | ||
|
||
/* Standard keys defined by unist: | ||
* https://github.com/syntax-tree/unist. | ||
* We don‘t include `data` though. */ | ||
var ignore = [ | ||
'type', | ||
'value', | ||
'children', | ||
'position' | ||
]; | ||
* We don’t ignore `data` though. */ | ||
var ignore = ['type', 'value', 'children', 'position'] | ||
|
||
/* Inspects a node, without using color. */ | ||
function noColor(node, pad) { | ||
return stripColor(inspect(node, pad)); | ||
return stripColor(inspect(node, pad)) | ||
} | ||
|
||
/* Inspects a node. */ | ||
function inspect(node, pad) { | ||
var result; | ||
var children; | ||
var index; | ||
var length; | ||
var result | ||
var children | ||
var index | ||
var length | ||
|
||
if (node && Boolean(node.length) && typeof node !== 'string') { | ||
length = node.length; | ||
index = -1; | ||
result = []; | ||
length = node.length | ||
index = -1 | ||
result = [] | ||
|
||
while (++index < length) { | ||
result[index] = inspect(node[index]); | ||
result[index] = inspect(node[index]) | ||
} | ||
|
||
return result.join('\n'); | ||
return result.join('\n') | ||
} | ||
|
||
if (!node || !node.type) { | ||
return String(node); | ||
return String(node) | ||
} | ||
|
||
result = [formatNode(node)]; | ||
children = node.children; | ||
length = children && children.length; | ||
index = -1; | ||
result = [formatNode(node)] | ||
children = node.children | ||
length = children && children.length | ||
index = -1 | ||
|
||
if (!length) { | ||
return result[0]; | ||
return result[0] | ||
} | ||
|
||
if (!pad || typeof pad === 'number') { | ||
pad = ''; | ||
pad = '' | ||
} | ||
|
||
while (++index < length) { | ||
node = children[index]; | ||
node = children[index] | ||
|
||
if (index === length - 1) { | ||
result.push(formatNesting(pad + STOP) + inspect(node, pad + ' ')); | ||
result.push(formatNesting(pad + '└─ ') + inspect(node, pad + ' ')) | ||
} else { | ||
result.push(formatNesting(pad + CONTINUE) + inspect(node, pad + CHAR_VERTICAL_LINE + ' ')); | ||
result.push(formatNesting(pad + '├─ ') + inspect(node, pad + '│ ')) | ||
} | ||
} | ||
|
||
return result.join('\n'); | ||
return result.join('\n') | ||
} | ||
|
||
/* Colored nesting formatter. */ | ||
function formatNesting(value) { | ||
return dim(value); | ||
return dim(value) | ||
} | ||
|
||
/* Compile a single position. */ | ||
function compile(pos) { | ||
var values = []; | ||
var values = [] | ||
|
||
if (!pos) { | ||
return null; | ||
return null | ||
} | ||
|
||
values = [ | ||
[pos.line || 1, pos.column || 1].join(':') | ||
]; | ||
values = [[pos.line || 1, pos.column || 1].join(':')] | ||
|
||
if ('offset' in pos) { | ||
values.push(String(pos.offset || 0)); | ||
values.push(String(pos.offset || 0)) | ||
} | ||
|
||
return values; | ||
return values | ||
} | ||
|
||
/* Compile a location. */ | ||
function stringify(start, end) { | ||
var values = []; | ||
var positions = []; | ||
var offsets = []; | ||
var values = [] | ||
var positions = [] | ||
var offsets = [] | ||
|
||
add(start); | ||
add(end); | ||
add(start) | ||
add(end) | ||
|
||
if (positions.length !== 0) { | ||
values.push(positions.join('-')); | ||
values.push(positions.join('-')) | ||
} | ||
|
||
if (offsets.length !== 0) { | ||
values.push(offsets.join('-')); | ||
values.push(offsets.join('-')) | ||
} | ||
|
||
return values.join(', '); | ||
return values.join(', ') | ||
|
||
/* Add a position. */ | ||
function add(position) { | ||
var tuple = compile(position); | ||
var tuple = compile(position) | ||
|
||
if (tuple) { | ||
positions.push(tuple[0]); | ||
positions.push(tuple[0]) | ||
|
||
if (tuple[1]) { | ||
offsets.push(tuple[1]); | ||
offsets.push(tuple[1]) | ||
} | ||
} | ||
} | ||
} | ||
|
||
/* Colored node formatter. */ | ||
function formatNode(node) { | ||
var log = node.type; | ||
var location = node.position || {}; | ||
var position = stringify(location.start, location.end); | ||
var key; | ||
var values = []; | ||
var value; | ||
var log = node.type | ||
var location = node.position || {} | ||
var position = stringify(location.start, location.end) | ||
var key | ||
var values = [] | ||
var value | ||
|
||
if (node.children) { | ||
log += dim('[') + yellow(node.children.length) + dim(']'); | ||
log += dim('[') + yellow(node.children.length) + dim(']') | ||
} else if (typeof node.value === 'string') { | ||
log += dim(': ') + green(JSON.stringify(node.value)); | ||
log += dim(': ') + green(JSON.stringify(node.value)) | ||
} | ||
|
||
if (position) { | ||
log += ' (' + position + ')'; | ||
log += ' (' + position + ')' | ||
} | ||
|
||
for (key in node) { | ||
value = node[key]; | ||
value = node[key] | ||
|
||
if ( | ||
ignore.indexOf(key) !== -1 || | ||
value === null || | ||
value === undefined || | ||
(typeof value === 'object' && isEmpty(value)) | ||
) { | ||
continue; | ||
continue | ||
} | ||
|
||
values.push('[' + key + '=' + JSON.stringify(value) + ']'); | ||
values.push('[' + key + '=' + JSON.stringify(value) + ']') | ||
} | ||
|
||
if (values.length !== 0) { | ||
log += ' ' + values.join(''); | ||
log += ' ' + values.join('') | ||
} | ||
|
||
return log; | ||
return log | ||
} | ||
|
||
/* Remove ANSI colour from `value`. */ | ||
function stripColor(value) { | ||
return value.replace(COLOR_EXPRESSION, ''); | ||
return value.replace(COLOR_EXPRESSION, '') | ||
} | ||
|
||
/* Factory to wrap values in ANSI colours. */ | ||
function ansiColor(open, close) { | ||
return color; | ||
return color | ||
|
||
function color(value) { | ||
return '\u001b[' + open + 'm' + value + '\u001b[' + close + 'm'; | ||
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm' | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.