diff options
author | Ruben Bridgewater <ruben@bridgewater.de> | 2019-01-25 08:26:53 +0100 |
---|---|---|
committer | Daniel Bevenius <daniel.bevenius@gmail.com> | 2019-02-06 07:30:28 +0100 |
commit | e3055dc5254502d2f02276f20c9a3f37b41e7f12 (patch) | |
tree | 64ba7551fd2ad97b817992127068eaa35b1fc8c8 /lib | |
parent | 8d2df41618266f6bcd5579d6de63a777149f4166 (diff) | |
download | android-node-v8-e3055dc5254502d2f02276f20c9a3f37b41e7f12.tar.gz android-node-v8-e3055dc5254502d2f02276f20c9a3f37b41e7f12.tar.bz2 android-node-v8-e3055dc5254502d2f02276f20c9a3f37b41e7f12.zip |
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: https://github.com/nodejs/node/pull/25731
Reviewed-By: Michaƫl Zasso <targos@protonmail.com>
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
Diffstat (limited to 'lib')
-rw-r--r-- | lib/repl.js | 58 |
1 files changed, 17 insertions, 41 deletions
diff --git a/lib/repl.js b/lib/repl.js index 30812c232b..df861843d1 100644 --- a/lib/repl.js +++ b/lib/repl.js @@ -89,18 +89,6 @@ let processTopLevelAwait; const parentModule = module; const replMap = new WeakMap(); -const GLOBAL_OBJECT_PROPERTIES = [ - 'NaN', 'Infinity', 'undefined', 'eval', 'parseInt', 'parseFloat', 'isNaN', - 'isFinite', 'decodeURI', 'decodeURIComponent', 'encodeURI', - 'encodeURIComponent', 'Object', 'Function', 'Array', 'String', 'Boolean', - 'Number', 'Date', 'RegExp', 'Error', 'EvalError', 'RangeError', - 'ReferenceError', 'SyntaxError', 'TypeError', 'URIError', 'Math', 'JSON' -]; -const GLOBAL_OBJECT_PROPERTY_MAP = {}; -for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) { - GLOBAL_OBJECT_PROPERTY_MAP[GLOBAL_OBJECT_PROPERTIES[n]] = - GLOBAL_OBJECT_PROPERTIES[n]; -} const kBufferedCommandSymbol = Symbol('bufferedCommand'); const kContextId = Symbol('contextId'); @@ -807,6 +795,10 @@ REPLServer.prototype.createContext = function() { }, () => { context = vm.createContext(); }); + for (const name of Object.getOwnPropertyNames(global)) { + Object.defineProperty(context, name, + Object.getOwnPropertyDescriptor(global, name)); + } context.global = context; const _console = new Console(this.outputStream); Object.defineProperty(context, 'console', { @@ -814,17 +806,6 @@ REPLServer.prototype.createContext = function() { writable: true, value: _console }); - - var names = Object.getOwnPropertyNames(global); - for (var n = 0; n < names.length; n++) { - var name = names[n]; - if (name === 'console' || name === 'global') - continue; - if (GLOBAL_OBJECT_PROPERTY_MAP[name] === undefined) { - Object.defineProperty(context, name, - Object.getOwnPropertyDescriptor(global, name)); - } - } } var module = new CJSModule('<repl>'); @@ -1137,19 +1118,19 @@ function complete(line, callback) { } completionGroups.push( filteredOwnPropertyNames.call(this, this.context)); - addStandardGlobals(completionGroups, filter); + if (filter !== '') addCommonWords(completionGroups); completionGroupsLoaded(); } else { this.eval('.scope', this.context, 'repl', function ev(err, globals) { if (err || !Array.isArray(globals)) { - addStandardGlobals(completionGroups, filter); + if (filter !== '') addCommonWords(completionGroups); } else if (Array.isArray(globals[0])) { // Add grouped globals for (var n = 0; n < globals.length; n++) completionGroups.push(globals[n]); } else { completionGroups.push(globals); - addStandardGlobals(completionGroups, filter); + if (filter !== '') addCommonWords(completionGroups); } completionGroupsLoaded(); }); @@ -1373,21 +1354,16 @@ function _memory(cmd) { } } -function addStandardGlobals(completionGroups, filter) { - // Global object properties - // (http://www.ecma-international.org/publications/standards/Ecma-262.htm) - completionGroups.push(GLOBAL_OBJECT_PROPERTIES); - // Common keywords. Exclude for completion on the empty string, b/c - // they just get in the way. - if (filter) { - completionGroups.push([ - 'async', 'await', 'break', 'case', 'catch', 'const', 'continue', - 'debugger', 'default', 'delete', 'do', 'else', 'export', 'false', - 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let', - 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try', - 'typeof', 'undefined', 'var', 'void', 'while', 'with', 'yield' - ]); - } +function addCommonWords(completionGroups) { + // Only words which do not yet exist as global property should be added to + // this list. + completionGroups.push([ + 'async', 'await', 'break', 'case', 'catch', 'const', 'continue', + 'debugger', 'default', 'delete', 'do', 'else', 'export', 'false', + 'finally', 'for', 'function', 'if', 'import', 'in', 'instanceof', 'let', + 'new', 'null', 'return', 'switch', 'this', 'throw', 'true', 'try', + 'typeof', 'var', 'void', 'while', 'with', 'yield' + ]); } function _turnOnEditorMode(repl) { |