From 5a4f767130e50002c98e214382a4dc74ab29f74a Mon Sep 17 00:00:00 2001 From: cola119 Date: Tue, 22 Mar 2022 18:48:09 +0900 Subject: [PATCH] debugger: add serializers for Map and Set of RemoteObject --- lib/internal/debugger/inspect_repl.js | 60 +++++++++++++++---- ...test-debugger-object-type-remote-object.js | 14 ++--- 2 files changed, 57 insertions(+), 17 deletions(-) diff --git a/lib/internal/debugger/inspect_repl.js b/lib/internal/debugger/inspect_repl.js index 4e7231fb268f803..a333d16faf85065 100644 --- a/lib/internal/debugger/inspect_repl.js +++ b/lib/internal/debugger/inspect_repl.js @@ -219,17 +219,52 @@ class ObjectPreview { [customInspectSymbol](depth, opts) { switch (this.type) { case 'object': { - const props = ArrayPrototypeMap(this.properties, (prop, idx) => { - const value = utilInspect(new PropertyPreview(prop)); - if (prop.name === `${idx}`) return value; - return `${prop.name}: ${value}`; - }); - if (this.overflow) { - ArrayPrototypePush(props, '...'); + switch (this.subtype) { + case 'date': + return utilInspect(new Date(this.description), opts); + case 'null': + return utilInspect(null, opts); + case 'regexp': + return opts.stylize(this.description, 'regexp'); + case 'set': { + if (!this.entries) { + return `${this.description} ${this.overflow ? '{ ... }' : '{}'}`; + } + const values = ArrayPrototypeMap(this.entries, (entry) => + utilInspect(new ObjectPreview(entry.value), opts)); + return `${this.description} { ${ArrayPrototypeJoin(values, ', ')} }`; + } + case 'map': { + if (!this.entries) { + return `${this.description} ${this.overflow ? '{ ... }' : '{}'}`; + } + const mappings = ArrayPrototypeMap(this.entries, (entry) => { + const key = utilInspect(new ObjectPreview(entry.key), opts); + const value = utilInspect(new ObjectPreview(entry.value), opts); + return `${key} => ${value}`; + }); + return `${this.description} { ${ArrayPrototypeJoin(mappings, ', ')} }`; + } + case 'array': + case undefined: { + if (this.properties.length === 0) { + return this.subtype === 'array' ? '[]' : '{}'; + } + const props = ArrayPrototypeMap(this.properties, (prop, idx) => { + const value = utilInspect(new PropertyPreview(prop)); + if (prop.name === `${idx}`) return value; + return `${prop.name}: ${value}`; + }); + if (this.overflow) { + ArrayPrototypePush(props, '...'); + } + const singleLine = ArrayPrototypeJoin(props, ', '); + const propString = singleLine.length > 60 ? ArrayPrototypeJoin(props, ',\n ') : singleLine; + return this.subtype === 'array' ? `[ ${propString} ]` : `{ ${propString} }`; + } + default: + return this.description; } - const singleLine = ArrayPrototypeJoin(props, ', '); - const propString = singleLine.length > 60 ? ArrayPrototypeJoin(props, ',\n ') : singleLine; - return this.subtype === 'array' ? `[ ${propString} ]` : `{ ${propString} }`; } default: return this.description; @@ -268,6 +303,11 @@ class RemoteObject { return utilInspect(null, opts); case 'regexp': return opts.stylize(this.description, 'regexp'); + case 'map': + case 'set': { + const preview = utilInspect(new ObjectPreview(this.preview), opts); + return `${this.description} ${preview}`; + } default: break; } diff --git a/test/parallel/test-debugger-object-type-remote-object.js b/test/parallel/test-debugger-object-type-remote-object.js index 9b992ed97476e2c..32b0b89d33b2acd 100644 --- a/test/parallel/test-debugger-object-type-remote-object.js +++ b/test/parallel/test-debugger-object-type-remote-object.js @@ -24,21 +24,21 @@ cli.waitForInitialBreak() .then(() => cli.command('exec /regex/g')) .then(() => assert.match(cli.output, /\/regex\/g/)) .then(() => cli.command('exec new Map()')) - .then(() => assert.match(cli.output, /{ size: 0 }/)) + .then(() => assert.match(cli.output, /Map\(0\) {}/)) .then(() => cli.command('exec new Map([["a",1],["b",2]])')) - .then(() => assert.match(cli.output, /{ size: 2 }/)) + .then(() => assert.match(cli.output, /Map\(2\) { a => 1, b => 2 }/)) .then(() => cli.command('exec new Set()')) - .then(() => assert.match(cli.output, /{ size: 0 }/)) + .then(() => assert.match(cli.output, /Set\(0\) {}/)) .then(() => cli.command('exec new Set([1,2])')) - .then(() => assert.match(cli.output, /{ size: 2 }/)) + .then(() => assert.match(cli.output, /Set\(2\) { 1, 2 }/)) .then(() => cli.command('exec new Set([{a:1},new Set([1])])')) - .then(() => assert.match(cli.output, /{ size: 2 }/)) + .then(() => assert.match(cli.output, /Set\(2\) { { a: 1 }, Set\(1\) { \.\.\. } }/)) .then(() => cli.command('exec a={}; a')) - .then(() => assert.match(cli.output, /{ {2}}/)) + .then(() => assert.match(cli.output, /{}/)) .then(() => cli.command('exec a={a:1,b:{c:1}}; a')) .then(() => assert.match(cli.output, /{ a: 1, b: Object }/)) .then(() => cli.command('exec a=[]; a')) - .then(() => assert.match(cli.output, /\[ {2}\]/)) + .then(() => assert.match(cli.output, /\[\]/)) .then(() => cli.command('exec a=[1,2]; a')) .then(() => assert.match(cli.output, /\[ 1, 2 \]/)) .then(() => cli.quit())