Skip to content

Commit 51253ba

Browse files
Dailyscatdanielleadams
authored andcommitted
debugger: add a command to set which lines to check for context
PR-URL: #46812 Reviewed-By: Kohei Ueno <[email protected]>
1 parent ba340a0 commit 51253ba

File tree

3 files changed

+84
-3
lines changed

3 files changed

+84
-3
lines changed

‎lib/internal/debugger/inspect_repl.js‎

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ const{
4646

4747
const{ERR_DEBUGGER_ERROR}=require('internal/errors').codes;
4848

49-
const{ validateString }=require('internal/validators');
49+
const{ validateString, validateNumber}=require('internal/validators');
5050

5151
constFS=require('fs');
5252
constPath=require('path');
@@ -81,7 +81,7 @@ out, o Step out, leaving the current function
8181
backtrace, bt Print the current backtrace
8282
list Print the source around the current line where execution
8383
is currently paused
84-
84+
setContextLineNumber Set which lines to check for context
8585
setBreakpoint, sb Set a breakpoint
8686
clearBreakpoint, cb Clear a breakpoint
8787
breakpoints List all known breakpoints
@@ -381,6 +381,7 @@ function createRepl(inspector){
381381
letcurrentBacktrace;
382382
letselectedFrame;
383383
letexitDebugRepl;
384+
letcontextLineNumber=2;
384385

385386
functionresetOnStart(){
386387
knownScripts={};
@@ -685,6 +686,15 @@ function createRepl(inspector){
685686
});
686687
}
687688

689+
functionsetContextLineNumber(delta=2){
690+
if(!selectedFrame){
691+
thrownewERR_DEBUGGER_ERROR('Requires execution to be paused');
692+
}
693+
validateNumber(delta,'delta',1);
694+
contextLineNumber=delta;
695+
print(`The contextLine has been changed to ${delta}.`);
696+
}
697+
688698
functionhandleBreakpointResolved({ breakpointId, location }){
689699
constscript=knownScripts[location.scriptId];
690700
constscriptUrl=script&&script.url;
@@ -897,7 +907,7 @@ function createRepl(inspector){
897907

898908
inspector.suspendReplWhile(()=>
899909
PromisePrototypeThen(
900-
SafePromiseAllReturnArrayLike([formatWatchers(true),selectedFrame.list(2)]),
910+
SafePromiseAllReturnArrayLike([formatWatchers(true),selectedFrame.list(contextLineNumber)]),
901911
({0: watcherList,1: context})=>{
902912
constbreakContext=watcherList ?
903913
`${watcherList}\n${inspect(context)}` :
@@ -1159,6 +1169,7 @@ function createRepl(inspector){
11591169
},
11601170

11611171
list,
1172+
setContextLineNumber,
11621173
});
11631174
aliasProperties(context,SHORTCUTS);
11641175
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
letx=0;
2+
x=1;
3+
x=2;
4+
x=3;
5+
x=4;
6+
x=5;
7+
x=6;
8+
x=7;
9+
x=8;
10+
x=9;
11+
x=10;
12+
x=11;
13+
x=12;
14+
x=13;
15+
x=14;
16+
x=15;
17+
x=16;
18+
x=17;
19+
x=18;
20+
module.exports=x;
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
import{skipIfInspectorDisabled}from'../common/index.mjs';
2+
skipIfInspectorDisabled();
3+
4+
import{path}from'../common/fixtures.mjs';
5+
importstartCLIfrom'../common/debugger.js';
6+
7+
importassertfrom'assert';
8+
9+
constscript=path('debugger','twenty-lines.js');
10+
constcli=startCLI([script]);
11+
12+
functiononFatal(error){
13+
cli.quit();
14+
throwerror;
15+
}
16+
17+
functiongetLastLine(output){
18+
constsplittedByLine=output.split('');
19+
returnsplittedByLine[splittedByLine.length-2];
20+
}
21+
22+
// Stepping through breakpoints.
23+
try{
24+
awaitcli.waitForInitialBreak();
25+
awaitcli.waitForPrompt();
26+
27+
awaitcli.command('setContextLineNumber("1")');
28+
assert.ok(cli.output.includes('argument must be of type number. Received type string'));
29+
30+
awaitcli.command('setContextLineNumber(0)');
31+
assert.ok(cli.output.includes('It must be >= 1. Received 0'));
32+
33+
// Make sure the initial value is 2.
34+
awaitcli.stepCommand('n');
35+
assert.ok(getLastLine(cli.output).includes('4 x = 3'));
36+
37+
awaitcli.command('setContextLineNumber(5)');
38+
awaitcli.stepCommand('n');
39+
assert.ok(getLastLine(cli.output).includes('8 x = 7'));
40+
41+
awaitcli.command('setContextLineNumber(3)');
42+
awaitcli.stepCommand('n');
43+
assert.ok(getLastLine(cli.output).includes('7 x = 6'));
44+
awaitcli.command('list(3)');
45+
assert.ok(getLastLine(cli.output).includes('7 x = 6'));
46+
47+
awaitcli.quit();
48+
}catch(error){
49+
onFatal(error);
50+
}

0 commit comments

Comments
(0)