Skip to content

Commit cb0bf61

Browse files
committed
use flags for prop_source, this will be useful later
1 parent 25abca7 commit cb0bf61

File tree

4 files changed

+41
-20
lines changed

4 files changed

+41
-20
lines changed

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

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import * as b from '../../../utils/builders.js';
22
import { extract_paths, is_simple_expression } from '../../../utils/ast.js';
33
import { error } from '../../../errors.js';
4+
import { PROPS_CALL_DEFAULT_VALUE, PROPS_IS_IMMUTABLE } from '../../../../constants.js';
45

56
/**
67
* @template {import('./types').ClientTransformState} State
@@ -359,29 +360,43 @@ export function get_props_method(binding, state, name, default_value) {
359360
(state.analysis.immutable ? binding.reassigned : binding.mutated);
360361

361362
if (needs_source) {
362-
args.push(b.literal(state.analysis.immutable));
363-
}
363+
let flags = 0;
364364

365-
if (default_value) {
366-
// To avoid eagerly evaluating the right-hand-side, we wrap it in a thunk if necessary
367-
if (is_simple_expression(default_value)) {
368-
args.push(default_value);
369-
} else {
370-
if (
371-
default_value.type === 'CallExpression' &&
372-
default_value.callee.type === 'Identifier' &&
373-
default_value.arguments.length === 0
374-
) {
375-
args.push(default_value.callee);
365+
/** @type {import('estree').Expression | undefined} */
366+
let arg;
367+
368+
if (state.analysis.immutable) {
369+
flags |= PROPS_IS_IMMUTABLE;
370+
}
371+
372+
if (default_value) {
373+
// To avoid eagerly evaluating the right-hand-side, we wrap it in a thunk if necessary
374+
if (is_simple_expression(default_value)) {
375+
arg = default_value;
376376
} else {
377-
args.push(b.thunk(default_value));
377+
if (
378+
default_value.type === 'CallExpression' &&
379+
default_value.callee.type === 'Identifier' &&
380+
default_value.arguments.length === 0
381+
) {
382+
arg = default_value.callee;
383+
} else {
384+
arg = b.thunk(default_value);
385+
}
386+
387+
flags |= PROPS_CALL_DEFAULT_VALUE;
378388
}
389+
}
379390

380-
args.push(b.true);
391+
if (flags || arg) {
392+
args.push(b.literal(flags));
393+
if (arg) args.push(arg);
381394
}
395+
396+
return b.call('$.prop_source', ...args);
382397
}
383398

384-
return b.call(needs_source ? '$.prop_source' : '$.prop', ...args);
399+
return b.call('$.prop', ...args);
385400
}
386401

387402
/**

packages/svelte/src/constants.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ export const EACH_IS_CONTROLLED = 1 << 3;
55
export const EACH_IS_ANIMATED = 1 << 4;
66
export const EACH_IS_IMMUTABLE = 1 << 6;
77

8+
export const PROPS_IS_IMMUTABLE = 1;
9+
export const PROPS_CALL_DEFAULT_VALUE = 1 << 1;
10+
811
/** List of Element events that will be delegated */
912
export const DelegatedEvents = [
1013
'beforeinput',

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { DEV } from 'esm-env';
22
import { subscribe_to_store } from '../../store/utils.js';
33
import { EMPTY_FUNC, run_all } from '../common.js';
44
import { get_descriptors, is_array } from './utils.js';
5+
import { PROPS_CALL_DEFAULT_VALUE, PROPS_IS_IMMUTABLE } from '../../constants.js';
56

67
export const SOURCE = 1;
78
export const DERIVED = 1 << 1;
@@ -1463,12 +1464,14 @@ export function is_store(val) {
14631464
* @template V
14641465
* @param {import('./types.js').MaybeSignal<Record<string, unknown>>} props_obj
14651466
* @param {string} key
1466-
* @param {boolean} immutable
1467+
* @param {number} flags
14671468
* @param {V | (() => V)} [default_value]
1468-
* @param {boolean} [call_default_value]
14691469
* @returns {import('./types.js').Signal<V> | (() => V)}
14701470
*/
1471-
export function prop_source(props_obj, key, immutable, default_value, call_default_value) {
1471+
export function prop_source(props_obj, key, flags, default_value) {
1472+
const call_default_value = (flags & PROPS_CALL_DEFAULT_VALUE) !== 0;
1473+
const immutable = (flags & PROPS_IS_IMMUTABLE) !== 0;
1474+
14721475
const props = is_signal(props_obj) ? get(props_obj) : props_obj;
14731476
const possible_signal = /** @type {import('./types.js').MaybeSignal<V>} */ (
14741477
expose(() => props[key])

packages/svelte/tests/snapshot/samples/svelte-element/_expected/client/index.svelte.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import * as $ from "svelte/internal";
66
export default function Svelte_element($$anchor, $$props) {
77
$.push($$props, true);
88

9-
let tag = $.prop_source($$props, "tag", true, 'hr');
9+
let tag = $.prop_source($$props, "tag", 1, 'hr');
1010
/* Init */
1111
var fragment = $.comment($$anchor);
1212
var node = $.child_frag(fragment);

0 commit comments

Comments
 (0)