Skip to content

Commit

Permalink
expose Console as global
Browse files Browse the repository at this point in the history
  • Loading branch information
bartlomieju committed Sep 4, 2019
1 parent 5c7c876 commit 5afed50
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 3 deletions.
1 change: 0 additions & 1 deletion js/console.ts
Original file line number Diff line number Diff line change
Expand Up @@ -492,7 +492,6 @@ export class Console {
indentLevel: number;
[isConsoleInstance]: boolean = false;

/** @internal */
constructor(private printFunc: PrintFunc) {
this.indentLevel = 0;
this[isConsoleInstance] = true;
Expand Down
1 change: 1 addition & 0 deletions js/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ window.fetch = fetchTypes.fetch;
window.clearTimeout = timers.clearTimeout;
window.clearInterval = timers.clearInterval;
window.console = new consoleTypes.Console(core.print);
window.Console = consoleTypes.Console;
window.setTimeout = timers.setTimeout;
window.setInterval = timers.setInterval;
window.location = (undefined as unknown) as domTypes.Location;
Expand Down
23 changes: 22 additions & 1 deletion js/globals_test.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
// Copyright 2018-2019 the Deno authors. All rights reserved. MIT license.
import { test, assert } from "./test_util.ts";
import {test, assert, assertArrayContains, assertEquals} from "./test_util.ts";

test(function globalThisExists(): void {
assert(globalThis != null);
Expand Down Expand Up @@ -103,3 +103,24 @@ test(async function windowQueueMicrotask(): Promise<void> {
await p1;
await p2;
});

test(function consoleAssignToWindow(): void {
const originalConsole = window.console;
const captured: Array<[string, boolean]> = [];
const capturingConsole = new Console((x: string, isErr?: boolean): void => {
captured.push([x, !!isErr]);
});

window.console = capturingConsole;
console.log("this is log");
console.error("this is error");
window.console = originalConsole;

assertEquals(
JSON.stringify([
["this is log\n", false],
["this is error\n", true],
]),
JSON.stringify(captured)
);
});
4 changes: 4 additions & 0 deletions js/lib.deno_runtime.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1274,6 +1274,7 @@ declare const fetch: typeof fetchTypes.fetch;
declare const clearTimeout: typeof timers.clearTimeout;
declare const clearInterval: typeof timers.clearInterval;
declare const console: consoleTypes.Console;
declare const Console: typeof consoleTypes.Console;
declare const setTimeout: typeof timers.setTimeout;
declare const setInterval: typeof timers.setInterval;
declare const location: domTypes.Location;
Expand Down Expand Up @@ -1314,6 +1315,7 @@ declare const removeEventListener: (
) => void;

declare type Blob = blob.DenoBlob;
declare type Console = typeof consoleTypes.Console;
declare type File = domTypes.DomFile;
declare type CustomEventInit = customEvent.CustomEventInit;
declare type CustomEvent = customEvent.CustomEvent;
Expand Down Expand Up @@ -1957,7 +1959,9 @@ declare namespace consoleTypes {
static kClearScreenDown: string;
}
const isConsoleInstance: unique symbol;
type PrintFunc = (x: string, isErr?: boolean) => void;
export class Console {
constructor(printFunc: PrintFunc);
private printFunc;
indentLevel: number;
[isConsoleInstance]: boolean;
Expand Down
1 change: 1 addition & 0 deletions js/test_util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export {
assertNotEquals,
assertStrictEq,
assertStrContains,
assertArrayContains,
unreachable
} from "./deps/https/deno.land/std/testing/asserts.ts";

Expand Down
2 changes: 1 addition & 1 deletion third_party
Submodule third_party updated 2713 files

0 comments on commit 5afed50

Please sign in to comment.