Skip to content

Add support for hiding positions #12

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 11 additions & 2 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,14 @@ function noColor(node) {
}

// Inspects a node.
function inspect(node) {
function inspect(node, options) {
var settings = options || {}
var showPositions = settings.showPositions

if (showPositions === null || showPositions === undefined) {
showPositions = true
}

return inspectValue(node, '')

function inspectValue(node, pad) {
Expand Down Expand Up @@ -82,7 +89,9 @@ function inspect(node) {
function formatNode(node) {
var result = [node.type]
var position = node.position || {}
var location = stringifyPosition(position.start, position.end)
var location = showPositions
? stringifyPosition(position.start, position.end)
: ''
var attributes = []
var key
var value
Expand Down
4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,8 @@
"build": "npm run build-bundle && npm run build-mangle",
"test-api": "node test",
"test-coverage": "nyc --reporter lcov tape test.js",
"test": "npm run format && npm run build && npm run test-coverage",
"dtslint": "dtslint types"
"test-types": "dtslint types",
"test": "npm run format && npm run build && npm run test-coverage && npm run test-types"
},
"prettier": {
"tabWidth": 2,
Expand Down
15 changes: 8 additions & 7 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -49,25 +49,26 @@ root[2]

## API

### `inspect(node)`
### `inspect(node[, options])`

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

###### Parameters
###### `options.showPositions`

* `node` ([`Node`][node]).
Whether to include positional information (`boolean`, default: `true`).

###### Returns

`string` — String representing `node`.

### `inspect.<style>[.<style>…](node)`
### `inspect.<style>[.<style>…](node[, options])`

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

To explicitly add or remove ANSI sequences, use either `inspect.color(node)`
or `inspect.noColor(node)`.
To explicitly add or remove ANSI sequences, use `inspect.color(node)` or
`inspect.noColor(node)`.

## Contribute

Expand Down
29 changes: 29 additions & 0 deletions test.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,35 @@ test('inspect()', function (t) {
'should work with just `offset` in `position`'
)

t.equal(
strip(inspect(retext().parse(paragraph), {showPositions: false})),
[
'RootNode[1]',
'└─ ParagraphNode[3]',
' ├─ SentenceNode[6]',
' │ ├─ WordNode[1]',
' │ │ └─ TextNode: "Some"',
' │ ├─ WhiteSpaceNode: " "',
' │ ├─ WordNode[1]',
' │ │ └─ TextNode: "simple"',
' │ ├─ WhiteSpaceNode: " "',
' │ ├─ WordNode[1]',
' │ │ └─ TextNode: "text"',
' │ └─ PunctuationNode: "."',
' ├─ WhiteSpaceNode: " "',
' └─ SentenceNode[6]',
' ├─ WordNode[1]',
' │ └─ TextNode: "Other"',
' ├─ WhiteSpaceNode: " "',
' ├─ PunctuationNode: "“"',
' ├─ WordNode[1]',
' │ └─ TextNode: "sentence"',
' ├─ PunctuationNode: "”"',
' └─ PunctuationNode: "."'
].join('\n'),
'should support `showPositions: false`'
)

t.end()
})

Expand Down
40 changes: 29 additions & 11 deletions types/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,27 +1,45 @@
// TypeScript Version: 3.0
import {Node} from 'unist'

export = inpect
declare namespace unistUtilInspect {
interface UnistUtilInspectOptions {
/**
* Whether to include position information.
*
* @defaultValue true
*/
showPositions?: boolean
}

/*
* Unist utility to inspect the details of a Unist Node
*
* @param node Node to inspect
*/
declare function inpect(node: Node): string

declare namespace inpect {
/**
* Inspect the given Node and include colors from the results
*
* @param node Node to inspect
* @param options Configuration
*/
function color(node: Node): string
function color(node: Node, options?: Partial<UnistUtilInspectOptions>): string

/**
* Inspect the given Node and exclude colors from the results
*
* @param node Node to inspect
* @param options Configuration
*/
function noColor(node: Node): string
function noColor(
node: Node,
options?: Partial<UnistUtilInspectOptions>
): string
}

/*
* Unist utility to inspect the details of a Unist Node
*
* @param node Node to inspect
* @param options Configuration
*/
declare function unistUtilInspect(
node: Node,
options?: Partial<unistUtilInspect.UnistUtilInspectOptions>
): string

export = unistUtilInspect
14 changes: 8 additions & 6 deletions types/tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
{
"compilerOptions": {
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unist-util-inspect": ["index.d.ts"]
}
"lib": ["es2015"],
"strict": true,
"baseUrl": ".",
"paths": {
"unist-util-inspect": [
"index.d.ts"
]
}
}
}
6 changes: 5 additions & 1 deletion types/tslint.json
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
{
"extends": "dtslint/dtslint.json"
"extends": "dtslint/dtslint.json",
"rules": {
"semicolon": false,
"whitespace": false
}
}
4 changes: 4 additions & 0 deletions types/unist-util-inspect-tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,7 @@ const node = {
}

const result: string = inspect(node)

const noColor: string = inspect.noColor(node)

const noPositions: string = inspect(node, {showPositions: false})