generated from garronej/ts-ci
-
Notifications
You must be signed in to change notification settings - Fork 6
/
assert.ts
44 lines (38 loc) 路 1.29 KB
/
assert.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
/* eslint-disable @typescript-eslint/no-unused-vars */
import { overwriteReadonlyProp } from "./lab/overwriteReadonlyProp.ts";
import { assertIsRefWrapper } from "./zz_internal/assertIsRefWrapper.ts";
/** @see <https://docs.tsafe.dev/assert#error-thrown> */
export class AssertionError extends Error {
constructor(msg: string | undefined) {
super(`Wrong assertion encountered` + (!msg ? "" : `: "${msg}"`));
Object.setPrototypeOf(this, new.target.prototype);
if (!this.stack) {
return;
}
try {
overwriteReadonlyProp(
this,
"stack",
this.stack
.split("\n")
.filter((...[, i]) => i !== 1 && i !== 2)
.join("\n"),
);
// eslint-disable-next-line no-empty
} catch {}
}
}
/** https://docs.tsafe.dev/assert */
// eslint-disable-next-line @typescript-eslint/no-explicit-any
export function assert<_T extends true>(condition?: any, msg?: string): asserts condition {
if (arguments.length === 0) {
condition = true;
}
if (assertIsRefWrapper.ref !== undefined) {
assertIsRefWrapper.ref = undefined;
return;
}
if (!condition) {
throw new AssertionError(msg);
}
}