Skip to content

Commit 392c70a

Browse files
committed
repl: prevent undefined ref in completion
Fixes: #7716 PR-URL: #7718 Reviewed-By: Anna Henningsen <anna@addaleax.net>
1 parent d525e6c commit 392c70a

File tree

2 files changed

+14
-14
lines changed

2 files changed

+14
-14
lines changed

lib/repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -735,12 +735,12 @@ function complete(line, callback) {
735735
});
736736
var flat = new ArrayStream(); // make a new "input" stream
737737
var magic = new REPLServer('', flat); // make a nested REPL
738+
replMap.set(magic, replMap.get(this));
738739
magic.context = magic.createContext();
739740
flat.run(tmp); // eval the flattened code
740741
// all this is only profitable if the nested REPL
741742
// does not have a bufferedCommand
742743
if (!magic.bufferedCommand) {
743-
replMap.set(magic, replMap.get(this));
744744
return magic.complete(line, callback);
745745
}
746746
}

test/parallel/test-repl-tab-complete-crash.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,24 +4,24 @@ const common = require('../common');
44
const assert = require('assert');
55
const repl = require('repl');
66

7-
var referenceErrorCount = 0;
8-
9-
common.ArrayStream.prototype.write = function(msg) {
10-
if (msg.startsWith('ReferenceError: ')) {
11-
referenceErrorCount++;
12-
}
13-
};
7+
common.ArrayStream.prototype.write = function(msg) {};
148

159
const putIn = new common.ArrayStream();
1610
const testMe = repl.start('', putIn);
1711

1812
// https://github.com/nodejs/node/issues/3346
19-
// Tab-completion for an undefined variable inside a function should report a
20-
// ReferenceError.
13+
// Tab-completion should be empty
2114
putIn.run(['.clear']);
2215
putIn.run(['function () {']);
23-
testMe.complete('arguments.');
16+
testMe.complete('arguments.', common.mustCall((err, completions) => {
17+
assert.strictEqual(err, null);
18+
assert.deepStrictEqual(completions, [[], 'arguments.']);
19+
}));
2420

25-
process.on('exit', function() {
26-
assert.strictEqual(referenceErrorCount, 1);
27-
});
21+
putIn.run(['.clear']);
22+
putIn.run(['function () {']);
23+
putIn.run(['undef;']);
24+
testMe.complete('undef.', common.mustCall((err, completions) => {
25+
assert.strictEqual(err, null);
26+
assert.deepStrictEqual(completions, [[], 'undef.']);
27+
}));

0 commit comments

Comments
 (0)