diff options
author | Kevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com> | 2019-08-17 09:51:51 -0700 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2019-08-17 12:51:51 -0400 |
commit | 9acb17742fc2c7ee503e6b2be8f88b97dae65dc6 (patch) | |
tree | 0a25e25a8f7049a1c124fb7825ce43af6eafc582 /js/console.ts | |
parent | de713e42c8807e3124c9b5d418a69d2ea3460058 (diff) |
Implement console.trace() (#2780)
groupCollapsed alias to group, remove noTrailingNewline, move newline
out of stringifyArgs, fix console.dir, add tests, and fix a repl log quirk.
For repl logging quirks, I believe we should not indent repl logging. If
we really want such indentation, we probably also want to indent "> "
prompts.
Diffstat (limited to 'js/console.ts')
-rw-r--r-- | js/console.ts | 54 |
1 files changed, 23 insertions, 31 deletions
diff --git a/js/console.ts b/js/console.ts index ed6040f1d..002be6e2f 100644 --- a/js/console.ts +++ b/js/console.ts @@ -11,7 +11,6 @@ type ConsoleOptions = Partial<{ depth: number; colors: boolean; indentLevel: number; - collapsedAt: number | null; }>; // Default depth of logging nested objects @@ -469,18 +468,13 @@ export function stringifyArgs( a++; } - const { collapsedAt, indentLevel } = options; - const isCollapsed = - collapsedAt != null && indentLevel != null && collapsedAt <= indentLevel; - if (!isCollapsed) { - if (indentLevel != null && indentLevel > 0) { - const groupIndent = " ".repeat(indentLevel); - if (str.indexOf("\n") !== -1) { - str = str.replace(/\n/g, `\n${groupIndent}`); - } - str = groupIndent + str; + const { indentLevel } = options; + if (indentLevel != null && indentLevel > 0) { + const groupIndent = " ".repeat(indentLevel); + if (str.indexOf("\n") !== -1) { + str = str.replace(/\n/g, `\n${groupIndent}`); } - str += "\n"; + str = groupIndent + str; } return str; @@ -494,13 +488,11 @@ export const isConsoleInstance = Symbol("isConsoleInstance"); export class Console { indentLevel: number; - collapsedAt: number | null; [isConsoleInstance]: boolean = false; /** @internal */ constructor(private printFunc: PrintFunc) { this.indentLevel = 0; - this.collapsedAt = null; this[isConsoleInstance] = true; // ref https://console.spec.whatwg.org/#console-namespace @@ -516,9 +508,8 @@ export class Console { log = (...args: unknown[]): void => { this.printFunc( stringifyArgs(args, { - indentLevel: this.indentLevel, - collapsedAt: this.collapsedAt - }), + indentLevel: this.indentLevel + }) + "\n", false ); }; @@ -530,16 +521,15 @@ export class Console { /** Writes the properties of the supplied `obj` to stdout */ dir = (obj: unknown, options: ConsoleOptions = {}): void => { - this.log(stringifyArgs([obj], options)); + this.printFunc(stringifyArgs([obj], options) + "\n", false); }; /** Writes the arguments to stdout */ warn = (...args: unknown[]): void => { this.printFunc( stringifyArgs(args, { - indentLevel: this.indentLevel, - collapsedAt: this.collapsedAt - }), + indentLevel: this.indentLevel + }) + "\n", true ); }; @@ -732,21 +722,12 @@ export class Console { this.indentLevel += 2; }; - groupCollapsed = (...label: unknown[]): void => { - if (this.collapsedAt == null) { - this.collapsedAt = this.indentLevel; - } - this.group(...label); - }; + groupCollapsed = this.group; groupEnd = (): void => { if (this.indentLevel > 0) { this.indentLevel -= 2; } - if (this.collapsedAt != null && this.collapsedAt >= this.indentLevel) { - this.collapsedAt = null; - this.log(); // When the collapsed state ended, outputs a sinle new line. - } }; clear = (): void => { @@ -755,6 +736,17 @@ export class Console { clearScreenDown(stdout); }; + trace = (...args: unknown[]): void => { + const message = stringifyArgs(args, { indentLevel: 0 }); + const err = { + name: "Trace", + message + }; + // @ts-ignore + Error.captureStackTrace(err, this.trace); + this.error((err as Error).stack); + }; + static [Symbol.hasInstance](instance: Console): boolean { return instance[isConsoleInstance]; } |