Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

repl: warn on assignment to _ #5438

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,7 @@ function REPLServer(prompt,
self.useGlobal = !!useGlobal;
self.ignoreUndefined = !!ignoreUndefined;
self.replMode = replMode || exports.REPL_MODE_SLOPPY;
self._last = undefined;

self._inTemplateLiteral = false;

Expand Down Expand Up @@ -467,7 +468,7 @@ function REPLServer(prompt,
// the second argument to this function is there, print it.
arguments.length === 2 &&
(!self.ignoreUndefined || ret !== undefined)) {
self.context._ = ret;
self._last = ret;
self.outputStream.write(self.writer(ret) + '\n');
}

Expand Down Expand Up @@ -544,13 +545,15 @@ REPLServer.prototype.createContext = function() {
this.lines = [];
this.lines.level = [];

const self = this;

// make built-in modules available directly
// (loaded lazily)
exports._builtinLibs.forEach(function(name) {
Object.defineProperty(context, name, {
get: function() {
var lib = require(name);
context._ = context[name] = lib;
self._last = context[name] = lib;
return lib;
},
// allow the creation of other globals with this name
Expand All @@ -562,11 +565,23 @@ REPLServer.prototype.createContext = function() {
});
});

Object.defineProperty(context, '_', {
configurable: true,
get: function() {
return self._last;
},
set: function(val) {
self._last = val;
self.outputStream.write('assignment to _ is reserved by the REPL\n');
}
});

return context;
};

REPLServer.prototype.resetContext = function() {
this.context = this.createContext();
this._last = undefined;

// Allow REPL extensions to extend the new context
this.emit('reset', this.context);
Expand Down
41 changes: 41 additions & 0 deletions test/parallel/test-repl-last-result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
'use strict';

require('../common');
const assert = require('assert');
const repl = require('repl');
const stream = require('stream');
const inputStream = new stream.PassThrough();
const outputStream = new stream.PassThrough();
let output = '';

outputStream.on('data', (data) => {
output += data;
});

process.on('exit', () => {
const lines = output.trim().split('\n');

assert.deepStrictEqual(lines, [
'undefined',
'assignment to _ is reserved by the REPL',
'42',
'42',
'85',
'85',
'undefined'
]);
});

const r = repl.start({
prompt: '',
input: inputStream,
output: outputStream
});

r.write('_;\n');
r.write('_ = 42;\n');
r.write('_;\n');
r.write('foo = 85;\n');
r.write('_;\n');
r.resetContext();
r.write('_;\n');