All files / src/internal/client freeze.js

100% Statements 44/44
100% Branches 8/8
100% Functions 1/1
100% Lines 40/40

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 412x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 2x 32x 32x 32x 32x 20x 32x 20x 20x 20x 1x 1x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 19x 12x 12x 12x  
import { DEV } from 'esm-env';
import { define_property, is_array, is_frozen, object_freeze } from '../shared/utils.js';
import { STATE_FROZEN_SYMBOL, STATE_SYMBOL } from './constants.js';
import * as e from './errors.js';
 
/**
 * Expects a value that was wrapped with `freeze` and makes it frozen in DEV.
 * @template T
 * @param {T} value
 * @returns {Readonly<T>}
 */
export function freeze(value) {
	if (
		typeof value === 'object' &&
		value !== null &&
		!is_frozen(value) &&
		!(STATE_FROZEN_SYMBOL in value)
	) {
		var copy = /** @type {T} */ (value);
 
		if (STATE_SYMBOL in value) {
			e.state_frozen_invalid_argument();
		}
 
		define_property(copy, STATE_FROZEN_SYMBOL, {
			value: true,
			writable: true,
			enumerable: false
		});
 
		// Freeze the object in DEV
		if (DEV) {
			object_freeze(copy);
		}
 
		return /** @type {Readonly<T>} */ (copy);
	}
 
	return value;
}