From 9751e1ca8fac9289b8643a077611aa6539c87033 Mon Sep 17 00:00:00 2001 From: "Yingbo (Max) Wang" Date: Tue, 25 Sep 2018 00:24:20 -0700 Subject: [PATCH 1/5] Add default console.log depth and console.dir with depth parameter. --- js/console.ts | 27 ++++++++++++++++++++------- js/console_test.ts | 8 ++++++++ 2 files changed, 28 insertions(+), 7 deletions(-) diff --git a/js/console.ts b/js/console.ts index c4c36e7b21f565..5714a32ed48f0a 100644 --- a/js/console.ts +++ b/js/console.ts @@ -1,5 +1,9 @@ // tslint:disable-next-line:no-any type ConsoleContext = Set; +type ConsoleOptions = Partial<{ showHidden: boolean, depth: number, colors: boolean }>; + +// Default depth of logging nested objects +const DEFAULT_MAX_DEPTH = 2; // tslint:disable-next-line:no-any function getClassInstanceName(instance: any): string { @@ -13,7 +17,7 @@ function getClassInstanceName(instance: any): string { } // tslint:disable-next-line:no-any -function stringify(ctx: ConsoleContext, value: any): string { +function stringify(ctx: ConsoleContext, value: any, level: number, maxLevel: number): string { switch (typeof value) { case "string": return value; @@ -39,12 +43,16 @@ function stringify(ctx: ConsoleContext, value: any): string { return "[Circular]"; } + if (level > maxLevel) { + return `[object]`; + } + ctx.add(value); const entries: string[] = []; if (Array.isArray(value)) { for (const el of value) { - entries.push(stringifyWithQuotes(ctx, el)); + entries.push(stringifyWithQuotes(ctx, el, level + 1, maxLevel)); } ctx.delete(value); @@ -63,7 +71,7 @@ function stringify(ctx: ConsoleContext, value: any): string { } for (const key of Object.keys(value)) { - entries.push(`${key}: ${stringifyWithQuotes(ctx, value[key])}`); + entries.push(`${key}: ${stringifyWithQuotes(ctx, value[key], level + 1, maxLevel)}`); } ctx.delete(value); @@ -87,24 +95,24 @@ function stringify(ctx: ConsoleContext, value: any): string { // Print strings when they are inside of arrays or objects with quotes // tslint:disable-next-line:no-any -function stringifyWithQuotes(ctx: ConsoleContext, value: any): string { +function stringifyWithQuotes(ctx: ConsoleContext, value: any, level: number, maxLevel: number): string { switch (typeof value) { case "string": return `"${value}"`; default: - return stringify(ctx, value); + return stringify(ctx, value, level, maxLevel); } } // tslint:disable-next-line:no-any -export function stringifyArgs(args: any[]): string { +export function stringifyArgs(args: any[], options: ConsoleOptions = {}): string { const out: string[] = []; for (const a of args) { if (typeof a === "string") { out.push(a); } else { // tslint:disable-next-line:no-any - out.push(stringify(new Set(), a)); + out.push(stringify(new Set(), a, 0, options.depth || DEFAULT_MAX_DEPTH)); } } return out.join(" "); @@ -123,6 +131,11 @@ export class Console { debug = this.log; info = this.log; + // tslint:disable-next-line:no-any + dir(obj: any, options: ConsoleOptions = {}) { + this.printFunc(stringifyArgs([obj], { depth: options.depth || DEFAULT_MAX_DEPTH })); + } + // tslint:disable-next-line:no-any warn(...args: any[]): void { // TODO Log to stderr. diff --git a/js/console_test.ts b/js/console_test.ts index cfe75dafe3ce68..93fb9e066e0781 100644 --- a/js/console_test.ts +++ b/js/console_test.ts @@ -91,3 +91,11 @@ test(function consoleTestStringifyCircular() { "Console { printFunc: [Function], debug: [Function: log], info: [Function: log], error: [Function: warn] }" ); }); + +test(function consoleTestStringifyWithDepth() { + const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } }; + assertEqual(stringifyArgs([nestedObj], { depth: 3 }), "{ a: { b: { c: { d: [object] } } } }"); + assertEqual(stringifyArgs([nestedObj], { depth: 4 }), "{ a: { b: { c: { d: { e: [object] } } } } }"); + assertEqual(stringifyArgs([nestedObj], { depth: 0 }), "{ a: { b: { c: [object] } } }"); + assertEqual(stringifyArgs([nestedObj], { depth: null }), "{ a: { b: { c: [object] } } }"); +}); From 3e4592f56c1ca7cea71e0913162306f87675b441 Mon Sep 17 00:00:00 2001 From: "Yingbo (Max) Wang" Date: Tue, 25 Sep 2018 16:48:57 -0700 Subject: [PATCH 2/5] Run format.py and lint.py. --- js/compiler.ts | 8 ++++---- js/compiler_test.ts | 16 +++++++++++---- js/console.ts | 50 +++++++++++++++++++++++++++++++++++---------- js/console_test.ts | 20 ++++++++++++++---- src/flags.rs | 6 ++---- src/handlers.rs | 2 +- 6 files changed, 74 insertions(+), 28 deletions(-) diff --git a/js/compiler.ts b/js/compiler.ts index f0f82ffb111fae..cf4d1906af83d3 100644 --- a/js/compiler.ts +++ b/js/compiler.ts @@ -418,10 +418,10 @@ export class DenoCompiler */ compile(moduleMetaData: ModuleMetaData): OutputCode { const recompile = !!this.recompile; - this._log( - "compiler.compile", - { filename: moduleMetaData.fileName, recompile } - ); + this._log("compiler.compile", { + filename: moduleMetaData.fileName, + recompile + }); if (!recompile && moduleMetaData.outputCode) { return moduleMetaData.outputCode; } diff --git a/js/compiler_test.ts b/js/compiler_test.ts index fef18ec3d26bca..e3be535bd45f4e 100644 --- a/js/compiler_test.ts +++ b/js/compiler_test.ts @@ -461,16 +461,24 @@ test(function compilerGetScriptFileNames() { test(function compilerRecompileFlag() { setup(); compilerInstance.run("foo/bar.ts", "/root/project"); - assertEqual(getEmitOutputStack.length, 1, "Expected only a single emitted file."); + assertEqual( + getEmitOutputStack.length, + 1, + "Expected only a single emitted file." + ); // running compiler against same file should use cached code compilerInstance.run("foo/bar.ts", "/root/project"); - assertEqual(getEmitOutputStack.length, 1, "Expected only a single emitted file."); + assertEqual( + getEmitOutputStack.length, + 1, + "Expected only a single emitted file." + ); compilerInstance.recompile = true; compilerInstance.run("foo/bar.ts", "/root/project"); assertEqual(getEmitOutputStack.length, 2, "Expected two emitted file."); assert( - getEmitOutputStack[0] === getEmitOutputStack[1], - "Expected same file to be emitted twice." + getEmitOutputStack[0] === getEmitOutputStack[1], + "Expected same file to be emitted twice." ); teardown(); }); diff --git a/js/console.ts b/js/console.ts index 5714a32ed48f0a..04c65aa0004e41 100644 --- a/js/console.ts +++ b/js/console.ts @@ -1,6 +1,10 @@ // tslint:disable-next-line:no-any type ConsoleContext = Set; -type ConsoleOptions = Partial<{ showHidden: boolean, depth: number, colors: boolean }>; +type ConsoleOptions = Partial<{ + showHidden: boolean; + depth: number; + colors: boolean; +}>; // Default depth of logging nested objects const DEFAULT_MAX_DEPTH = 2; @@ -16,8 +20,13 @@ function getClassInstanceName(instance: any): string { return ""; } -// tslint:disable-next-line:no-any -function stringify(ctx: ConsoleContext, value: any, level: number, maxLevel: number): string { +function stringify( + ctx: ConsoleContext, + // tslint:disable-next-line:no-any + value: any, + level: number, + maxLevel: number +): string { switch (typeof value) { case "string": return value; @@ -71,7 +80,14 @@ function stringify(ctx: ConsoleContext, value: any, level: number, maxLevel: num } for (const key of Object.keys(value)) { - entries.push(`${key}: ${stringifyWithQuotes(ctx, value[key], level + 1, maxLevel)}`); + entries.push( + `${key}: ${stringifyWithQuotes( + ctx, + value[key], + level + 1, + maxLevel + )}` + ); } ctx.delete(value); @@ -94,8 +110,13 @@ function stringify(ctx: ConsoleContext, value: any, level: number, maxLevel: num } // Print strings when they are inside of arrays or objects with quotes -// tslint:disable-next-line:no-any -function stringifyWithQuotes(ctx: ConsoleContext, value: any, level: number, maxLevel: number): string { +function stringifyWithQuotes( + ctx: ConsoleContext, + // tslint:disable-next-line:no-any + value: any, + level: number, + maxLevel: number +): string { switch (typeof value) { case "string": return `"${value}"`; @@ -104,15 +125,20 @@ function stringifyWithQuotes(ctx: ConsoleContext, value: any, level: number, max } } -// tslint:disable-next-line:no-any -export function stringifyArgs(args: any[], options: ConsoleOptions = {}): string { +export function stringifyArgs( + // tslint:disable-next-line:no-any + args: any[], + options: ConsoleOptions = {} +): string { const out: string[] = []; for (const a of args) { if (typeof a === "string") { out.push(a); } else { - // tslint:disable-next-line:no-any - out.push(stringify(new Set(), a, 0, options.depth || DEFAULT_MAX_DEPTH)); + out.push( + // tslint:disable-next-line:no-any + stringify(new Set(), a, 0, options.depth || DEFAULT_MAX_DEPTH) + ); } } return out.join(" "); @@ -133,7 +159,9 @@ export class Console { // tslint:disable-next-line:no-any dir(obj: any, options: ConsoleOptions = {}) { - this.printFunc(stringifyArgs([obj], { depth: options.depth || DEFAULT_MAX_DEPTH })); + this.printFunc( + stringifyArgs([obj], { depth: options.depth || DEFAULT_MAX_DEPTH }) + ); } // tslint:disable-next-line:no-any diff --git a/js/console_test.ts b/js/console_test.ts index 93fb9e066e0781..d77a144568dd92 100644 --- a/js/console_test.ts +++ b/js/console_test.ts @@ -94,8 +94,20 @@ test(function consoleTestStringifyCircular() { test(function consoleTestStringifyWithDepth() { const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } }; - assertEqual(stringifyArgs([nestedObj], { depth: 3 }), "{ a: { b: { c: { d: [object] } } } }"); - assertEqual(stringifyArgs([nestedObj], { depth: 4 }), "{ a: { b: { c: { d: { e: [object] } } } } }"); - assertEqual(stringifyArgs([nestedObj], { depth: 0 }), "{ a: { b: { c: [object] } } }"); - assertEqual(stringifyArgs([nestedObj], { depth: null }), "{ a: { b: { c: [object] } } }"); + assertEqual( + stringifyArgs([nestedObj], { depth: 3 }), + "{ a: { b: { c: { d: [object] } } } }" + ); + assertEqual( + stringifyArgs([nestedObj], { depth: 4 }), + "{ a: { b: { c: { d: { e: [object] } } } } }" + ); + assertEqual( + stringifyArgs([nestedObj], { depth: 0 }), + "{ a: { b: { c: [object] } } }" + ); + assertEqual( + stringifyArgs([nestedObj], { depth: null }), + "{ a: { b: { c: [object] } } }" + ); }); diff --git a/src/flags.rs b/src/flags.rs index ceeb9e564dc16a..5c10416a28179b 100644 --- a/src/flags.rs +++ b/src/flags.rs @@ -157,8 +157,7 @@ fn parse_core_args(args: Vec) -> (Vec, Vec) { } true - }) - .collect(); + }).collect(); // Replace args being sent to V8 for idx in 0..args.len() { @@ -224,7 +223,6 @@ pub fn v8_set_flags(args: Vec) -> Vec { let cstr = CStr::from_ptr(*ptr as *const i8); let slice = cstr.to_str().unwrap(); slice.to_string() - }) - .chain(rest.into_iter()) + }).chain(rest.into_iter()) .collect() } diff --git a/src/handlers.rs b/src/handlers.rs index eef0911747b357..769d8313b4bb4c 100644 --- a/src/handlers.rs +++ b/src/handlers.rs @@ -146,7 +146,7 @@ fn permission_denied() -> DenoError { fn not_implemented() -> DenoError { DenoError::from(std::io::Error::new( std::io::ErrorKind::Other, - "Not implemented" + "Not implemented", )) } From b1646b065f8a14d6d195ca8615de05cd68a9e42f Mon Sep 17 00:00:00 2001 From: "Yingbo (Max) Wang" Date: Tue, 25 Sep 2018 17:21:04 -0700 Subject: [PATCH 3/5] Correctly handle case when passed-in depth equals 0. Simplify ConsoleOptions passing. --- js/console.ts | 17 +++++++++++------ js/console_test.ts | 13 +++++-------- 2 files changed, 16 insertions(+), 14 deletions(-) diff --git a/js/console.ts b/js/console.ts index 04c65aa0004e41..4e94b3f5af7720 100644 --- a/js/console.ts +++ b/js/console.ts @@ -52,7 +52,7 @@ function stringify( return "[Circular]"; } - if (level > maxLevel) { + if (level >= maxLevel) { return `[object]`; } @@ -136,8 +136,15 @@ export function stringifyArgs( out.push(a); } else { out.push( - // tslint:disable-next-line:no-any - stringify(new Set(), a, 0, options.depth || DEFAULT_MAX_DEPTH) + // use default maximum depth for null or undefined argument + stringify( + // tslint:disable-next-line:no-any + new Set(), + a, + 0, + // tslint:disable-next-line:triple-equals + options.depth != undefined ? options.depth : DEFAULT_MAX_DEPTH + ) ); } } @@ -159,9 +166,7 @@ export class Console { // tslint:disable-next-line:no-any dir(obj: any, options: ConsoleOptions = {}) { - this.printFunc( - stringifyArgs([obj], { depth: options.depth || DEFAULT_MAX_DEPTH }) - ); + this.printFunc(stringifyArgs([obj], options)); } // tslint:disable-next-line:no-any diff --git a/js/console_test.ts b/js/console_test.ts index d77a144568dd92..a3308839227f7c 100644 --- a/js/console_test.ts +++ b/js/console_test.ts @@ -68,7 +68,7 @@ test(function consoleTestStringifyCircular() { nestedObj.o = circularObj; - const nestedObjExpected = `{ num: 1, bool: true, str: "a", method: [Function: method], asyncMethod: [AsyncFunction: asyncMethod], generatorMethod: [GeneratorFunction: generatorMethod], un: undefined, nu: null, arrowFunc: [Function: arrowFunc], extendedClass: Extended { a: 1, b: 2 }, nFunc: [Function], extendedCstr: [Function: Extended], o: { num: 2, bool: false, str: "b", method: [Function: method], un: undefined, nu: null, nested: [Circular], emptyObj: {}, arr: [ 1, "s", false, null, [Circular] ], baseClass: Base { a: 1 } } }`; + const nestedObjExpected = `{ num: 1, bool: true, str: "a", method: [Function: method], asyncMethod: [AsyncFunction: asyncMethod], generatorMethod: [GeneratorFunction: generatorMethod], un: undefined, nu: null, arrowFunc: [Function: arrowFunc], extendedClass: Extended { a: 1, b: 2 }, nFunc: [Function], extendedCstr: [Function: Extended], o: { num: 2, bool: false, str: "b", method: [Function: method], un: undefined, nu: null, nested: [Circular], emptyObj: [object], arr: [object], baseClass: [object] } }`; assertEqual(stringify(1), "1"); assertEqual(stringify("s"), "s"); @@ -96,18 +96,15 @@ test(function consoleTestStringifyWithDepth() { const nestedObj: any = { a: { b: { c: { d: { e: { f: 42 } } } } } }; assertEqual( stringifyArgs([nestedObj], { depth: 3 }), - "{ a: { b: { c: { d: [object] } } } }" + "{ a: { b: { c: [object] } } }" ); assertEqual( stringifyArgs([nestedObj], { depth: 4 }), - "{ a: { b: { c: { d: { e: [object] } } } } }" - ); - assertEqual( - stringifyArgs([nestedObj], { depth: 0 }), - "{ a: { b: { c: [object] } } }" + "{ a: { b: { c: { d: [object] } } } }" ); + assertEqual(stringifyArgs([nestedObj], { depth: 0 }), "[object]"); assertEqual( stringifyArgs([nestedObj], { depth: null }), - "{ a: { b: { c: [object] } } }" + "{ a: { b: [object] } }" ); }); From d982e950583f289d8c26fca41849adbba283900e Mon Sep 17 00:00:00 2001 From: "Yingbo (Max) Wang" Date: Sun, 30 Sep 2018 10:43:04 -0700 Subject: [PATCH 4/5] Execute ./tools/format.py. --- js/console.ts | 22 +++++++++++++++++----- js/console_test.ts | 3 ++- js/dom_types.ts | 8 ++++---- js/fetch_test.ts | 23 +++++++++++++---------- website/app.js | 2 +- 5 files changed, 37 insertions(+), 21 deletions(-) diff --git a/js/console.ts b/js/console.ts index 7b5e208a540c41..717ab89066a33b 100644 --- a/js/console.ts +++ b/js/console.ts @@ -34,10 +34,15 @@ function createFunctionString(value: Function, ctx: ConsoleContext): string { } // tslint:disable-next-line:no-any -function createArrayString(value: any[], ctx: ConsoleContext, level: number, maxLevel: number): string { +function createArrayString( + value: any[], + ctx: ConsoleContext, + level: number, + maxLevel: number +): string { const entries: string[] = []; for (const el of value) { - entries.push(stringifyWithQuotes(ctx, el, level+1, maxLevel)); + entries.push(stringifyWithQuotes(ctx, el, level + 1, maxLevel)); } ctx.delete(value); if (entries.length === 0) { @@ -47,7 +52,12 @@ function createArrayString(value: any[], ctx: ConsoleContext, level: number, max } // tslint:disable-next-line:no-any -function createObjectString(value: any, ctx: ConsoleContext, level: number, maxLevel: number): string { +function createObjectString( + value: any, + ctx: ConsoleContext, + level: number, + maxLevel: number +): string { const entries: string[] = []; let baseString = ""; @@ -58,7 +68,9 @@ function createObjectString(value: any, ctx: ConsoleContext, level: number, maxL } for (const key of Object.keys(value)) { - entries.push(`${key}: ${stringifyWithQuotes(ctx, value[key], level+1, maxLevel)}`); + entries.push( + `${key}: ${stringifyWithQuotes(ctx, value[key], level + 1, maxLevel)}` + ); } ctx.delete(value); @@ -105,7 +117,7 @@ function stringify( if (level >= maxLevel) { return `[object]`; } - + ctx.add(value); if (value instanceof Error) { diff --git a/js/console_test.ts b/js/console_test.ts index 609b6d7a7f947a..9e6a37256ce3ff 100644 --- a/js/console_test.ts +++ b/js/console_test.ts @@ -107,7 +107,8 @@ test(function consoleTestStringifyWithDepth() { stringifyArgs([nestedObj], { depth: null }), "{ a: { b: [object] } }" ); - +}); + test(function consoleTestError() { class MyError extends Error { constructor(msg: string) { diff --git a/js/dom_types.ts b/js/dom_types.ts index e3f75e48869d04..d59f1165e3860e 100644 --- a/js/dom_types.ts +++ b/js/dom_types.ts @@ -103,8 +103,8 @@ export interface URLSearchParams { */ set(name: string, value: string): void; /** - * Sort all key/value pairs contained in this object in place - * and return undefined. The sort order is according to Unicode + * Sort all key/value pairs contained in this object in place + * and return undefined. The sort order is according to Unicode * code points of the keys. */ sort(): void; @@ -112,8 +112,8 @@ export interface URLSearchParams { * Returns a query string suitable for use in a URL. */ toString(): string; - /** - * Iterates over each name-value pair in the query + /** + * Iterates over each name-value pair in the query * and invokes the given function. */ forEach( diff --git a/js/fetch_test.ts b/js/fetch_test.ts index 9d76cb6ba4bb84..7e2177350ea3cf 100644 --- a/js/fetch_test.ts +++ b/js/fetch_test.ts @@ -44,7 +44,7 @@ testPerm({ net: true }, async function fetchBlob() { assertEqual(blob.size, Number(headers.get("Content-Length"))); }); -// Logic heavily copied from web-platform-tests, make +// Logic heavily copied from web-platform-tests, make // sure pass mostly header basic test /* tslint:disable-next-line:max-line-length */ // ref: https://github.com/web-platform-tests/wpt/blob/7c50c216081d6ea3c9afe553ee7b64534020a1b2/fetch/api/headers/headers-basic.html @@ -68,17 +68,17 @@ test(function newHeaderTest() { }); const headerDict = { - "name1": "value1", - "name2": "value2", - "name3": "value3", - "name4": undefined, + name1: "value1", + name2: "value2", + name3: "value3", + name4: undefined, "Content-Type": "value4" }; const headerSeq = []; for (const name in headerDict) { headerSeq.push([name, headerDict[name]]); } - + test(function newHeaderWithSequence() { const headers = new Headers(headerSeq); for (const name in headerDict) { @@ -123,7 +123,10 @@ test(function headerHasSuccess() { for (const name in headerDict) { assert(headers.has(name), "headers has name " + name); /* tslint:disable-next-line:max-line-length */ - assert(!headers.has("nameNotInHeaders"), "headers do not have header: nameNotInHeaders"); + assert( + !headers.has("nameNotInHeaders"), + "headers do not have header: nameNotInHeaders" + ); } }); @@ -145,9 +148,9 @@ test(function headerGetSuccess() { }); const headerEntriesDict = { - "name1": "value1", - "Name2": "value2", - "name": "value3", + name1: "value1", + Name2: "value2", + name: "value3", "content-Type": "value4", "Content-Typ": "value5", "Content-Types": "value6" diff --git a/website/app.js b/website/app.js index 3281cd3cf721c9..da29797a8640b1 100644 --- a/website/app.js +++ b/website/app.js @@ -148,7 +148,7 @@ export async function main() { categories: sha1List }, y: { - label: "seconds", + label: "seconds" } } }); From 44b628b4329a8098ce6401f14a1148f38ad11b22 Mon Sep 17 00:00:00 2001 From: "Yingbo (Max) Wang" Date: Sun, 30 Sep 2018 14:27:38 -0700 Subject: [PATCH 5/5] Add tslint:disable rules. --- js/console.ts | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/js/console.ts b/js/console.ts index 717ab89066a33b..51798d79d7d970 100644 --- a/js/console.ts +++ b/js/console.ts @@ -33,8 +33,8 @@ function createFunctionString(value: Function, ctx: ConsoleContext): string { return `[${cstrName}]`; } -// tslint:disable-next-line:no-any function createArrayString( + // tslint:disable-next-line:no-any value: any[], ctx: ConsoleContext, level: number, @@ -51,8 +51,8 @@ function createArrayString( return `[ ${entries.join(", ")} ]`; } -// tslint:disable-next-line:no-any function createObjectString( + // tslint:disable-next-line:no-any value: any, ctx: ConsoleContext, level: number,