Skip to content

Commit

Permalink
feat: adds getOwnPropertyDescriptor handler
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Mar 11, 2024
1 parent dd58444 commit 36f31a4
Show file tree
Hide file tree
Showing 4 changed files with 52 additions and 9 deletions.
9 changes: 1 addition & 8 deletions src/events/handlers/defineProperty.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,10 @@ import Nexo from "../../types/Nexo.js";
import { isTraceable, map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";

type descriptor = {
enumerable?: boolean;
writable?: boolean;
configurable?: boolean;
value?: unknown;
};

const defineProperty = (
mock: Nexo.Mock,
key: Nexo.objectKey,
descriptor: descriptor,
descriptor: PropertyDescriptor,
): boolean => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);
Expand Down
35 changes: 35 additions & 0 deletions src/events/handlers/getOwnPropertyDescriptor.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import Nexo from "../../types/Nexo.js";
import { getTarget, map } from "../../utils/index.js";
import ProxyEvent from "../ProxyEvent.js";

const getOwnPropertyDescriptor = (
mock: Nexo.Mock,
key: Nexo.objectKey,
): PropertyDescriptor => {
const proxy = map.tracables.get(mock);
const data = map.proxies.get(proxy);

const { sandbox } = data;
const scope = data.scope.deref();
const value = getTarget(sandbox.get(key), true);

const event = new ProxyEvent("handler.getOwnPropertyDescriptor", {
proxy,
key,
});

scope.emit(event.name, event);

if (event.defaultPrevented) {
return event.returnValue;
}

return {
configurable: false,
enumerable: true,
writable: false,
value,
};
};

export default getOwnPropertyDescriptor;
11 changes: 10 additions & 1 deletion src/events/handlers/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,16 @@ import get from "./get.js";
import set from "./set.js";
import defineProperty from "./defineProperty.js";
import deleteProperty from "./deleteProperty.js";
import getOwnPropertyDescriptor from "./getOwnPropertyDescriptor.js";
import construct from "./construct.js";
import apply from "./apply.js";

export default { get, set, defineProperty, deleteProperty, construct, apply };
export default {
get,
set,
defineProperty,
deleteProperty,
getOwnPropertyDescriptor,
construct,
apply,
};
6 changes: 6 additions & 0 deletions src/types/Nexo.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ declare namespace Nexo {
| "handler.set"
| "handler.defineProperty"
| "handler.deleteProperty"
| "handler.getOwnPropertyDescriptor"
| "handler.apply"
| "handler.construct";

Expand All @@ -61,6 +62,11 @@ declare namespace Nexo {
key: objectKey;
}

interface getOwnPropertyDescriptorHandler extends ProxyEvent {
name: "handler.getOwnPropertyDescriptor";
key: objectKey;
}

interface applyHandler extends ProxyEvent {
name: "handler.apply";
args: unknown[];
Expand Down

0 comments on commit 36f31a4

Please sign in to comment.