Skip to content

Commit 7f4debe

Browse files
committed
tweak implementation
1 parent 016f02b commit 7f4debe

File tree

5 files changed

+76
-31
lines changed

5 files changed

+76
-31
lines changed

packages/svelte/src/compiler/migrate/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ import {
1818
import { migrate_svelte_ignore } from '../utils/extract_svelte_ignore.js';
1919
import { validate_component_options } from '../validate-options.js';
2020
import { is_reserved, is_svg, is_void } from '../../utils.js';
21-
import { regex_is_valid_identifier } from '../../regexes.js';
21+
import { regex_is_valid_identifier } from '../phases/patterns.js';
2222

2323
const regex_style_tags = /(<style[^>]+>)([\S\s]*?)(<\/style>)/g;
2424
const style_placeholder = '/*$$__STYLE_CONTENT__$$*/';

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

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,11 @@ export function VariableDeclaration(node, context) {
8989
binding.kind === 'bindable_prop' &&
9090
should_proxy(initial, context.state.scope)
9191
) {
92-
initial = b.call('$.proxy', initial, dev && b.literal(id.name));
92+
initial = b.call('$.proxy', initial);
93+
94+
if (dev) {
95+
initial = b.call('$.tag_proxy', initial, b.literal(id.name));
96+
}
9397
}
9498

9599
if (is_prop_source(binding, context.state)) {
@@ -132,7 +136,11 @@ export function VariableDeclaration(node, context) {
132136
const is_proxy = should_proxy(value, context.state.scope);
133137

134138
if (rune === '$state' && is_proxy) {
135-
value = b.call('$.proxy', value, dev && b.literal(id.name));
139+
value = b.call('$.proxy', value);
140+
141+
if (dev) {
142+
value = b.call('$.tag_proxy', value, b.literal(id.name));
143+
}
136144
}
137145

138146
if (is_state) {

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

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
import { UNINITIALIZED } from '../../../constants.js';
33
import { snapshot } from '../../shared/clone.js';
44
import { define_property } from '../../shared/utils.js';
5-
import { DERIVED, STATE_SYMBOL } from '#client/constants';
5+
import { DERIVED, PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
66
import { effect_tracking } from '../reactivity/effects.js';
77
import { active_reaction, captured_signals, set_captured_signals, untrack } from '../runtime.js';
88

@@ -190,6 +190,16 @@ export function tag(source, name) {
190190
return source;
191191
}
192192

193+
/**
194+
* @param {unknown} value
195+
* @param {string} label
196+
*/
197+
export function tag_proxy(value, label) {
198+
// @ts-expect-error
199+
value?.[PROXY_PATH_SYMBOL]?.(label);
200+
return value;
201+
}
202+
193203
/**
194204
* @param {unknown} value
195205
*/

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export { add_locations } from './dev/elements.js';
77
export { hmr } from './dev/hmr.js';
88
export { create_ownership_validator } from './dev/ownership.js';
99
export { check_target, legacy_api } from './dev/legacy.js';
10-
export { trace, tag } from './dev/tracing.js';
10+
export { trace, tag, tag_proxy } from './dev/tracing.js';
1111
export { inspect } from './dev/inspect.js';
1212
export { validate_snippet_args } from './dev/validation.js';
1313
export { await_block as await } from './dom/blocks/await.js';

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

Lines changed: 53 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import { state as source, set } from './reactivity/sources.js';
1212
import { PROXY_PATH_SYMBOL, STATE_SYMBOL } from '#client/constants';
1313
import { UNINITIALIZED } from '../../constants.js';
1414
import * as e from './errors.js';
15-
import { get_stack, tag } from './dev/tracing.js';
15+
import { get_stack, tag, tag_proxy } from './dev/tracing.js';
1616
import { tracing_mode_flag } from '../flags/index.js';
1717

1818
// TODO move all regexes into shared module?
@@ -21,10 +21,9 @@ const regex_is_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
2121
/**
2222
* @template T
2323
* @param {T} value
24-
* @param {string} [path]
2524
* @returns {T}
2625
*/
27-
export function proxy(value, path) {
26+
export function proxy(value) {
2827
// if non-proxyable, or is already a proxy, return `value`
2928
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
3029
return value;
@@ -39,7 +38,7 @@ export function proxy(value, path) {
3938
/** @type {Map<any, Source<any>>} */
4039
var sources = new Map();
4140
var is_proxied_array = is_array(value);
42-
var version = DEV ? tag(source(0), `${path} version`) : source(0);
41+
var version = source(0);
4342

4443
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
4544
var reaction = active_reaction;
@@ -62,10 +61,12 @@ export function proxy(value, path) {
6261
if (is_proxied_array) {
6362
// We need to create the length source eagerly to ensure that
6463
// mutations to the array are properly synced with our proxy
65-
const length_source = source(/** @type {any[]} */ (value).length, stack);
66-
sources.set('length', DEV ? tag(length_source, to_trace_name('length')) : length_source);
64+
sources.set('length', source(/** @type {any[]} */ (value).length, stack));
6765
}
6866

67+
/** Used in dev for $inspect.trace() */
68+
var path = '';
69+
6970
/** @param {string | symbol} prop */
7071
function to_trace_name(prop) {
7172
if (typeof prop === 'symbol') return `${path}[Symbol(${prop.description ?? ''})]`;
@@ -84,7 +85,7 @@ export function proxy(value, path) {
8485
var label = to_trace_name(prop);
8586

8687
tag(source, label);
87-
source.v?.[PROXY_PATH_SYMBOL]?.(label);
88+
tag_proxy(source.v, label);
8889
}
8990
}
9091

@@ -107,13 +108,18 @@ export function proxy(value, path) {
107108

108109
if (s === undefined) {
109110
s = with_parent(() => source(descriptor.value, stack));
110-
s = DEV && typeof prop === 'string' ? tag(s, to_trace_name(prop)) : s;
111111
sources.set(prop, s);
112+
113+
if (DEV && typeof prop === 'string') {
114+
tag(s, to_trace_name(prop));
115+
}
112116
} else {
113-
set(
114-
s,
115-
with_parent(() => proxy(descriptor.value, to_trace_name(prop)))
116-
);
117+
var p = with_parent(() => proxy(descriptor.value));
118+
set(s, p);
119+
120+
if (DEV) {
121+
tag_proxy(p, to_trace_name(prop));
122+
}
117123
}
118124

119125
return true;
@@ -125,8 +131,12 @@ export function proxy(value, path) {
125131
if (s === undefined) {
126132
if (prop in target) {
127133
const s = with_parent(() => source(UNINITIALIZED, stack));
128-
sources.set(prop, DEV ? tag(s, to_trace_name(prop)) : s);
134+
sources.set(prop, s);
129135
update_version(version);
136+
137+
if (DEV) {
138+
tag(s, to_trace_name(prop));
139+
}
130140
}
131141
} else {
132142
// When working with arrays, we need to also ensure we update the length when removing
@@ -160,10 +170,19 @@ export function proxy(value, path) {
160170

161171
// create a source, but only if it's an own property and not a prototype property
162172
if (s === undefined && (!exists || get_descriptor(target, prop)?.writable)) {
163-
s = with_parent(() =>
164-
source(proxy(exists ? target[prop] : UNINITIALIZED, to_trace_name(prop)), stack)
165-
);
166-
s = DEV ? tag(s, to_trace_name(prop)) : s;
173+
s = with_parent(() => {
174+
var p = proxy(exists ? target[prop] : UNINITIALIZED);
175+
var s = source(p, stack);
176+
177+
if (DEV) {
178+
var label = to_trace_name(prop);
179+
tag(s, label);
180+
tag_proxy(p, label);
181+
}
182+
183+
return s;
184+
});
185+
167186
sources.set(prop, s);
168187
}
169188

@@ -211,10 +230,19 @@ export function proxy(value, path) {
211230
(active_effect !== null && (!has || get_descriptor(target, prop)?.writable))
212231
) {
213232
if (s === undefined) {
214-
s = with_parent(() =>
215-
source(has ? proxy(target[prop], to_trace_name(prop)) : UNINITIALIZED, stack)
216-
);
217-
s = DEV ? tag(s, to_trace_name(prop)) : s;
233+
s = with_parent(() => {
234+
var p = has ? proxy(target[prop]) : UNINITIALIZED;
235+
var s = source(p, stack);
236+
237+
if (DEV) {
238+
var label = to_trace_name(prop);
239+
tag(s, label);
240+
tag_proxy(p, label);
241+
}
242+
243+
return s;
244+
});
245+
218246
sources.set(prop, s);
219247
}
220248

@@ -255,12 +283,12 @@ export function proxy(value, path) {
255283
if (s === undefined) {
256284
if (!has || get_descriptor(target, prop)?.writable) {
257285
s = with_parent(() => source(undefined, stack));
258-
var p = with_parent(() => proxy(value, to_trace_name(prop)));
286+
var p = with_parent(() => proxy(value));
259287

260288
if (DEV) {
261289
var label = to_trace_name(prop);
262290
tag(s, label);
263-
p?.[PROXY_PATH_SYMBOL]?.(label);
291+
tag_proxy(p, label);
264292
}
265293

266294
set(s, p);
@@ -269,11 +297,10 @@ export function proxy(value, path) {
269297
} else {
270298
has = s.v !== UNINITIALIZED;
271299

272-
p = with_parent(() => proxy(value, to_trace_name(prop)));
300+
p = with_parent(() => proxy(value));
273301

274302
if (DEV) {
275-
label = to_trace_name(prop);
276-
p?.[PROXY_PATH_SYMBOL]?.(label);
303+
tag_proxy(p, to_trace_name(prop));
277304
}
278305

279306
set(s, p);

0 commit comments

Comments
 (0)