Skip to content

Commit e3055dc

Browse files
BridgeARdanbev
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 8d2df41 commit e3055dc

File tree

2 files changed

+18
-42
lines changed

2 files changed

+18
-42
lines changed

lib/repl.js

Lines changed: 17 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -89,18 +89,6 @@ let processTopLevelAwait;
8989
const parentModule = module;
9090
const replMap = new WeakMap();
9191

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

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

830811
var module = new CJSModule('<repl>');
@@ -1137,19 +1118,19 @@ function complete(line, callback) {
11371118
}
11381119
completionGroups.push(
11391120
filteredOwnPropertyNames.call(this, this.context));
1140-
addStandardGlobals(completionGroups, filter);
1121+
if (filter !== '') addCommonWords(completionGroups);
11411122
completionGroupsLoaded();
11421123
} else {
11431124
this.eval('.scope', this.context, 'repl', function ev(err, globals) {
11441125
if (err || !Array.isArray(globals)) {
1145-
addStandardGlobals(completionGroups, filter);
1126+
if (filter !== '') addCommonWords(completionGroups);
11461127
} else if (Array.isArray(globals[0])) {
11471128
// Add grouped globals
11481129
for (var n = 0; n < globals.length; n++)
11491130
completionGroups.push(globals[n]);
11501131
} else {
11511132
completionGroups.push(globals);
1152-
addStandardGlobals(completionGroups, filter);
1133+
if (filter !== '') addCommonWords(completionGroups);
11531134
}
11541135
completionGroupsLoaded();
11551136
});
@@ -1373,21 +1354,16 @@ function _memory(cmd) {
13731354
}
13741355
}
13751356

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

13931369
function _turnOnEditorMode(repl) {

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

Lines changed: 1 addition & 1 deletion
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)