Skip to content
This repository was archived by the owner on Feb 1, 2022. It is now read-only.

Commit 4219a98

Browse files
author
Jan Krems
committed
refactor: Decouple source snippet from repl
1 parent fbb8f24 commit 4219a98

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

lib/internal/inspect-repl.js

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -96,14 +96,14 @@ function leftPad(n, prefix, maxN) {
9696
return prefix + ' '.repeat(nspaces) + s;
9797
}
9898

99-
function markSourceColumn(sourceText, position, repl) {
99+
function markSourceColumn(sourceText, position, useColors) {
100100
if (!sourceText) return '';
101101

102102
const head = sourceText.slice(0, position);
103103
let tail = sourceText.slice(position);
104104

105105
// Colourize char if stdout supports colours
106-
if (repl.useColors) {
106+
if (useColors) {
107107
tail = tail.replace(/(.+?)([^\w]|$)/, '\u001b[32m$1\u001b[39m$2');
108108
}
109109

@@ -256,13 +256,16 @@ function createRepl(inspector) {
256256
}
257257
resetOnStart();
258258

259-
const print = inspector.print.bind(inspector);
260-
261259
const INSPECT_OPTIONS = { colors: inspector.stdout.isTTY };
262260
function inspect(value) {
263261
return util.inspect(value, INSPECT_OPTIONS);
264262
}
265263

264+
function print(value, oneline = false) {
265+
const text = typeof value === 'string' ? value : inspect(value);
266+
return inspector.print(text, oneline);
267+
}
268+
266269
function getCurrentLocation() {
267270
if (!selectedFrame) {
268271
throw new Error('Requires execution to be paused');
@@ -319,11 +322,7 @@ function createRepl(inspector) {
319322
this.delta = delta;
320323
}
321324

322-
toString() {
323-
return this[util.inspect.custom]();
324-
}
325-
326-
[util.inspect.custom]() {
325+
[util.inspect.custom](depth, options) {
327326
const { scriptId, lineNumber, columnNumber, delta, scriptSource } = this;
328327
const start = Math.max(1, lineNumber - delta + 1);
329328
const end = lineNumber + delta + 1;
@@ -334,7 +333,7 @@ function createRepl(inspector) {
334333
const isCurrent = i === (lineNumber + 1);
335334

336335
const markedLine = isCurrent
337-
? markSourceColumn(lineText, columnNumber, inspector.repl)
336+
? markSourceColumn(lineText, columnNumber, options.colors)
338337
: lineText;
339338

340339
let isBreakpoint = false;
@@ -512,7 +511,7 @@ function createRepl(inspector) {
512511
// List source code
513512
function list(delta = 5) {
514513
return selectedFrame.list(delta)
515-
.then(print, (error) => {
514+
.then(null, (error) => {
516515
print('You can\'t list source code right now');
517516
throw error;
518517
});
@@ -700,8 +699,12 @@ function createRepl(inspector) {
700699

701700
inspector.suspendReplWhile(() =>
702701
Promise.all([formatWatchers(true), selectedFrame.list(2)])
703-
.then(([watcherList, context]) => (watcherList ? `${watcherList}\n${context}` : context))
704-
.then(print));
702+
.then(([watcherList, context]) => {
703+
if (watcherList) {
704+
return `${watcherList}\n${inspect(context)}`;
705+
}
706+
return context;
707+
}).then(print));
705708
});
706709

707710
function handleResumed() {

0 commit comments

Comments
 (0)