Skip to content

Commit bce37c6

Browse files
Dailyscatdanielleadams
authored andcommitted
debugger: improve validations and documents for watch and unwatch
- debugger: add string validation for watch(expr) - debugger: add help document for watch(index) - test: add test for watch(index) command - doc: add information on unwatch(index) option PR-URL: #46947 Reviewed-By: Kohei Ueno <[email protected]>
1 parent 0e3077d commit bce37c6

File tree

4 files changed

+34
-4
lines changed

4 files changed

+34
-4
lines changed

doc/api/debugger.md

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ debug>
197197
after)
198198
* `watch(expr)`: Add expression to watch list
199199
* `unwatch(expr)`: Remove expression from watch list
200+
* `unwatch(index)`: Remove expression at specific index from watch list
200201
* `watchers`: List all watchers and their values (automatically listed on each
201202
breakpoint)
202203
* `repl`: Open debugger's repl for evaluation in debugging script's context

lib/internal/debugger/inspect_repl.js

+2
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,7 @@ breakOnNone Don't pause on exceptions (this is the default)
9191
9292
watch(expr) Start watching the given expression
9393
unwatch(expr) Stop watching an expression
94+
unwatch(index) Stop watching an expression at specific index from watch list
9495
watchers Print all watched expressions and their current values
9596
9697
exec(expr), p(expr), exec expr, p expr
@@ -1078,6 +1079,7 @@ function createRepl(inspector) {
10781079
},
10791080

10801081
watch(expr) {
1082+
validateString(expr, 'expression');
10811083
ArrayPrototypePush(watchedExpressions, expr);
10821084
},
10831085

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
'use strict';
2+
const common = require('../common');
3+
4+
common.skipIfInspectorDisabled();
5+
6+
const fixtures = require('../common/fixtures');
7+
const startCLI = require('../common/debugger');
8+
9+
const assert = require('assert');
10+
11+
const cli = startCLI([fixtures.path('debugger/break.js')]);
12+
13+
(async () => {
14+
await cli.waitForInitialBreak();
15+
await cli.command('watch()');
16+
await cli.waitFor(/ERR_INVALID_ARG_TYPE/);
17+
assert.match(cli.output, /TypeError \[ERR_INVALID_ARG_TYPE\]: The "expression" argument must be of type string\. Received undefined/);
18+
})()
19+
.finally(() => cli.quit())
20+
.then(common.mustCall());

test/sequential/test-debugger-watchers.mjs

+11-4
Original file line numberDiff line numberDiff line change
@@ -28,18 +28,25 @@ try {
2828
await cli.command('watchers');
2929

3030
assert.match(cli.output, /x is not defined/);
31-
31+
assert.match(cli.output, /1: "Hello" = 'Hello'/);
32+
assert.match(cli.output, /2: 42 = 42/);
33+
assert.match(cli.output, /3: NaN = NaN/);
34+
assert.match(cli.output, /4: true = true/);
35+
assert.match(cli.output, /5: \[1, 2\] = \[ 1, 2 \]/);
36+
assert.match(cli.output, /6: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
37+
'shows "..." for process.env');
38+
39+
await cli.command('unwatch(4)');
3240
await cli.command('unwatch("42")');
3341
await cli.stepCommand('n');
3442

3543
assert.match(cli.output, /0: x = 10/);
3644
assert.match(cli.output, /1: "Hello" = 'Hello'/);
3745
assert.match(cli.output, /2: NaN = NaN/);
38-
assert.match(cli.output, /3: true = true/);
39-
assert.match(cli.output, /4: \[1, 2\] = \[ 1, 2 \]/);
46+
assert.match(cli.output, /3: \[1, 2\] = \[ 1, 2 \]/);
4047
assert.match(
4148
cli.output,
42-
/5: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
49+
/4: process\.env =\n\s+\{[\s\S]+,\n\s+\.\.\. \}/,
4350
'shows "..." for process.env'
4451
);
4552

0 commit comments

Comments
 (0)