From 392c70a8273e270b9a0b1d04905138733a6efaff Mon Sep 17 00:00:00 2001 From: Evan Lucas Date: Fri, 22 Jul 2016 07:11:56 -0500 Subject: [PATCH] repl: prevent undefined ref in completion Fixes: https://github.com/nodejs/node/issues/7716 PR-URL: https://github.com/nodejs/node/pull/7718 Reviewed-By: Anna Henningsen --- lib/repl.js | 2 +- test/parallel/test-repl-tab-complete-crash.js | 26 +++++++++---------- 2 files changed, 14 insertions(+), 14 deletions(-) diff --git a/lib/repl.js b/lib/repl.js index 4f9827564afb3a..b2befe588bb906 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -735,12 +735,12 @@ function complete(line, callback) { }); var flat = new ArrayStream(); // make a new "input" stream var magic = new REPLServer('', flat); // make a nested REPL + replMap.set(magic, replMap.get(this)); magic.context = magic.createContext(); flat.run(tmp); // eval the flattened code // all this is only profitable if the nested REPL // does not have a bufferedCommand if (!magic.bufferedCommand) { - replMap.set(magic, replMap.get(this)); return magic.complete(line, callback); } } diff --git a/test/parallel/test-repl-tab-complete-crash.js b/test/parallel/test-repl-tab-complete-crash.js index ce283757438192..0874ba41f074fa 100644 --- a/test/parallel/test-repl-tab-complete-crash.js +++ b/test/parallel/test-repl-tab-complete-crash.js @@ -4,24 +4,24 @@ const common = require('../common'); const assert = require('assert'); const repl = require('repl'); -var referenceErrorCount = 0; - -common.ArrayStream.prototype.write = function(msg) { - if (msg.startsWith('ReferenceError: ')) { - referenceErrorCount++; - } -}; +common.ArrayStream.prototype.write = function(msg) {}; const putIn = new common.ArrayStream(); const testMe = repl.start('', putIn); // https://github.com/nodejs/node/issues/3346 -// Tab-completion for an undefined variable inside a function should report a -// ReferenceError. +// Tab-completion should be empty putIn.run(['.clear']); putIn.run(['function () {']); -testMe.complete('arguments.'); +testMe.complete('arguments.', common.mustCall((err, completions) => { + assert.strictEqual(err, null); + assert.deepStrictEqual(completions, [[], 'arguments.']); +})); -process.on('exit', function() { - assert.strictEqual(referenceErrorCount, 1); -}); +putIn.run(['.clear']); +putIn.run(['function () {']); +putIn.run(['undef;']); +testMe.complete('undef.', common.mustCall((err, completions) => { + assert.strictEqual(err, null); + assert.deepStrictEqual(completions, [[], 'undef.']); +}));