Home / File/ ownership.js — svelte Source File

ownership.js — svelte Source File

Architecture documentation for ownership.js, a javascript file in the svelte codebase. 7 imports, 0 dependents.

File javascript ClientRuntime Hydration 7 imports 2 functions

Entity Profile

Dependency Diagram

graph LR
  ad878f27_7e4b_5069_1b06_1750c58a617d["ownership.js"]
  cb946435_ce66_d1e8_6bee_287bdb07e7c5["utils.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> cb946435_ce66_d1e8_6bee_287bdb07e7c5
  73865c3c_2786_c9ac_d34f_b51d28b3a29e["constants.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> 73865c3c_2786_c9ac_d34f_b51d28b3a29e
  48cf26f8_bf34_fd7a_3d52_cc963051e167["context.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> 48cf26f8_bf34_fd7a_3d52_cc963051e167
  df278ca2_0a6c_fefe_09f2_b397500fe3c2["warnings.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> df278ca2_0a6c_fefe_09f2_b397500fe3c2
  2aa63f4e_82c9_33e3_ac6c_5f3d46250522["utils.js"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> 2aa63f4e_82c9_33e3_ac6c_5f3d46250522
  28c6afed_f4f8_7245_50fe_f12a7fd10cba["sanitize_location"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> 28c6afed_f4f8_7245_50fe_f12a7fd10cba
  54c2bfce_50b6_b8cc_4371_e1e14f283fb3["constants"]
  ad878f27_7e4b_5069_1b06_1750c58a617d --> 54c2bfce_50b6_b8cc_4371_e1e14f283fb3
  style ad878f27_7e4b_5069_1b06_1750c58a617d fill:#6366f1,stroke:#818cf8,color:#fff

Relationship Graph

Source Code

/** @typedef {{ file: string, line: number, column: number }} Location */

import { get_descriptor } from '../../shared/utils.js';
import { LEGACY_PROPS, STATE_SYMBOL } from '#client/constants';
import { FILENAME } from '../../../constants.js';
import { component_context } from '../context.js';
import * as w from '../warnings.js';
import { sanitize_location } from '../../../utils.js';

/**
 * Sets up a validator that
 * - traverses the path of a prop to find out if it is allowed to be mutated
 * - checks that the binding chain is not interrupted
 * @param {Record<string, any>} props
 */
export function create_ownership_validator(props) {
	const component = component_context?.function;
	const parent = component_context?.p?.function;

	return {
		/**
		 * @param {string} prop
		 * @param {any[]} path
		 * @param {any} result
		 * @param {number} line
		 * @param {number} column
		 */
		mutation: (prop, path, result, line, column) => {
			const name = path[0];
			if (is_bound_or_unset(props, name) || !parent) {
				return result;
			}

			/** @type {any} */
			let value = props;

			for (let i = 0; i < path.length - 1; i++) {
				value = value[path[i]];
				if (!value?.[STATE_SYMBOL]) {
					return result;
				}
			}

			const location = sanitize_location(`${component[FILENAME]}:${line}:${column}`);

			w.ownership_invalid_mutation(name, location, prop, parent[FILENAME]);

			return result;
		},
		/**
		 * @param {any} key
		 * @param {any} child_component
		 * @param {() => any} value
		 */
		binding: (key, child_component, value) => {
			if (!is_bound_or_unset(props, key) && parent && value()?.[STATE_SYMBOL]) {
				w.ownership_invalid_binding(
					component[FILENAME],
					key,
					child_component[FILENAME],
					parent[FILENAME]
				);
			}
		}
	};
}

/**
 * @param {Record<string, any>} props
 * @param {string} prop_name
 */
function is_bound_or_unset(props, prop_name) {
	// Can be the case when someone does `mount(Component, props)` with `let props = $state({...})`
	// or `createClassComponent(Component, props)`
	const is_entry_props = STATE_SYMBOL in props || LEGACY_PROPS in props;
	return (
		!!get_descriptor(props, prop_name)?.set ||
		(is_entry_props && prop_name in props) ||
		!(prop_name in props)
	);
}

Domain

Subdomains

Frequently Asked Questions

What does ownership.js do?
ownership.js is a source file in the svelte codebase, written in javascript. It belongs to the ClientRuntime domain, Hydration subdomain.
What functions are defined in ownership.js?
ownership.js defines 2 function(s): create_ownership_validator, is_bound_or_unset.
What does ownership.js depend on?
ownership.js imports 7 module(s): constants, constants.js, context.js, sanitize_location, utils.js, utils.js, warnings.js.
Where is ownership.js in the architecture?
ownership.js is located at packages/svelte/src/internal/client/dev/ownership.js (domain: ClientRuntime, subdomain: Hydration, directory: packages/svelte/src/internal/client/dev).

Analyze Your Own Codebase

Get architecture documentation, dependency graphs, and domain analysis for your codebase in minutes.

Try Supermodel Free