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

Commit 7795c53

Browse files
author
Jan Krems
committed
refactor: Move more code in createRepl scope
1 parent 9877660 commit 7795c53

File tree

1 file changed

+128
-128
lines changed

1 file changed

+128
-128
lines changed

lib/internal/inspect-repl.js

Lines changed: 128 additions & 128 deletions
Original file line numberDiff line numberDiff line change
@@ -69,166 +69,166 @@ scripts List application scripts that are currently loaded
6969
scripts(true) List all scripts (including node-internals)
7070
`.trim();
7171

72-
function getRelativePath(filename) {
73-
const dir = `${Path.resolve()}/`;
72+
function createRepl(inspector) {
73+
function getRelativePath(filename) {
74+
const dir = `${Path.resolve()}/`;
7475

75-
// Change path to relative, if possible
76-
if (filename.indexOf(dir) === 0) {
77-
return filename.slice(dir.length);
76+
// Change path to relative, if possible
77+
if (filename.indexOf(dir) === 0) {
78+
return filename.slice(dir.length);
79+
}
80+
return filename;
7881
}
79-
return filename;
80-
}
8182

82-
function toCallback(promise, callback) {
83-
function forward(...args) {
84-
process.nextTick(() => callback(...args));
83+
function toCallback(promise, callback) {
84+
function forward(...args) {
85+
process.nextTick(() => callback(...args));
86+
}
87+
promise.then(forward.bind(null, null), forward);
8588
}
86-
promise.then(forward.bind(null, null), forward);
87-
}
8889

89-
// Adds spaces and prefix to number
90-
// maxN is a maximum number we should have space for
91-
function leftPad(n, prefix, maxN) {
92-
const s = n.toString();
93-
const nchars = Math.max(2, String(maxN).length) + 1;
94-
const nspaces = nchars - s.length - 1;
90+
// Adds spaces and prefix to number
91+
// maxN is a maximum number we should have space for
92+
function leftPad(n, prefix, maxN) {
93+
const s = n.toString();
94+
const nchars = Math.max(2, String(maxN).length) + 1;
95+
const nspaces = nchars - s.length - 1;
9596

96-
return prefix + ' '.repeat(nspaces) + s;
97-
}
97+
return prefix + ' '.repeat(nspaces) + s;
98+
}
9899

99-
function markSourceColumn(sourceText, position, useColors) {
100-
if (!sourceText) return '';
100+
function markSourceColumn(sourceText, position, useColors) {
101+
if (!sourceText) return '';
101102

102-
const head = sourceText.slice(0, position);
103-
let tail = sourceText.slice(position);
103+
const head = sourceText.slice(0, position);
104+
let tail = sourceText.slice(position);
104105

105-
// Colourize char if stdout supports colours
106-
if (useColors) {
107-
tail = tail.replace(/(.+?)([^\w]|$)/, '\u001b[32m$1\u001b[39m$2');
108-
}
106+
// Colourize char if stdout supports colours
107+
if (useColors) {
108+
tail = tail.replace(/(.+?)([^\w]|$)/, '\u001b[32m$1\u001b[39m$2');
109+
}
109110

110-
// Return source line with coloured char at `position`
111-
return [head, tail].join('');
112-
}
111+
// Return source line with coloured char at `position`
112+
return [head, tail].join('');
113+
}
113114

114-
function extractErrorMessage(stack) {
115-
if (!stack) return '<unknown>';
116-
const m = stack.match(/^\w+: ([^\n]+)/);
117-
return m ? m[1] : stack;
118-
}
115+
function extractErrorMessage(stack) {
116+
if (!stack) return '<unknown>';
117+
const m = stack.match(/^\w+: ([^\n]+)/);
118+
return m ? m[1] : stack;
119+
}
119120

120-
function convertResultToError(result) {
121-
const { className, description } = result;
122-
const err = new Error(extractErrorMessage(description));
123-
err.stack = description;
124-
Object.defineProperty(err, 'name', { value: className });
125-
return err;
126-
}
121+
function convertResultToError(result) {
122+
const { className, description } = result;
123+
const err = new Error(extractErrorMessage(description));
124+
err.stack = description;
125+
Object.defineProperty(err, 'name', { value: className });
126+
return err;
127+
}
127128

128-
class RemoteObject {
129-
constructor(attributes) {
130-
Object.assign(this, attributes);
131-
if (this.type === 'number') {
132-
this.value = this.unserializableValue ? +this.unserializableValue : +this.value;
129+
class RemoteObject {
130+
constructor(attributes) {
131+
Object.assign(this, attributes);
132+
if (this.type === 'number') {
133+
this.value = this.unserializableValue ? +this.unserializableValue : +this.value;
134+
}
133135
}
134-
}
135136

136-
[util.inspect.custom](depth, opts) {
137-
function formatProperty(prop) {
138-
switch (prop.type) {
139-
case 'string':
140-
case 'undefined':
141-
return util.inspect(prop.value, opts);
137+
[util.inspect.custom](depth, opts) {
138+
function formatProperty(prop) {
139+
switch (prop.type) {
140+
case 'string':
141+
case 'undefined':
142+
return util.inspect(prop.value, opts);
143+
144+
case 'number':
145+
case 'boolean':
146+
return opts.stylize(prop.value, prop.type);
147+
148+
case 'object':
149+
case 'symbol':
150+
if (prop.subtype === 'date') {
151+
return util.inspect(new Date(prop.value), opts);
152+
}
153+
if (prop.subtype === 'array') {
154+
return opts.stylize(prop.value, 'special');
155+
}
156+
return opts.stylize(prop.value, prop.subtype || 'special');
142157

143-
case 'number':
158+
default:
159+
return prop.value;
160+
}
161+
}
162+
switch (this.type) {
144163
case 'boolean':
145-
return opts.stylize(prop.value, prop.type);
164+
case 'number':
165+
case 'string':
166+
case 'undefined':
167+
return util.inspect(this.value, opts);
146168

147-
case 'object':
148169
case 'symbol':
149-
if (prop.subtype === 'date') {
150-
return util.inspect(new Date(prop.value), opts);
151-
}
152-
if (prop.subtype === 'array') {
153-
return opts.stylize(prop.value, 'special');
154-
}
155-
return opts.stylize(prop.value, prop.subtype || 'special');
170+
return opts.stylize(this.description, 'special');
156171

157-
default:
158-
return prop.value;
159-
}
160-
}
161-
switch (this.type) {
162-
case 'boolean':
163-
case 'number':
164-
case 'string':
165-
case 'undefined':
166-
return util.inspect(this.value, opts);
167-
168-
case 'symbol':
169-
return opts.stylize(this.description, 'special');
170-
171-
case 'function': {
172-
const fnNameMatch = (this.description).match(/^(?:function\*? )?([^(\s]+)\(/);
173-
const fnName = fnNameMatch ? `: ${fnNameMatch[1]}` : '';
174-
const formatted = `[${this.className}${fnName}]`;
175-
return opts.stylize(formatted, 'special');
176-
}
172+
case 'function': {
173+
const fnNameMatch = (this.description).match(/^(?:function\*? )?([^(\s]+)\(/);
174+
const fnName = fnNameMatch ? `: ${fnNameMatch[1]}` : '';
175+
const formatted = `[${this.className}${fnName}]`;
176+
return opts.stylize(formatted, 'special');
177+
}
177178

178-
case 'object':
179-
switch (this.subtype) {
180-
case 'date':
181-
return util.inspect(new Date(this.description), opts);
179+
case 'object':
180+
switch (this.subtype) {
181+
case 'date':
182+
return util.inspect(new Date(this.description), opts);
182183

183-
case 'null':
184-
return util.inspect(null, opts);
184+
case 'null':
185+
return util.inspect(null, opts);
185186

186-
case 'regexp':
187-
return opts.stylize(this.description, 'regexp');
187+
case 'regexp':
188+
return opts.stylize(this.description, 'regexp');
188189

189-
default:
190-
break;
191-
}
192-
if (this.preview) {
193-
const props = this.preview.properties
194-
.map((prop, idx) => {
195-
const value = formatProperty(prop);
196-
if (prop.name === `${idx}`) return value;
197-
return `${prop.name}: ${value}`;
198-
});
199-
if (this.preview.overflow) {
200-
props.push('...');
190+
default:
191+
break;
201192
}
202-
const singleLine = props.join(', ');
203-
const propString = singleLine.length > 60 ? props.join(',\n ') : singleLine;
204-
return this.subtype === 'array' ? `[ ${propString} ]` : `{ ${propString} }`;
205-
}
206-
return this.description;
193+
if (this.preview) {
194+
const props = this.preview.properties
195+
.map((prop, idx) => {
196+
const value = formatProperty(prop);
197+
if (prop.name === `${idx}`) return value;
198+
return `${prop.name}: ${value}`;
199+
});
200+
if (this.preview.overflow) {
201+
props.push('...');
202+
}
203+
const singleLine = props.join(', ');
204+
const propString = singleLine.length > 60 ? props.join(',\n ') : singleLine;
205+
return this.subtype === 'array' ? `[ ${propString} ]` : `{ ${propString} }`;
206+
}
207+
return this.description;
207208

208-
default:
209-
return this.description;
209+
default:
210+
return this.description;
211+
}
210212
}
211213
}
212-
}
213214

214-
function convertResultToRemoteObject({ result, wasThrown }) {
215-
if (wasThrown) return convertResultToError(result);
216-
return new RemoteObject(result);
217-
}
215+
function convertResultToRemoteObject({ result, wasThrown }) {
216+
if (wasThrown) return convertResultToError(result);
217+
return new RemoteObject(result);
218+
}
218219

219-
function copyOwnProperties(target, source) {
220-
Object.getOwnPropertyNames(source).forEach((prop) => {
221-
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop));
222-
});
223-
}
220+
function copyOwnProperties(target, source) {
221+
Object.getOwnPropertyNames(source).forEach((prop) => {
222+
Object.defineProperty(target, prop, Object.getOwnPropertyDescriptor(source, prop));
223+
});
224+
}
224225

225-
function aliasProperties(target, mapping) {
226-
Object.keys(mapping).forEach((key) => {
227-
Object.defineProperty(target, mapping[key], Object.getOwnPropertyDescriptor(target, key));
228-
});
229-
}
226+
function aliasProperties(target, mapping) {
227+
Object.keys(mapping).forEach((key) => {
228+
Object.defineProperty(target, mapping[key], Object.getOwnPropertyDescriptor(target, key));
229+
});
230+
}
230231

231-
function createRepl(inspector) {
232232
const { Debugger, Runtime } = inspector;
233233

234234
let repl; // eslint-disable-line prefer-const

0 commit comments

Comments
 (0)