mirror of https://github.com/sveltejs/svelte
parent
dbf849bd2a
commit
6814926ce0
@ -0,0 +1,19 @@
|
||||
/** @import { Identifier, Node } from 'estree' */
|
||||
/** @import { Context } from '../../types' */
|
||||
import is_reference from 'is-reference';
|
||||
import * as b from '../../../../../utils/builders.js';
|
||||
import { serialize_get_binding } from './shared/utils.js';
|
||||
|
||||
/**
|
||||
* @param {Identifier} node
|
||||
* @param {Context} context
|
||||
*/
|
||||
export function Identifier(node, { path, state }) {
|
||||
if (is_reference(node, /** @type {Node} */ (path.at(-1)))) {
|
||||
if (node.name === '$$props') {
|
||||
return b.id('$$sanitized_props');
|
||||
}
|
||||
|
||||
return serialize_get_binding(node, state);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,34 @@
|
||||
/** @import { Expression, Identifier } from 'estree' */
|
||||
/** @import { ServerTransformState } from '../../../types' */
|
||||
import * as b from '../../../../../../utils/builders.js';
|
||||
|
||||
/**
|
||||
* @param {Identifier} node
|
||||
* @param {ServerTransformState} state
|
||||
* @returns {Expression}
|
||||
*/
|
||||
export function serialize_get_binding(node, state) {
|
||||
const binding = state.scope.get(node.name);
|
||||
|
||||
if (binding === null || node === binding.node) {
|
||||
// No associated binding or the declaration itself which shouldn't be transformed
|
||||
return node;
|
||||
}
|
||||
|
||||
if (binding.kind === 'store_sub') {
|
||||
const store_id = b.id(node.name.slice(1));
|
||||
return b.call(
|
||||
'$.store_get',
|
||||
b.assignment('??=', b.id('$$store_subs'), b.object([])),
|
||||
b.literal(node.name),
|
||||
serialize_get_binding(store_id, state)
|
||||
);
|
||||
}
|
||||
|
||||
if (Object.hasOwn(state.getters, node.name)) {
|
||||
const getter = state.getters[node.name];
|
||||
return typeof getter === 'function' ? getter(node) : getter;
|
||||
}
|
||||
|
||||
return node;
|
||||
}
|
||||
Loading…
Reference in new issue