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: Don’t complete expressions when eval() failed #6328

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
5 changes: 3 additions & 2 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -628,7 +628,7 @@ ArrayStream.prototype.write = function() {};

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

function intFilter(item) {
// filters out anything not starting with A-Z, a-z, $ or _
Expand Down Expand Up @@ -806,7 +806,8 @@ REPLServer.prototype.complete = function(line, callback) {
});
}
} else {
this.eval(expr, this.context, 'repl', function(e, obj) {
const evalExpr = `try { ${expr} } catch (e) {}`;
this.eval(evalExpr, this.context, 'repl', function(e, obj) {
// if (e) console.log(e);

if (obj != null) {
Expand Down
57 changes: 30 additions & 27 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,33 +3,21 @@
var common = require('../common');
var assert = require('assert');
var repl = require('repl');
var referenceErrors = 0;
var expectedReferenceErrors = 0;

function getDoNotCallFunction() {
expectedReferenceErrors += 1;
return function() {
assert(false);
};
}

process.on('exit', function() {
assert.strictEqual(referenceErrors, expectedReferenceErrors);
});
function getNoResultsFunction() {
return common.mustCall((err, data) => {
assert.ifError(err);
assert.deepStrictEqual(data[0], []);
});
}

var works = [['inner.one'], 'inner.o'];
const putIn = new common.ArrayStream();
var testMe = repl.start('', putIn);

// Some errors are passed to the domain, but do not callback
testMe._domain.on('error', function(err) {
// Errors come from another context, so instanceof doesn't work
var str = err.toString();

if (/^ReferenceError:/.test(str))
referenceErrors++;
else
assert(false);
assert.ifError(err);
});

// Tab Complete will not break in an object literal
Expand All @@ -38,7 +26,7 @@ putIn.run([
'var inner = {',
'one:1'
]);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

testMe.complete('console.lo', common.mustCall(function(error, data) {
assert.deepStrictEqual(data, [['console.log'], 'console.lo']);
Expand All @@ -58,7 +46,7 @@ putIn.run([
'?',
'{one: 1} : '
]);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

putIn.run(['.clear']);

Expand All @@ -74,7 +62,7 @@ testMe.complete('inner.o', common.mustCall(function(error, data) {
// When you close the function scope tab complete will not return the
// locally scoped variable
putIn.run(['};']);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

putIn.run(['.clear']);

Expand Down Expand Up @@ -129,7 +117,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

putIn.run(['.clear']);

Expand All @@ -142,7 +130,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

putIn.run(['.clear']);

Expand All @@ -156,7 +144,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', getDoNotCallFunction());
testMe.complete('inner.o', getNoResultsFunction());

putIn.run(['.clear']);

Expand Down Expand Up @@ -254,6 +242,21 @@ testMe.complete('obj.', common.mustCall(function(error, data) {
putIn.run(['.clear']);
putIn.run(['function a() {}']);

testMe.complete('a().b.', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [[], undefined]);
testMe.complete('a().b.', getNoResultsFunction());

// Works when prefixed with spaces
putIn.run(['.clear']);
putIn.run(['var obj = {1:"a","1a":"b",a:"b"};']);

testMe.complete(' obj.', common.mustCall((error, data) => {
assert.strictEqual(data[0].indexOf('obj.1'), -1);
assert.strictEqual(data[0].indexOf('obj.1a'), -1);
assert.notStrictEqual(data[0].indexOf('obj.a'), -1);
}));

// Works inside assignments
putIn.run(['.clear']);

testMe.complete('var log = console.lo', common.mustCall((error, data) => {
assert.deepStrictEqual(data, [['console.log'], 'console.lo']);
}));