summaryrefslogtreecommitdiff
path: root/cli/js
diff options
context:
space:
mode:
authorKevin (Kun) "Kassimo" Qian <kevinkassimo@gmail.com>2020-01-16 16:42:58 -0800
committerRy Dahl <ry@tinyclouds.org>2020-01-16 19:42:58 -0500
commit0a78bfb836f520d3709a32c8a978d08d33198d83 (patch)
treeaa96e2164ea39d8856a4eca7eeb5c21388d8a4b7 /cli/js
parentcad7b3e4fe4e368882628dbde8512f36d142098b (diff)
Add Deno.symbols and move internal fields for test (#3693)
Diffstat (limited to 'cli/js')
-rw-r--r--cli/js/console.ts9
-rw-r--r--cli/js/console_test.ts10
-rw-r--r--cli/js/deno.ts14
-rw-r--r--cli/js/error_stack.ts3
-rw-r--r--cli/js/error_stack_test.ts2
-rw-r--r--cli/js/globals.ts6
-rw-r--r--cli/js/headers_test.ts2
-rw-r--r--cli/js/internals.ts16
-rw-r--r--cli/js/internals_test.ts10
-rw-r--r--cli/js/lib.deno_runtime.d.ts14
-rw-r--r--cli/js/mixins/dom_iterable.ts4
-rw-r--r--cli/js/mixins/dom_iterable_test.ts5
-rw-r--r--cli/js/symbols.ts13
-rw-r--r--cli/js/symbols_test.ts7
-rw-r--r--cli/js/unit_tests.ts2
15 files changed, 92 insertions, 25 deletions
diff --git a/cli/js/console.ts b/cli/js/console.ts
index f366eb776..f5830b4a1 100644
--- a/cli/js/console.ts
+++ b/cli/js/console.ts
@@ -4,6 +4,7 @@ import { TypedArray } from "./types.ts";
import { TextEncoder } from "./text_encoding.ts";
import { File, stdout } from "./files.ts";
import { cliTable } from "./console_table.ts";
+import { exposeForTest } from "./internals.ts";
type ConsoleContext = Set<unknown>;
type ConsoleOptions = Partial<{
@@ -363,9 +364,7 @@ function createObjectString(
}
}
-/** TODO Do not expose this from "deno" namespace.
- * @internal
- */
+/** @internal */
export function stringifyArgs(
args: unknown[],
options: ConsoleOptions = {}
@@ -785,3 +784,7 @@ export function inspect(value: unknown, options?: ConsoleOptions): string {
);
}
}
+
+// Expose these fields to internalObject for tests.
+exposeForTest("Console", Console);
+exposeForTest("stringifyArgs", stringifyArgs);
diff --git a/cli/js/console_test.ts b/cli/js/console_test.ts
index 126ad868b..b80dd8284 100644
--- a/cli/js/console_test.ts
+++ b/cli/js/console_test.ts
@@ -4,15 +4,19 @@ import { assert, assertEquals, test } from "./test_util.ts";
// Some of these APIs aren't exposed in the types and so we have to cast to any
// in order to "trick" TypeScript.
const {
- Console,
- customInspect,
- stringifyArgs,
inspect,
writeSync,
stdout
// eslint-disable-next-line @typescript-eslint/no-explicit-any
} = Deno as any;
+const customInspect = Deno.symbols.customInspect;
+const {
+ Console,
+ stringifyArgs
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+} = Deno[Deno.symbols.internal] as any;
+
function stringify(...args: unknown[]): string {
return stringifyArgs(args).replace(/\n$/, "");
}
diff --git a/cli/js/deno.ts b/cli/js/deno.ts
index ed100ef22..a754bc4e2 100644
--- a/cli/js/deno.ts
+++ b/cli/js/deno.ts
@@ -100,7 +100,7 @@ export {
Signal
} from "./process.ts";
export { transpileOnly, compile, bundle } from "./compiler_api.ts";
-export { inspect, customInspect } from "./console.ts";
+export { inspect } from "./console.ts";
export { build, OperatingSystem, Arch } from "./build.ts";
export { version } from "./version.ts";
export const args: string[] = [];
@@ -110,18 +110,10 @@ export const args: string[] = [];
/** @internal */
export { core } from "./core.ts";
-/** @internal */
-export { setPrepareStackTrace } from "./error_stack.ts";
-
-// TODO Don't expose Console nor stringifyArgs.
-/** @internal */
-export { Console, stringifyArgs } from "./console.ts";
-// TODO Don't expose DomIterableMixin.
-/** @internal */
-export { DomIterableMixin } from "./mixins/dom_iterable.ts";
-
/** The current process id of the runtime. */
export let pid: number;
/** Reflects the NO_COLOR environment variable: https://no-color.org/ */
export let noColor: boolean;
+
+export { symbols } from "./symbols.ts";
diff --git a/cli/js/error_stack.ts b/cli/js/error_stack.ts
index 824fa6f87..7f19773bd 100644
--- a/cli/js/error_stack.ts
+++ b/cli/js/error_stack.ts
@@ -4,6 +4,7 @@
import * as dispatch from "./dispatch.ts";
import { sendSync } from "./dispatch_json.ts";
import { assert } from "./util.ts";
+import { exposeForTest } from "./internals.ts";
export interface Location {
/** The full url for the module, e.g. `file://some/file.ts` or
@@ -271,3 +272,5 @@ function prepareStackTrace(
export function setPrepareStackTrace(ErrorConstructor: typeof Error): void {
ErrorConstructor.prepareStackTrace = prepareStackTrace;
}
+
+exposeForTest("setPrepareStackTrace", setPrepareStackTrace);
diff --git a/cli/js/error_stack_test.ts b/cli/js/error_stack_test.ts
index 79711ace4..e4e44c77f 100644
--- a/cli/js/error_stack_test.ts
+++ b/cli/js/error_stack_test.ts
@@ -2,7 +2,7 @@
import { test, assert } from "./test_util.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
-const { setPrepareStackTrace } = Deno as any;
+const { setPrepareStackTrace } = Deno[Deno.symbols.internal] as any;
interface CallSite {
getThis(): unknown;
diff --git a/cli/js/globals.ts b/cli/js/globals.ts
index d06bc2f6f..c7f3b23f2 100644
--- a/cli/js/globals.ts
+++ b/cli/js/globals.ts
@@ -32,6 +32,8 @@ import * as request from "./request.ts";
// symbols required.
import { core } from "./core.ts";
+import { internalObject } from "./internals.ts";
+
// During the build process, augmentations to the variable `window` in this
// file are tracked and created as part of default library that is built into
// Deno, we only need to declare the enough to compile Deno.
@@ -69,6 +71,10 @@ declare global {
// A self reference to the global object.
window.window = window;
+// Add internal object to Deno object.
+// This is not exposed as part of the Deno types.
+// @ts-ignore
+Deno[Deno.symbols.internal] = internalObject;
// This is the Deno namespace, it is handled differently from other window
// properties when building the runtime type library, as the whole module
// is flattened into a single namespace.
diff --git a/cli/js/headers_test.ts b/cli/js/headers_test.ts
index 52a4594e6..15e1191f5 100644
--- a/cli/js/headers_test.ts
+++ b/cli/js/headers_test.ts
@@ -3,7 +3,7 @@ import { test, assert, assertEquals } from "./test_util.ts";
const {
stringifyArgs
// eslint-disable-next-line @typescript-eslint/no-explicit-any
-} = Deno as any;
+} = Deno[Deno.symbols.internal] as any;
// Logic heavily copied from web-platform-tests, make
// sure pass mostly header basic test
diff --git a/cli/js/internals.ts b/cli/js/internals.ts
new file mode 100644
index 000000000..6aae1be48
--- /dev/null
+++ b/cli/js/internals.ts
@@ -0,0 +1,16 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+export const internalSymbol = Symbol("Deno.internal");
+
+// The object where all the internal fields for testing will be living.
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export const internalObject: { [key: string]: any } = {};
+
+// Register a field to internalObject for test access,
+// through Deno[Deno.symbols.internal][name].
+// eslint-disable-next-line @typescript-eslint/no-explicit-any
+export function exposeForTest(name: string, value: any): void {
+ Object.defineProperty(internalObject, name, {
+ value,
+ enumerable: false
+ });
+}
diff --git a/cli/js/internals_test.ts b/cli/js/internals_test.ts
new file mode 100644
index 000000000..055995fdf
--- /dev/null
+++ b/cli/js/internals_test.ts
@@ -0,0 +1,10 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { test, assert } from "./test_util.ts";
+
+test(function internalsExists(): void {
+ const {
+ stringifyArgs
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
+ } = Deno[Deno.symbols.internal] as any;
+ assert(!!stringifyArgs);
+});
diff --git a/cli/js/lib.deno_runtime.d.ts b/cli/js/lib.deno_runtime.d.ts
index f95236af7..d45ad2540 100644
--- a/cli/js/lib.deno_runtime.d.ts
+++ b/cli/js/lib.deno_runtime.d.ts
@@ -1507,10 +1507,6 @@ declare namespace Deno {
colors: boolean;
indentLevel: number;
}>;
- /** A symbol which can be used as a key for a custom method which will be called
- * when `Deno.inspect()` is called, or when the object is logged to the console.
- */
- export const customInspect: unique symbol;
/**
* `inspect()` converts input into string that has the same format
* as printed by `console.log(...)`;
@@ -1947,6 +1943,16 @@ declare namespace Deno {
// @url js/deno.d.ts
export const args: string[];
+
+ /** Special Deno related symbols. */
+ export const symbols: {
+ /** Symbol to access exposed internal Deno API */
+ readonly internal: unique symbol;
+ /** A symbol which can be used as a key for a custom method which will be called
+ * when `Deno.inspect()` is called, or when the object is logged to the console.
+ */
+ readonly customInspect: unique symbol;
+ };
}
// @url js/globals.ts
diff --git a/cli/js/mixins/dom_iterable.ts b/cli/js/mixins/dom_iterable.ts
index bbd1905ce..dcbd5150e 100644
--- a/cli/js/mixins/dom_iterable.ts
+++ b/cli/js/mixins/dom_iterable.ts
@@ -3,6 +3,7 @@
import { DomIterable } from "../dom_types.ts";
import { window } from "../window.ts";
import { requiredArguments } from "../util.ts";
+import { exposeForTest } from "../internals.ts";
// eslint-disable-next-line @typescript-eslint/no-explicit-any
type Constructor<T = {}> = new (...args: any[]) => T;
@@ -10,7 +11,6 @@ type Constructor<T = {}> = new (...args: any[]) => T;
/** Mixes in a DOM iterable methods into a base class, assumes that there is
* a private data iterable that is part of the base class, located at
* `[dataSymbol]`.
- * TODO Don't expose DomIterableMixin from "deno" namespace.
*/
export function DomIterableMixin<K, V, TBase extends Constructor>(
Base: TBase,
@@ -80,3 +80,5 @@ export function DomIterableMixin<K, V, TBase extends Constructor>(
return DomIterable;
}
+
+exposeForTest("DomIterableMixin", DomIterableMixin);
diff --git a/cli/js/mixins/dom_iterable_test.ts b/cli/js/mixins/dom_iterable_test.ts
index d3147b061..57e655989 100644
--- a/cli/js/mixins/dom_iterable_test.ts
+++ b/cli/js/mixins/dom_iterable_test.ts
@@ -21,7 +21,10 @@ function setup() {
// This is using an internal API we don't want published as types, so having
// to cast to any to "trick" TypeScript
// eslint-disable-next-line @typescript-eslint/no-explicit-any
- DomIterable: (Deno as any).DomIterableMixin(Base, dataSymbol)
+ DomIterable: (Deno[Deno.symbols.internal] as any).DomIterableMixin(
+ Base,
+ dataSymbol
+ )
};
}
diff --git a/cli/js/symbols.ts b/cli/js/symbols.ts
new file mode 100644
index 000000000..4a8a6abfe
--- /dev/null
+++ b/cli/js/symbols.ts
@@ -0,0 +1,13 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { internalSymbol } from "./internals.ts";
+import { customInspect } from "./console.ts";
+
+/** Special Deno related symbols. */
+export const symbols = {
+ /** Symbol to access exposed internal Deno API */
+ internal: internalSymbol,
+ /** A symbol which can be used as a key for a custom method which will be called
+ * when `Deno.inspect()` is called, or when the object is logged to the console.
+ */
+ customInspect
+};
diff --git a/cli/js/symbols_test.ts b/cli/js/symbols_test.ts
new file mode 100644
index 000000000..26fe12375
--- /dev/null
+++ b/cli/js/symbols_test.ts
@@ -0,0 +1,7 @@
+// Copyright 2018-2020 the Deno authors. All rights reserved. MIT license.
+import { test, assert } from "./test_util.ts";
+
+test(function symbolsExists(): void {
+ assert("internal" in Deno.symbols);
+ assert("customInspect" in Deno.symbols);
+});
diff --git a/cli/js/unit_tests.ts b/cli/js/unit_tests.ts
index d886d6f5d..084661ab8 100644
--- a/cli/js/unit_tests.ts
+++ b/cli/js/unit_tests.ts
@@ -25,6 +25,7 @@ import "./form_data_test.ts";
import "./get_random_values_test.ts";
import "./globals_test.ts";
import "./headers_test.ts";
+import "./internals_test.ts";
import "./link_test.ts";
import "./location_test.ts";
import "./make_temp_dir_test.ts";
@@ -42,6 +43,7 @@ import "./rename_test.ts";
import "./request_test.ts";
import "./resources_test.ts";
import "./stat_test.ts";
+import "./symbols_test.ts";
import "./symlink_test.ts";
import "./text_encoding_test.ts";
import "./timers_test.ts";