-
Notifications
You must be signed in to change notification settings - Fork 29.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
repl: show lexically scoped vars in tab completion #16591
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
Use the V8 inspector protocol, if available, to query the list of lexically scoped variables (defined with `let`, `const` or `class`). PR-URL: #16591 Fixes: #983 Reviewed-By: Timothy Gu <timothygu99@gmail.com> Reviewed-By: Ruben Bridgewater <ruben@bridgewater.de>
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
'use strict'; | ||
|
||
const hasInspector = process.config.variables.v8_enable_inspector === 1; | ||
const inspector = hasInspector ? require('inspector') : undefined; | ||
|
||
let session; | ||
|
||
function sendInspectorCommand(cb, onError) { | ||
if (!hasInspector) return onError(); | ||
if (session === undefined) session = new inspector.Session(); | ||
try { | ||
session.connect(); | ||
try { | ||
return cb(session); | ||
} finally { | ||
session.disconnect(); | ||
} | ||
} catch (e) { | ||
return onError(); | ||
} | ||
} | ||
|
||
module.exports = { | ||
sendInspectorCommand | ||
}; |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -59,6 +59,7 @@ const Module = require('module'); | |
const domain = require('domain'); | ||
const debug = util.debuglog('repl'); | ||
const errors = require('internal/errors'); | ||
const { sendInspectorCommand } = require('internal/util/inspector'); | ||
|
||
const parentModule = module; | ||
const replMap = new WeakMap(); | ||
|
@@ -76,6 +77,7 @@ for (var n = 0; n < GLOBAL_OBJECT_PROPERTIES.length; n++) { | |
GLOBAL_OBJECT_PROPERTIES[n]; | ||
} | ||
const kBufferedCommandSymbol = Symbol('bufferedCommand'); | ||
const kContextId = Symbol('contextId'); | ||
|
||
try { | ||
// hack for require.resolve("./relative") to work properly. | ||
|
@@ -158,6 +160,8 @@ function REPLServer(prompt, | |
self.last = undefined; | ||
self.breakEvalOnSigint = !!breakEvalOnSigint; | ||
self.editorMode = false; | ||
// Context id for use with the inspector protocol. | ||
self[kContextId] = undefined; | ||
|
||
// just for backwards compat, see github.com/joyent/node/pull/7127 | ||
self.rli = this; | ||
|
@@ -755,7 +759,16 @@ REPLServer.prototype.createContext = function() { | |
if (this.useGlobal) { | ||
context = global; | ||
} else { | ||
context = vm.createContext(); | ||
sendInspectorCommand((session) => { | ||
session.post('Runtime.enable'); | ||
session.on('Runtime.executionContextCreated', ({ params }) => { | ||
this[kContextId] = params.context.id; | ||
}); | ||
context = vm.createContext(); | ||
session.post('Runtime.disable'); | ||
}, () => { | ||
context = vm.createContext(); | ||
}); | ||
context.global = context; | ||
const _console = new Console(this.outputStream); | ||
Object.defineProperty(context, 'console', { | ||
|
@@ -890,6 +903,18 @@ function filteredOwnPropertyNames(obj) { | |
return Object.getOwnPropertyNames(obj).filter(intFilter); | ||
} | ||
|
||
function getGlobalLexicalScopeNames(contextId) { | ||
return sendInspectorCommand((session) => { | ||
let names = []; | ||
session.post('Runtime.globalLexicalScopeNames', { | ||
executionContextId: contextId | ||
}, (error, result) => { | ||
if (!error) names = result.names; | ||
}); | ||
return names; | ||
}, () => []); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Wait – you can use the inspector to do synchronous exception of the running process? This is absolutely amazing?? 😍 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, it's cool. However in reality it's actually a released Zalgo:
I guess either nobody thought of this problem when the API or added, or synchronous calling in those circumstances under which it's possible is too valuable to force asynchrony, that it remains this way. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
I would go with that. ;) There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, many thanks to @ofrobots who explained this to me at NINA! |
||
|
||
REPLServer.prototype.complete = function() { | ||
this.completer.apply(this, arguments); | ||
}; | ||
|
@@ -1053,6 +1078,7 @@ function complete(line, callback) { | |
// If context is instance of vm.ScriptContext | ||
// Get global vars synchronously | ||
if (this.useGlobal || vm.isContext(this.context)) { | ||
completionGroups.push(getGlobalLexicalScopeNames(this[kContextId])); | ||
var contextProto = this.context; | ||
while (contextProto = Object.getPrototypeOf(contextProto)) { | ||
completionGroups.push( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
'use strict'; | ||
|
||
const common = require('../common'); | ||
const assert = require('assert'); | ||
const repl = require('repl'); | ||
|
||
common.skipIfInspectorDisabled(); | ||
|
||
// This test verifies that the V8 inspector API is usable in the REPL. | ||
|
||
const putIn = new common.ArrayStream(); | ||
let output = ''; | ||
putIn.write = function(data) { | ||
output += data; | ||
}; | ||
|
||
const testMe = repl.start('', putIn); | ||
|
||
putIn.run(['const myVariable = 42']); | ||
|
||
testMe.complete('myVar', common.mustCall((error, data) => { | ||
assert.deepStrictEqual(data, [['myVariable'], 'myVar']); | ||
})); | ||
|
||
putIn.run([ | ||
'const inspector = require("inspector")', | ||
'const session = new inspector.Session()', | ||
'session.connect()', | ||
'session.post("Runtime.evaluate", { expression: "1 + 1" }, console.log)', | ||
'session.disconnect()' | ||
]); | ||
|
||
assert(output.includes( | ||
"null { result: { type: 'number', value: 2, description: '2' } }")); |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -24,6 +24,7 @@ | |
const common = require('../common'); | ||
const assert = require('assert'); | ||
const fixtures = require('../common/fixtures'); | ||
const hasInspector = process.config.variables.v8_enable_inspector === 1; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This doesn't seem to be used? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm going to use it when I figure out how to make the test work |
||
|
||
// We have to change the directory to ../fixtures before requiring repl | ||
// in order to make the tests for completion of node_modules work properly | ||
|
@@ -529,3 +530,23 @@ editorStream.run(['.editor']); | |
editor.completer('var log = console.l', common.mustCall((error, data) => { | ||
assert.deepStrictEqual(data, [['console.log'], 'console.l']); | ||
})); | ||
|
||
{ | ||
// tab completion of lexically scoped variables | ||
const stream = new common.ArrayStream(); | ||
const testRepl = repl.start({ stream }); | ||
|
||
stream.run([` | ||
let lexicalLet = true; | ||
const lexicalConst = true; | ||
class lexicalKlass {} | ||
`]); | ||
|
||
['Let', 'Const', 'Klass'].forEach((type) => { | ||
const query = `lexical${type[0]}`; | ||
const expected = hasInspector ? [[`lexical${type}`], query] : []; | ||
testRepl.complete(query, common.mustCall((error, data) => { | ||
assert.deepStrictEqual(data, expected); | ||
})); | ||
}); | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would be helpful to clarify that this ID refers to the context ID in the inspector protocol,
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done