Skip to content

Commit 7820dd4

Browse files
committed
debugger: refactor serializing logics by adding PropertyPreview and ObjectPreview
1 parent 1d0da48 commit 7820dd4

File tree

1 file changed

+55
-49
lines changed

1 file changed

+55
-49
lines changed

lib/internal/debugger/inspect_repl.js

Lines changed: 55 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,60 @@ function convertResultToError(result) {
183183
return err;
184184
}
185185

186+
class PropertyPreview {
187+
constructor(attributes) {
188+
ObjectAssign(this, attributes);
189+
}
190+
191+
[customInspectSymbol](depth, opts) {
192+
switch (this.type) {
193+
case 'string':
194+
case 'undefined':
195+
return utilInspect(this.value, opts);
196+
case 'number':
197+
case 'boolean':
198+
return opts.stylize(this.value, this.type);
199+
case 'object':
200+
case 'symbol':
201+
if (this.subtype === 'date') {
202+
return utilInspect(new Date(this.value), opts);
203+
}
204+
if (this.subtype === 'array') {
205+
return opts.stylize(this.value, 'special');
206+
}
207+
return opts.stylize(this.value, this.subtype || 'special');
208+
default:
209+
return this.value;
210+
}
211+
}
212+
}
213+
214+
class ObjectPreview {
215+
constructor(attributes) {
216+
ObjectAssign(this, attributes);
217+
}
218+
219+
[customInspectSymbol](depth, opts) {
220+
switch (this.type) {
221+
case 'object': {
222+
const props = ArrayPrototypeMap(this.properties, (prop, idx) => {
223+
const value = utilInspect(new PropertyPreview(prop));
224+
if (prop.name === `${idx}`) return value;
225+
return `${prop.name}: ${value}`;
226+
});
227+
if (this.overflow) {
228+
ArrayPrototypePush(props, '...');
229+
}
230+
const singleLine = ArrayPrototypeJoin(props, ', ');
231+
const propString = singleLine.length > 60 ? ArrayPrototypeJoin(props, ',\n ') : singleLine;
232+
return this.subtype === 'array' ? `[ ${propString} ]` : `{ ${propString} }`;
233+
}
234+
default:
235+
return this.description;
236+
}
237+
}
238+
}
239+
186240
class RemoteObject {
187241
constructor(attributes) {
188242
ObjectAssign(this, attributes);
@@ -193,82 +247,34 @@ class RemoteObject {
193247
}
194248

195249
[customInspectSymbol](depth, opts) {
196-
function formatProperty(prop) {
197-
switch (prop.type) {
198-
case 'string':
199-
case 'undefined':
200-
return utilInspect(prop.value, opts);
201-
202-
case 'number':
203-
case 'boolean':
204-
return opts.stylize(prop.value, prop.type);
205-
206-
case 'object':
207-
case 'symbol':
208-
if (prop.subtype === 'date') {
209-
return utilInspect(new Date(prop.value), opts);
210-
}
211-
if (prop.subtype === 'array') {
212-
return opts.stylize(prop.value, 'special');
213-
}
214-
return opts.stylize(prop.value, prop.subtype || 'special');
215-
216-
default:
217-
return prop.value;
218-
}
219-
}
220250
switch (this.type) {
221251
case 'boolean':
222252
case 'number':
223253
case 'string':
224254
case 'undefined':
225255
return utilInspect(this.value, opts);
226-
227256
case 'symbol':
228257
return opts.stylize(this.description, 'special');
229-
230258
case 'function': {
231259
const fnName = extractFunctionName(this.description);
232260
const formatted = `[${this.className}${fnName}]`;
233261
return opts.stylize(formatted, 'special');
234262
}
235-
236263
case 'object':
237264
switch (this.subtype) {
238265
case 'date':
239266
return utilInspect(new Date(this.description), opts);
240-
241267
case 'null':
242268
return utilInspect(null, opts);
243-
244269
case 'regexp':
245270
return opts.stylize(this.description, 'regexp');
246-
247271
default:
248272
break;
249273
}
250274
if (this.preview) {
251-
const props = ArrayPrototypeMap(
252-
this.preview.properties,
253-
(prop, idx) => {
254-
const value = formatProperty(prop);
255-
if (prop.name === `${idx}`) return value;
256-
return `${prop.name}: ${value}`;
257-
});
258-
if (this.preview.overflow) {
259-
ArrayPrototypePush(props, '...');
260-
}
261-
const singleLine = ArrayPrototypeJoin(props, ', ');
262-
const propString =
263-
singleLine.length > 60 ?
264-
ArrayPrototypeJoin(props, ',\n ') :
265-
singleLine;
266-
267-
return this.subtype === 'array' ?
268-
`[ ${propString} ]` : `{ ${propString} }`;
275+
return utilInspect(new ObjectPreview(this.preview), opts);
269276
}
270277
return this.description;
271-
272278
default:
273279
return this.description;
274280
}

0 commit comments

Comments
 (0)