Skip to content

Commit 2ca9f94

Browse files
lanceaddaleax
authored andcommitted
repl: make REPLServer.bufferedCommand private
The `REPLServer.bufferedCommand` property was undocumented, except for its usage appearing, unexplained, in an example for `REPLServer.defineCommand`. This commit deprecates that property, privatizes it, and adds a `REPLServer.clearBufferedCommand()` function that will clear the buffer. PR-URL: #13687 Reviewed-By: Colin Ihrig <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: James Snell <[email protected]> Reviewed-By: Michael Dawson <[email protected]> Reviewed-By: Ruben Bridgewater <[email protected]> Refs: #12686
1 parent 98ddab4 commit 2ca9f94

File tree

3 files changed

+52
-18
lines changed

3 files changed

+52
-18
lines changed

doc/api/deprecations.md

+9
Original file line numberDiff line numberDiff line change
@@ -645,6 +645,14 @@ with inappropriate names has been deprecated.
645645
*Note*: As the original API was undocumented and not generally useful for
646646
non-internal code, no replacement API is provided.
647647

648+
<a id="DEP0074"></a>
649+
### DEP0074: REPLServer.bufferedCommand
650+
651+
Type: Runtime
652+
653+
The `REPLServer.bufferedCommand` property was deprecated in favor of
654+
[`REPLServer.clearBufferedCommand()`][].
655+
648656
[`Buffer.allocUnsafeSlow(size)`]: buffer.html#buffer_class_method_buffer_allocunsafeslow_size
649657
[`Buffer.from(array)`]: buffer.html#buffer_class_method_buffer_from_array
650658
[`Buffer.from(buffer)`]: buffer.html#buffer_class_method_buffer_from_buffer
@@ -708,3 +716,4 @@ non-internal code, no replacement API is provided.
708716
[alloc_unsafe_size]: buffer.html#buffer_class_method_buffer_allocunsafe_size
709717
[from_arraybuffer]: buffer.html#buffer_class_method_buffer_from_arraybuffer_byteoffset_length
710718
[from_string_encoding]: buffer.html#buffer_class_method_buffer_from_string_encoding
719+
[`REPLServer.clearBufferedCommand()`]: repl.html#repl_replserver_clearbufferedcommand

doc/api/repl.md

+11-1
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,7 @@ const replServer = repl.start({ prompt: '> ' });
335335
replServer.defineCommand('sayhello', {
336336
help: 'Say hello',
337337
action(name) {
338-
this.bufferedCommand = '';
338+
this.clearBufferedCommand();
339339
console.log(`Hello, ${name}!`);
340340
this.displayPrompt();
341341
}
@@ -375,6 +375,16 @@ The `replServer.displayPrompt` method is primarily intended to be called from
375375
within the action function for commands registered using the
376376
`replServer.defineCommand()` method.
377377

378+
### replServer.clearBufferedCommand()
379+
<!-- YAML
380+
added: REPLACEME
381+
-->
382+
383+
The `replServer.clearBufferedComand()` method clears any command that has been
384+
buffered but not yet executed. This method is primarily intended to be
385+
called from within the action function for commands registered using the
386+
`replServer.defineCommand()` method.
387+
378388
## repl.start([options])
379389
<!-- YAML
380390
added: v0.1.91

lib/repl.js

+32-17
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
7373
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
7474
GLOBAL_OBJECT_PROPERTIES[n];
7575
}
76+
const kBufferedCommandSymbol = Symbol('bufferedCommand');
7677

7778
try {
7879
// hack for require.resolve("./relative") to work properly.
@@ -300,7 +301,7 @@ function REPLServer(prompt,
300301
} else {
301302
top.outputStream.write(`Thrown: ${String(e)}\n`);
302303
}
303-
top.bufferedCommand = '';
304+
top.clearBufferedCommand();
304305
top.lines.level = [];
305306
top.displayPrompt();
306307
});
@@ -326,9 +327,17 @@ function REPLServer(prompt,
326327
self.outputStream = output;
327328

328329
self.resetContext();
329-
self.bufferedCommand = '';
330330
self.lines.level = [];
331331

332+
self.clearBufferedCommand();
333+
Object.defineProperty(this, 'bufferedCommand', {
334+
get: util.deprecate(() => self[kBufferedCommandSymbol],
335+
'REPLServer.bufferedCommand is deprecated', 'DEP0074'),
336+
set: util.deprecate((val) => self[kBufferedCommandSymbol] = val,
337+
'REPLServer.bufferedCommand is deprecated', 'DEP0074'),
338+
enumerable: true
339+
});
340+
332341
// Figure out which "complete" function to use.
333342
self.completer = (typeof options.completer === 'function') ?
334343
options.completer : completer;
@@ -376,7 +385,8 @@ function REPLServer(prompt,
376385
self.clearLine();
377386
self.turnOffEditorMode();
378387

379-
if (!(self.bufferedCommand && self.bufferedCommand.length > 0) && empty) {
388+
const cmd = self[kBufferedCommandSymbol];
389+
if (!(cmd && cmd.length > 0) && empty) {
380390
if (sawSIGINT) {
381391
self.close();
382392
sawSIGINT = false;
@@ -388,7 +398,7 @@ function REPLServer(prompt,
388398
sawSIGINT = false;
389399
}
390400

391-
self.bufferedCommand = '';
401+
self.clearBufferedCommand();
392402
self.lines.level = [];
393403
self.displayPrompt();
394404
});
@@ -399,7 +409,7 @@ function REPLServer(prompt,
399409
sawSIGINT = false;
400410

401411
if (self.editorMode) {
402-
self.bufferedCommand += cmd + '\n';
412+
self[kBufferedCommandSymbol] += cmd + '\n';
403413

404414
// code alignment
405415
const matches = self._sawKeyPress ? cmd.match(/^\s+/) : null;
@@ -427,15 +437,15 @@ function REPLServer(prompt,
427437
if (self.parseREPLKeyword(keyword, rest) === true) {
428438
return;
429439
}
430-
if (!self.bufferedCommand) {
440+
if (!self[kBufferedCommandSymbol]) {
431441
self.outputStream.write('Invalid REPL keyword\n');
432442
finish(null);
433443
return;
434444
}
435445
}
436446
}
437447

438-
const evalCmd = self.bufferedCommand + cmd + '\n';
448+
const evalCmd = self[kBufferedCommandSymbol] + cmd + '\n';
439449

440450
debug('eval %j', evalCmd);
441451
self.eval(evalCmd, self.context, 'repl', finish);
@@ -444,11 +454,11 @@ function REPLServer(prompt,
444454
debug('finish', e, ret);
445455
self.memory(cmd);
446456

447-
if (e && !self.bufferedCommand && cmd.trim().startsWith('npm ')) {
457+
if (e && !self[kBufferedCommandSymbol] && cmd.trim().startsWith('npm ')) {
448458
self.outputStream.write('npm should be run outside of the ' +
449459
'node repl, in your normal shell.\n' +
450460
'(Press Control-D to exit.)\n');
451-
self.bufferedCommand = '';
461+
self.clearBufferedCommand();
452462
self.displayPrompt();
453463
return;
454464
}
@@ -460,7 +470,7 @@ function REPLServer(prompt,
460470
// {
461471
// ... x: 1
462472
// ... }
463-
self.bufferedCommand += cmd + '\n';
473+
self[kBufferedCommandSymbol] += cmd + '\n';
464474
self.displayPrompt();
465475
return;
466476
} else {
@@ -469,7 +479,7 @@ function REPLServer(prompt,
469479
}
470480

471481
// Clear buffer if no SyntaxErrors
472-
self.bufferedCommand = '';
482+
self.clearBufferedCommand();
473483
sawCtrlD = false;
474484

475485
// If we got any output - print it (if no error)
@@ -495,7 +505,7 @@ function REPLServer(prompt,
495505
self.outputStream.write(`${self._initialPrompt}.editor\n`);
496506
self.outputStream.write(
497507
'// Entering editor mode (^D to finish, ^C to cancel)\n');
498-
self.outputStream.write(`${self.bufferedCommand}\n`);
508+
self.outputStream.write(`${self[kBufferedCommandSymbol]}\n`);
499509
self.prompt(true);
500510
} else {
501511
self.displayPrompt(true);
@@ -569,6 +579,10 @@ exports.start = function(prompt,
569579
return repl;
570580
};
571581

582+
REPLServer.prototype.clearBufferedCommand = function clearBufferedCommand() {
583+
this[kBufferedCommandSymbol] = '';
584+
};
585+
572586
REPLServer.prototype.close = function close() {
573587
if (this.terminal && this._flushing && !this._closingOnFlush) {
574588
this._closingOnFlush = true;
@@ -647,7 +661,7 @@ REPLServer.prototype.resetContext = function() {
647661

648662
REPLServer.prototype.displayPrompt = function(preserveCursor) {
649663
var prompt = this._initialPrompt;
650-
if (this.bufferedCommand.length) {
664+
if (this[kBufferedCommandSymbol].length) {
651665
prompt = '...';
652666
const len = this.lines.level.length ? this.lines.level.length - 1 : 0;
653667
const levelInd = '..'.repeat(len);
@@ -742,7 +756,8 @@ REPLServer.prototype.complete = function() {
742756
// getter code.
743757
function complete(line, callback) {
744758
// There may be local variables to evaluate, try a nested REPL
745-
if (this.bufferedCommand !== undefined && this.bufferedCommand.length) {
759+
if (this[kBufferedCommandSymbol] !== undefined &&
760+
this[kBufferedCommandSymbol].length) {
746761
// Get a new array of inputted lines
747762
var tmp = this.lines.slice();
748763
// Kill off all function declarations to push all local variables into
@@ -759,7 +774,7 @@ function complete(line, callback) {
759774
flat.run(tmp); // eval the flattened code
760775
// all this is only profitable if the nested REPL
761776
// does not have a bufferedCommand
762-
if (!magic.bufferedCommand) {
777+
if (!magic[kBufferedCommandSymbol]) {
763778
return magic.complete(line, callback);
764779
}
765780
}
@@ -1172,7 +1187,7 @@ function defineDefaultCommands(repl) {
11721187
repl.defineCommand('break', {
11731188
help: 'Sometimes you get stuck, this gets you out',
11741189
action: function() {
1175-
this.bufferedCommand = '';
1190+
this.clearBufferedCommand();
11761191
this.displayPrompt();
11771192
}
11781193
});
@@ -1186,7 +1201,7 @@ function defineDefaultCommands(repl) {
11861201
repl.defineCommand('clear', {
11871202
help: clearMessage,
11881203
action: function() {
1189-
this.bufferedCommand = '';
1204+
this.clearBufferedCommand();
11901205
if (!this.useGlobal) {
11911206
this.outputStream.write('Clearing context...\n');
11921207
this.resetContext();

0 commit comments

Comments
 (0)