Skip to content

Commit b7ab151

Browse files
authored
Add support for hiding positions
Closes GH-10. Closes GH-12. Reviewed-by: Christian Murphy <christian.murphy.42@gmail.com>
1 parent d1f26cc commit b7ab151

File tree

8 files changed

+96
-29
lines changed

8 files changed

+96
-29
lines changed

index.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,14 @@ function noColor(node) {
2929
}
3030

3131
// Inspects a node.
32-
function inspect(node) {
32+
function inspect(node, options) {
33+
var settings = options || {}
34+
var showPositions = settings.showPositions
35+
36+
if (showPositions === null || showPositions === undefined) {
37+
showPositions = true
38+
}
39+
3340
return inspectValue(node, '')
3441

3542
function inspectValue(node, pad) {
@@ -85,7 +92,9 @@ function inspect(node) {
8592
var result = [node.type]
8693
var kind = node.tagName || node.name
8794
var position = node.position || {}
88-
var location = stringifyPosition(position.start, position.end)
95+
var location = showPositions
96+
? stringifyPosition(position.start, position.end)
97+
: ''
8998
var attributes = []
9099
var key
91100
var value

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
"build": "npm run build-bundle && npm run build-mangle",
6363
"test-api": "node test",
6464
"test-coverage": "nyc --reporter lcov tape test.js",
65-
"test": "npm run format && npm run build && npm run test-coverage",
66-
"dtslint": "dtslint types"
65+
"test-types": "dtslint types",
66+
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
6767
},
6868
"prettier": {
6969
"tabWidth": 2,

readme.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -49,25 +49,26 @@ root[2]
4949

5050
## API
5151

52-
### `inspect(node)`
52+
### `inspect(node[, options])`
5353

54-
By default, color support is enabled in Node.js and turned off anywhere else.
54+
Inspect the given [`node`][node].
55+
By default, colors are added in Node, and not in other places.
5556
See below on how to change that.
5657

57-
###### Parameters
58+
###### `options.showPositions`
5859

59-
* `node` ([`Node`][node]).
60+
Whether to include positional information (`boolean`, default: `true`).
6061

6162
###### Returns
6263

6364
`string` — String representing `node`.
6465

65-
### `inspect.<style>[.<style>…](node)`
66+
### `inspect.<style>[.<style>…](node[, options])`
6667

6768
Where `<style>` is either `color` or `noColor`.
6869

69-
To explicitly add or remove ANSI sequences, use either `inspect.color(node)`
70-
or `inspect.noColor(node)`.
70+
To explicitly add or remove ANSI sequences, use `inspect.color(node)` or
71+
`inspect.noColor(node)`.
7172

7273
## Contribute
7374

test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -235,6 +235,35 @@ test('inspect()', function (t) {
235235
'should work with just `offset` in `position`'
236236
)
237237

238+
t.equal(
239+
strip(inspect(retext().parse(paragraph), {showPositions: false})),
240+
[
241+
'RootNode[1]',
242+
'└─ ParagraphNode[3]',
243+
' ├─ SentenceNode[6]',
244+
' │ ├─ WordNode[1]',
245+
' │ │ └─ TextNode: "Some"',
246+
' │ ├─ WhiteSpaceNode: " "',
247+
' │ ├─ WordNode[1]',
248+
' │ │ └─ TextNode: "simple"',
249+
' │ ├─ WhiteSpaceNode: " "',
250+
' │ ├─ WordNode[1]',
251+
' │ │ └─ TextNode: "text"',
252+
' │ └─ PunctuationNode: "."',
253+
' ├─ WhiteSpaceNode: " "',
254+
' └─ SentenceNode[6]',
255+
' ├─ WordNode[1]',
256+
' │ └─ TextNode: "Other"',
257+
' ├─ WhiteSpaceNode: " "',
258+
' ├─ PunctuationNode: "“"',
259+
' ├─ WordNode[1]',
260+
' │ └─ TextNode: "sentence"',
261+
' ├─ PunctuationNode: "”"',
262+
' └─ PunctuationNode: "."'
263+
].join('\n'),
264+
'should support `showPositions: false`'
265+
)
266+
238267
t.end()
239268
})
240269

types/index.d.ts

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,27 +1,45 @@
11
// TypeScript Version: 3.0
22
import {Node} from 'unist'
33

4-
export = inpect
4+
declare namespace unistUtilInspect {
5+
interface UnistUtilInspectOptions {
6+
/**
7+
* Whether to include position information.
8+
*
9+
* @defaultValue true
10+
*/
11+
showPositions?: boolean
12+
}
513

6-
/*
7-
* Unist utility to inspect the details of a Unist Node
8-
*
9-
* @param node Node to inspect
10-
*/
11-
declare function inpect(node: Node): string
12-
13-
declare namespace inpect {
1414
/**
1515
* Inspect the given Node and include colors from the results
1616
*
1717
* @param node Node to inspect
18+
* @param options Configuration
1819
*/
19-
function color(node: Node): string
20+
function color(node: Node, options?: Partial<UnistUtilInspectOptions>): string
2021

2122
/**
2223
* Inspect the given Node and exclude colors from the results
2324
*
2425
* @param node Node to inspect
26+
* @param options Configuration
2527
*/
26-
function noColor(node: Node): string
28+
function noColor(
29+
node: Node,
30+
options?: Partial<UnistUtilInspectOptions>
31+
): string
2732
}
33+
34+
/*
35+
* Unist utility to inspect the details of a Unist Node
36+
*
37+
* @param node Node to inspect
38+
* @param options Configuration
39+
*/
40+
declare function unistUtilInspect(
41+
node: Node,
42+
options?: Partial<unistUtilInspect.UnistUtilInspectOptions>
43+
): string
44+
45+
export = unistUtilInspect

types/tsconfig.json

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
{
22
"compilerOptions": {
3-
"lib": ["es2015"],
4-
"strict": true,
5-
"baseUrl": ".",
6-
"paths": {
7-
"unist-util-inspect": ["index.d.ts"]
8-
}
3+
"lib": ["es2015"],
4+
"strict": true,
5+
"baseUrl": ".",
6+
"paths": {
7+
"unist-util-inspect": [
8+
"index.d.ts"
9+
]
10+
}
911
}
1012
}

types/tslint.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
11
{
2-
"extends": "dtslint/dtslint.json"
2+
"extends": "dtslint/dtslint.json",
3+
"rules": {
4+
"semicolon": false,
5+
"whitespace": false
6+
}
37
}

types/unist-util-inspect-tests.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,7 @@ const node = {
2828
}
2929

3030
const result: string = inspect(node)
31+
32+
const noColor: string = inspect.noColor(node)
33+
34+
const noPositions: string = inspect(node, {showPositions: false})

0 commit comments

Comments
 (0)