MCPcopy Index your code
hub / github.com/react/react / makeReadOnly

Function makeReadOnly

compiler/packages/make-read-only-util/src/makeReadOnly.ts:91–148  ·  view source on GitHub ↗
(o: T, source: string)

Source from the content-addressed store, hash-verified

89
90 // Changes an object to be read-only, returns its input
91 function makeReadOnly<T>(o: T, source: string): T {
92 if (typeof o !== 'object' || o == null) {
93 return o;
94 } else if (
95 o.constructor?.name != null &&
96 skippedClasses.includes(o.constructor.name)
97 ) {
98 return o;
99 }
100
101 const {existed, entry: cache} = getOrInsertDefault(savedROObjects, o);
102
103 for (const [k, entry] of cache.entries()) {
104 const currentProp = Object.getOwnPropertyDescriptor(o, k);
105 if (currentProp && !isWriteable(currentProp)) {
106 continue;
107 }
108 const currentPropGetter = currentProp?.get;
109 const cachedGetter = entry.getter;
110
111 if (currentPropGetter !== cachedGetter) {
112 // cache is currently holding an old property
113 // - it may have been deleted
114 // - it may have been deleted + re-set
115 // (meaning that new value is not proxied,
116 // and the current proxied value is stale)
117 cache.delete(k);
118 if (!currentProp) {
119 logger('FORGET_DELETE_PROP_IMMUT', source, k);
120 } else if (currentProp) {
121 logger('FORGET_CHANGE_PROP_IMMUT', source, k);
122 addProperty(o, source, k, currentProp, cache);
123 }
124 }
125 }
126 for (const [k, prop] of Object.entries(
127 Object.getOwnPropertyDescriptors(o),
128 )) {
129 if (!cache.has(k) && isWriteable(prop)) {
130 if (
131 prop.hasOwnProperty('set') ||
132 prop.hasOwnProperty('get') ||
133 k === 'current'
134 ) {
135 // - we currently don't handle accessor properties
136 // - we currently have no other way of checking whether an object
137 // is a `ref` (i.e. returned by useRef).
138 continue;
139 }
140
141 if (existed) {
142 logger('FORGET_ADD_PROP_IMMUT', source, k);
143 }
144 addProperty(o, source, k, prop, cache);
145 }
146 }
147 return o;
148 }

Callers 2

getFunction · 0.85

Calls 6

isWriteableFunction · 0.85
loggerFunction · 0.85
addPropertyFunction · 0.85
entriesMethod · 0.80
getOrInsertDefaultFunction · 0.70
hasMethod · 0.65

Tested by

no test coverage detected