chore: ensure $state always uses === equality

pull/9725/head
Dominic Gannaway 3 years ago
parent 2fa06447cf
commit 541cf8c762

@ -360,11 +360,14 @@ export function get_props_method(binding, state, name, default_value) {
* @param {import('estree').VariableDeclarator} declarator
* @param {import('../../scope').Scope} scope
* @param {import('estree').Expression} value
* @param {boolean} runes
*/
export function create_state_declarators(declarator, scope, value) {
export function create_state_declarators(declarator, scope, value, runes) {
// in the simple `let count = $state(0)` case, we rewrite `$state` as `$.source`
if (declarator.id.type === 'Identifier') {
return [b.declarator(declarator.id, b.call('$.source', value))];
return [
b.declarator(declarator.id, runes ? b.call('$.source', value) : b.call('$.source', value))
];
}
const tmp = scope.generate('tmp');
@ -374,7 +377,14 @@ export function create_state_declarators(declarator, scope, value) {
...paths.map((path) => {
const value = path.expression?.(b.id(tmp));
const binding = scope.get(/** @type {import('estree').Identifier} */ (path.node).name);
return b.declarator(path.node, binding?.kind === 'state' ? b.call('$.source', value) : value);
return b.declarator(
path.node,
binding?.kind === 'state'
? runes
? b.call('$.source', value, b.id('$.equals'))
: b.call('$.source', value)
: value
);
})
];
}

@ -86,7 +86,8 @@ export const javascript_visitors_legacy = {
...create_state_declarators(
declarator,
state.scope,
/** @type {import('estree').Expression} */ (declarator.init && visit(declarator.init))
/** @type {import('estree').Expression} */ (declarator.init && visit(declarator.init)),
false
)
);
}

@ -247,7 +247,7 @@ export const javascript_visitors_runes = {
continue;
}
declarations.push(...create_state_declarators(declarator, state.scope, value));
declarations.push(...create_state_declarators(declarator, state.scope, value, true));
}
if (declarations.length === 0) {

@ -149,6 +149,8 @@ function default_equals(a, b) {
return a === b;
}
export { default_equals as equals };
/**
* @template V
* @param {import('./types.js').SignalFlags} flags

@ -38,7 +38,8 @@ export {
reactive_import,
effect_active,
user_root_effect,
inspect
inspect,
equals
} from './client/runtime.js';
export * from './client/validate.js';

Loading…
Cancel
Save