Skip to content

Commit

Permalink
🐳 chore: Lower minimum compatibility target to ES2015
Browse files Browse the repository at this point in the history
  • Loading branch information
Snowflyt committed Nov 12, 2024
1 parent 696e8cc commit 091bd4a
Show file tree
Hide file tree
Showing 6 changed files with 62 additions and 17 deletions.
2 changes: 1 addition & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ const config = {
parserOptions: {
ecmaVersion: "latest",
sourceType: "module",
project: ["./tsconfig.json", "./tsconfig.eslint.json"],
project: ["./tsconfig.json", "./tsconfig.test.json", "./tsconfig.eslint.json"],
tsconfigRootDir: __dirname,
},
ignorePatterns: ["!.lintstagedrc.js"],
Expand Down
20 changes: 15 additions & 5 deletions src/README.example.proof.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,13 @@ test("banner", () => {
});

const program = fetchAdminData()
.resume("log", (msg) => console.log(msg))
.resume("log", (msg) => {
console.log(msg);
})
.provideBy("user", () => ({ id: 1, name: "Alice", role: "admin" }) satisfies User)
.catch("auth", (err) => console.error("Authorization error:", err));
.catch("auth", (err) => {
console.error("Authorization error:", err);
});

expect(program).to(equal<Effected<never, void>>);
});
Expand Down Expand Up @@ -162,10 +166,16 @@ test("Usage", () => {
console.log("Parameters:", params);
return 42;
})
.resume("println", console.log)
.resume("println", (...args) => {
console.log(...args);
})
.provide("currentUser", { id: 1, name: "Charlie", role: "admin" })
.catch("authentication", () => console.error("Authentication error"))
.catch("unauthorized", () => console.error("Unauthorized error"));
.catch("authentication", () => {
console.error("Authentication error");
})
.catch("unauthorized", () => {
console.error("Unauthorized error");
});

expect(handled4).to(equal<Effected<never, void | User>>);
expect(handled4.runSync()).to(equal<void | User>);
Expand Down
34 changes: 29 additions & 5 deletions src/effected.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,10 @@ export function effect<Name extends string | symbol, Resumable extends boolean =
};
});
if (options && (options as any)._overrideFunctionName === false) return result as never;
return renameFunction(result, typeof name === "string" ? name : name.description || "") as never;
return renameFunction(
result,
typeof name === "string" ? name : name.toString().slice(7, -1) || "",
) as never;
}

/**
Expand Down Expand Up @@ -220,7 +223,7 @@ export class Effected<out E extends Effect, out R> implements Iterable<E, R, unk

private constructor(fn: () => Iterator<E, R, unknown>, magicWords?: string) {
if (magicWords !== "Yes, I’m sure I want to call the constructor of Effected directly.")
console.warn(
logger.warn(
"You should not call the constructor of `Effected` directly. Use `effected` instead.",
);

Expand Down Expand Up @@ -364,7 +367,7 @@ export class Effected<out E extends Effect, out R> implements Iterable<E, R, unk
if (terminated === "with-value") message += ` with ${stringify(terminatedValue)}`;
}
message += "). Only the first handler will be used.";
console.warn(message);
logger.warn(message);
};
const resume = (...args: [] | [R]) => {
if (resumed || terminated) {
Expand Down Expand Up @@ -1198,7 +1201,7 @@ const stringify = (x: unknown, space: number = 0): string => {
const indent = (level: number): string => (space > 0 ? " ".repeat(level * space) : "");

const serialize = (value: unknown, level: number): string => {
if (typeof value === "bigint") return `${value}n`;
if (typeof value === "bigint") return `${value as any}n`;
if (typeof value === "function")
return value.name ? `[Function: ${value.name}]` : "[Function (anonymous)]";
if (typeof value === "symbol") return value.toString();
Expand All @@ -1219,7 +1222,7 @@ const stringify = (x: unknown, space: number = 0): string => {
.map((item) => serialize(item, nextLevel))
.join(space > 0 ? `,\n${indent(nextLevel)}` : ", ");
let result = `[${space > 0 ? "\n" + indent(nextLevel) : ""}${arrayItems}${space > 0 ? "\n" + indent(level) : ""}]`;
if (className !== "Array ") result = `${className.trimEnd()}(${value.length}) ${result}`;
if (className !== "Array ") result = `${className.trim()}(${value.length}) ${result}`;
return result;
}

Expand All @@ -1242,3 +1245,24 @@ const stringify = (x: unknown, space: number = 0): string => {

return serialize(x, 0);
};

// `console` is not standard in JavaScript. Though rare, it is possible that `console` is not
// available in some environments. We use a proxy to handle this case and ignore errors if `console`
// is not available.
const logger: {
debug(...data: unknown[]): void;
error(...data: unknown[]): void;
log(...data: unknown[]): void;
warn(...data: unknown[]): void;
} = new Proxy({} as never, {
get:
(_, prop) =>
(...args: unknown[]) => {
try {
// @ts-expect-error - Cannot find name 'console'
console[prop](...args);
} catch {
// Ignore
}
},
});
2 changes: 0 additions & 2 deletions tsconfig.build.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
"noEmit": false,
"outDir": "dist"
},
"include": ["src"],
"exclude": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"],
"tsc-alias": {
"resolveFullPaths": true
}
Expand Down
10 changes: 6 additions & 4 deletions tsconfig.json
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
{
"compilerOptions": {
"target": "ES2020",
"target": "ES2015",
"useDefineForClassFields": true,
"lib": ["ES2020"],
"module": "ESNext",
"lib": ["ES2015", "ES2018.AsyncGenerator"],
"module": "ES2015",
"types": [],

/* Bundler mode */
"moduleResolution": "Bundler",
Expand All @@ -25,5 +26,6 @@
/* Type */
"declaration": true
},
"include": ["src"]
"include": ["src"],
"exclude": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"]
}
11 changes: 11 additions & 0 deletions tsconfig.test.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"extends": "./tsconfig.json",
"compilerOptions": {
"target": "ESNext",
"useDefineForClassFields": true,
"lib": ["ESNext"],
"module": "ESNext"
},
"include": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"],
"exclude": []
}

0 comments on commit 091bd4a

Please sign in to comment.