Skip to content

Commit 6d55fea

Browse files
committed
Merge in getter & setter fix
2 parents ff70493 + e0a8a73 commit 6d55fea

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,10 @@ and this project adheres to [**Semantic Versioning v2.0.0**](https://semver.org/
1313

1414
## Unreleased ##
1515

16+
### Fixed ###
17+
18+
* Fixed objects with getters & setters
19+
1620
### Security ###
1721

1822
* Update dependencies

src/deepFreeze.ts

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,23 @@
66
import { GenericKey } from "./types";
77
import { canValueHaveProperties, getPropertyKeys } from "./_internal/utils";
88

9+
const deepFreezeKeysOfObject = (obj: Record<GenericKey, unknown>, keys: readonly GenericKey[]) => {
10+
for (const key of keys) {
11+
const descriptor: PropertyDescriptor = (Object.getOwnPropertyDescriptor(obj, key) as PropertyDescriptor);
12+
13+
deepFreeze(descriptor.get);
14+
deepFreeze(descriptor.set);
15+
deepFreeze(descriptor.value);
16+
}
17+
};
18+
19+
deepFreeze(deepFreezeKeysOfObject);
20+
921
const deepFreezePrototypeExcludingConstructor = (prototype: Record<GenericKey, unknown>) => {
1022
const keys: GenericKey[] = getPropertyKeys(prototype)
1123
.filter((key: GenericKey) => (key !== "constructor"));
1224

13-
for (const key of keys) {
14-
deepFreeze((prototype[key]));
15-
}
25+
deepFreezeKeysOfObject(prototype, keys);
1626
};
1727

1828
deepFreeze(deepFreezePrototypeExcludingConstructor);
@@ -78,9 +88,10 @@ function deepFreeze<T>(obj: T): Readonly<T> {
7888
return deepFreezeFunctionWithPrototype(obj);
7989
}
8090

81-
for (const key of getPropertyKeys(obj)) {
82-
deepFreeze((obj as Record<GenericKey, unknown>)[key]);
83-
}
91+
deepFreezeKeysOfObject(
92+
(obj as Record<GenericKey, unknown>),
93+
getPropertyKeys(obj),
94+
);
8495

8596
if ((obj instanceof Map) || (obj instanceof Set)) {
8697
for (const [key, value] of obj.entries()) {

test/deepFreeze.ts

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,38 @@ describe("function deepFreeze()", function() {
9696
assert.strictEqual(foo.b[1].abc, 123);
9797
assert.strictEqual(foo.b[1].xyz, "asd");
9898
});
99+
100+
it("should work with object getters", function() {
101+
const yVal: number = 64;
102+
103+
const obj = {
104+
get x(): never {
105+
throw new Error("Don't invoke me, bro");
106+
},
107+
108+
get y(): number {
109+
return yVal;
110+
},
111+
};
112+
113+
assert.doesNotThrow(() => {
114+
deepFreeze(obj);
115+
});
116+
});
117+
118+
it("should work with object setters", function() {
119+
const obj = {
120+
set x(_: unknown) {
121+
throw new Error("Don't invoke me either, bro");
122+
},
123+
124+
set y(_: unknown) {
125+
void _;
126+
}
127+
};
128+
129+
assert.doesNotThrow(() => {
130+
deepFreeze(obj);
131+
});
132+
});
99133
});

0 commit comments

Comments
 (0)