Skip to content

Commit 743465e

Browse files
committed
make inspect take a single argument
1 parent 2fb380f commit 743465e

File tree

6 files changed

+29
-28
lines changed

6 files changed

+29
-28
lines changed

packages/svelte/src/compiler/phases/2-analyze/validation.js

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,13 +521,19 @@ function validate_call_expression(node, scope, path) {
521521

522522
if (rune === '$effect.active') {
523523
if (node.arguments.length !== 0) {
524-
error(node, 'invalid-rune-args-length', '$effect.active', [0]);
524+
error(node, 'invalid-rune-args-length', rune, [0]);
525525
}
526526
}
527527

528528
if (rune === '$effect.root') {
529529
if (node.arguments.length !== 1) {
530-
error(node, 'invalid-rune-args-length', '$effect.root', [1]);
530+
error(node, 'invalid-rune-args-length', rune, [1]);
531+
}
532+
}
533+
534+
if (rune === '$inspect') {
535+
if (node.arguments.length < 1 || node.arguments.length > 2) {
536+
error(node, 'invalid-rune-args-length', rune, [1, 2]);
531537
}
532538
}
533539
}

packages/svelte/src/compiler/phases/3-transform/client/visitors/javascript-runes.js

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -309,11 +309,12 @@ export const javascript_visitors_runes = {
309309

310310
if (rune === '$inspect') {
311311
if (state.options.dev) {
312-
const args = /** @type {import('estree').Expression[]} */ (
313-
node.arguments.map((arg) => visit(arg))
314-
);
312+
const arg = /** @type {import('estree').Expression} */ (visit(node.arguments[0]));
313+
const fn =
314+
node.arguments[1] &&
315+
/** @type {import('estree').Expression} */ (visit(node.arguments[1]));
315316

316-
return b.call('$.inspect', b.thunk(b.array(args)));
317+
return b.call('$.inspect', b.thunk(arg), fn);
317318
}
318319

319320
return b.unary('void', b.literal(0));

packages/svelte/src/internal/client/runtime.js

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1794,28 +1794,22 @@ function deep_read(value, visited = new Set()) {
17941794
}
17951795

17961796
/**
1797-
* @param {() => import('./types.js').MaybeSignal<>[]} get_values
1797+
* @param {() => import('./types.js').MaybeSignal<>} get_value
1798+
* @param {Function} inspect
17981799
* @returns {void}
17991800
*/
1800-
export function inspect(get_values) {
1801+
export function inspect(get_value, inspect = console.log) {
18011802
let initial = true;
18021803

18031804
pre_effect(() => {
18041805
const fn = () => {
1805-
const values = get_values();
1806-
1807-
if (typeof values.at(-1) === 'function') {
1808-
const inspect = /** @type {Function} */ (values[values.length - 1]);
1809-
inspect(!initial, ...values.slice(0, -1));
1810-
} else {
1811-
// eslint-disable-next-line no-console
1812-
console.log(...values);
1813-
}
1806+
const value = get_value();
1807+
inspect(value, initial ? 'init' : 'update');
18141808
};
18151809

18161810
inspect_fn = fn;
1817-
const values = get_values();
1818-
deep_read(values);
1811+
const value = get_value();
1812+
deep_read(value);
18191813
inspect_fn = null;
18201814

18211815
const signals = inspect_captured_signals.slice();

packages/svelte/tests/runtime-runes/samples/inspect-trace/main.svelte

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
let x = $state(0);
33
let y = $state(0);
44
5-
$inspect(x, (changed, x) => {
6-
if (changed) console.log(new Error(), x);
5+
$inspect(x, (x, type) => {
6+
if (type === 'update') console.log(new Error(), x);
77
});
88
</script>
99

packages/svelte/tests/runtime-runes/samples/inspect/_config.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,6 @@ export default test({
2929
b2.click();
3030
await Promise.resolve();
3131

32-
assert.deepEqual(log, [0, 1]);
32+
assert.deepEqual(log, [0, 'init', 1, 'update']);
3333
}
3434
});

sites/svelte-5-preview/src/routes/docs/content/01-api/02-runes.md

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -243,8 +243,8 @@ Note that you can still use `export const` and `export function` to expose thing
243243

244244
## `$inspect`
245245

246-
The `$inspect` rune is roughly equivalent to `console.log`, with the exception that it will re-run whenever the
247-
arguments change. `$inspect` tracks reactive state deeply, meaning that updating something inside an object
246+
The `$inspect` rune is roughly equivalent to `console.log`, with the exception that it will re-run whenever its
247+
argument changes. `$inspect` tracks reactive state deeply, meaning that updating something inside an object
248248
or array using [fine-grained reactivity](/docs/fine-grained-reactivity) will cause it to re-fire.
249249

250250
```svelte
@@ -259,15 +259,15 @@ or array using [fine-grained reactivity](/docs/fine-grained-reactivity) will cau
259259
<input bind:value={message} />
260260
```
261261

262-
If the last argument is a function, it will be invoked instead of `console.log`. The first argument is `changed`
263-
`false` when it initially runs, `true` thereafter — followed by the current values:
262+
If a callback is also provided, it will be invoked instead of `console.log`. The first argument to the callback
263+
is the current value. The second is either `"init"` or `"update"`.
264264

265265
```svelte
266266
<script>
267267
let count = $state(0);
268268
269-
$inspect(count, (changed, count) => {
270-
if (changed) {
269+
$inspect(count, (count, type) => {
270+
if (type === 'update') {
271271
// or `console.trace`, or whatever you want. This makes it
272272
// easy to find what caused the state to update
273273
debugger;

0 commit comments

Comments
 (0)