summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSteven Guerrero <stephenguerrero43@gmail.com>2020-11-19 07:23:42 -0500
committerGitHub <noreply@github.com>2020-11-19 07:23:42 -0500
commit315d889afa38e976b106a3769cab206db31d5ce8 (patch)
treeb3243c90f6678cb6d2909bc9f6acceb412b21a6f
parente3c3fc58cbc5053866928d33ffe25340c2bacc02 (diff)
fix(std/node): correct typings for global, globalThis, window (#8363)
-rw-r--r--std/node/buffer_test.ts14
-rw-r--r--std/node/global.d.ts28
-rw-r--r--std/node/global.ts25
-rw-r--r--std/node/global_test.ts52
-rw-r--r--std/node/process_test.ts1
5 files changed, 89 insertions, 31 deletions
diff --git a/std/node/buffer_test.ts b/std/node/buffer_test.ts
index a360c91ff..7fbc8f3ad 100644
--- a/std/node/buffer_test.ts
+++ b/std/node/buffer_test.ts
@@ -1,20 +1,8 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import { assert, assertEquals, assertThrows } from "../testing/asserts.ts";
+import { assertEquals, assertThrows } from "../testing/asserts.ts";
import { Buffer } from "./buffer.ts";
Deno.test({
- name: "Buffer global scope",
- fn() {
- // deno-lint-ignore ban-ts-comment
- // @ts-expect-error
- assert(window.Buffer === Buffer);
- // deno-lint-ignore ban-ts-comment
- // @ts-expect-error
- assert(globalThis.Buffer === Buffer);
- },
-});
-
-Deno.test({
name: "alloc fails on negative numbers",
fn() {
assertThrows(
diff --git a/std/node/global.d.ts b/std/node/global.d.ts
new file mode 100644
index 000000000..b02a682c6
--- /dev/null
+++ b/std/node/global.d.ts
@@ -0,0 +1,28 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { process as processModule } from "./process.ts";
+import { Buffer as bufferModule } from "./buffer.ts";
+
+// d.ts files allow us to declare Buffer as a value and as a type
+// type something = Buffer | something_else; is quite common
+
+type GlobalType = {
+ process: typeof processModule;
+ Buffer: typeof bufferModule;
+};
+
+declare global {
+ interface Window {
+ global: GlobalType;
+ }
+
+ interface globalThis {
+ global: GlobalType;
+ }
+
+ var global: GlobalType;
+ var process: typeof processModule;
+ var Buffer: typeof bufferModule;
+ type Buffer = bufferModule;
+}
+
+export {};
diff --git a/std/node/global.ts b/std/node/global.ts
index b102edddc..0ef688e63 100644
--- a/std/node/global.ts
+++ b/std/node/global.ts
@@ -1,35 +1,24 @@
// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
-import process from "./process.ts";
-import { Buffer as buffer } from "./buffer.ts";
+/// <reference path="./global.d.ts" />
+import { process as processModule } from "./process.ts";
+import { Buffer as bufferModule } from "./_buffer.ts";
-Object.defineProperty(globalThis, Symbol.toStringTag, {
- value: "global",
+Object.defineProperty(globalThis, "global", {
+ value: globalThis,
writable: false,
enumerable: false,
configurable: true,
});
-// deno-lint-ignore no-explicit-any
-(globalThis as any)["global"] = globalThis;
-
-// Define the type for the global declration
-type Process = typeof process;
-type Buffer = typeof buffer;
-
Object.defineProperty(globalThis, "process", {
- value: process,
+ value: processModule,
enumerable: false,
writable: true,
configurable: true,
});
-declare global {
- const process: Process;
- const Buffer: Buffer;
-}
-
Object.defineProperty(globalThis, "Buffer", {
- value: buffer,
+ value: bufferModule,
enumerable: false,
writable: true,
configurable: true,
diff --git a/std/node/global_test.ts b/std/node/global_test.ts
new file mode 100644
index 000000000..9ce0b2132
--- /dev/null
+++ b/std/node/global_test.ts
@@ -0,0 +1,52 @@
+import "./global.ts";
+import { assert, assertStrictEquals } from "../testing/asserts.ts";
+import { Buffer as BufferModule } from "./buffer.ts";
+import processModule from "./process.ts";
+
+// Definitions for this are quite delicate
+// This ensures modifications to the global namespace don't break on TypeScript
+
+// TODO
+// Deno lint marks globals defined by this module as undefined
+// probably gonna change in the future
+
+Deno.test("global is correctly defined", () => {
+ // deno-lint-ignore no-undef
+ assertStrictEquals(global, globalThis);
+ // deno-lint-ignore no-undef
+ assertStrictEquals(global.Buffer, BufferModule);
+ // deno-lint-ignore no-undef
+ assertStrictEquals(global.process, process);
+});
+
+Deno.test("Buffer is correctly defined", () => {
+ //Check that Buffer is defined as a type as well
+ type x = Buffer;
+ // deno-lint-ignore no-undef
+ assertStrictEquals(Buffer, BufferModule);
+ // deno-lint-ignore no-undef
+ assert(Buffer.from);
+ // deno-lint-ignore no-undef
+ assertStrictEquals(global.Buffer, BufferModule);
+ // deno-lint-ignore no-undef
+ assert(global.Buffer.from);
+ assertStrictEquals(globalThis.Buffer, BufferModule);
+ assert(globalThis.Buffer.from);
+ assertStrictEquals(window.Buffer, BufferModule);
+ assert(window.Buffer.from);
+});
+
+Deno.test("process is correctly defined", () => {
+ // deno-lint-ignore no-undef
+ assertStrictEquals(process, processModule);
+ // deno-lint-ignore no-undef
+ assert(process.arch);
+ // deno-lint-ignore no-undef
+ assertStrictEquals(global.process, processModule);
+ // deno-lint-ignore no-undef
+ assert(global.process.arch);
+ assertStrictEquals(globalThis.process, processModule);
+ assert(globalThis.process.arch);
+ assertStrictEquals(window.process, processModule);
+ assert(window.process.arch);
+});
diff --git a/std/node/process_test.ts b/std/node/process_test.ts
index ee566853e..34ce50d7a 100644
--- a/std/node/process_test.ts
+++ b/std/node/process_test.ts
@@ -6,6 +6,7 @@ import * as path from "../path/mod.ts";
import * as all from "./process.ts";
import { argv, env } from "./process.ts";
import { delay } from "../async/delay.ts";
+import "./global.ts";
// NOTE: Deno.execPath() (and thus process.argv) currently requires --allow-env
// (Also Deno.env.toObject() (and process.env) requires --allow-env but it's more obvious)