Skip to content

Commit 5c44d72

Browse files
committed
tidy up
1 parent 8c934ff commit 5c44d72

File tree

5 files changed

+34
-61
lines changed

5 files changed

+34
-61
lines changed

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

Lines changed: 29 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -353,67 +353,53 @@ export function serialize_hoistable_params(node, context) {
353353
}
354354

355355
/**
356-
*
357-
* @param {import('#compiler').Binding} binding
358356
* @param {import('./types').ComponentClientTransformState} state
359357
* @param {string} name
360-
* @param {import('estree').Expression | null} [default_value]
358+
* @param {import('estree').Expression | null} [initial]
361359
* @returns
362360
*/
363-
export function get_props_method(binding, state, name, default_value) {
361+
export function get_prop_source(state, name, initial) {
364362
/** @type {import('estree').Expression[]} */
365363
const args = [b.id('$$props'), b.literal(name)];
366364

367-
// Use $.prop_source in the following cases:
368-
// - accessors/mutated: needs to be able to set the prop value from within
369-
// - default value: we set the fallback value only initially, and it's not possible to know this timing in $.prop
370-
const needs_source =
371-
default_value ||
372-
state.analysis.accessors ||
373-
(state.analysis.immutable ? binding.reassigned : binding.mutated);
365+
let flags = 0;
374366

375-
if (needs_source) {
376-
let flags = 0;
377-
378-
/** @type {import('estree').Expression | undefined} */
379-
let arg;
367+
if (state.analysis.immutable) {
368+
flags |= PROPS_IS_IMMUTABLE;
369+
}
380370

381-
if (state.analysis.immutable) {
382-
flags |= PROPS_IS_IMMUTABLE;
383-
}
371+
if (state.analysis.runes) {
372+
flags |= PROPS_IS_RUNES;
373+
}
384374

385-
if (state.analysis.runes) {
386-
flags |= PROPS_IS_RUNES;
387-
}
375+
/** @type {import('estree').Expression | undefined} */
376+
let arg;
388377

389-
if (default_value) {
390-
// To avoid eagerly evaluating the right-hand-side, we wrap it in a thunk if necessary
391-
if (is_simple_expression(default_value)) {
392-
arg = default_value;
378+
if (initial) {
379+
// To avoid eagerly evaluating the right-hand-side, we wrap it in a thunk if necessary
380+
if (is_simple_expression(initial)) {
381+
arg = initial;
382+
} else {
383+
if (
384+
initial.type === 'CallExpression' &&
385+
initial.callee.type === 'Identifier' &&
386+
initial.arguments.length === 0
387+
) {
388+
arg = initial.callee;
393389
} else {
394-
if (
395-
default_value.type === 'CallExpression' &&
396-
default_value.callee.type === 'Identifier' &&
397-
default_value.arguments.length === 0
398-
) {
399-
arg = default_value.callee;
400-
} else {
401-
arg = b.thunk(default_value);
402-
}
403-
404-
flags |= PROPS_CALL_DEFAULT_VALUE;
390+
arg = b.thunk(initial);
405391
}
406-
}
407392

408-
if (flags || arg) {
409-
args.push(b.literal(flags));
410-
if (arg) args.push(arg);
393+
flags |= PROPS_CALL_DEFAULT_VALUE;
411394
}
395+
}
412396

413-
return b.call('$.prop_source', ...args);
397+
if (flags || arg) {
398+
args.push(b.literal(flags));
399+
if (arg) args.push(arg);
414400
}
415401

416-
return b.call('$.prop', ...args);
402+
return b.call('$.prop_source', ...args);
417403
}
418404

419405
/**

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

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import { is_hoistable_function } from '../../utils.js';
22
import * as b from '../../../../utils/builders.js';
33
import { extract_paths } from '../../../../utils/ast.js';
4-
import { create_state_declarators, get_props_method, serialize_get_binding } from '../utils.js';
4+
import { create_state_declarators, get_prop_source, serialize_get_binding } from '../utils.js';
55

66
/** @type {import('../types.js').ComponentVisitors} */
77
export const javascript_visitors_legacy = {
@@ -55,7 +55,7 @@ export const javascript_visitors_legacy = {
5555
b.declarator(
5656
path.node,
5757
binding.kind === 'prop'
58-
? get_props_method(binding, state, binding.prop_alias ?? name, value)
58+
? get_prop_source(state, binding.prop_alias ?? name, value)
5959
: value
6060
)
6161
);
@@ -75,8 +75,7 @@ export const javascript_visitors_legacy = {
7575
declarations.push(
7676
b.declarator(
7777
declarator.id,
78-
get_props_method(
79-
binding,
78+
get_prop_source(
8079
state,
8180
binding.prop_alias ?? declarator.id.name,
8281
declarator.init &&

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { get_rune } from '../../../scope.js';
22
import { is_hoistable_function } from '../../utils.js';
33
import * as b from '../../../../utils/builders.js';
44
import * as assert from '../../../../utils/assert.js';
5-
import { create_state_declarators, get_props_method, should_proxy } from '../utils.js';
5+
import { create_state_declarators, get_prop_source, should_proxy } from '../utils.js';
66
import { unwrap_ts_expression } from '../../../../utils/ast.js';
77

88
/** @type {import('../types.js').ComponentVisitors} */
@@ -185,7 +185,7 @@ export const javascript_visitors_runes = {
185185
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
186186

187187
if (binding.reassigned || state.analysis.accessors || initial) {
188-
declarations.push(b.declarator(id, get_props_method(binding, state, name, initial)));
188+
declarations.push(b.declarator(id, get_prop_source(state, name, initial)));
189189
}
190190
} else {
191191
// RestElement

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

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1479,17 +1479,6 @@ export function prop_source(props, key, flags, default_value) {
14791479
return /** @type {import('./types.js').Signal<V>} */ (source_signal);
14801480
}
14811481

1482-
/**
1483-
* If the prop is readonly and has no fallback value, we can use this function, else we need to use `prop_source`.
1484-
* @param {Record<string, unknown>} props
1485-
* @param {string} key
1486-
* @returns {any}
1487-
*/
1488-
export function prop(props, key) {
1489-
// TODO skip this, and rewrite as `$$props.foo`
1490-
return () => props[key];
1491-
}
1492-
14931482
/**
14941483
* @param {boolean} immutable
14951484
* @param {unknown} a

packages/svelte/src/internal/index.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@ export {
77
source,
88
mutable_source,
99
derived,
10-
prop,
1110
prop_source,
1211
user_effect,
1312
render_effect,

0 commit comments

Comments
 (0)