Skip to content

Commit 7840fcf

Browse files
committed
🐳 chore: Lower minimum compatibility target to ES2015
1 parent 696e8cc commit 7840fcf

File tree

6 files changed

+62
-17
lines changed

6 files changed

+62
-17
lines changed

.eslintrc.cjs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ const config = {
1818
parserOptions: {
1919
ecmaVersion: "latest",
2020
sourceType: "module",
21-
project: ["./tsconfig.json", "./tsconfig.eslint.json"],
21+
project: ["./tsconfig.json", "./tsconfig.test.json", "./tsconfig.eslint.json"],
2222
tsconfigRootDir: __dirname,
2323
},
2424
ignorePatterns: ["!.lintstagedrc.js"],

src/README.example.proof.ts

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,13 @@ test("banner", () => {
3131
});
3232

3333
const program = fetchAdminData()
34-
.resume("log", (msg) => console.log(msg))
34+
.resume("log", (msg) => {
35+
console.log(msg);
36+
})
3537
.provideBy("user", () => ({ id: 1, name: "Alice", role: "admin" }) satisfies User)
36-
.catch("auth", (err) => console.error("Authorization error:", err));
38+
.catch("auth", (err) => {
39+
console.error("Authorization error:", err);
40+
});
3741

3842
expect(program).to(equal<Effected<never, void>>);
3943
});
@@ -162,10 +166,16 @@ test("Usage", () => {
162166
console.log("Parameters:", params);
163167
return 42;
164168
})
165-
.resume("println", console.log)
169+
.resume("println", (...args) => {
170+
console.log(...args);
171+
})
166172
.provide("currentUser", { id: 1, name: "Charlie", role: "admin" })
167-
.catch("authentication", () => console.error("Authentication error"))
168-
.catch("unauthorized", () => console.error("Unauthorized error"));
173+
.catch("authentication", () => {
174+
console.error("Authentication error");
175+
})
176+
.catch("unauthorized", () => {
177+
console.error("Unauthorized error");
178+
});
169179

170180
expect(handled4).to(equal<Effected<never, void | User>>);
171181
expect(handled4.runSync()).to(equal<void | User>);

src/effected.ts

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,10 @@ export function effect<Name extends string | symbol, Resumable extends boolean =
6969
};
7070
});
7171
if (options && (options as any)._overrideFunctionName === false) return result as never;
72-
return renameFunction(result, typeof name === "string" ? name : name.description || "") as never;
72+
return renameFunction(
73+
result,
74+
typeof name === "string" ? name : name.toString().slice(7, -1) || "",
75+
) as never;
7376
}
7477

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

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

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

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

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

12431246
return serialize(x, 0);
12441247
};
1248+
1249+
// `console` is not standard in JavaScript. Though rare, it is possible that `console` is not
1250+
// available in some environments. We use a proxy to handle this case and ignore errors if `console`
1251+
// is not available.
1252+
const logger: {
1253+
debug(...data: unknown[]): void;
1254+
error(...data: unknown[]): void;
1255+
log(...data: unknown[]): void;
1256+
warn(...data: unknown[]): void;
1257+
} = new Proxy({} as never, {
1258+
get:
1259+
(_, prop) =>
1260+
(...args: unknown[]) => {
1261+
try {
1262+
// @ts-expect-error - Cannot find name 'console'
1263+
console[prop](...args);
1264+
} catch {
1265+
// Ignore
1266+
}
1267+
},
1268+
});

tsconfig.build.json

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55
"noEmit": false,
66
"outDir": "dist"
77
},
8-
"include": ["src"],
9-
"exclude": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"],
108
"tsc-alias": {
119
"resolveFullPaths": true
1210
}

tsconfig.json

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
{
22
"compilerOptions": {
3-
"target": "ES2020",
3+
"target": "ES2015",
44
"useDefineForClassFields": true,
5-
"lib": ["ES2020"],
6-
"module": "ESNext",
5+
"lib": ["ES2015", "ES2018.AsyncGenerator"],
6+
"module": "ES2015",
7+
"types": [],
78

89
/* Bundler mode */
910
"moduleResolution": "Bundler",
@@ -25,5 +26,6 @@
2526
/* Type */
2627
"declaration": true
2728
},
28-
"include": ["src"]
29+
"include": ["src"],
30+
"exclude": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"]
2931
}

tsconfig.test.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"extends": "./tsconfig.json",
3+
"compilerOptions": {
4+
"target": "ESNext",
5+
"useDefineForClassFields": true,
6+
"lib": ["ESNext"],
7+
"module": "ESNext"
8+
},
9+
"include": ["src/**/*.proof.ts", "src/**/*.spec.ts", "src/**/*.bench.ts"],
10+
"exclude": []
11+
}

0 commit comments

Comments
 (0)