8000 extract `onchange` callbacks from options by Rich-Harris · Pull Request #15579 · sveltejs/svelte · GitHub
[go: up one dir, main page]

Skip to content

extract onchange callbacks from options #15579

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

Merged
merged 10 commits into from
Mar 22, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
fix: revert unwrap args in case of spread
  • Loading branch information
paoloricciuti committed Mar 21, 2025
commit e4c799210bc1de227ed6f18ee7fffaf7f16b697c
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { ClassBody, Expression, Identifier, Literal, MethodDefinition, PrivateIdentifier, PropertyDefinition, SpreadElement } from 'estree' */
/** @import { ClassBody, Expression, Identifier, Literal, MethodDefinition, PrivateIdentifier, PropertyDefinition } from 'estree' */
/** @import { } from '#compiler' */
/** @import { Context, StateField } from '../types' */
import { dev, is_ignored } from '../../../../state.js';
Expand Down Expand Up @@ -113,37 +113,23 @@ export function ClassBody(node, context) {
let value = null;

if (definition.value.arguments.length > 0) {
let init = /** @type {Expression | SpreadElement} **/ (
const init = /** @type {Expression} **/ (
context.visit(definition.value.arguments[0], child_state)
);

if (field.kind === 'state' || field.kind === 'raw_state') {
let onchange;
if (
/** @type {Expression | SpreadElement} */ (/** @type {unknown} */ (init)).type ===
'SpreadElement'
) {
const argument = init.argument;
init = b.member(argument, '0', true);
onchange = b.member(b.member(argument, '1', true), 'onchange', false, true);
}
if (!onchange) {
onchange = get_onchange(
/** @type {Expression} */ (definition.value.arguments[1]),
// @ts-ignore mismatch between Context and ComponentContext. TODO look into
context
);
}
const onchange = get_onchange(
/** @type {Expression} */ (definition.value.arguments[1]),
// @ts-ignore mismatch between Context and ComponentContext. TODO look into
context
);

value =
field.kind === 'state' && should_proxy(init, context.state.scope)
? b.call('$.assignable_proxy', init, onchange)
: b.call('$.state', init, onchange);
} else {
value = b.call(
'$.derived',
field.kind === 'derived_by' ? init : b.thunk(/** @type {Expression} */ (init))
);
value = b.call('$.derived', field.kind === 'derived_by' ? init : b.thunk(init));
}
} else {
// if no arguments, we know it's state as `$derived()` is a compile error
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
/** @import { CallExpression, Expression, Identifier, Literal, VariableDeclaration, VariableDeclarator, SpreadElement } from 'estree' */
/** @import { CallExpression, Expression, Identifier, Literal, VariableDeclaration, VariableDeclarator } from 'estree' */
/** @import { Binding } from '#compiler' */
/** @import { ComponentClientTransformState, ComponentContext } from '../types' */
import { dev } from '../../../../state.js';
Expand Down Expand Up @@ -117,20 +117,9 @@ export function VariableDeclaration(node, context) {
}

const args = /** @type {CallExpression} */ (init).arguments;
let value =
args.length > 0
? /** @type {Expression | SpreadElement} */ (context.visit(args[0]))
: b.void0;
let onchange;

if (value.type === 'SpreadElement') {
const argument = value.argument;
value = b.member(argument, '0', true);
onchange = b.member(b.member(argument, '1', true), 'onchange', false, true);
}
if (!onchange) {
onchange = get_onchange(/** @type {Expression} */ (args[1]), context);
}
const value = args.length > 0 ? /** @type {Expression} */ (context.visit(args[0])) : b.void0;

const onchange = get_onchange(/** @type {Expression} */ (args[1]), context);

if (rune === '$state' || rune === '$state.raw') {
/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
/** @import { Expression, Property, SpreadElement } from 'estree' */
/** @import { Expression, Property } from 'estree' */
/** @import { ComponentContext } from '../../types' */
import * as b from '../../../../../utils/builders.js';

/**
* Extract the `onchange` callback from the options passed to `$state`
* @param {Expression | SpreadElement} options
* @param {Expression} options
* @param {ComponentContext} context
* @returns {Expression | undefined}
*/
Expand All @@ -27,9 +27,5 @@ export function get_onchange(options, context) {
return /** @type {Expression} */ (context.visit(onchange.value));
}

if (options.type === 'SpreadElement') {
return b.member(b.member(options.argument, '0', true), 'onchange');
}

return b.member(/** @type {Expression} */ (context.visit(options)), 'onchange');
}
0