Skip to content

Commit

Permalink
fix: ajusta typescript configs
Browse files Browse the repository at this point in the history
  • Loading branch information
drusco committed Aug 31, 2023
1 parent 4086671 commit 7120e6c
Show file tree
Hide file tree
Showing 50 changed files with 168 additions and 130 deletions.
1 change: 0 additions & 1 deletion .eslintrc.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,5 @@
"@typescript-eslint/no-unused-vars": "error",
"@typescript-eslint/no-explicit-any": "off",
"@typescript-eslint/no-namespace": "off"

}
}
1 change: 1 addition & 0 deletions .husky/pre-commit
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
. "$(dirname -- "$0")/_/husky.sh"

npm run lint
npm run check
npm test
2 changes: 1 addition & 1 deletion jest.config.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"transform": {
"^.+\\.ts$": "@swc/jest"
"^.+\\.(t|j)sx?$": "@swc/jest"
},
"runtime": "@side/jest-runtime",
"moduleNameMapper": {
Expand Down
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@
"scripts": {
"prepare": "husky install",
"dev": "tsc --watch",
"build": "tsc",
"check": "tsc --project .",
"build": "npm run clean && tsc --project src",
"clean": "rm -rf dist",
"test": "jest",
"memtest": "node --expose-gc ./node_modules/.bin/jest --runInBand --logHeapUsage",
"lint": "eslint --fix --ext .js,.ts,.json src"
"lint": "eslint --fix --ext .js,.ts,.json ."
},
"author": {
"name": "Pedro Gallardo",
Expand Down
12 changes: 6 additions & 6 deletions src/Emulator.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emulator from "./Emulator";
import Exotic from "./types/Exotic";
import Emulator from "./Emulator.js";
import Exotic from "./types/Exotic.js";

describe("(lib) Emulator", () => {
it("Can simulate a browser window's context", async () => {
Expand Down Expand Up @@ -29,17 +29,17 @@ describe("(lib) Emulator", () => {

external.addListener(
"proxy",
(origin: Exotic.proxy.origin, target: any) => {
browser.include(origin, target);
(encodedProxy: string, origin: Exotic.proxy.origin, target: any) => {
browser.include(encodedProxy, origin, target);
},
);

external.addListener("get", (values: any[], use: Exotic.FunctionLike) => {
use(browser.decode(values).map((arg: any) => browser.target(arg)));
});

const window = external.useRef("window", global);
const browserWindow = browser.useRef("window");
const window = external.link("window", global);
const browserWindow = browser.link("window");

window.test = "connected";
window.test2 = window.method();
Expand Down
12 changes: 4 additions & 8 deletions src/Emulator.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import lib from "./lib/index.js";
import Exotic from "./types/Exotic.js";
import EventEmitter from "events";
import findProxy from "./utils/findProxy.js";
import map from "./utils/map.js";

export default class Emulator extends EventEmitter implements Exotic.Emulator {
constructor(options?: Exotic.emulator.options) {
Expand All @@ -22,8 +20,8 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
return lib.methods.use(this, value);
}

useRef(key: Exotic.key, value?: any): Exotic.Proxy {
return lib.methods.useRef(this, key, value);
link(key: Exotic.key, value?: any): Exotic.Proxy {
return lib.methods.link(this, key, value);
}

target(value?: any): any {
Expand Down Expand Up @@ -78,9 +76,7 @@ export default class Emulator extends EventEmitter implements Exotic.Emulator {
return lib.methods.include(this, encodedProxy, origin, target);
}

getId(value: any): number {
const proxy = findProxy(value);
if (!proxy) return -1;
return map.proxies.get(proxy).id;
exec(func: Exotic.FunctionLike, map: Record<string, Exotic.Proxy>): any {
return lib.methods.exec(this, func, map);
}
}
6 changes: 3 additions & 3 deletions src/lib/constructor.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import Emulator from "../Emulator";
import Exotic from "../types/Exotic";
import { map, constants } from "../utils";
import Emulator from "../Emulator.js";
import Exotic from "../types/Exotic.js";
import { map, constants } from "../utils/index.js";

describe("(constructor)", () => {
it("Creates a new emulator", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/getters/length.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(getter) length", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/getters/refs.spec.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(getter) refs", () => {
it("Returns an array of all reference keys in use", () => {
const reference = Date.now().toString();
$.useRef(reference);
$.link(reference);
expect($.refs.includes(reference)).toBe(true);
});
});
2 changes: 1 addition & 1 deletion src/lib/methods/decode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) decode", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/encode.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";

const $ = new Emulator();

Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/entries.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) entries", () => {
Expand Down
17 changes: 17 additions & 0 deletions src/lib/methods/exec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import Exotic from "../../types/Exotic.js";
import encode from "./encode.js";
import createProxy from "../../utils/createProxy.js";

export default function exec(
scope: Exotic.Emulator,
func: Exotic.FunctionLike,
map: Record<string, Exotic.Proxy>,
): Exotic.Proxy {
let target = func.toString();
const keys = Object.keys(map);
keys.sort((a, b) => b.length - a.length);
keys.forEach((key) => {
target = target.replaceAll(key, encode(map[key]));
});
return createProxy(scope, { action: "exec" }, target);
}
4 changes: 2 additions & 2 deletions src/lib/methods/get.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emulator from "../../Emulator";
import Exotic from "../../types/Exotic";
import Emulator from "../../Emulator.js";
import Exotic from "../../types/Exotic.js";

describe("(method) get", () => {
it("Gets the external value of a single proxy", async () => {
Expand Down
14 changes: 7 additions & 7 deletions src/lib/methods/include.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import Emulator from "../../Emulator";
import Exotic from "../../types/Exotic";
import Emulator from "../../Emulator.js";
import Exotic from "../../types/Exotic.js";

const $ = new Emulator();

Expand All @@ -14,18 +14,18 @@ describe("(method) include", () => {
it("Creates a proxy from a reference key with default target", async () => {
const reference = "test";
const target = ["example"];
const origin: Exotic.proxy.origin = { ref: reference };
const origin: Exotic.proxy.origin = { action: "link", key: reference };

await $.include("", origin, target);

expect($.refs.includes(reference)).toBe(true);
expect($.useRef(reference)).toBe($.use(target));
expect($.link(reference)).toBe($.use(target));
});

it("Creates a proxy from a reference key using a target listener", async () => {
const reference = "listener";
const target = {};
const origin: Exotic.proxy.origin = { ref: reference };
const origin: Exotic.proxy.origin = { action: "link", key: reference };

$.on("reference", (ref: Exotic.key, use: Exotic.FunctionLike) => {
if (ref === "listener") {
Expand All @@ -37,7 +37,7 @@ describe("(method) include", () => {
await $.include("", origin, target);

expect($.refs.includes(reference)).toBe(true);
expect($.useRef(reference)).toBe($.use(target));
expect($.link(reference)).toBe($.use(target));
});

it("Creates a proxy from an origin with a 'set' action", () => {
Expand Down Expand Up @@ -69,7 +69,7 @@ describe("(method) include", () => {
class MyClass {}
const proxy = $.use(MyClass);
const origin: Exotic.proxy.origin = {
action: "construct",
action: "build",
proxy,
args: [10],
};
Expand Down
15 changes: 10 additions & 5 deletions src/lib/methods/include.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,17 +14,17 @@ export default function include(
origin: Exotic.proxy.origin,
target?: any,
): any {
const decodedOrigin = decode(scope, origin);
const decodedOrigin: Exotic.proxy.origin = decode(scope, origin);
const decodedTarget = decode(scope, target);
const proxyAsTarget = findProxy(decodedTarget);
const newTarget = proxyAsTarget ? proxyAsTarget : target;
const { action, proxy, key, value, that, args, ref } = decodedOrigin;
const { action, proxy, key, value, that, args } = decodedOrigin;

if (ref) {
if (action === "link") {
// creates proxy by reference
return new Promise((resolve) => {
let done = false;
scope.emit("reference", ref, (target: any) => {
scope.emit("reference", key, (target: any) => {
done = true;
createProxy(scope, decodedOrigin, target);
resolve(undefined);
Expand All @@ -35,6 +35,11 @@ export default function include(
});
}

if (action === "exec") {
const program = new Function("$", `return (${decodedTarget})($)`);
return createProxy(scope, decodedOrigin, program(scope));
}

if (!action) {
// creates proxy by target
return createProxy(scope, decodedOrigin, newTarget);
Expand All @@ -61,7 +66,7 @@ export default function include(
case "apply":
proxyFromTrap = traps.apply(mock, that, args);
break;
case "construct":
case "build":
proxyFromTrap = traps.construct(mock, args);
break;
}
Expand Down
6 changes: 4 additions & 2 deletions src/lib/methods/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import useRef from "./useRef.js";
import link from "./link.js";
import use from "./use.js";
import target from "./target.js";
import parent from "./parent.js";
Expand All @@ -12,9 +12,10 @@ import encode from "./encode.js";
import decode from "./decode.js";
import get from "./get.js";
import include from "./include.js";
import exec from "./exec.js";

export default {
useRef,
link,
use,
target,
parent,
Expand All @@ -28,4 +29,5 @@ export default {
decode,
get,
include,
exec,
};
2 changes: 1 addition & 1 deletion src/lib/methods/isRevoked.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) isRevoked", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/keys.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) keys", () => {
Expand Down
27 changes: 27 additions & 0 deletions src/lib/methods/link.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) link", () => {
it("Can access a proxy using a string", () => {
const ref = "test";
const proxy = $.link(ref);

expect(proxy).toBe($.link(ref));
});

it("Allows empty string as reference key", () => {
const ref = "";
const proxy = $.link(ref);

expect(proxy).toBe($.link(ref));
});

it("Allows a target value as a second parameter", () => {
const ref = "ref_and_target";
const target = [];
const proxy = $.link(ref, target);

expect(proxy).toBe($.link(ref));
expect($.target(proxy)).toBe(target);
});
});
4 changes: 2 additions & 2 deletions src/lib/methods/useRef.ts → src/lib/methods/link.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import Exotic from "../../types/Exotic.js";
import { createProxy } from "../../utils/index.js";

export default function useRef(
export default function link(
scope: Exotic.Emulator,
key: Exotic.key,
value?: any,
): Exotic.Proxy {
// create a proxy by reference key
return createProxy(scope, { ref: key }, value);
return createProxy(scope, { action: "link", key }, value);
}
2 changes: 1 addition & 1 deletion src/lib/methods/parent.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) parent", () => {
Expand Down
10 changes: 5 additions & 5 deletions src/lib/methods/revoke.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) revoke", () => {
Expand Down Expand Up @@ -36,15 +36,15 @@ describe("(method) revoke", () => {

it("Removes the reference binded to a proxy", () => {
const ref = "myref";
const proxy = $.useRef(ref);
const proxy = $.link(ref);
const inner = proxy.inner;
const deep = proxy.inner.inner;

$.revoke(proxy);

expect($.refs.includes(ref)).toBe(false);
expect($.useRef(ref)).not.toBe(proxy);
expect($.useRef(ref)).not.toBe(inner);
expect($.useRef(ref)).not.toBe(deep);
expect($.link(ref)).not.toBe(proxy);
expect($.link(ref)).not.toBe(inner);
expect($.link(ref)).not.toBe(deep);
});
});
2 changes: 1 addition & 1 deletion src/lib/methods/revokeAll.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) revokeAll", () => {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/methods/target.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import Emulator from "../../Emulator";
import Emulator from "../../Emulator.js";
const $ = new Emulator();

describe("(method) target", () => {
Expand Down
4 changes: 2 additions & 2 deletions src/lib/methods/use.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { isTraceable } from "../../utils";
import Emulator from "../../Emulator";
import { isTraceable } from "../../utils/index.js";
import Emulator from "../../Emulator.js";

const $ = new Emulator();

Expand Down
Loading

0 comments on commit 7120e6c

Please sign in to comment.