1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 19:26:13 +02:00
metamask-extension/app/scripts/lib/freezeGlobals.js
Mark Stacey ac01c5c89a
Consistent jsdoc syntax (#7755)
* Specify type before parameter name

Various JSDoc `@param` entries were specified as `name {type}` rather
than `{type} name`.

A couple of `@return` entries have been given types as well.

* Use JSDoc optional syntax rather than Closure syntax

* Use @returns rather than @return

* Use consistent built-in type capitalization

Primitive types are lower-case, and Object is upper-case.

* Separate param/return description with a dash
2020-01-13 14:36:36 -04:00

42 lines
1.2 KiB
JavaScript

/**
* Freezes the Promise global and prevents its reassignment.
*/
import deepFreeze from 'deep-freeze-strict'
if (
process.env.IN_TEST !== 'true' &&
process.env.METAMASK_ENV !== 'test'
) {
freeze(global, 'Promise')
}
/**
* Makes a key:value pair on a target object immutable, with limitations.
* The key cannot be reassigned or deleted, and the value is recursively frozen
* using Object.freeze.
*
* Because of JavaScript language limitations, this is does not mean that the
* value is completely immutable. It is, however, better than nothing.
*
* @param {Object} target - The target object to freeze a property on.
* @param {string} key - The key to freeze.
* @param {any} [value] - The value to freeze, if different from the existing value on the target.
* @param {boolean} [enumerable=true] - If given a value, whether the property is enumerable.
*/
function freeze (target, key, value, enumerable = true) {
const opts = {
configurable: false, writable: false,
}
if (value !== undefined) {
opts.value = deepFreeze(value)
opts.enumerable = enumerable
} else {
target[key] = deepFreeze(target[key])
}
Object.defineProperty(target, key, opts)
}