Skip to content

Commit e66cb58

Browse files
BridgeARaddaleax
authored andcommitted
repl: simplify and improve completion
The completion lists used a hand crafted list of global entries that was redundant due to also using the actual global properties for tab completion. Those entries ended up in an separated completion group which did not seem useful. PR-URL: #25731 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
1 parent 1c6fade commit e66cb58

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

lib/repl.js

+17-41
Original file line numberDiff line numberDiff line change
@@ -90,18 +90,6 @@ let processTopLevelAwait;
9090
const parentModule = module;
9191
const replMap = new WeakMap();
9292

93-
const GLOBAL_OBJECT_PROPERTIES = [
94-
'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN',
95-
'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI',
96-
'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean',
97-
'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError',
98-
'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON'
99-
];
100-
const GLOBAL_OBJECT_PROPERTY_MAP = {};
101-
for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) {
102-
GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] =
103-
GLOBAL_OBJECT_PROPERTIES[n];
104-
}
10593
const kBufferedCommandSymbol = Symbol('bufferedCommand');
10694
const kContextId = Symbol('contextId');
10795

@@ -806,24 +794,17 @@ REPLServer.prototype.createContext = function() {
806794
}, () => {
807795
context = vm.createContext();
808796
});
797+
for (const name of Object.getOwnPropertyNames(global)) {
798+
Object.defineProperty(context, name,
799+
Object.getOwnPropertyDescriptor(global, name));
800+
}
809801
context.global = context;
810802
const _console = new Console(this.outputStream);
811803
Object.defineProperty(context, 'console', {
812804
configurable: true,
813805
writable: true,
814806
value: _console
815807
});
816-
817-
var names = Object.getOwnPropertyNames(global);
818-
for (var n = 0; n < names.length; n++) {
819-
var name = names[n];
820-
if (name === 'console' || name === 'global')
821-
continue;
822-
if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) {
823-
Object.defineProperty(context, name,
824-
Object.getOwnPropertyDescriptor(global, name));
825-
}
826-
}
827808
}
828809

829810
var module = new CJSModule('<repl>');
@@ -1135,19 +1116,19 @@ function complete(line, callback) {
11351116
}
11361117
completionGroups.push(
11371118
filteredOwnPropertyNames.call(this, this.context));
1138-
addStandardGlobals(completionGroups, filter);
1119+
if (filter !== '') addCommonWords(completionGroups);
11391120
completionGroupsLoaded();
11401121
} else {
11411122
this.eval('.scope', this.context, 'repl', function ev(err, globals) {
11421123
if (err || !Array.isArray(globals)) {
1143-
addStandardGlobals(completionGroups, filter);
1124+
if (filter !== '') addCommonWords(completionGroups);
11441125
} else if (Array.isArray(globals[0])) {
11451126
// Add grouped globals
11461127
for (var n = 0; n < globals.length; n++)
11471128
completionGroups.push(globals[n]);
11481129
} else {
11491130
completionGroups.push(globals);
1150-
addStandardGlobals(completionGroups, filter);
1131+
if (filter !== '') addCommonWords(completionGroups);
11511132
}
11521133
completionGroupsLoaded();
11531134
});
@@ -1371,21 +1352,16 @@ function _memory(cmd) {
13711352
}
13721353
}
13731354

1374-
function addStandardGlobals(completionGroups, filter) {
1375-
// Global object properties
1376-
// (http://www.ecma-international.org/publications/standards/Ecma-262.htm)
1377-
completionGroups.push(GLOBAL_OBJECT_PROPERTIES);
1378-
// Common keywords. Exclude for completion on the empty string, b/c
1379-
// they just get in the way.
1380-
if (filter) {
1381-
completionGroups.push([
1382-
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
1383-
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
1384-
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
1385-
'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
1386-
'typeof', 'undefined', 'var', 'void', 'while', 'with', 'yield'
1387-
]);
1388-
}
1355+
function addCommonWords(completionGroups) {
1356+
// Only words which do not yet exist as global property should be added to
1357+
// this list.
1358+
completionGroups.push([
1359+
'async', 'await', 'break', 'case', 'catch', 'const', 'continue',
1360+
'debugger', 'default', 'delete', 'do', 'else', 'export', 'false',
1361+
'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let',
1362+
'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try',
1363+
'typeof', 'var', 'void', 'while', 'with', 'yield'
1364+
]);
13891365
}
13901366

13911367
function _turnOnEditorMode(repl) {

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -457,7 +457,7 @@ const testNonGlobal = repl.start({
457457
useGlobal: false
458458
});
459459

460-
const builtins = [['Infinity', '', 'Int16Array', 'Int32Array',
460+
const builtins = [['Infinity', 'Int16Array', 'Int32Array',
461461
'Int8Array'], 'I'];
462462

463463
if (common.hasIntl) {

0 commit comments

Comments
 (0)