Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/common/assert.ts
5221 views
1
/*---------------------------------------------------------------------------------------------
2
* Copyright (c) Microsoft Corporation. All rights reserved.
3
* Licensed under the MIT License. See License.txt in the project root for license information.
4
*--------------------------------------------------------------------------------------------*/
5
6
import { BugIndicatingError, onUnexpectedError } from './errors.js';
7
8
/**
9
* Throws an error with the provided message if the provided value does not evaluate to a true Javascript value.
10
*
11
* @deprecated Use `assert(...)` instead.
12
* This method is usually used like this:
13
* ```ts
14
* import * as assert from 'vs/base/common/assert';
15
* assert.ok(...);
16
* ```
17
*
18
* However, `assert` in that example is a user chosen name.
19
* There is no tooling for generating such an import statement.
20
* Thus, the `assert(...)` function should be used instead.
21
*/
22
export function ok(value?: unknown, message?: string) {
23
if (!value) {
24
throw new Error(message ? `Assertion failed (${message})` : 'Assertion Failed');
25
}
26
}
27
28
export function assertNever(value: never, message = 'Unreachable'): never {
29
throw new Error(message);
30
}
31
32
export function softAssertNever(value: never): void {
33
// no-op
34
}
35
36
/**
37
* Asserts that a condition is `truthy`.
38
*
39
* @throws provided {@linkcode messageOrError} if the {@linkcode condition} is `falsy`.
40
*
41
* @param condition The condition to assert.
42
* @param messageOrError An error message or error object to throw if condition is `falsy`.
43
*/
44
export function assert(
45
condition: boolean,
46
messageOrError: string | Error = 'unexpected state',
47
): asserts condition {
48
if (!condition) {
49
// if error instance is provided, use it, otherwise create a new one
50
const errorToThrow = typeof messageOrError === 'string'
51
? new BugIndicatingError(`Assertion Failed: ${messageOrError}`)
52
: messageOrError;
53
54
throw errorToThrow;
55
}
56
}
57
58
/**
59
* Like assert, but doesn't throw.
60
*/
61
export function softAssert(condition: boolean, message = 'Soft Assertion Failed'): void {
62
if (!condition) {
63
onUnexpectedError(new BugIndicatingError(message));
64
}
65
}
66
67
/**
68
* condition must be side-effect free!
69
*/
70
export function assertFn(condition: () => boolean): void {
71
if (!condition()) {
72
// eslint-disable-next-line no-debugger
73
debugger;
74
// Reevaluate `condition` again to make debugging easier
75
condition();
76
onUnexpectedError(new BugIndicatingError('Assertion Failed'));
77
}
78
}
79
80
export function checkAdjacentItems<T>(items: readonly T[], predicate: (item1: T, item2: T) => boolean): boolean {
81
let i = 0;
82
while (i < items.length - 1) {
83
const a = items[i];
84
const b = items[i + 1];
85
if (!predicate(a, b)) {
86
return false;
87
}
88
i++;
89
}
90
return true;
91
}
92
93