Skip to content
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

debugger: Add set context line number command #46812

Merged
Merged
19 changes: 17 additions & 2 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ out, o Step out, leaving the current function
backtrace, bt Print the current backtrace
list Print the source around the current line where execution
is currently paused

setContextLineNumber Set which lines to check for context
setBreakpoint, sb Set a breakpoint
clearBreakpoint, cb Clear a breakpoint
breakpoints List all known breakpoints
Expand Down Expand Up @@ -381,6 +381,7 @@ function createRepl(inspector) {
let currentBacktrace;
let selectedFrame;
let exitDebugRepl;
let contextLineNumber = 2;

function resetOnStart() {
knownScripts = {};
Expand Down Expand Up @@ -685,6 +686,19 @@ function createRepl(inspector) {
});
}

function setContextLineNumber(delta = 2) {
if (!selectedFrame) {
cola119 marked this conversation as resolved.
Show resolved Hide resolved
throw new ERR_DEBUGGER_ERROR('Requires execution to be paused');
}
try {
contextLineNumber = delta;
print(`The contextLine has been changed to ${delta}.`);
} catch (error) {
print("You can't setContextLineNumber source code right now");
throw error;
}
cola119 marked this conversation as resolved.
Show resolved Hide resolved
}

function handleBreakpointResolved({ breakpointId, location }) {
const script = knownScripts[location.scriptId];
const scriptUrl = script && script.url;
Expand Down Expand Up @@ -897,7 +911,7 @@ function createRepl(inspector) {

inspector.suspendReplWhile(() =>
PromisePrototypeThen(
SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(2)]),
SafePromiseAllReturnArrayLike([formatWatchers(true), selectedFrame.list(contextLineNumber)]),
({ 0: watcherList, 1: context }) => {
const breakContext = watcherList ?
`${watcherList}\n${inspect(context)}` :
Expand Down Expand Up @@ -1159,6 +1173,7 @@ function createRepl(inspector) {
},

list,
setContextLineNumber,
});
aliasProperties(context, SHORTCUTS);
}
Expand Down
20 changes: 20 additions & 0 deletions test/fixtures/debugger/twenty-lines.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
let x = 0;
x = 1;
x = 2;
x = 3;
x = 4;
x = 5;
x = 6;
x = 7;
x = 8;
x = 9;
x = 10;
x = 11;
x = 12;
x = 13;
x = 14;
x = 15;
x = 16;
x = 17;
x = 18;
module.exports = x;
cola119 marked this conversation as resolved.
Show resolved Hide resolved
44 changes: 44 additions & 0 deletions test/sequential/test-debugger-set-context-line-number.mjs
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { skipIfInspectorDisabled } from '../common/index.mjs';
skipIfInspectorDisabled();

import { path } from '../common/fixtures.mjs';
import startCLI from '../common/debugger.js';

import assert from 'assert';

const script = path('debugger', 'twenty-lines.js');
const cli = startCLI([script]);

function onFatal(error) {
cli.quit();
throw error;
}

function getLastLine(output) {
const splitedByLine = output.split(';');
return splitedByLine[splitedByLine.length - 2];
cola119 marked this conversation as resolved.
Show resolved Hide resolved
}

// Stepping through breakpoints.
try {
await cli.waitForInitialBreak();
await cli.waitForPrompt();

// Make sure the initial value is 2.
await cli.stepCommand('n');
assert.ok(getLastLine(cli.output).includes('4 x = 3'));

await cli.command('setContextLineNumber(5)');
await cli.stepCommand('n');
assert.ok(getLastLine(cli.output).includes('8 x = 7'));

await cli.command('setContextLineNumber(3)');
await cli.stepCommand('n');
assert.ok(getLastLine(cli.output).includes('7 x = 6'));
await cli.command('list(3)');
assert.ok(getLastLine(cli.output).includes('7 x = 6'));

await cli.quit();
} catch (error) {
onFatal(error);
}