1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-12-31 11:34:15 -05:00

Add Deno.symbols and move internal fields for test (#3693)

This commit is contained in:
Kevin (Kun) "Kassimo" Qian 2020-01-16 16:42:58 -08:00 committed by Ry Dahl
parent cad7b3e4fe
commit 0a78bfb836
15 changed files with 92 additions and 25 deletions

View file

@ -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);

View file

@ -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$/, "");
}

View file

@ -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";

View file

@ -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);

View file

@ -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;

View file

@ -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.

View file

@ -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

16
cli/js/internals.ts Normal file
View file

@ -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
});
}

10
cli/js/internals_test.ts Normal file
View file

@ -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);
});

View file

@ -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

View file

@ -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);

View file

@ -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
)
};
}

13
cli/js/symbols.ts Normal file
View file

@ -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
};

7
cli/js/symbols_test.ts Normal file
View file

@ -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);
});

View file

@ -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";