Skip to content

Commit 5382dea

Browse files
addaleaxMyles Borins
authored and
Myles Borins
committed
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 <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent f69416c commit 5382dea

File tree

2 files changed

+9
-1
lines changed

2 files changed

+9
-1
lines changed

lib/repl.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -627,7 +627,7 @@ ArrayStream.prototype.write = function() {};
627627

628628
const requireRE = /\brequire\s*\(['"](([\w\.\/-]+\/)?([\w\.\/-]*))/;
629629
const simpleExpressionRE =
630-
/(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
630+
/^\s*(([a-zA-Z_$](?:\w|\$)*)\.)*([a-zA-Z_$](?:\w|\$)*)\.?$/;
631631

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

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

+8
Original file line numberDiff line numberDiff line change
@@ -251,3 +251,11 @@ testMe.complete('obj.', common.mustCall(function(error, data) {
251251
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
252252
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
253253
}));
254+
255+
// Don't try to complete results of non-simple expressions
256+
putIn.run(['.clear']);
257+
putIn.run(['function a() {}']);
258+
259+
testMe.complete('a().b.', common.mustCall((error, data) => {
260+
assert.deepEqual(data, [[], undefined]);
261+
}));

0 commit comments

Comments
 (0)