Skip to content

Commit 4b613d3

Browse files
rubystargos
authored andcommitted
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. See also: #21586 which was discovered during the testing of this fix. Fixes: #15199 PR-URL: #21588 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Tiancheng "Timothy" Gu <[email protected]> Reviewed-By: Anna Henningsen <[email protected]>
1 parent bba500d commit 4b613d3

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
@@ -1229,20 +1229,20 @@ function complete(line, callback) {
12291229
// Completion group 0 is the "closest"
12301230
// (least far up the inheritance chain)
12311231
// so we put its completions last: to be closest in the REPL.
1232-
for (i = completionGroups.length - 1; i >= 0; i--) {
1232+
for (i = 0; i < completionGroups.length; i++) {
12331233
group = completionGroups[i];
12341234
group.sort();
1235-
for (var j = 0; j < group.length; j++) {
1235+
for (var j = group.length - 1; j >= 0; j--) {
12361236
c = group[j];
12371237
if (!hasOwnProperty(uniq, c)) {
1238-
completions.push(c);
1238+
completions.unshift(c);
12391239
uniq[c] = true;
12401240
}
12411241
}
1242-
completions.push(''); // Separator btwn groups
1242+
completions.unshift(''); // Separator btwn groups
12431243
}
1244-
while (completions.length && completions[completions.length - 1] === '') {
1245-
completions.pop();
1244+
while (completions.length && completions[0] === '') {
1245+
completions.shift();
12461246
}
12471247
}
12481248

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)