diff options
author | cjihrig <cjihrig@gmail.com> | 2022-06-12 15:20:16 -0400 |
---|---|---|
committer | Colin Ihrig <cjihrig@gmail.com> | 2022-06-15 09:52:28 -0400 |
commit | eadf943e594a5ebf95ddf7e007cfb7d2dcff130e (patch) | |
tree | a5360b4b296e17342aa6731209963e3d86e6757c /ext/console/02_console.js | |
parent | 5bde4c7ecae35cb29a003504eafa03f1776ee3a0 (diff) |
fix(console): constrol inspect() indent with option (#14867)
This commit updates the Deno.inspect() logic to use the
indentLevel option to control indentation instead of passing
around separate indent/level parameters internally.
Refs: https://github.com/denoland/deno/issues/8099
Refs: https://github.com/denoland/deno/issues/14171
Diffstat (limited to 'ext/console/02_console.js')
-rw-r--r-- | ext/console/02_console.js | 148 |
1 files changed, 65 insertions, 83 deletions
diff --git a/ext/console/02_console.js b/ext/console/02_console.js index 71eb58198..b98a4a1ba 100644 --- a/ext/console/02_console.js +++ b/ext/console/02_console.js @@ -329,13 +329,13 @@ return inspectOptions.colors ? fn : (s) => s; } - function inspectFunction(value, level, inspectOptions) { + function inspectFunction(value, inspectOptions) { const cyan = maybeColor(colors.cyan, inspectOptions); if ( ReflectHas(value, customInspect) && typeof value[customInspect] === "function" ) { - return String(value[customInspect](inspect, inspectOptions, level)); + return String(value[customInspect](inspect, inspectOptions)); } // Might be Function/AsyncFunction/GeneratorFunction/AsyncGeneratorFunction let cstrName = ObjectGetPrototypeOf(value)?.constructor?.name; @@ -357,7 +357,6 @@ ) { const [propString, refIndex] = inspectRawObject( value, - level, inspectOptions, ); refStr = refIndex; @@ -380,12 +379,11 @@ function inspectIterable( value, - level, options, inspectOptions, ) { const cyan = maybeColor(colors.cyan, inspectOptions); - if (level >= inspectOptions.depth) { + if (inspectOptions.indentLevel >= inspectOptions.depth) { return cyan(`[${options.typeName}]`); } @@ -435,15 +433,16 @@ throw err; } if (entriesLength < inspectOptions.iterableLimit) { + inspectOptions.indentLevel++; ArrayPrototypePush( entries, options.entryHandler( el, - level + 1, inspectOptions, FunctionPrototypeBind(next, iter), ), ); + inspectOptions.indentLevel--; } entriesLength++; } @@ -459,6 +458,7 @@ const iPrefix = `${options.displayName ? options.displayName + " " : ""}`; + const level = inspectOptions.indentLevel; const initIndentation = `\n${ StringPrototypeRepeat(DEFAULT_INDENT, level + 1) }`; @@ -630,12 +630,11 @@ function _inspectValue( value, - level, inspectOptions, ) { const proxyDetails = core.getProxyDetails(value); if (proxyDetails != null && inspectOptions.showProxy) { - return inspectProxy(proxyDetails, level, inspectOptions); + return inspectProxy(proxyDetails, inspectOptions); } const green = maybeColor(colors.green, inspectOptions); @@ -665,7 +664,7 @@ return handleCircular(value, cyan); } - return inspectFunction(value, level, inspectOptions); + return inspectFunction(value, inspectOptions); case "object": // null is bold if (value === null) { return bold("null"); @@ -675,7 +674,7 @@ return handleCircular(value, cyan); } - return inspectObject(value, level, inspectOptions, proxyDetails); + return inspectObject(value, inspectOptions, proxyDetails); default: // Not implemented is red return red("[Not Implemented]"); @@ -684,13 +683,12 @@ function inspectValue( value, - level, inspectOptions, ) { ArrayPrototypePush(CTX_STACK, value); let x; try { - x = _inspectValue(value, level, inspectOptions); + x = _inspectValue(value, inspectOptions); } finally { ArrayPrototypePop(CTX_STACK); } @@ -790,7 +788,6 @@ // Print strings when they are inside of arrays or objects with quotes function inspectValueWithQuotes( value, - level, inspectOptions, ) { const abbreviateSize = @@ -806,13 +803,12 @@ return green(quoteString(trunc)); // Quoted strings are green } default: - return inspectValue(value, level, inspectOptions); + return inspectValue(value, inspectOptions); } } function inspectArray( value, - level, inspectOptions, ) { const gray = maybeColor(colors.gray, inspectOptions); @@ -820,7 +816,7 @@ typeName: "Array", displayName: "", delims: ["[", "]"], - entryHandler: (entry, level, inspectOptions, next) => { + entryHandler: (entry, inspectOptions, next) => { const [index, val] = entry; let i = index; if (!ObjectPrototypeHasOwnProperty(value, i)) { @@ -833,19 +829,18 @@ const ending = emptyItems > 1 ? "s" : ""; return gray(`<${emptyItems} empty item${ending}>`); } else { - return inspectValueWithQuotes(val, level, inspectOptions); + return inspectValueWithQuotes(val, inspectOptions); } }, group: inspectOptions.compact, sort: false, }; - return inspectIterable(value, level, options, inspectOptions); + return inspectIterable(value, options, inspectOptions); } function inspectTypedArray( typedArrayName, value, - level, inspectOptions, ) { const valueLength = value.length; @@ -853,60 +848,62 @@ typeName: typedArrayName, displayName: `${typedArrayName}(${valueLength})`, delims: ["[", "]"], - entryHandler: (entry, level, inspectOptions) => { + entryHandler: (entry, inspectOptions) => { const val = entry[1]; - return inspectValueWithQuotes(val, level + 1, inspectOptions); + inspectOptions.indentLevel++; + const inspectedValue = inspectValueWithQuotes(val, inspectOptions); + inspectOptions.indentLevel--; + return inspectedValue; }, group: inspectOptions.compact, sort: false, }; - return inspectIterable(value, level, options, inspectOptions); + return inspectIterable(value, options, inspectOptions); } function inspectSet( value, - level, inspectOptions, ) { const options = { typeName: "Set", displayName: "Set", delims: ["{", "}"], - entryHandler: (entry, level, inspectOptions) => { + entryHandler: (entry, inspectOptions) => { const val = entry[1]; - return inspectValueWithQuotes(val, level + 1, inspectOptions); + inspectOptions.indentLevel++; + const inspectedValue = inspectValueWithQuotes(val, inspectOptions); + inspectOptions.indentLevel--; + return inspectedValue; }, group: false, sort: inspectOptions.sorted, }; - return inspectIterable(value, level, options, inspectOptions); + return inspectIterable(value, options, inspectOptions); } function inspectMap( value, - level, inspectOptions, ) { const options = { typeName: "Map", displayName: "Map", delims: ["{", "}"], - entryHandler: (entry, level, inspectOptions) => { + entryHandler: (entry, inspectOptions) => { const [key, val] = entry; - return `${ - inspectValueWithQuotes( - key, - level + 1, - inspectOptions, - ) - } => ${inspectValueWithQuotes(val, level + 1, inspectOptions)}`; + inspectOptions.indentLevel++; + const inspectedValue = `${ + inspectValueWithQuotes(key, inspectOptions) + } => ${inspectValueWithQuotes(val, inspectOptions)}`; + inspectOptions.indentLevel--; + return inspectedValue; }, group: false, sort: inspectOptions.sorted, }; return inspectIterable( value, - level, options, inspectOptions, ); @@ -1046,7 +1043,6 @@ function inspectPromise( value, - level, inspectOptions, ) { const cyan = maybeColor(colors.cyan, inspectOptions); @@ -1062,17 +1058,13 @@ ? "" : `${red("<rejected>")} `; - const str = `${prefix}${ - inspectValueWithQuotes( - result, - level + 1, - inspectOptions, - ) - }`; + inspectOptions.indentLevel++; + const str = `${prefix}${inspectValueWithQuotes(result, inspectOptions)}`; + inspectOptions.indentLevel--; if (str.length + PROMISE_STRING_BASE_LENGTH > LINE_BREAKING_LENGTH) { return `Promise {\n${ - StringPrototypeRepeat(DEFAULT_INDENT, level + 1) + StringPrototypeRepeat(DEFAULT_INDENT, inspectOptions.indentLevel + 1) }${str}\n}`; } @@ -1081,20 +1073,18 @@ function inspectProxy( targetAndHandler, - level, inspectOptions, ) { - return `Proxy ${inspectArray(targetAndHandler, level, inspectOptions)}`; + return `Proxy ${inspectArray(targetAndHandler, inspectOptions)}`; } function inspectRawObject( value, - level, inspectOptions, ) { const cyan = maybeColor(colors.cyan, inspectOptions); - if (level >= inspectOptions.depth) { + if (inspectOptions.indentLevel >= inspectOptions.depth) { return [cyan("[Object]"), ""]; // wrappers are in cyan } @@ -1130,6 +1120,8 @@ const red = maybeColor(colors.red, inspectOptions); + inspectOptions.indentLevel++; + for (const key of stringKeys) { if (inspectOptions.getters) { let propertyValue; @@ -1140,11 +1132,7 @@ error = error_; } const inspectedValue = error == null - ? inspectValueWithQuotes( - propertyValue, - level + 1, - inspectOptions, - ) + ? inspectValueWithQuotes(propertyValue, inspectOptions) : red(`[Thrown ${error.name}: ${error.message}]`); ArrayPrototypePush( entries, @@ -1163,7 +1151,7 @@ ArrayPrototypePush( entries, `${maybeQuoteString(key)}: ${ - inspectValueWithQuotes(value[key], level + 1, inspectOptions) + inspectValueWithQuotes(value[key], inspectOptions) }`, ); } @@ -1187,11 +1175,7 @@ error = error_; } const inspectedValue = error == null - ? inspectValueWithQuotes( - propertyValue, - level + 1, - inspectOptions, - ) + ? inspectValueWithQuotes(propertyValue, inspectOptions) : red(`Thrown ${error.name}: ${error.message}`); ArrayPrototypePush( entries, @@ -1210,22 +1194,30 @@ ArrayPrototypePush( entries, `[${maybeQuoteSymbol(key)}]: ${ - inspectValueWithQuotes(value[key], level + 1, inspectOptions) + inspectValueWithQuotes(value[key], inspectOptions) }`, ); } } } + inspectOptions.indentLevel--; + // Making sure color codes are ignored when calculating the total length - const totalLength = entries.length + level + + const totalLength = entries.length + inspectOptions.indentLevel + colors.stripColor(ArrayPrototypeJoin(entries, "")).length; if (entries.length === 0) { baseString = "{}"; } else if (totalLength > LINE_BREAKING_LENGTH || !inspectOptions.compact) { - const entryIndent = StringPrototypeRepeat(DEFAULT_INDENT, level + 1); - const closingIndent = StringPrototypeRepeat(DEFAULT_INDENT, level); + const entryIndent = StringPrototypeRepeat( + DEFAULT_INDENT, + inspectOptions.indentLevel + 1, + ); + const closingIndent = StringPrototypeRepeat( + DEFAULT_INDENT, + inspectOptions.indentLevel, + ); baseString = `{\n${entryIndent}${ ArrayPrototypeJoin(entries, `,\n${entryIndent}`) }${inspectOptions.trailingComma ? "," : ""}\n${closingIndent}}`; @@ -1250,7 +1242,6 @@ function inspectObject( value, - level, inspectOptions, proxyDetails, ) { @@ -1258,7 +1249,7 @@ ReflectHas(value, customInspect) && typeof value[customInspect] === "function" ) { - return String(value[customInspect](inspect, inspectOptions, level)); + return String(value[customInspect](inspect, inspectOptions)); } // This non-unique symbol is used to support op_crates, ie. // in extensions/web we don't want to depend on public @@ -1274,13 +1265,13 @@ // to the `Deno` namespace in web workers. Remove when the `Deno` // namespace is always enabled. return String( - value[privateCustomInspect](inspect, inspectOptions, level), + value[privateCustomInspect](inspect, inspectOptions), ); } if (ObjectPrototypeIsPrototypeOf(ErrorPrototype, value)) { return inspectError(value, maybeColor(colors.cyan, inspectOptions)); } else if (ArrayIsArray(value)) { - return inspectArray(value, level, inspectOptions); + return inspectArray(value, inspectOptions); } else if (ObjectPrototypeIsPrototypeOf(NumberPrototype, value)) { return inspectNumberObject(value, inspectOptions); } else if (ObjectPrototypeIsPrototypeOf(BigIntPrototype, value)) { @@ -1292,7 +1283,7 @@ } else if (ObjectPrototypeIsPrototypeOf(SymbolPrototype, value)) { return inspectSymbolObject(value, inspectOptions); } else if (ObjectPrototypeIsPrototypeOf(PromisePrototype, value)) { - return inspectPromise(value, level, inspectOptions); + return inspectPromise(value, inspectOptions); } else if (ObjectPrototypeIsPrototypeOf(RegExpPrototype, value)) { return inspectRegExp(value, inspectOptions); } else if (ObjectPrototypeIsPrototypeOf(DatePrototype, value)) { @@ -1300,13 +1291,11 @@ } else if (ObjectPrototypeIsPrototypeOf(SetPrototype, value)) { return inspectSet( proxyDetails ? proxyDetails[0] : value, - level, inspectOptions, ); } else if (ObjectPrototypeIsPrototypeOf(MapPrototype, value)) { return inspectMap( proxyDetails ? proxyDetails[0] : value, - level, inspectOptions, ); } else if (ObjectPrototypeIsPrototypeOf(WeakSetPrototype, value)) { @@ -1317,12 +1306,11 @@ return inspectTypedArray( ObjectGetPrototypeOf(value).constructor.name, value, - level, inspectOptions, ); } else { // Otherwise, default object formatting - let [insp, refIndex] = inspectRawObject(value, level, inspectOptions); + let [insp, refIndex] = inspectRawObject(value, inspectOptions); insp = refIndex + insp; return insp; } @@ -1836,11 +1824,7 @@ } } else if (ArrayPrototypeIncludes(["O", "o"], char)) { // Format as an object. - formattedArg = inspectValue( - args[a++], - 0, - rInspectOptions, - ); + formattedArg = inspectValue(args[a++], rInspectOptions); } else if (char == "c") { const value = args[a++]; if (!noColor) { @@ -1881,7 +1865,7 @@ string += args[a]; } else { // Use default maximum depth for null or undefined arguments. - string += inspectValue(args[a], 0, rInspectOptions); + string += inspectValue(args[a], rInspectOptions); } } @@ -2052,7 +2036,7 @@ } const stringifyValue = (value) => - inspectValueWithQuotes(value, 0, { + inspectValueWithQuotes(value, { ...DEFAULT_INSPECT_OPTIONS, depth: 1, }); @@ -2219,11 +2203,9 @@ inspectOptions = {}, ) { circular = undefined; - return inspectValue(value, 0, { + return inspectValue(value, { ...DEFAULT_INSPECT_OPTIONS, ...inspectOptions, - // TODO(nayeemrmn): Indent level is not supported. - indentLevel: 0, }); } |