Skip to content
This repository has been archived by the owner on Apr 22, 2023. It is now read-only.

REPL: tab complete for Arrays #25819

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
14 changes: 13 additions & 1 deletion lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -602,7 +602,19 @@ REPLServer.prototype.complete = function(line, callback) {
// if (e) console.log(e);

if (obj != null) {
if (util.isObject(obj) || util.isFunction(obj)) {
if (Array.isArray(obj)) {
// Array elements will contain many integer values
// if we return them in memberGroups they will appear
// as obj.0 obj.100 etc. which is not valid syntax.
// Additionally for large arrays this can make for a
// very cluttered list. So I filter them out.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you change the language on the last two sentences to get rid of the I.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

heh, yes, I completely ignored the comments in the original patch.. I'll edit those.

memberGroups
.push(Object
.getOwnPropertyNames(obj)
.filter(function(item) {
return isNaN(parseInt(item));
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe use +item instead of parseInt(). For example, parseInt('3c') returns 3.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that's actually a 'feature' ;-) ... consider.. var m = []; m['123abc'] = 'a' .. if you do tab completion, the 123abc and use +item, the 123abc would still appear, which ends up having the same problem this patch is trying to fix. Using parseInt filters those out, even if they are valid keys for the object.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, carry on then :-)

}));
} else if (util.isObject(obj) || util.isFunction(obj)) {
memberGroups.push(Object.getOwnPropertyNames(obj));
}
// works for non-objects
Expand Down
107 changes: 107 additions & 0 deletions test/simple/test-repl-tab-complete-array-remove-integer-elements.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// Copyright Joyent, Inc. and other Node contributors.
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we still do the header in the joyent/node branches?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, for consistency, all the other tests still have this header.

//
// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to permit
// persons to whom the Software is furnished to do so, subject to the
// following conditions:
//
// The above copyright notice and this permission notice shall be included
// in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
// OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN
// NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
// DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE
// USE OR OTHER DEALINGS IN THE SOFTWARE.

var common = require('../common');
var assert = require('assert');
var util = require('util');

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No need for the blank line here.

var repl = require('repl');

// A stream to push an array into a REPL
function ArrayStream() {
this.run = function(data) {
var self = this;
data.forEach(function(line) {
self.emit('data', line + '\n');
});
}
}
util.inherits(ArrayStream, require('stream').Stream);
ArrayStream.prototype.readable = true;
ArrayStream.prototype.writable = true;
ArrayStream.prototype.resume = function() {};
ArrayStream.prototype.write = function() {};

var array_elements = [
[ 'asdf.__defineGetter__', 'asdf.__defineSetter__',
'asdf.__lookupGetter__', 'asdf.__lookupSetter__',
'asdf.__proto__', 'asdf.constructor',
'asdf.hasOwnProperty', 'asdf.isPrototypeOf',
'asdf.propertyIsEnumerable',
'asdf.toLocaleString', 'asdf.toString',
'asdf.valueOf', '', 'asdf.concat', 'asdf.constructor',
'asdf.every', 'asdf.filter',
'asdf.forEach', 'asdf.indexOf',
'asdf.join', 'asdf.lastIndexOf',
'asdf.length', 'asdf.map',
'asdf.pop', 'asdf.push', 'asdf.reduce',
'asdf.reduceRight', 'asdf.reverse', 'asdf.shift',
'asdf.slice', 'asdf.some', 'asdf.sort', 'asdf.splice',
'asdf.toLocaleString', 'asdf.toString', 'asdf.unshift',
'', 'asdf.length' ],
'asdf.' ];

var object_elements = [
[ 'asdf.__defineGetter__', 'asdf.__defineSetter__',
'asdf.__lookupGetter__', 'asdf.__lookupSetter__',
'asdf.__proto__', 'asdf.constructor',
'asdf.hasOwnProperty', 'asdf.isPrototypeOf',
'asdf.propertyIsEnumerable',
'asdf.toLocaleString', 'asdf.toString',
'asdf.valueOf', '', 'asdf.1'],

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmmm asdf.1 is still invalid

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking into the test... it appears that somewhere along the line, test-repl-tab-complete stopped working properly. The entire test is not run and exits with a success without executing all of the code. Investigating now

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The issue with the test exiting part of the way through has been fixed in the converged repo.

'asdf.' ];

var putIn = new ArrayStream();
var testMe = repl.start('', putIn);

// Tab Complete will not return integer indexes
putIn.run(['.clear']);
putIn.run([
'asdf = [\'one\', 2, 3]',
]);
testMe.complete('asdf.', function(error, data) {
assert.deepEqual(data, array_elements);
});

// Tab Complete will return additional non-integer indexes
putIn.run(['.clear']);
putIn.run([
'asdf = [\'one\', 2, 3]',
'asdf.qwer = 1'
]);

var array_with_extra = array_elements;
array_with_extra[0].push('qwer');

testMe.complete('asdf.', function(error, data) {
assert.deepEqual(data, array_with_extra);
});

// Tab Complete will return integer indexes on an object
putIn.run(['.clear']);
putIn.run([
'asdf = {}',
'asdf[1] = 1'
]);
testMe.complete('asdf.', function(error, data) {
assert.deepEqual(data, object_elements);
});