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

+14-3
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
const FS = require('fs');
5252
const Path = 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
let currentBacktrace;
382382
let selectedFrame;
383383
let exitDebugRepl;
384+
let contextLineNumber = 2;
384385

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

689+
function setContextLineNumber(delta = 2) {
690+
if (!selectedFrame) {
691+
throw new ERR_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
function handleBreakpointResolved({ breakpointId, location }) {
689699
const script = knownScripts[location.scriptId];
690700
const scriptUrl = 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
const breakContext = 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
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
let x = 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;
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+
import startCLI from '../common/debugger.js';
6+
7+
import assert from 'assert';
8+
9+
const script = path('debugger', 'twenty-lines.js');
10+
const cli = startCLI([script]);
11+
12+
function onFatal(error) {
13+
cli.quit();
14+
throw error;
15+
}
16+
17+
function getLastLine(output) {
18+
const splittedByLine = output.split(';');
19+
return splittedByLine[splittedByLine.length - 2];
20+
}
21+
22+
// Stepping through breakpoints.
23+
try {
24+
await cli.waitForInitialBreak();
25+
await cli.waitForPrompt();
26+
27+
await cli.command('setContextLineNumber("1")');
28+
assert.ok(cli.output.includes('argument must be of type number. Received type string'));
29+
30+
await cli.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+
await cli.stepCommand('n');
35+
assert.ok(getLastLine(cli.output).includes('4 x = 3'));
36+
37+
await cli.command('setContextLineNumber(5)');
38+
await cli.stepCommand('n');
39+
assert.ok(getLastLine(cli.output).includes('8 x = 7'));
40+
41+
await cli.command('setContextLineNumber(3)');
42+
await cli.stepCommand('n');
43+
assert.ok(getLastLine(cli.output).includes('7 x = 6'));
44+
await cli.command('list(3)');
45+
assert.ok(getLastLine(cli.output).includes('7 x = 6'));
46+
47+
await cli.quit();
48+
} catch (error) {
49+
onFatal(error);
50+
}

0 commit comments

Comments
 (0)