-
Notifications
You must be signed in to change notification settings - Fork 30k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
repl: show lexically scoped vars in tab completion
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
1 parent
983b028
commit 30273d4
Showing
5 changed files
with
108 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' } }")); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters