add state.getters as alternative to binding.expression

pull/12530/head
Rich Harris 2 years ago
parent f402d01454
commit 273fdfe1f7

@ -65,6 +65,7 @@ export function client_component(source, analysis, options) {
preserve_whitespace: options.preserveWhitespace,
public_state: new Map(),
private_state: new Map(),
getters: new Map(),
in_constructor: false,
// these are set inside the `Fragment` visitor, and cannot be used until then
@ -582,6 +583,7 @@ export function client_module(analysis, options) {
legacy_reactive_statements: new Map(),
public_state: new Map(),
private_state: new Map(),
getters: new Map(),
in_constructor: false
};

@ -84,6 +84,11 @@ export function serialize_get_binding(node, state) {
return b.call(node);
}
const getter = state.getters.get(binding);
if (getter) {
return typeof getter === 'function' ? getter(node) : getter;
}
if (binding.expression) {
return typeof binding.expression === 'function' ? binding.expression(node) : binding.expression;
}

@ -221,6 +221,11 @@ function serialize_get_binding(node, state) {
);
}
const getter = state.getters.get(binding);
if (getter) {
return typeof getter === 'function' ? getter(node) : getter;
}
if (binding.expression) {
return typeof binding.expression === 'function' ? binding.expression(node) : binding.expression;
}
@ -1983,6 +1988,7 @@ export function server_component(analysis, options) {
namespace: options.namespace,
preserve_whitespace: options.preserveWhitespace,
private_derived: new Map(),
getters: new Map(),
skip_hydration_boundaries: false
};
@ -2293,7 +2299,8 @@ export function server_module(analysis, options) {
// to be present for `javascript_visitors_legacy` and so is included in module
// transform state as well as component transform state
legacy_reactive_statements: new Map(),
private_derived: new Map()
private_derived: new Map(),
getters: new Map()
};
const module = /** @type {import('estree').Program} */ (

@ -1,10 +1,12 @@
import type { Scope } from '../scope.js';
import type { SvelteNode, ValidatedModuleCompileOptions } from '#compiler';
import type { Binding, SvelteNode, ValidatedModuleCompileOptions } from '#compiler';
import type { Analysis } from '../types.js';
import type { Expression, Identifier } from 'estree';
export interface TransformState {
readonly analysis: Analysis;
readonly options: ValidatedModuleCompileOptions;
readonly scope: Scope;
readonly scopes: Map<SvelteNode, Scope>;
readonly getters: Map<Binding, Expression | ((id: Identifier) => Expression)>;
}

@ -308,6 +308,7 @@ export interface Binding {
/**
* If this is set, all references should use this expression instead of the identifier name.
* If a function is given, it will be called with the identifier at that location and should return the new expression.
* @deprecated use `state.getters.get(binding)` instead
*/
expression: Expression | ((id: Identifier) => Expression) | null;
/** If this is set, all mutations should use this expression */

Loading…
Cancel
Save