All files / src/internal/client/dev ownership.js

96.47% Statements 219/227
88.46% Branches 46/52
100% Functions 10/10
96.42% Lines 216/224

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 2252x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 887x 887x 887x 887x 887x 887x 887x 9757x 9757x 9757x 8758x 8758x 8758x 8758x 8758x 8758x 9757x 887x 887x 887x 2x 2x 2x 2x 2x 753x 753x 753x 753x 753x 753x 753x 753x 753x 737x 737x 737x 737x 737x 737x 737x     16x 16x 16x 16x 16x 16x         2x 2x 2x 2x 2x 2x 2x 2x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 67x 2x 2x 67x 67x 67x 67x 67x 67x 67x 2x 2x 2x 2x 2x 2x 2x 2x 2x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 22x 2x 2x 2x 2x 2x 2x 62x 62x 50x 50x 62x 10x 10x 50x 50x 50x 2x 2x 2x 2x 2x 2x 2x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 367x 2x 2x 2x 2x 2x 505x 505x 222x 222x 505x 26x 26x 222x 222x 222x 2x 2x 2x 2x 2x 272x 272x 148x 172x 172x 148x 148x 148x 148x 148x 148x 148x 5x 148x 5x 5x 14x 14x 6x 6x 6x     6x 14x 5x 148x 272x 2x 2x 2x 2x 2x 753x 753x 753x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 4x 753x  
/** @typedef {{ file: string, line: number, column: number }} Location */
 
import { STATE_SYMBOL } from '../constants.js';
import { untrack } from '../runtime.js';
import { get_descriptors } from '../utils.js';
 
/** @type {Record<string, Array<{ start: Location, end: Location, component: Function }>>} */
const boundaries = {};
 
const chrome_pattern = /at (?:.+ \()?(.+):(\d+):(\d+)\)?$/;
const firefox_pattern = /@(.+):(\d+):(\d+)$/;
 
function get_stack() {
	const stack = new Error().stack;
	if (!stack) return null;
 
	const entries = [];
 
	for (const line of stack.split('\n')) {
		let match = chrome_pattern.exec(line) ?? firefox_pattern.exec(line);
 
		if (match) {
			entries.push({
				file: match[1],
				line: +match[2],
				column: +match[3]
			});
		}
	}
 
	return entries;
}
 
/**
 * Determines which `.svelte` component is responsible for a given state change
 * @returns {Function | null}
 */
function get_component() {
	// first 4 lines are svelte internals; adjust this number if we change the internal call stack
	const stack = get_stack()?.slice(4);
	if (!stack) return null;
 
	for (let i = 0; i < stack.length; i++) {
		const entry = stack[i];
		const modules = boundaries[entry.file];
		if (!modules) {
			// If the first entry is not a component, that means the modification very likely happened
			// within a .svelte.js file, possibly triggered by a component. Since these files are not part
			// of the bondaries/component context heuristic, we need to bail in this case, else we would
			// have false positives when the .svelte.ts file provides a state creator function, encapsulating
			// the state and its mutations, and is being called from a component other than the one who
			// called the state creator function.
			if (i === 0) return null;
			continue;
		}
 
		for (const module of modules) {
			if (module.start.line < entry.line && module.end.line > entry.line) {
				return module.component;
			}
		}
	}

	return null;
}
 
/**
 * Together with `mark_module_end`, this function establishes the boundaries of a `.svelte` file,
 * such that subsequent calls to `get_component` can tell us which component is responsible
 * for a given state change
 * @param {Function} component
 */
export function mark_module_start(component) {
	const start = get_stack()?.[2];
 
	if (start) {
		(boundaries[start.file] ??= []).push({
			start,
			// @ts-expect-error
			end: null,
			component
		});
	}
}
 
export function mark_module_end() {
	const end = get_stack()?.[2];
 
	if (end) {
		const boundaries_file = boundaries[end.file];
		boundaries_file[boundaries_file.length - 1].end = end;
	}
}
 
let add_owner_visited = new Set();
 
/**
 *
 * @param {any} object
 * @param {any} owner
 */
export function add_owner(object, owner) {
	// Needed because ownership addition can invoke getters on a proxy,
	// calling add_owner anew, so just keeping the set as part of
	// add_owner_to_object would not be enough.
	const prev = add_owner_visited;
	try {
		add_owner_visited = new Set(add_owner_visited);
		untrack(() => {
			add_owner_to_object(object, owner, add_owner_visited);
		});
	} finally {
		add_owner_visited = prev;
	}
}
 
/**
 * @param {any} object
 * @param {Function} owner
 * @param {Set<any>} visited
 */
function add_owner_to_object(object, owner, visited) {
	if (visited.has(object)) return;
	visited.add(object);
 
	if (object?.[STATE_SYMBOL]?.o && !object[STATE_SYMBOL].o.has(owner)) {
		object[STATE_SYMBOL].o.add(owner);
	}
	// Not inside previous if-block; there could be normal objects in-between
	traverse_for_owners(object, (nested) => add_owner_to_object(nested, owner, visited));
}
 
let strip_owner_visited = new Set();
 
/**
 * @param {any} object
 */
export function strip_owner(object) {
	// Needed because ownership stripping can invoke getters on a proxy,
	// calling strip_owner anew, so just keeping the set as part of
	// strip_owner_from_object would not be enough.
	const prev = strip_owner_visited;
	try {
		untrack(() => {
			strip_owner_from_object(object, strip_owner_visited);
		});
	} finally {
		strip_owner_visited = prev;
	}
}
 
/**
 * @param {any} object
 * @param {Set<any>} visited
 */
function strip_owner_from_object(object, visited) {
	if (visited.has(object)) return;
	visited.add(object);
 
	if (object?.[STATE_SYMBOL]?.o) {
		object[STATE_SYMBOL].o = null;
	}
	// Not inside previous if-block; there could be normal objects in-between
	traverse_for_owners(object, (nested) => strip_owner_from_object(nested, visited));
}
 
/**
 * @param {any} object
 * @param {(obj: any) => void} cb
 */
function traverse_for_owners(object, cb) {
	if (typeof object === 'object' && object !== null && !(object instanceof EventTarget)) {
		for (const key in object) {
			cb(object[key]);
		}
		// deal with state on classes
		const proto = Object.getPrototypeOf(object);
		if (
			proto !== Object.prototype &&
			proto !== Array.prototype &&
			proto !== Map.prototype &&
			proto !== Set.prototype &&
			proto !== Date.prototype
		) {
			const descriptors = get_descriptors(proto);
			for (let key in descriptors) {
				const get = descriptors[key].get;
				if (get) {
					try {
						cb(object[key]);
					} catch (e) {
						// continue
					}
				}
			}
		}
	}
}
 
/**
 * @param {Set<Function>} owners
 */
export function check_ownership(owners) {
	const component = get_component();
 
	if (component && !owners.has(component)) {
		let original = [...owners][0];
 
		let message =
			// @ts-expect-error
			original.filename !== component.filename
				? // @ts-expect-error
					`${component.filename} mutated a value owned by ${original.filename}. This is strongly discouraged`
				: 'Mutating a value outside the component that created it is strongly discouraged';
 
		// eslint-disable-next-line no-console
		console.warn(
			`${message}. Consider passing values to child components with \`bind:\`, or use a callback instead.`
		);
 
		// eslint-disable-next-line no-console
		console.trace();
	}
}