|
| 1 | +/** |
| 2 | + * Minimal subset of @vitest/pretty-format for forwarded console objects. |
| 3 | + * |
| 4 | + * This keeps its basic/complex printer split, path-based circular reference |
| 5 | + * tracking, and bounded array/object traversal. It omits plugins, colors, |
| 6 | + * indentation, specialized collection printers, toJSON calls, output-budget |
| 7 | + * heuristics, and option validation. |
| 8 | + * |
| 9 | + * At introduction, this formatter and its forward-console integration added |
| 10 | + * about 2.7 kB raw / 1.0 kB gzip to Vite's client bundle, compared with an |
| 11 | + * estimated 27.6 kB raw / 7.0 kB gzip for a tree-shaken direct dependency on |
| 12 | + * @vitest/pretty-format. |
| 13 | + * |
| 14 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 15 | + * Copyright (c) 2021-Present VoidZero Inc. and Vitest contributors. |
| 16 | + * MIT License. |
| 17 | + */ |
| 18 | + |
| 19 | +interface Config { |
| 20 | + maxDepth: number |
| 21 | + maxWidth: number |
| 22 | +} |
| 23 | + |
| 24 | +export type PrettyFormatOptions = Partial<Config> |
| 25 | + |
| 26 | +type Refs = unknown[] |
| 27 | + |
| 28 | +const DEFAULT_OPTIONS: Config = { |
| 29 | + // Node's default inspect depth is 2 levels below the root. This formatter |
| 30 | + // counts the root as the first level, so 3 produces equivalent nesting. |
| 31 | + maxDepth: 3, |
| 32 | + // Match Node's default maxArrayLength, and also apply it to object properties |
| 33 | + // so both deep and wide object graphs remain bounded. |
| 34 | + maxWidth: 100, |
| 35 | +} |
| 36 | + |
| 37 | +const objectToString = Object.prototype.toString |
| 38 | +const errorToString = Error.prototype.toString |
| 39 | +const identifierRE = /^[a-z_$][\w$]*$/i |
| 40 | + |
| 41 | +export function prettyFormat( |
| 42 | + value: unknown, |
| 43 | + options: PrettyFormatOptions = {}, |
| 44 | +): string { |
| 45 | + const config: Config = { ...DEFAULT_OPTIONS, ...options } |
| 46 | + return printer(value, config, 0, []) |
| 47 | +} |
| 48 | + |
| 49 | +function printer( |
| 50 | + value: unknown, |
| 51 | + config: Config, |
| 52 | + depth: number, |
| 53 | + refs: Refs, |
| 54 | +): string { |
| 55 | + const basic = printBasicValue(value) |
| 56 | + return basic ?? printComplexValue(value as object, config, depth, refs) |
| 57 | +} |
| 58 | + |
| 59 | +function printBasicValue(value: unknown): string | undefined { |
| 60 | + if (value == null) { |
| 61 | + return String(value) |
| 62 | + } |
| 63 | + if (typeof value === 'string') { |
| 64 | + return JSON.stringify(value) |
| 65 | + } |
| 66 | + if (typeof value === 'number') { |
| 67 | + return Object.is(value, -0) ? '-0' : String(value) |
| 68 | + } |
| 69 | + if (typeof value === 'bigint') { |
| 70 | + return `${value}n` |
| 71 | + } |
| 72 | + if (typeof value === 'boolean' || typeof value === 'symbol') { |
| 73 | + return String(value) |
| 74 | + } |
| 75 | + if (typeof value === 'function') { |
| 76 | + return value.name ? `[Function: ${value.name}]` : '[Function]' |
| 77 | + } |
| 78 | + |
| 79 | + const type = objectToString.call(value) |
| 80 | + if (type === '[object Date]') { |
| 81 | + return Number.isNaN(+(value as Date)) |
| 82 | + ? 'Date { NaN }' |
| 83 | + : (value as Date).toISOString() |
| 84 | + } |
| 85 | + if (type === '[object Error]' || value instanceof Error) { |
| 86 | + return `[${errorToString.call(value)}]` |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +function printComplexValue( |
| 91 | + value: object, |
| 92 | + config: Config, |
| 93 | + depth: number, |
| 94 | + refs: Refs, |
| 95 | +): string { |
| 96 | + if (refs.includes(value)) { |
| 97 | + return '[Circular]' |
| 98 | + } |
| 99 | + refs = [...refs, value] |
| 100 | + |
| 101 | + const hitMaxDepth = ++depth > config.maxDepth |
| 102 | + if (Array.isArray(value)) { |
| 103 | + return hitMaxDepth |
| 104 | + ? '[Array]' |
| 105 | + : `[${printListItems(value, config, depth, refs)}]` |
| 106 | + } |
| 107 | + return hitMaxDepth |
| 108 | + ? '[Object]' |
| 109 | + : `{${printObjectProperties( |
| 110 | + value as Record<string, unknown>, |
| 111 | + config, |
| 112 | + depth, |
| 113 | + refs, |
| 114 | + )}}` |
| 115 | +} |
| 116 | + |
| 117 | +function printListItems( |
| 118 | + value: unknown[], |
| 119 | + config: Config, |
| 120 | + depth: number, |
| 121 | + refs: Refs, |
| 122 | +): string { |
| 123 | + const width = Math.min(value.length, config.maxWidth) |
| 124 | + const result: string[] = [] |
| 125 | + for (let i = 0; i < width; i++) { |
| 126 | + result.push(i in value ? printer(value[i], config, depth, refs) : '') |
| 127 | + } |
| 128 | + if (width < value.length) { |
| 129 | + result.push(`…(${value.length - width})`) |
| 130 | + } |
| 131 | + return withSpacing(result) |
| 132 | +} |
| 133 | + |
| 134 | +function printObjectProperties( |
| 135 | + value: Record<string, unknown>, |
| 136 | + config: Config, |
| 137 | + depth: number, |
| 138 | + refs: Refs, |
| 139 | +): string { |
| 140 | + const keys = Object.keys(value) |
| 141 | + const width = Math.min(keys.length, config.maxWidth) |
| 142 | + const result: string[] = [] |
| 143 | + for (let i = 0; i < width; i++) { |
| 144 | + const key = keys[i] |
| 145 | + const name = |
| 146 | + key !== '__proto__' && identifierRE.test(key) ? key : JSON.stringify(key) |
| 147 | + result.push(`${name}: ${printer(value[key], config, depth, refs)}`) |
| 148 | + } |
| 149 | + if (width < keys.length) { |
| 150 | + result.push(`…(${keys.length - width})`) |
| 151 | + } |
| 152 | + return withSpacing(result) |
| 153 | +} |
| 154 | + |
| 155 | +function withSpacing(values: string[]): string { |
| 156 | + return values.length === 0 ? '' : ` ${values.join(', ')} ` |
| 157 | +} |
0 commit comments