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
20 changes: 17 additions & 3 deletions lib/internal/debugger/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ const {

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

const { validateString } = require('internal/validators');
const { validateString, validateNumber } = require('internal/validators');

const FS = require('fs');
const Path = require('path');
Expand Down 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,18 @@ 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');
}
if (delta === 0) {
return print('A value greater than 0 is required');
}
validateNumber(delta, 'delta', 1);
cola119 marked this conversation as resolved.
Show resolved Hide resolved
contextLineNumber = delta;
print(`The contextLine has been changed to ${delta}.`);
}

function handleBreakpointResolved({ breakpointId, location }) {
const script = knownScripts[location.scriptId];
const scriptUrl = script && script.url;
Expand Down Expand Up @@ -897,7 +910,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 +1172,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;
50 changes: 50 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,50 @@
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 splittedByLine = output.split(';');
return splittedByLine[splittedByLine.length - 2];
}

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

await cli.command('setContextLineNumber("1")');
assert.ok(cli.output.includes('argument must be of type number. Received type string'));

await cli.command('setContextLineNumber(0)');
assert.ok(cli.output.includes('A value greater than 0 is required'));

// 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);
}