unist-util-inspect
Advanced tools
Comparing version 8.0.0 to 8.1.0
@@ -1,6 +0,2 @@ | ||
export type Options = typeof import('./lib/index.js') | ||
/** | ||
* Deprecated, use `Options`. | ||
*/ | ||
export type InspectOptions = Options | ||
export {inspect, inspectColor, inspectNoColor} from './lib/index.js' | ||
export type {Options, Options as InspectOptions} from './lib/types.js' | ||
export {inspectColor, inspectNoColor, inspect} from './lib/index.js' |
10
index.js
@@ -1,8 +0,2 @@ | ||
/** | ||
* @typedef {import('./lib/index.js')} Options | ||
* | ||
* @typedef {Options} InspectOptions | ||
* Deprecated, use `Options`. | ||
*/ | ||
export {inspect, inspectColor, inspectNoColor} from './lib/index.js' | ||
// Note: types exposed from `index.d.ts`. | ||
export {inspectColor, inspectNoColor, inspect} from './lib/index.js' |
@@ -1,1 +0,2 @@ | ||
export const color: false | ||
export const color: false; | ||
//# sourceMappingURL=color.default.d.ts.map |
@@ -1,1 +0,2 @@ | ||
export const color: true | ||
export const color: true; | ||
//# sourceMappingURL=color.node.d.ts.map |
@@ -6,3 +6,3 @@ /** | ||
* Tree to inspect. | ||
* @param {Options | null | undefined} [options] | ||
* @param {Readonly<Options> | null | undefined} [options] | ||
* Configuration. | ||
@@ -12,26 +12,24 @@ * @returns {string} | ||
*/ | ||
export function inspectNoColor( | ||
tree: unknown, | ||
options?: Options | null | undefined | ||
): string | ||
export function inspect(tree: unknown, options?: Readonly<Options> | null | undefined): string; | ||
/** | ||
* Inspects a node, using color. | ||
* Inspect a node, without color. | ||
* | ||
* @deprecated | ||
* Use `inspect` instead, with `color: false`. | ||
* @param {unknown} tree | ||
* Tree to inspect. | ||
* @param {Options | null | undefined} [options] | ||
* Configuration (optional). | ||
* @param {Readonly<Omit<Options, 'color'>> | null | undefined} [options] | ||
* Configuration. | ||
* @returns {string} | ||
* Pretty printed `tree`. | ||
*/ | ||
export function inspectColor( | ||
tree: unknown, | ||
options?: Options | null | undefined | ||
): string | ||
export function inspectNoColor(tree: unknown, options?: Readonly<Omit<Options, "color">> | null | undefined): string; | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @deprecated | ||
* Use `inspect` instead, with `color: true`. | ||
* @param {unknown} tree | ||
* Tree to inspect. | ||
* @param {Options | null | undefined} [options] | ||
* @param {Readonly<Omit<Options, 'color'>> | null | undefined} [options] | ||
* Configuration (optional). | ||
@@ -41,24 +39,4 @@ * @returns {string} | ||
*/ | ||
export function inspect( | ||
tree: unknown, | ||
options?: Options | null | undefined | ||
): string | ||
export type Node = import('unist').Node | ||
/** | ||
* Configuration. | ||
*/ | ||
export type Options = { | ||
/** | ||
* Whether to include positional information (default: `true`). | ||
*/ | ||
showPositions?: boolean | null | undefined | ||
} | ||
/** | ||
* Info passed around. | ||
*/ | ||
export type State = { | ||
/** | ||
* Whether to include positional information. | ||
*/ | ||
showPositions: boolean | ||
} | ||
export function inspectColor(tree: unknown, options?: Readonly<Omit<Options, "color">> | null | undefined): string; | ||
import type { Options } from 'unist-util-inspect'; | ||
//# sourceMappingURL=index.d.ts.map |
119
lib/index.js
/** | ||
* @typedef {import('unist').Node} Node | ||
* @import {Options} from 'unist-util-inspect' | ||
* @import {Node} from 'unist' | ||
* @import {State} from './types.js' | ||
*/ | ||
/** | ||
* @typedef Options | ||
* Configuration. | ||
* @property {boolean | null | undefined} [showPositions=true] | ||
* Whether to include positional information (default: `true`). | ||
* | ||
* @typedef State | ||
* Info passed around. | ||
* @property {boolean} showPositions | ||
* Whether to include positional information. | ||
*/ | ||
import {color as colorDefault} from '#conditional-color' | ||
import {color} from 'unist-util-inspect/do-not-use-conditional-color' | ||
/** @type {Readonly<Options>} */ | ||
const emptyOptions = {} | ||
// To do: next major (?): use `Object.hasOwn`. | ||
const own = {}.hasOwnProperty | ||
/** | ||
* Inspect a node, with color in Node, without color in browsers. | ||
* Inspect a node, without color. | ||
* | ||
* @param tree | ||
* @param {unknown} tree | ||
* Tree to inspect. | ||
* @param options | ||
* Configuration (optional). | ||
* @returns | ||
* @param {Readonly<Options> | null | undefined} [options] | ||
* Configuration. | ||
* @returns {string} | ||
* Pretty printed `tree`. | ||
*/ | ||
/* c8 ignore next */ | ||
export const inspect = color ? inspectColor : inspectNoColor | ||
export function inspect(tree, options) { | ||
const settings = options || emptyOptions | ||
const color = | ||
typeof settings.color === 'boolean' ? settings.color : colorDefault | ||
const showPositions = | ||
typeof settings.showPositions === 'boolean' ? settings.showPositions : true | ||
/** @type {State} */ | ||
const state = { | ||
bold: color ? ansiColor(1, 22) : identity, | ||
dim: color ? ansiColor(2, 22) : identity, | ||
green: color ? ansiColor(32, 39) : identity, | ||
showPositions, | ||
yellow: color ? ansiColor(33, 39) : identity | ||
} | ||
const own = {}.hasOwnProperty | ||
return inspectValue(tree, state) | ||
} | ||
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 */ | ||
// To do: remove. | ||
/** | ||
* Inspect a node, without color. | ||
* | ||
* @deprecated | ||
* Use `inspect` instead, with `color: false`. | ||
* @param {unknown} tree | ||
* Tree to inspect. | ||
* @param {Options | null | undefined} [options] | ||
* @param {Readonly<Omit<Options, 'color'>> | null | undefined} [options] | ||
* Configuration. | ||
@@ -56,11 +57,14 @@ * @returns {string} | ||
export function inspectNoColor(tree, options) { | ||
return inspectColor(tree, options).replace(colorExpression, '') | ||
return inspect(tree, {...options, color: false}) | ||
} | ||
// To do: remove. | ||
/** | ||
* Inspects a node, using color. | ||
* | ||
* @deprecated | ||
* Use `inspect` instead, with `color: true`. | ||
* @param {unknown} tree | ||
* Tree to inspect. | ||
* @param {Options | null | undefined} [options] | ||
* @param {Readonly<Omit<Options, 'color'>> | null | undefined} [options] | ||
* Configuration (optional). | ||
@@ -71,13 +75,3 @@ * @returns {string} | ||
export function inspectColor(tree, options) { | ||
/** @type {State} */ | ||
const state = { | ||
showPositions: | ||
!options || | ||
options.showPositions === null || | ||
options.showPositions === undefined | ||
? true | ||
: options.showPositions | ||
} | ||
return inspectValue(tree, state) | ||
return inspect(tree, {...options, color: true}) | ||
} | ||
@@ -137,3 +131,3 @@ | ||
result.push( | ||
dim( | ||
state.dim( | ||
(index < nodes.length - 1 ? '├' : '└') + | ||
@@ -146,3 +140,4 @@ '─' + | ||
inspectValue(nodes[index], state), | ||
(index < nodes.length - 1 ? dim('│') : ' ') + ' '.repeat(size + 2), | ||
(index < nodes.length - 1 ? state.dim('│') : ' ') + | ||
' '.repeat(size + 2), | ||
true | ||
@@ -212,3 +207,6 @@ ) | ||
result.push( | ||
key + dim(':') + (/\s/.test(formatted.charAt(0)) ? '' : ' ') + formatted | ||
key + | ||
state.dim(':') + | ||
(/\s/.test(formatted.charAt(0)) ? '' : ' ') + | ||
formatted | ||
) | ||
@@ -220,3 +218,3 @@ } | ||
(isArrayUnknown(object.children) && object.children.length > 0 | ||
? dim('│') | ||
? state.dim('│') | ||
: ' ') + ' ' | ||
@@ -262,3 +260,3 @@ ) | ||
function formatNode(node, state) { | ||
const result = [bold(node.type)] | ||
const result = [state.bold(node.type)] | ||
// Cast as record to allow indexing. | ||
@@ -276,9 +274,13 @@ const map = /** @type {Record<string, unknown>} */ ( | ||
if (isArrayUnknown(map.children)) { | ||
result.push(dim('['), yellow(String(map.children.length)), dim(']')) | ||
result.push( | ||
state.dim('['), | ||
state.yellow(String(map.children.length)), | ||
state.dim(']') | ||
) | ||
} else if (typeof map.value === 'string') { | ||
result.push(' ', green(inspectNonTree(map.value))) | ||
result.push(' ', state.green(inspectNonTree(map.value))) | ||
} | ||
if (position) { | ||
result.push(' ', dim('('), position, dim(')')) | ||
result.push(' ', state.dim('('), position, state.dim(')')) | ||
} | ||
@@ -408,1 +410,10 @@ | ||
} | ||
/** | ||
* @template T | ||
* @param {T} value | ||
* @returns {T} | ||
*/ | ||
function identity(value) { | ||
return value | ||
} |
{ | ||
"name": "unist-util-inspect", | ||
"version": "8.0.0", | ||
"version": "8.1.0", | ||
"description": "unist utility to inspect nodes", | ||
@@ -28,5 +28,5 @@ "license": "MIT", | ||
"type": "module", | ||
"exports": { | ||
".": "./index.js", | ||
"./do-not-use-conditional-color": { | ||
"exports": "./index.js", | ||
"imports": { | ||
"#conditional-color": { | ||
"node": "./lib/color.node.js", | ||
@@ -47,9 +47,9 @@ "default": "./lib/color.default.js" | ||
"@types/node": "^20.0.0", | ||
"c8": "^8.0.0", | ||
"c8": "^10.0.0", | ||
"chalk": "^5.0.0", | ||
"hastscript": "^7.0.0", | ||
"prettier": "^2.0.0", | ||
"remark-cli": "^11.0.0", | ||
"remark-preset-wooorm": "^9.0.0", | ||
"retext": "^8.0.0", | ||
"hastscript": "^9.0.0", | ||
"prettier": "^3.0.0", | ||
"remark-cli": "^12.0.0", | ||
"remark-preset-wooorm": "^10.0.0", | ||
"retext": "^9.0.0", | ||
"strip-ansi": "^7.0.0", | ||
@@ -59,5 +59,5 @@ "type-coverage": "^2.0.0", | ||
"unist-builder": "^4.0.0", | ||
"xast-util-from-xml": "^3.0.0", | ||
"xastscript": "^3.0.0", | ||
"xo": "^0.54.0" | ||
"xast-util-from-xml": "^4.0.0", | ||
"xastscript": "^4.0.0", | ||
"xo": "^0.58.0" | ||
}, | ||
@@ -67,3 +67,3 @@ "scripts": { | ||
"build": "tsc --build --clean && tsc --build && type-coverage", | ||
"format": "remark . -qfo && prettier . -w --loglevel warn && xo --fix", | ||
"format": "remark . -qfo && prettier . -w --log-level warn && xo --fix", | ||
"test-api": "node --conditions development test.js", | ||
@@ -93,4 +93,32 @@ "test-coverage": "c8 --100 --reporter lcov npm run test-api", | ||
"xo": { | ||
"prettier": true | ||
"overrides": [ | ||
{ | ||
"files": [ | ||
"**/*.d.ts" | ||
], | ||
"rules": { | ||
"@typescript-eslint/array-type": [ | ||
"error", | ||
{ | ||
"default": "generic" | ||
} | ||
], | ||
"@typescript-eslint/ban-types": [ | ||
"error", | ||
{ | ||
"extendDefaults": true | ||
} | ||
], | ||
"@typescript-eslint/consistent-type-definitions": [ | ||
"error", | ||
"interface" | ||
] | ||
} | ||
} | ||
], | ||
"prettier": true, | ||
"rules": { | ||
"unicorn/prefer-string-replace-all": "off" | ||
} | ||
} | ||
} |
@@ -15,15 +15,15 @@ # unist-util-inspect | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`inspect(tree[, options])`](#inspecttree-options) | ||
* [`inspectColor(tree[, options])`](#inspectcolortree-options) | ||
* [`inspectNoColor(tree[, options])`](#inspectnocolortree-options) | ||
* [`Options`](#options) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
* [What is this?](#what-is-this) | ||
* [When should I use this?](#when-should-i-use-this) | ||
* [Install](#install) | ||
* [Use](#use) | ||
* [API](#api) | ||
* [`inspect(tree[, options])`](#inspecttree-options) | ||
* [`inspectColor(tree[, options])`](#inspectcolortree-options) | ||
* [`inspectNoColor(tree[, options])`](#inspectnocolortree-options) | ||
* [`Options`](#options) | ||
* [Types](#types) | ||
* [Compatibility](#compatibility) | ||
* [Contribute](#contribute) | ||
* [License](#license) | ||
@@ -106,6 +106,6 @@ ## What is this? | ||
* `tree` ([`Node`][node]) | ||
— tree to inspect | ||
* `options` ([`Options`][api-options], optional) | ||
— configuration | ||
* `tree` ([`Node`][node]) | ||
— tree to inspect | ||
* `options` ([`Options`][api-options], optional) | ||
— configuration | ||
@@ -118,2 +118,4 @@ ###### Returns | ||
> 🪦 **Deprecated**: use `color` option of `inspect`. | ||
Inspect a tree, with color. | ||
@@ -124,2 +126,4 @@ Otherwise same as [`inspect`][api-inspect]. | ||
> 🪦 **Deprecated**: use `color` option of `inspect`. | ||
Inspect a tree, without color. | ||
@@ -134,4 +138,6 @@ Otherwise same as [`inspect`][api-inspect]. | ||
* `showPositions` (`boolean`, default: `true`) | ||
— whether to include positional information | ||
* `color` (`boolean`, default: `true` in Node, `false` otherwise) | ||
— whether to use ANSI colors | ||
* `showPositions` (`boolean`, default: `true`) | ||
— whether to include positional information | ||
@@ -138,0 +144,0 @@ ## Types |
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
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
21881
16
462
227