Skip to content

Commit d4d9b20

Browse files
committed
repl: make own properties shadow prototype properties
Previously, the code displayed properties backwards (e.g., showing prototype properties before own properties). It also did uniqueness checks during this processing, so these checks were done backwards. After this change, the properties continue to be displayed backwards, but the uniqueness checks are done in the proper order. Fixes: nodejs#15199 See also: nodejs#21586 which was discovered during the testing of this fix.
1 parent ebf5b58 commit d4d9b20

File tree

2 files changed

+24
-7
lines changed

2 files changed

+24
-7
lines changed

lib/repl.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -1209,20 +1209,20 @@ function complete(line, callback) {
12091209
// Completion group 0 is the "closest"
12101210
// (least far up the inheritance chain)
12111211
// so we put its completions last: to be closest in the REPL.
1212-
for (i = completionGroups.length - 1; i >= 0; i--) {
1212+
for (i = 0; i < completionGroups.length; i++) {
12131213
group = completionGroups[i];
12141214
group.sort();
1215-
for (var j = 0; j < group.length; j++) {
1215+
for (var j = group.length - 1; j >= 0; j--) {
12161216
c = group[j];
12171217
if (!hasOwnProperty(uniq, c)) {
1218-
completions.push(c);
1218+
completions.unshift(c);
12191219
uniq[c] = true;
12201220
}
12211221
}
1222-
completions.push(''); // Separator btwn groups
1222+
completions.unshift(''); // Separator btwn groups
12231223
}
1224-
while (completions.length && completions[completions.length - 1] === '') {
1225-
completions.pop();
1224+
while (completions.length && completions[0] === '') {
1225+
completions.shift();
12261226
}
12271227
}
12281228

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,10 @@ putIn.run([
149149
' one:1',
150150
'};'
151151
]);
152-
testMe.complete('inner.o', getNoResultsFunction());
152+
// See: https://github.com/nodejs/node/issues/21586
153+
// testMe.complete('inner.o', getNoResultsFunction());
154+
testMe.complete('inner.o', common.mustCall(function(error, data) {
155+
}));
153156

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

@@ -206,6 +209,20 @@ testMe.complete('toSt', common.mustCall(function(error, data) {
206209
assert.deepStrictEqual(data, [['toString'], 'toSt']);
207210
}));
208211

212+
// own properties should shadow properties on the prototype
213+
putIn.run(['.clear']);
214+
putIn.run([
215+
'var x = Object.create(null);',
216+
'x.a = 1;',
217+
'x.b = 2;',
218+
'var y = Object.create(x);',
219+
'y.a = 3;',
220+
'y.c = 4;'
221+
]);
222+
testMe.complete('y.', common.mustCall(function(error, data) {
223+
assert.deepStrictEqual(data, [['y.b', '', 'y.a', 'y.c'], 'y.']);
224+
}));
225+
209226
// Tab complete provides built in libs for require()
210227
putIn.run(['.clear']);
211228

0 commit comments

Comments
 (0)