Skip to content

Commit 4f5bb8b

Browse files
addaleaxjasnell
authored andcommitted
repl: don’t complete non-simple expressions
Change the regular expression that recognizes “simple” JS expressions to requiring that the full line needs to match it. Previously, in terms like `a().b.`, `b.` would be a partial match. This meant that completion would evaluate `b` and either fail with a `ReferenceError` or, if `b` was some global, return the properties of the global `b` object. PR-URL: #6192 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 49efb20 commit 4f5bb8b

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/repl.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -647,7 +647,7 @@ ArrayStream.prototype.write = function() {};
647647

648648
const requireRE = /\brequire\s*\(['"](([\w\.\/-]+\/)?([\w\.\/-]*))/;
649649
const simpleExpressionRE =
650-
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
650+
/^\s*(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
651651

652652
function intFilter(item) {
653653
// filters out anything not starting with A-Z, a-z, $ or _

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -249,3 +249,11 @@ testMe.complete('obj.', common.mustCall(function(error, data) {
249249
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
250250
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
251251
}));
252+
253+
// Don't try to complete results of non-simple expressions
254+
putIn.run(['.clear']);
255+
putIn.run(['function a() {}']);
256+
257+
testMe.complete('a().b.', common.mustCall((error, data) => {
258+
assert.deepEqual(data, [[], undefined]);
259+
}));

0 commit comments

Comments
 (0)