unist-util-inspect
Advanced tools
Comparing version 7.0.1 to 7.0.2
@@ -0,40 +1,6 @@ | ||
export type Options = typeof import('./lib/index.js') | ||
/** | ||
* Inspects a node, without using color. | ||
* | ||
* @param {unknown} node | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectNoColor( | ||
node: unknown, | ||
options?: Options | undefined | ||
): string | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @param {unknown} tree | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectColor( | ||
tree: unknown, | ||
options?: Options | undefined | ||
): string | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @param {unknown} tree | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspect(tree: unknown, options?: Options | undefined): string | ||
export type Node = import('unist').Node | ||
export type Position = import('unist').Position | ||
export type Point = import('unist').Point | ||
export type Options = { | ||
showPositions?: boolean | undefined | ||
} | ||
/** | ||
* Deprecated, use `Options`. | ||
*/ | ||
export type InspectOptions = Options | ||
export {inspect, inspectColor, inspectNoColor} from './lib/index.js' |
306
index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @typedef {import('unist').Position} Position | ||
* @typedef {import('unist').Point} Point | ||
* @typedef {import('./lib/index.js')} Options | ||
* | ||
* @typedef Options | ||
* @property {boolean} [showPositions=true] | ||
* | ||
* @typedef {Options} InspectOptions | ||
@@ -13,299 +8,2 @@ * Deprecated, use `Options`. | ||
import {color} from './color.js' | ||
/* c8 ignore next */ | ||
export const inspect = color ? inspectColor : inspectNoColor | ||
const own = {}.hasOwnProperty | ||
const bold = ansiColor(1, 22) | ||
const dim = ansiColor(2, 22) | ||
const yellow = ansiColor(33, 39) | ||
const green = ansiColor(32, 39) | ||
// ANSI color regex. | ||
/* eslint-disable no-control-regex */ | ||
const colorExpression = | ||
/(?:(?:\u001B\[)|\u009B)(?:\d{1,3})?(?:(?:;\d{0,3})*)?[A-M|f-m]|\u001B[A-M]/g | ||
/* eslint-enable no-control-regex */ | ||
/** | ||
* Inspects a node, without using color. | ||
* | ||
* @param {unknown} node | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectNoColor(node, options) { | ||
return inspectColor(node, options).replace(colorExpression, '') | ||
} | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @param {unknown} tree | ||
* @param {Options} [options] | ||
* @returns {string} | ||
*/ | ||
export function inspectColor(tree, options = {}) { | ||
const positions = | ||
options.showPositions === null || options.showPositions === undefined | ||
? true | ||
: options.showPositions | ||
return inspectValue(tree) | ||
/** | ||
* @param {unknown} node | ||
* @returns {string} | ||
*/ | ||
function inspectValue(node) { | ||
if (node && typeof node === 'object' && 'length' in node) { | ||
// @ts-expect-error looks like a list of nodes. | ||
return inspectNodes(node) | ||
} | ||
// @ts-expect-error looks like a single node. | ||
if (node && node.type) { | ||
// @ts-expect-error looks like a single node. | ||
return inspectTree(node) | ||
} | ||
return inspectNonTree(node) | ||
} | ||
/** | ||
* @param {unknown} value | ||
* @returns {string} | ||
*/ | ||
function inspectNonTree(value) { | ||
return JSON.stringify(value) | ||
} | ||
/** | ||
* @param {Array<Node>} nodes | ||
* @returns {string} | ||
*/ | ||
function inspectNodes(nodes) { | ||
const size = String(nodes.length - 1).length | ||
/** @type {Array<string>} */ | ||
const result = [] | ||
let index = -1 | ||
while (++index < nodes.length) { | ||
result.push( | ||
dim( | ||
(index < nodes.length - 1 ? '├' : '└') + | ||
'─' + | ||
String(index).padEnd(size) | ||
) + | ||
' ' + | ||
indent( | ||
inspectValue(nodes[index]), | ||
(index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2), | ||
true | ||
) | ||
) | ||
} | ||
return result.join('\n') | ||
} | ||
/** | ||
* @param {Record<string, unknown>} object | ||
* @returns {string} | ||
*/ | ||
// eslint-disable-next-line complexity | ||
function inspectFields(object) { | ||
/** @type {Array<string>} */ | ||
const result = [] | ||
/** @type {string} */ | ||
let key | ||
for (key in object) { | ||
/* c8 ignore next 1 */ | ||
if (!own.call(object, key)) continue | ||
const value = object[key] | ||
/** @type {string} */ | ||
let formatted | ||
if ( | ||
value === undefined || | ||
// Standard keys defined by unist that we format differently. | ||
// <https://github.com/syntax-tree/unist> | ||
key === 'type' || | ||
key === 'value' || | ||
key === 'children' || | ||
key === 'position' || | ||
// Ignore `name` (from xast) and `tagName` (from `hast`) when string. | ||
(typeof value === 'string' && (key === 'name' || key === 'tagName')) | ||
) { | ||
continue | ||
} | ||
// A single node. | ||
if ( | ||
value && | ||
typeof value === 'object' && | ||
// @ts-expect-error looks like a node. | ||
value.type && | ||
key !== 'data' && | ||
key !== 'attributes' && | ||
key !== 'properties' | ||
) { | ||
// @ts-expect-error looks like a node. | ||
formatted = inspectTree(value) | ||
} | ||
// A list of nodes. | ||
else if ( | ||
value && | ||
Array.isArray(value) && | ||
// Looks like a node. | ||
// type-coverage:ignore-next-line | ||
value[0] && | ||
// Looks like a node. | ||
// type-coverage:ignore-next-line | ||
value[0].type | ||
) { | ||
formatted = '\n' + inspectNodes(value) | ||
} else { | ||
formatted = inspectNonTree(value) | ||
} | ||
result.push( | ||
key + dim(':') + (/\s/.test(formatted.charAt(0)) ? '' : ' ') + formatted | ||
) | ||
} | ||
return indent( | ||
result.join('\n'), | ||
// @ts-expect-error looks like a parent node. | ||
(object.children && object.children.length > 0 ? dim('│') : ' ') + ' ' | ||
) | ||
} | ||
/** | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
function inspectTree(node) { | ||
const result = [formatNode(node)] | ||
// @ts-expect-error: looks like a record. | ||
const fields = inspectFields(node) | ||
// @ts-expect-error looks like a parent. | ||
const content = inspectNodes(node.children || []) | ||
if (fields) result.push(fields) | ||
if (content) result.push(content) | ||
return result.join('\n') | ||
} | ||
/** | ||
* Colored node formatter. | ||
* | ||
* @param {Node} node | ||
* @returns {string} | ||
*/ | ||
function formatNode(node) { | ||
const result = [bold(node.type)] | ||
/** @type {string|undefined} */ | ||
// @ts-expect-error: might be available. | ||
const kind = node.tagName || node.name | ||
const position = positions ? stringifyPosition(node.position) : '' | ||
if (typeof kind === 'string') { | ||
result.push('<', kind, '>') | ||
} | ||
// @ts-expect-error: looks like a parent. | ||
if (node.children) { | ||
// @ts-expect-error looks like a parent. | ||
result.push(dim('['), yellow(node.children.length), dim(']')) | ||
// @ts-expect-error: looks like a literal. | ||
} else if (typeof node.value === 'string') { | ||
// @ts-expect-error: looks like a literal. | ||
result.push(' ', green(inspectNonTree(node.value))) | ||
} | ||
if (position) { | ||
result.push(' ', dim('('), position, dim(')')) | ||
} | ||
return result.join('') | ||
} | ||
} | ||
/** | ||
* @param {string} value | ||
* @param {string} indentation | ||
* @param {boolean} [ignoreFirst=false] | ||
* @returns {string} | ||
*/ | ||
function indent(value, indentation, ignoreFirst) { | ||
const lines = value.split('\n') | ||
let index = ignoreFirst ? 0 : -1 | ||
if (!value) return value | ||
while (++index < lines.length) { | ||
lines[index] = indentation + lines[index] | ||
} | ||
return lines.join('\n') | ||
} | ||
/** | ||
* @param {Position|undefined} [value] | ||
* @returns {string} | ||
*/ | ||
function stringifyPosition(value) { | ||
/** @type {Position} */ | ||
// @ts-expect-error: fine. | ||
const position = value || {} | ||
/** @type {Array<string>} */ | ||
const result = [] | ||
/** @type {Array<string>} */ | ||
const positions = [] | ||
/** @type {Array<string>} */ | ||
const offsets = [] | ||
point(position.start) | ||
point(position.end) | ||
if (positions.length > 0) result.push(positions.join('-')) | ||
if (offsets.length > 0) result.push(offsets.join('-')) | ||
return result.join(', ') | ||
/** | ||
* @param {Point} value | ||
*/ | ||
function point(value) { | ||
if (value) { | ||
positions.push((value.line || 1) + ':' + (value.column || 1)) | ||
if ('offset' in value) { | ||
offsets.push(String(value.offset || 0)) | ||
} | ||
} | ||
} | ||
} | ||
/** | ||
* Factory to wrap values in ANSI colours. | ||
* | ||
* @param {number} open | ||
* @param {number} close | ||
* @returns {function(string): string} | ||
*/ | ||
function ansiColor(open, close) { | ||
return color | ||
/** | ||
* @param {string} value | ||
* @returns {string} | ||
*/ | ||
function color(value) { | ||
return '\u001B[' + open + 'm' + value + '\u001B[' + close + 'm' | ||
} | ||
} | ||
export {inspect, inspectColor, inspectNoColor} from './lib/index.js' |
{ | ||
"name": "unist-util-inspect", | ||
"version": "7.0.1", | ||
"version": "7.0.2", | ||
"description": "unist utility to inspect nodes", | ||
@@ -30,13 +30,10 @@ "license": "MIT", | ||
"browser": { | ||
"./color.js": "./color-browser.js" | ||
"./lib/color.js": "./lib/color-browser.js" | ||
}, | ||
"react-native": { | ||
"./color.js": "./color-browser.js" | ||
"./lib/color.js": "./lib/color-browser.js" | ||
}, | ||
"types": "index.d.ts", | ||
"files": [ | ||
"color.d.ts", | ||
"color.js", | ||
"color-browser.d.ts", | ||
"color-browser.js", | ||
"lib/", | ||
"index.d.ts", | ||
@@ -49,3 +46,3 @@ "index.js" | ||
"devDependencies": { | ||
"@types/tape": "^4.0.0", | ||
"@types/node": "^18.0.0", | ||
"c8": "^7.0.0", | ||
@@ -58,5 +55,3 @@ "chalk": "^5.0.0", | ||
"retext": "^8.0.0", | ||
"rimraf": "^3.0.0", | ||
"strip-ansi": "^7.0.0", | ||
"tape": "^5.0.0", | ||
"type-coverage": "^2.0.0", | ||
@@ -67,10 +62,10 @@ "typescript": "^4.0.0", | ||
"xastscript": "^3.0.0", | ||
"xo": "^0.51.0" | ||
"xo": "^0.53.0" | ||
}, | ||
"scripts": { | ||
"prepack": "npm run build && npm run format", | ||
"build": "rimraf \"*.d.ts\" && tsc && type-coverage", | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"test-api": "node test.js", | ||
"test-coverage": "c8 --check-coverage --branches 100 --functions 100 --lines 100 --statements 100 --reporter lcov node test.js", | ||
"test-api": "node --conditions development test.js", | ||
"test-coverage": "c8 --check-coverage --100 --reporter lcov npm run test-api", | ||
"test": "npm run build && npm run format && npm run test-coverage" | ||
@@ -77,0 +72,0 @@ }, |
@@ -20,5 +20,6 @@ # unist-util-inspect | ||
* [API](#api) | ||
* [`inspect(node[, options])`](#inspectnode-options) | ||
* [`inspectColor(node[, options])`](#inspectcolornode-options) | ||
* [`inspectNoColor(node[, options])`](#inspectnocolornode-options) | ||
* [`inspect(tree[, options])`](#inspecttree-options) | ||
* [`inspectColor(tree[, options])`](#inspectcolortree-options) | ||
* [`inspectNoColor(tree[, options])`](#inspectnocolortree-options) | ||
* [`Options`](#options) | ||
* [Types](#types) | ||
@@ -42,3 +43,3 @@ * [Compatibility](#compatibility) | ||
This package is [ESM only][esm]. | ||
In Node.js (version 12.20+, 14.14+, 16.0+, 18.0+), install with [npm][]: | ||
In Node.js (version 14.14+ and 16.0+), install with [npm][]: | ||
@@ -52,3 +53,3 @@ ```sh | ||
```js | ||
import {inspect} from "https://esm.sh/unist-util-inspect@8" | ||
import {inspect} from 'https://esm.sh/unist-util-inspect@7' | ||
``` | ||
@@ -60,3 +61,3 @@ | ||
<script type="module"> | ||
import {inspect} from "https://esm.sh/unist-util-inspect@7?bundle" | ||
import {inspect} from 'https://esm.sh/unist-util-inspect@7?bundle' | ||
</script> | ||
@@ -98,32 +99,44 @@ ``` | ||
This package exports the identifiers `inspect`, `inspectColor`, and | ||
`inspectNoColor`. | ||
This package exports the identifiers [`inspect`][api-inspect], | ||
[`inspectColor`][api-inspectcolor], and [`inspectNoColor`][api-inspectnocolor]. | ||
There is no default export. | ||
### `inspect(node[, options])` | ||
### `inspect(tree[, options])` | ||
Inspect the given `node` ([`Node`][node]). | ||
By default, colors are added in Node, and not in other places. | ||
See below on how to change that. | ||
Inspect a tree, with color in Node, without color in browsers. | ||
###### `options.showPositions` | ||
###### Parameters | ||
Whether to include positional information (`boolean`, default: `true`). | ||
* `tree` ([`Node`][node]) | ||
— tree to inspect | ||
* `options` ([`Options`][api-options], optional) | ||
— configuration | ||
##### Returns | ||
###### Returns | ||
Pretty printed `node` (`string`). | ||
Pretty printed `tree` (`string`). | ||
### `inspectColor(node[, options])` | ||
### `inspectColor(tree[, options])` | ||
Inspect with ANSI color sequences (default in Node, Deno). | ||
Inspect a tree, with color. | ||
Otherwise same as [`inspect`][api-inspect]. | ||
### `inspectNoColor(node[, options])` | ||
### `inspectNoColor(tree[, options])` | ||
Inspect without ANSI color sequences (default in browser, `react-native`). | ||
Inspect a tree, without color. | ||
Otherwise same as [`inspect`][api-inspect]. | ||
### `Options` | ||
Configuration (TypeScript type). | ||
###### Fields | ||
* `showPositions` (`boolean`, default: `true`) | ||
— whether to include positional information | ||
## Types | ||
This package is fully typed with [TypeScript][]. | ||
It exports the additional type `Options`. | ||
It exports the additional type [`Options`][api-options]. | ||
@@ -134,3 +147,3 @@ ## Compatibility | ||
versions of Node.js. | ||
As of now, that is Node.js 12.20+, 14.14+, 16.0+, and 18.0+. | ||
As of now, that is Node.js 14.14+ and 16.0+. | ||
Our projects sometimes work with older versions, but this is not guaranteed. | ||
@@ -203,1 +216,9 @@ | ||
[node]: https://github.com/syntax-tree/unist#node | ||
[api-inspect]: #inspecttree-options | ||
[api-inspectcolor]: #inspectcolortree-options | ||
[api-inspectnocolor]: #inspectnocolortree-options | ||
[api-options]: #options |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
19199
15
11
432
218
1