Skip to content

Commit 310d4ba

Browse files
committed
fix: prevent null access on tracing_expressions for nested tracing
1 parent cfd10c2 commit 310d4ba

File tree

3 files changed

+70
-1
lines changed

3 files changed

+70
-1
lines changed

packages/svelte/src/internal/client/dev/tracing.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@ export function trace(label, fn) {
119119
console.groupEnd();
120120
}
121121

122-
if (previously_tracing_expressions !== null) {
122+
if (previously_tracing_expressions !== null && tracing_expressions !== null) {
123123
for (const [signal, entry] of tracing_expressions.entries) {
124124
var prev_entry = previously_tracing_expressions.get(signal);
125125

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
import { flushSync } from 'svelte';
2+
import { test } from '../../test';
3+
4+
/**
5+
* @param {any[]} logs
6+
*/
7+
function normalise_trace_logs(logs) {
8+
let normalised = [];
9+
for (let i = 0; i < logs.length; i++) {
10+
const log = logs[i];
11+
12+
if (typeof log === 'string' && log.includes('%c')) {
13+
const split = log.split('%c');
14+
normalised.push({
15+
log: (split[0].length !== 0 ? split[0] : split[1]).trim(),
16+
highlighted: logs[i + 1] === 'color: CornflowerBlue; font-weight: bold'
17+
});
18+
i++;
19+
} else if (log instanceof Error) {
20+
continue;
21+
} else {
22+
normalised.push({ log });
23+
}
24+
}
25+
return normalised;
26+
}
27+
28+
export default test({
29+
compileOptions: {
30+
dev: true
31+
},
32+
33+
test({ assert, target, logs }) {
34+
// initial log, everything is highlighted
35+
36+
assert.deepEqual(normalise_trace_logs(logs), [
37+
{ log: 'iife', highlighted: false },
38+
{ log: '$state', highlighted: true },
39+
{ log: 0 },
40+
{ log: 'effect', highlighted: false }
41+
]);
42+
43+
logs.length = 0;
44+
45+
const button = target.querySelector('button');
46+
button?.click();
47+
flushSync();
48+
49+
assert.deepEqual(normalise_trace_logs(logs), [
50+
{ log: 'iife', highlighted: false },
51+
{ log: '$state', highlighted: true },
52+
{ log: 1 },
53+
{ log: 'effect', highlighted: false }
54+
]);
55+
}
56+
});
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<script>
2+
let count = $state(0);
3+
4+
$effect(() => {
5+
$inspect.trace('effect');
6+
(()=>{
7+
$inspect.trace("iife");
8+
count;
9+
})();
10+
});
11+
</script>
12+
13+
<button onclick={() => count++}>{count}</button>

0 commit comments

Comments
 (0)