Skip to content

Commit 9307ad5

Browse files
committed
tweak implementation
1 parent 35cbd04 commit 9307ad5

File tree

2 files changed

+33
-34
lines changed

2 files changed

+33
-34
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 '../phases/patterns.js';
21+
import { regex_is_valid_identifier } from '../../regexes.js';
2222

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

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

Lines changed: 32 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,9 @@ import * as e from './errors.js';
1515
import { get_stack, tag } from './dev/tracing.js';
1616
import { tracing_mode_flag } from '../flags/index.js';
1717

18+
// TODO move all regexes into shared module?
19+
const regex_is_valid_identifier = /^[a-zA-Z_$][a-zA-Z_$0-9]*$/;
20+
1821
/**
1922
* @template T
2023
* @param {T} value
@@ -23,17 +26,11 @@ import { tracing_mode_flag } from '../flags/index.js';
2326
* @returns {T}
2427
*/
2528
export function proxy(value, path, change_path = false) {
26-
// if `DEV`, change the proxy `path` since we don't know if its still "owned" by its original source
27-
if (
28-
DEV &&
29-
change_path &&
30-
typeof value === 'object' &&
31-
value !== null &&
32-
STATE_SYMBOL in value &&
33-
PROXY_PATH_SYMBOL in value
34-
) {
35-
value[PROXY_PATH_SYMBOL] = path;
29+
if (DEV && change_path) {
30+
// @ts-expect-error
31+
value?.[PROXY_PATH_SYMBOL]?.(path);
3632
}
33+
3734
// if non-proxyable, or is already a proxy, return `value`
3835
if (typeof value !== 'object' || value === null || STATE_SYMBOL in value) {
3936
return value;
@@ -52,16 +49,6 @@ export function proxy(value, path, change_path = false) {
5249

5350
var stack = DEV && tracing_mode_flag ? get_stack('CreatedAt') : null;
5451
var reaction = active_reaction;
55-
/** @type {(prop: any) => any} */
56-
var to_trace_name = DEV
57-
? (prop) => {
58-
return typeof prop === 'symbol'
59-
? `${path}[Symbol(${prop.description ?? ''})]`
60-
: typeof prop === 'number' || Number(prop) === Number(prop)
61-
? `${path}[${prop}]`
62-
: `${path}.${prop}`;
63-
}
64-
: (prop) => undefined;
6552

6653
/**
6754
* @template T
@@ -85,6 +72,28 @@ export function proxy(value, path, change_path = false) {
8572
sources.set('length', DEV ? tag(length_source, to_trace_name('length')) : length_source);
8673
}
8774

75+
/** @param {string | symbol} prop */
76+
function to_trace_name(prop) {
77+
if (typeof prop === 'symbol') return `${path}[Symbol(${prop.description ?? ''})]`;
78+
if (regex_is_valid_identifier.test(prop)) return `${path}.${prop}`;
79+
return /^\d+$/.test(prop) ? `${path}[${prop}]` : `${path}['${prop}']`;
80+
}
81+
82+
/** @param {string} new_path */
83+
function update_path(new_path) {
84+
path = new_path;
85+
86+
tag(version, `${path} version`);
87+
88+
// rename all child sources and child proxies
89+
for (const [prop, source] of sources) {
90+
var label = to_trace_name(prop);
91+
92+
tag(source, label);
93+
source.v?.[PROXY_PATH_SYMBOL]?.(label);
94+
}
95+
}
96+
8897
return new Proxy(/** @type {any} */ (value), {
8998
defineProperty(_, prop, descriptor) {
9099
if (
@@ -147,8 +156,9 @@ export function proxy(value, path, change_path = false) {
147156
if (prop === STATE_SYMBOL) {
148157
return value;
149158
}
159+
150160
if (DEV && prop === PROXY_PATH_SYMBOL) {
151-
return path;
161+
return update_path;
152162
}
153163

154164
var s = sources.get(prop);
@@ -195,7 +205,7 @@ export function proxy(value, path, change_path = false) {
195205
},
196206

197207
has(target, prop) {
198-
if (prop === STATE_SYMBOL || (DEV && prop === PROXY_PATH_SYMBOL)) {
208+
if (prop === STATE_SYMBOL) {
199209
return true;
200210
}
201211

@@ -224,17 +234,6 @@ export function proxy(value, path, change_path = false) {
224234
},
225235

226236
set(target, prop, value, receiver) {
227-
if (DEV && prop === PROXY_PATH_SYMBOL) {
228-
path = value;
229-
tag(version, `${path} version`);
230-
// rename all child sources and child proxies
231-
for (const [prop, source] of sources) {
232-
tag(source, to_trace_name(prop));
233-
if (typeof source.v === 'object' && source.v !== null && PROXY_PATH_SYMBOL in source.v) {
234-
source.v[PROXY_PATH_SYMBOL] = to_trace_name(prop);
235-
}
236-
}
237-
}
238237
var s = sources.get(prop);
239238
var has = prop in target;
240239

0 commit comments

Comments
 (0)