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 fixes #2052

Closed
wants to merge 3 commits 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
6 changes: 2 additions & 4 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -611,9 +611,7 @@ REPLServer.prototype.complete = function(line, callback) {
if (!expr) {
// If context is instance of vm.ScriptContext
// Get global vars synchronously
if (this.useGlobal ||
this.context.constructor &&
this.context.constructor.name === 'Context') {
if (this.useGlobal || vm.isContext(this.context)) {
var contextProto = this.context;
while (contextProto = Object.getPrototypeOf(contextProto)) {
completionGroups.push(Object.getOwnPropertyNames(contextProto));
Expand Down Expand Up @@ -996,7 +994,7 @@ function isRecoverableError(e, self) {
self._inTemplateLiteral = true;
return true;
}
return /^(Unexpected end of input|Unexpected token :)/.test(message);
return /^(Unexpected end of input|Unexpected token)/.test(message);
}
return false;
}
Expand Down
82 changes: 59 additions & 23 deletions test/parallel/test-repl-tab-complete.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,18 @@
var common = require('../common');
var assert = require('assert');
var util = require('util');

var repl = require('repl');
var referenceErrors = 0;
var completionCount = 0;

function doNotCall() {
assert(false);
}

process.on('exit', function() {
assert.strictEqual(referenceErrors, 6);
assert.strictEqual(completionCount, 12);
});

// A stream to push an array into a REPL
function ArrayStream() {
Expand All @@ -21,27 +31,37 @@ ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function() {};

var works = [['inner.one'], 'inner.o'];
var doesNotBreak = [[], 'inner.o'];

var putIn = new 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);
});

// Tab Complete will not break in an object literal
putIn.run(['.clear']);
putIn.run([
'var inner = {',
'one:1'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

testMe.complete('console.lo', function(error, data) {
completionCount++;
assert.deepEqual(data, [['console.log'], 'console.lo']);
});

// Tab Complete will return globaly scoped variables
putIn.run(['};']);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -53,9 +73,7 @@ putIn.run([
'?',
'{one: 1} : '
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

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

Expand All @@ -65,15 +83,14 @@ putIn.run([
'var inner = {one:1};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

// When you close the function scope tab complete will not return the
// locally scoped variable
putIn.run(['};']);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

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

Expand All @@ -85,6 +102,7 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -99,6 +117,7 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

Expand All @@ -114,12 +133,12 @@ putIn.run([
'};'
]);
testMe.complete('inner.o', function(error, data) {
completionCount++;
assert.deepEqual(data, works);
});

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

// currently does not work, but should not break note the inner function
// def has the params and { on a separate line
putIn.run([
'var top = function() {',
Expand All @@ -129,9 +148,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

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

Expand All @@ -144,9 +161,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

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

Expand All @@ -160,9 +175,7 @@ putIn.run([
' one:1',
'};'
]);
testMe.complete('inner.o', function(error, data) {
assert.deepEqual(data, doesNotBreak);
});
testMe.complete('inner.o', doNotCall);

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

Expand All @@ -171,6 +184,7 @@ putIn.run([
'var str = "test";'
]);
testMe.complete('str.len', function(error, data) {
completionCount++;
assert.deepEqual(data, [['str.length'], 'str.len']);
});

Expand All @@ -182,27 +196,49 @@ var spaceTimeout = setTimeout(function() {
}, 1000);

testMe.complete(' ', function(error, data) {
completionCount++;
assert.deepEqual(data, [[], undefined]);
clearTimeout(spaceTimeout);
});

// tab completion should pick up the global "toString" object, and
// any other properties up the "global" object's prototype chain
testMe.complete('toSt', function(error, data) {
completionCount++;
assert.deepEqual(data, [['toString'], 'toSt']);
});

// Tab complete provides built in libs for require()
putIn.run(['.clear']);

testMe.complete('require(\'', function(error, data) {
completionCount++;
assert.strictEqual(error, null);
repl._builtinLibs.forEach(function(lib) {
assert.notStrictEqual(data[0].indexOf(lib), -1, lib + ' not found');
});
});

testMe.complete('require(\'n', function(error, data) {
completionCount++;
assert.strictEqual(error, null);
assert.deepEqual(data, [['net'], 'n']);
assert.strictEqual(data.length, 2);
assert.strictEqual(data[1], 'n');
assert.notStrictEqual(data[0].indexOf('net'), -1);
// It's possible to pick up non-core modules too
data[0].forEach(function(completion) {
if (completion)
assert(/^n/.test(completion));
});
});

// Make sure tab completion works on context properties
putIn.run(['.clear']);

putIn.run([
'var custom = "test";'
]);
testMe.complete('cus', function(error, data) {
completionCount++;
assert.deepEqual(data, [['custom'], 'cus']);
});
33 changes: 33 additions & 0 deletions test/parallel/test-repl-unexpected-token-recoverable.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
'use strict';
/*
* This is a regression test for https://github.com/joyent/node/issues/8874.
*/
var common = require('../common');
var assert = require('assert');

var spawn = require('child_process').spawn;
// use -i to force node into interactive mode, despite stdout not being a TTY
var args = [ '-i' ];
var child = spawn(process.execPath, args);

var input = 'var foo = "bar\\\nbaz"';
// Match '...' as well since it marks a multi-line statement
var expectOut = /^> ... undefined\n/;

child.stderr.setEncoding('utf8');
child.stderr.on('data', function(c) {
throw new Error('child.stderr be silent');
});

child.stdout.setEncoding('utf8');
var out = '';
child.stdout.on('data', function(c) {
out += c;
});

child.stdout.on('end', function() {
assert(expectOut.test(out));
console.log('ok');
});

child.stdin.end(input);