diff options
author | 迷渡 <justjavac@gmail.com> | 2018-12-15 03:36:08 +0800 |
---|---|---|
committer | Ryan Dahl <ry@tinyclouds.org> | 2018-12-14 14:36:08 -0500 |
commit | 769994bd4ee352b636a6a5e58d346cf07539bfe6 (patch) | |
tree | 716d3b4219854a24c25d4b8f482dac544cc93e27 /js/console.ts | |
parent | 0bb43ebbfcbc378810f75c43a2be3369729921f7 (diff) |
`console.assert` should not throw error (#1335)
Diffstat (limited to 'js/console.ts')
-rw-r--r-- | js/console.ts | 22 |
1 files changed, 19 insertions, 3 deletions
diff --git a/js/console.ts b/js/console.ts index bb597d9f2..311ca83b7 100644 --- a/js/console.ts +++ b/js/console.ts @@ -383,11 +383,27 @@ export class Console { /** Writes an error message to stdout if the assertion is `false`. If the * assertion is `true`, nothing happens. + * + * ref: https://console.spec.whatwg.org/#assert */ // tslint:disable-next-line:no-any - assert = (condition: boolean, ...args: any[]): void => { - if (!condition) { - throw new Error(`Assertion failed: ${stringifyArgs(args)}`); + assert = (condition?: boolean, ...args: any[]): void => { + if (condition) { + return; } + + if (args.length === 0) { + this.error("Assertion failed"); + return; + } + + const [first, ...rest] = args; + + if (typeof first === "string") { + this.error(`Assertion failed: ${first}`, ...rest); + return; + } + + this.error(`Assertion failed:`, ...args); }; } |