Skip to content

Commit

Permalink
fix: log more potential bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
RebeccaStevens committed Jan 8, 2024
1 parent 8777513 commit 3348e90
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 11 deletions.
7 changes: 5 additions & 2 deletions src/preprocessor.ts
Expand Up @@ -8,7 +8,7 @@ import {
type Scope,
type ScopeToIdentifiersToRemoveMap,
} from "./types";
import { isNotNull } from "./utils";
import { assertNeverAndLog, isNotNull } from "./utils";

/**
* Walk the ast to gather all the infomation we need to process the code.
Expand Down Expand Up @@ -167,7 +167,10 @@ function createVisitor<State>(
}

default: {
assertNever(`Unexpected variable declarator "${element.type}"`); // Shouldn't happen... probably. Needs code example if it can.
// Shouldn't happen... probably. Needs code example if it can.
assertNeverAndLog(
`Unexpected variable declarator type "${element.type}"`,
);
}
}
} while (stack.length > 0);
Expand Down
13 changes: 4 additions & 9 deletions src/process.ts
Expand Up @@ -18,6 +18,7 @@ import {
type ReadonlyScopeToIdentifiersToRemoveMap,
type Scope,
} from "./types";
import { assertNeverAndLog } from "./utils";

/**
* Processes the provided AST to remove assert statements and assertion imports.
Expand Down Expand Up @@ -199,15 +200,9 @@ function removeNodeSmart(
}
}

assertNever(`Potentially unsafe node removal - ${scope.node.type}`);

/* v8 ignore next 6 */
// @ts-expect-error Unreachable code.
console.error(
`Potentially unsafe node removal - ${scope.node.type}. Please report this issue.`,
);
// @ts-expect-error Unreachable code.
return void removeNode(code, removedNodes, scope.node);
/* v8 ignore next 2 */
removeNode(code, removedNodes, scope.node);
assertNeverAndLog(`Potentially unsafe node removal - ${scope.node.type}.`);
}

/**
Expand Down
14 changes: 14 additions & 0 deletions src/utils.ts
@@ -1,3 +1,5 @@
import { fail as assertNever } from "node:assert/strict";

/**
* Checks if the given element is not null.
*
Expand All @@ -7,3 +9,15 @@
export function isNotNull<T>(element: T | null): element is T {
return element !== null;
}

/**
* Log the message and fail if not in production.
*
* @param message - The message to log.
*/
export function assertNeverAndLog(message: string): never {
console.error(
`Deassert: ${message}\nPlease report this issue at https://github.com/RebeccaStevens/deassert/issues`,
);
assertNever();
}

0 comments on commit 3348e90

Please sign in to comment.