Skip to content

breaking: add $props.bindable() #10804

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 20 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 15 additions & 9 deletions packages/svelte/src/compiler/phases/2-analyze/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -422,7 +422,7 @@ export function analyze_component(root, options) {
options,
ast_type: ast === instance.ast ? 'instance' : ast === template.ast ? 'template' : 'module',
parent_element: null,
has_props_rune: false,
has_props_rune: [false, false],
component_slots: new Set(),
expression: null,
private_derived_state: [],
Expand All @@ -436,7 +436,7 @@ export function analyze_component(root, options) {
);
}
} else {
instance.scope.declare(b.id('$$props'), 'prop', 'synthetic');
instance.scope.declare(b.id('$$props'), 'bindable_prop', 'synthetic');
instance.scope.declare(b.id('$$restProps'), 'rest_prop', 'synthetic');

for (const { ast, scope, scopes } of [module, instance, template]) {
Expand All @@ -446,7 +446,7 @@ export function analyze_component(root, options) {
analysis,
options,
parent_element: null,
has_props_rune: false,
has_props_rune: [false, false],
ast_type: ast === instance.ast ? 'instance' : ast === template.ast ? 'template' : 'module',
instance_scope: instance.scope,
reactive_statement: null,
Expand All @@ -466,7 +466,10 @@ export function analyze_component(root, options) {
}

for (const [name, binding] of instance.scope.declarations) {
if (binding.kind === 'prop' && binding.node.name !== '$$props') {
if (
(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
binding.node.name !== '$$props'
) {
const references = binding.references.filter(
(r) => r.node !== binding.node && r.path.at(-1)?.type !== 'ExportSpecifier'
);
Expand Down Expand Up @@ -758,7 +761,7 @@ const legacy_scope_tweaker = {
(binding.kind === 'normal' &&
(binding.declaration_kind === 'let' || binding.declaration_kind === 'var'))
) {
binding.kind = 'prop';
binding.kind = 'bindable_prop';
if (specifier.exported.name !== specifier.local.name) {
binding.prop_alias = specifier.exported.name;
}
Expand Down Expand Up @@ -796,7 +799,7 @@ const legacy_scope_tweaker = {
for (const declarator of node.declaration.declarations) {
for (const id of extract_identifiers(declarator.id)) {
const binding = /** @type {import('#compiler').Binding} */ (state.scope.get(id.name));
binding.kind = 'prop';
binding.kind = 'bindable_prop';
}
}
}
Expand Down Expand Up @@ -854,7 +857,8 @@ const runes_scope_tweaker = {
rune !== '$state.frozen' &&
rune !== '$derived' &&
rune !== '$derived.by' &&
rune !== '$props'
rune !== '$props' &&
rune !== '$props.bindable'
)
return;

Expand All @@ -870,10 +874,12 @@ const runes_scope_tweaker = {
? 'derived'
: path.is_rest
? 'rest_prop'
: 'prop';
: rune === '$props.bindable'
? 'bindable_prop'
: 'prop';
}

if (rune === '$props') {
if (rune === '$props' || rune === '$props.bindable') {
for (const property of /** @type {import('estree').ObjectPattern} */ (node.id).properties) {
if (property.type !== 'Property') continue;

Expand Down
2 changes: 1 addition & 1 deletion packages/svelte/src/compiler/phases/2-analyze/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export interface AnalysisState {
options: ValidatedCompileOptions;
ast_type: 'instance' | 'template' | 'module';
parent_element: string | null;
has_props_rune: boolean;
has_props_rune: [props: boolean, bindings: boolean];
/** Which slots the current parent component has */
component_slots: Set<string>;
/** The current {expression}, if any */
Expand Down
28 changes: 18 additions & 10 deletions packages/svelte/src/compiler/phases/2-analyze/validation.js
Original file line number Diff line number Diff line change
Expand Up @@ -299,17 +299,19 @@ const validation = {
error(node, 'invalid-binding-expression');
}

const binding = context.state.scope.get(left.name);

if (
assignee.type === 'Identifier' &&
node.name !== 'this' // bind:this also works for regular variables
) {
const binding = context.state.scope.get(left.name);
// reassignment
if (
!binding ||
(binding.kind !== 'state' &&
binding.kind !== 'frozen_state' &&
binding.kind !== 'prop' &&
binding.kind !== 'bindable_prop' &&
binding.kind !== 'each' &&
binding.kind !== 'store_sub' &&
!binding.mutated)
Expand All @@ -328,8 +330,6 @@ const validation = {
// TODO handle mutations of non-state/props in runes mode
}

const binding = context.state.scope.get(left.name);

if (node.name === 'group') {
if (!binding) {
error(node, 'INTERNAL', 'Cannot find declaration for bind:group');
Expand Down Expand Up @@ -775,12 +775,17 @@ function validate_call_expression(node, scope, path) {

const parent = /** @type {import('#compiler').SvelteNode} */ (get_parent(path, -1));

if (rune === '$props') {
if (rune === '$props' || rune === '$props.bindable') {
if (parent.type === 'VariableDeclarator') return;
error(node, 'invalid-props-location');
}

if (rune === '$state' || rune === '$derived' || rune === '$derived.by') {
if (
rune === '$state' ||
rune === '$state.frozen' ||
rune === '$derived' ||
rune === '$derived.by'
) {
if (parent.type === 'VariableDeclarator') return;
if (parent.type === 'PropertyDefinition' && !parent.static && !parent.computed) return;
error(node, 'invalid-state-location', rune);
Expand Down Expand Up @@ -871,7 +876,7 @@ export const validation_runes_js = {
error(node, 'invalid-rune-args-length', rune, [1]);
} else if (rune === '$state' && args.length > 1) {
error(node, 'invalid-rune-args-length', rune, [0, 1]);
} else if (rune === '$props') {
} else if (rune === '$props' || rune === '$props.bindable') {
error(node, 'invalid-props-location');
}
},
Expand Down Expand Up @@ -1054,15 +1059,18 @@ export const validation_runes = merge(validation, a11y_validators, {
error(node, 'invalid-rune-args-length', rune, [1]);
} else if (rune === '$state' && args.length > 1) {
error(node, 'invalid-rune-args-length', rune, [0, 1]);
} else if (rune === '$props') {
if (state.has_props_rune) {
} else if (rune === '$props' || rune === '$props.bindable') {
if (
(rune === '$props' && state.has_props_rune[0]) ||
(rune === '$props.bindable' && state.has_props_rune[1])
) {
error(node, 'duplicate-props-rune');
}

state.has_props_rune = true;
state.has_props_rune[rune === '$props' ? 0 : 1] = true;

if (args.length > 0) {
error(node, 'invalid-rune-args-length', '$props', [0]);
error(node, 'invalid-rune-args-length', rune, [0]);
}

if (node.id.type !== 'ObjectPattern') {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,7 +239,7 @@ export function client_component(source, analysis, options) {
);
});

const properties = analysis.exports.map(({ name, alias }) => {
const component_returned_object = analysis.exports.map(({ name, alias }) => {
const expression = serialize_get_binding(b.id(name), instance_state);

if (expression.type === 'Identifier' && !options.dev) {
Expand All @@ -249,21 +249,46 @@ export function client_component(source, analysis, options) {
return b.get(alias ?? name, [b.return(expression)]);
});

if (analysis.accessors) {
for (const [name, binding] of analysis.instance.scope.declarations) {
if (binding.kind !== 'prop' || name.startsWith('$$')) continue;
const properties = [...analysis.instance.scope.declarations].filter(
([name, binding]) =>
(binding.kind === 'prop' || binding.kind === 'bindable_prop') && !name.startsWith('$$')
);

if (analysis.runes && options.dev) {
/** @type {import('estree').Literal[]} */
const bindable = [];
for (const [name, binding] of properties) {
if (binding.kind === 'bindable_prop') {
bindable.push(b.literal(binding.prop_alias ?? name));
}
}
instance.body.unshift(
b.stmt(b.call('$.validate_prop_bindings', b.id('$$props'), b.array(bindable)))
);
}

if (analysis.accessors) {
for (const [name, binding] of properties) {
const key = binding.prop_alias ?? name;
if (
binding.kind === 'prop' &&
[...analysis.instance.scope.declarations].some(
([name, d]) => d.kind === 'bindable_prop' && (d.prop_alias ?? name) === key
)
) {
// bindable prop takes precedence
continue;
}

properties.push(
component_returned_object.push(
b.get(key, [b.return(b.call(b.id(name)))]),
b.set(key, [b.stmt(b.call(b.id(name), b.id('$$value'))), b.stmt(b.call('$.flushSync'))])
);
}
}

if (options.legacy.componentApi) {
properties.push(
component_returned_object.push(
b.init('$set', b.id('$.update_legacy_props')),
b.init(
'$on',
Expand All @@ -279,7 +304,7 @@ export function client_component(source, analysis, options) {
)
);
} else if (options.dev) {
properties.push(
component_returned_object.push(
b.init(
'$set',
b.thunk(
Expand Down Expand Up @@ -347,16 +372,16 @@ export function client_component(source, analysis, options) {

append_styles();
component_block.body.push(
properties.length > 0
? b.return(b.call('$.pop', b.object(properties)))
component_returned_object.length > 0
? b.return(b.call('$.pop', b.object(component_returned_object)))
: b.stmt(b.call('$.pop'))
);

if (analysis.uses_rest_props) {
/** @type {string[]} */
const named_props = analysis.exports.map(({ name, alias }) => alias ?? name);
for (const [name, binding] of analysis.instance.scope.declarations) {
if (binding.kind === 'prop') named_props.push(binding.prop_alias ?? name);
if (binding.kind === 'bindable_prop') named_props.push(binding.prop_alias ?? name);
}

component_block.body.unshift(
Expand Down Expand Up @@ -463,9 +488,7 @@ export function client_component(source, analysis, options) {
/** @type {import('estree').Property[]} */
const props_str = [];

for (const [name, binding] of analysis.instance.scope.declarations) {
if (binding.kind !== 'prop' || name.startsWith('$$')) continue;

for (const [name, binding] of properties) {
const key = binding.prop_alias ?? name;
const prop_def = typeof ce === 'boolean' ? {} : ce.props?.[key] || {};
if (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ export function serialize_get_binding(node, state) {
return typeof binding.expression === 'function' ? binding.expression(node) : binding.expression;
}

if (binding.kind === 'prop') {
if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
if (binding.node.name === '$$props') {
// Special case for $$props which only exists in the old world
// TODO this probably shouldn't have a 'prop' binding kind
Expand Down Expand Up @@ -377,6 +377,7 @@ export function serialize_set_binding(node, context, fallback, options) {
binding.kind !== 'state' &&
binding.kind !== 'frozen_state' &&
binding.kind !== 'prop' &&
binding.kind !== 'bindable_prop' &&
binding.kind !== 'each' &&
binding.kind !== 'legacy_reactive' &&
!is_store
Expand All @@ -389,7 +390,7 @@ export function serialize_set_binding(node, context, fallback, options) {

const serialize = () => {
if (left === node.left) {
if (binding.kind === 'prop') {
if (binding.kind === 'prop' || binding.kind === 'bindable_prop') {
return b.call(left, value);
} else if (is_store) {
return b.call('$.store_set', serialize_get_binding(b.id(left_name), state), value);
Expand Down Expand Up @@ -467,7 +468,7 @@ export function serialize_set_binding(node, context, fallback, options) {
b.call('$.untrack', b.id('$' + left_name))
);
} else if (!state.analysis.runes) {
if (binding.kind === 'prop') {
if (binding.kind === 'bindable_prop') {
return b.call(
left,
b.sequence([
Expand Down Expand Up @@ -571,7 +572,7 @@ function get_hoistable_params(node, context) {
params.push(b.id(binding.expression.object.arguments[0].name));
} else if (
// If we are referencing a simple $$props value, then we need to reference the object property instead
binding.kind === 'prop' &&
(binding.kind === 'prop' || binding.kind === 'bindable_prop') &&
!binding.reassigned &&
binding.initial === null &&
!context.state.analysis.accessors
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ export const global_visitors = {
binding?.kind === 'each' ||
binding?.kind === 'legacy_reactive' ||
binding?.kind === 'prop' ||
binding?.kind === 'bindable_prop' ||
is_store
) {
/** @type {import('estree').Expression[]} */
Expand All @@ -64,7 +65,7 @@ export const global_visitors = {
fn += '_store';
args.push(serialize_get_binding(b.id(name), state), b.call('$' + name));
} else {
if (binding.kind === 'prop') fn += '_prop';
if (binding.kind === 'prop' || binding.kind === 'bindable_prop') fn += '_prop';
args.push(b.id(name));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ export const javascript_visitors_legacy = {
state.scope.get_bindings(declarator)
);
const has_state = bindings.some((binding) => binding.kind === 'state');
const has_props = bindings.some((binding) => binding.kind === 'prop');
const has_props = bindings.some((binding) => binding.kind === 'bindable_prop');

if (!has_state && !has_props) {
const init = declarator.init;
Expand Down Expand Up @@ -80,7 +80,7 @@ export const javascript_visitors_legacy = {
declarations.push(
b.declarator(
path.node,
binding.kind === 'prop'
binding.kind === 'bindable_prop'
? get_prop_source(binding, state, binding.prop_alias ?? name, value)
: value
)
Expand Down Expand Up @@ -168,7 +168,7 @@ export const javascript_visitors_legacy = {

// If the binding is a prop, we need to deep read it because it could be fine-grained $state
// from a runes-component, where mutations don't trigger an update on the prop as a whole.
if (name === '$$props' || name === '$$restProps' || binding.kind === 'prop') {
if (name === '$$props' || name === '$$restProps' || binding.kind === 'bindable_prop') {
serialized = b.call('$.deep_read_state', serialized);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ export const javascript_visitors_runes = {
continue;
}

if (rune === '$props') {
if (rune === '$props' || rune === '$props.bindable') {
assert.equal(declarator.id.type, 'ObjectPattern');

/** @type {string[]} */
Expand Down Expand Up @@ -224,16 +224,12 @@ export const javascript_visitors_runes = {
}
} else {
// RestElement
declarations.push(
b.declarator(
property.argument,
b.call(
'$.rest_props',
b.id('$$props'),
b.array(seen.map((name) => b.literal(name)))
)
)
);
/** @type {import('estree').Expression[]} */
const args = [b.id('$$props'), b.array(seen.map((name) => b.literal(name)))];
if (rune === '$props.bindable') {
args.push(b.literal(true));
}
declarations.push(b.declarator(property.argument, b.call('$.rest_props', ...args)));
}
}

Expand Down
Loading