GeezerVison is showing github.com — repl: show lexically scoped vars in tab completion · sg-evals/nodejs-node@416c0ec
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboard +25Lines changed: 25 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const hasInspector = process.config.variables.v8_enable_inspector === 1; | | 4 | +const inspector = hasInspector ? require('inspector') : undefined; | | 5 | + | | 6 | +let session; | | 7 | + | | 8 | +function sendInspectorCommand(cb, onError) { | | 9 | + if (!hasInspector) return onError(); | | 10 | + if (session === undefined) session = new inspector.Session(); | | 11 | + try { | | 12 | + session.connect(); | | 13 | + try { | | 14 | + return cb(session); | | 15 | + } finally { | | 16 | + session.disconnect(); | | 17 | + } | | 18 | + } catch (e) { | | 19 | + return onError(); | | 20 | + } | | 21 | +} | | 22 | + | | 23 | +module.exports = { | | 24 | + sendInspectorCommand | | 25 | +}; |
Collapse file Copy file name to clipboardExpand all lines: lib/repl.js +27-1Lines changed: 27 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -59,6 +59,7 @@ const Module = require('module'); | 59 | 59 | const domain = require('domain'); | 60 | 60 | const debug = util.debuglog('repl'); | 61 | 61 | const errors = require('internal/errors'); | | 62 | +const { sendInspectorCommand } = require('internal/util/inspector'); | 62 | 63 |
| 63 | 64 | const parentModule = module; | 64 | 65 | const replMap = new WeakMap(); | @@ -76,6 +77,7 @@ for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) { | 76 | 77 | GLOBAL_OBJECT_PROPERTIES[n]; | 77 | 78 | } | 78 | 79 | const kBufferedCommandSymbol = Symbol('bufferedCommand'); | | 80 | +const kContextId = Symbol('contextId'); | 79 | 81 |
| 80 | 82 | try { | 81 | 83 | // hack for require.resolve("./relative") to work properly. | @@ -158,6 +160,8 @@ function REPLServer(prompt, | 158 | 160 | self.last = undefined; | 159 | 161 | self.breakEvalOnSigint = !!breakEvalOnSigint; | 160 | 162 | self.editorMode = false; | | 163 | + // Context id for use with the inspector protocol. | | 164 | + self[kContextId] = undefined; | 161 | 165 |
| 162 | 166 | // just for backwards compat, see github.com/joyent/node/pull/7127 | 163 | 167 | self.rli = this; | @@ -755,7 +759,16 @@ REPLServer.prototype.createContext = function() { | 755 | 759 | if (this.useGlobal) { | 756 | 760 | context = global; | 757 | 761 | } else { | 758 | | - context = vm.createContext(); | | 762 | + sendInspectorCommand((session) => { | | 763 | + session.post('Runtime.enable'); | | 764 | + session.on('Runtime.executionContextCreated', ({ params }) => { | | 765 | + this[kContextId] = params.context.id; | | 766 | + }); | | 767 | + context = vm.createContext(); | | 768 | + session.post('Runtime.disable'); | | 769 | + }, () => { | | 770 | + context = vm.createContext(); | | 771 | + }); | 759 | 772 | context.global = context; | 760 | 773 | const _console = new Console(this.outputStream); | 761 | 774 | Object.defineProperty(context, 'console', { | @@ -890,6 +903,18 @@ function filteredOwnPropertyNames(obj) { | 890 | 903 | return Object.getOwnPropertyNames(obj).filter(intFilter); | 891 | 904 | } | 892 | 905 |
| | 906 | +function getGlobalLexicalScopeNames(contextId) { | | 907 | + return sendInspectorCommand((session) => { | | 908 | + let names = []; | | 909 | + session.post('Runtime.globalLexicalScopeNames', { | | 910 | + executionContextId: contextId | | 911 | + }, (error, result) => { | | 912 | + if (!error) names = result.names; | | 913 | + }); | | 914 | + return names; | | 915 | + }, () => []); | | 916 | +} | | 917 | + | 893 | 918 | REPLServer.prototype.complete = function() { | 894 | 919 | this.completer.apply(this, arguments); | 895 | 920 | }; | @@ -1053,6 +1078,7 @@ function complete(line, callback) { | 1053 | 1078 | // If context is instance of vm.ScriptContext | 1054 | 1079 | // Get global vars synchronously | 1055 | 1080 | if (this.useGlobal || vm.isContext(this.context)) { | | 1081 | + completionGroups.push(getGlobalLexicalScopeNames(this[kContextId])); | 1056 | 1082 | var contextProto = this.context; | 1057 | 1083 | while (contextProto = Object.getPrototypeOf(contextProto)) { | 1058 | 1084 | completionGroups.push( | |
Collapse file Copy file name to clipboardExpand all lines: node.gyp +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 129 | 129 | 'lib/internal/url.js', | 130 | 130 | 'lib/internal/util.js', | 131 | 131 | 'lib/internal/util/comparisons.js', | | 132 | + 'lib/internal/util/inspector.js', | 132 | 133 | 'lib/internal/util/types.js', | 133 | 134 | 'lib/internal/http2/core.js', | 134 | 135 | 'lib/internal/http2/compat.js', | |
Collapse file Copy file name to clipboard +34Lines changed: 34 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +'use strict'; | | 2 | + | | 3 | +const common = require('../common'); | | 4 | +const assert = require('assert'); | | 5 | +const repl = require('repl'); | | 6 | + | | 7 | +common.skipIfInspectorDisabled(); | | 8 | + | | 9 | +// This test verifies that the V8 inspector API is usable in the REPL. | | 10 | + | | 11 | +const putIn = new common.ArrayStream(); | | 12 | +let output = ''; | | 13 | +putIn.write = function(data) { | | 14 | + output += data; | | 15 | +}; | | 16 | + | | 17 | +const testMe = repl.start('', putIn); | | 18 | + | | 19 | +putIn.run(['const myVariable = 42']); | | 20 | + | | 21 | +testMe.complete('myVar', common.mustCall((error, data) => { | | 22 | + assert.deepStrictEqual(data, [['myVariable'], 'myVar']); | | 23 | +})); | | 24 | + | | 25 | +putIn.run([ | | 26 | + 'const inspector = require("inspector")', | | 27 | + 'const session = new inspector.Session()', | | 28 | + 'session.connect()', | | 29 | + 'session.post("Runtime.evaluate", { expression: "1 + 1" }, console.log)', | | 30 | + 'session.disconnect()' | | 31 | +]); | | 32 | + | | 33 | +assert(output.includes( | | 34 | + "null { result: { type: 'number', value: 2, description: '2' } }")); |
Collapse file Copy file name to clipboardExpand all lines: test/parallel/test-repl-tab-complete.js +21Lines changed: 21 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 24 | 24 | const common = require('../common'); | 25 | 25 | const assert = require('assert'); | 26 | 26 | const fixtures = require('../common/fixtures'); | | 27 | +const hasInspector = process.config.variables.v8_enable_inspector === 1; | 27 | 28 |
| 28 | 29 | // We have to change the directory to ../fixtures before requiring repl | 29 | 30 | // in order to make the tests for completion of node_modules work properly | @@ -529,3 +530,23 @@ editorStream.run(['.editor']); | 529 | 530 | editor.completer('var log = console.l', common.mustCall((error, data) => { | 530 | 531 | assert.deepStrictEqual(data, [['console.log'], 'console.l']); | 531 | 532 | })); | | 533 | + | | 534 | +{ | | 535 | + // tab completion of lexically scoped variables | | 536 | + const stream = new common.ArrayStream(); | | 537 | + const testRepl = repl.start({ stream }); | | 538 | + | | 539 | + stream.run([` | | 540 | + let lexicalLet = true; | | 541 | + const lexicalConst = true; | | 542 | + class lexicalKlass {} | | 543 | + `]); | | 544 | + | | 545 | + ['Let', 'Const', 'Klass'].forEach((type) => { | | 546 | + const query = `lexical${type[0]}`; | | 547 | + const expected = hasInspector ? [[`lexical${type}`], query] : []; | | 548 | + testRepl.complete(query, common.mustCall((error, data) => { | | 549 | + assert.deepStrictEqual(data, expected); | | 550 | + })); | | 551 | + }); | | 552 | +} |
|