Skip to content

Commit

Permalink
debugger: add serializers for Map and Set of RemoteObject
Browse files Browse the repository at this point in the history
  • Loading branch information
cola119 committed Mar 21, 2022
1 parent 7820dd4 commit a69bc81
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 17 deletions.
55 changes: 45 additions & 10 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -219,17 +219,33 @@ 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 '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;
Expand Down Expand Up @@ -268,6 +284,25 @@ class RemoteObject {
return utilInspect(null, opts);
case 'regexp':
return opts.stylize(this.description, 'regexp');
case 'set': {
if (!this.preview.entries) {
return `${this.description} {}`;
}
const values = ArrayPrototypeMap(this.preview.entries, (entry) =>
utilInspect(new ObjectPreview(entry.value), opts));
return `${this.description} { ${ArrayPrototypeJoin(values, ', ')} }`;
}
case 'map': {
if (!this.preview.entries) {
return `${this.description} {}`;
}
const mappings = ArrayPrototypeMap(this.preview.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, ', ')} }`;
}
default:
break;
}
Expand Down
14 changes: 7 additions & 7 deletions test/parallel/test-debugger-object-type-remote-object.js
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit a69bc81

Please sign in to comment.