Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertions: optional debugger #34

Merged
merged 2 commits into from Jul 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/shiny-flowers-fold.md
@@ -0,0 +1,5 @@
---
'emery': minor
---

Assertions: support opt-out of debugger
16 changes: 10 additions & 6 deletions src/assertions.ts
Expand Up @@ -9,9 +9,13 @@
// intentional design decision. The goal is to promote consideration from
// consumers when dealing with potentially ambiguous conditions like `0` or
// `''`, which can introduce "subtle" bugs.
export function assert(condition: boolean, message = 'Assert failed'): asserts condition {
export function assert(
condition: boolean,
message = 'Assert failed',
options = { debug: true },
): asserts condition {
if (!condition) {
developmentDebugger();
developmentDebugger(options.debug);
throw new TypeError(message);
}
}
Expand All @@ -22,14 +26,14 @@ export function assert(condition: boolean, message = 'Assert failed'): asserts c
* @throws always
* @returns void
*/
export function assertNever(condition: never): never {
developmentDebugger();
export function assertNever(condition: never, options = { debug: true }): never {
developmentDebugger(options.debug);
throw new Error(`Unexpected call to assertNever: '${condition}'`);
}

/** Pause execution in development to aid debugging. */
function developmentDebugger() {
if (process.env.NODE_ENV === 'production') {
function developmentDebugger(enabled?: boolean): void {
if (!enabled || process.env.NODE_ENV === 'production') {
return;
}

Expand Down