Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
microsoft
GitHub Repository: microsoft/vscode
Path: blob/main/src/vs/base/common/assert.ts
3291 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
/**
33
* Asserts that a condition is `truthy`.
34
*
35
* @throws provided {@linkcode messageOrError} if the {@linkcode condition} is `falsy`.
36
*
37
* @param condition The condition to assert.
38
* @param messageOrError An error message or error object to throw if condition is `falsy`.
39
*/
40
export function assert(
41
condition: boolean,
42
messageOrError: string | Error = 'unexpected state',
43
): asserts condition {
44
if (!condition) {
45
// if error instance is provided, use it, otherwise create a new one
46
const errorToThrow = typeof messageOrError === 'string'
47
? new BugIndicatingError(`Assertion Failed: ${messageOrError}`)
48
: messageOrError;
49
50
throw errorToThrow;
51
}
52
}
53
54
/**
55
* Like assert, but doesn't throw.
56
*/
57
export function softAssert(condition: boolean, message = 'Soft Assertion Failed'): void {
58
if (!condition) {
59
onUnexpectedError(new BugIndicatingError(message));
60
}
61
}
62
63
/**
64
* condition must be side-effect free!
65
*/
66
export function assertFn(condition: () => boolean): void {
67
if (!condition()) {
68
// eslint-disable-next-line no-debugger
69
debugger;
70
// Reevaluate `condition` again to make debugging easier
71
condition();
72
onUnexpectedError(new BugIndicatingError('Assertion Failed'));
73
}
74
}
75
76
export function checkAdjacentItems<T>(items: readonly T[], predicate: (item1: T, item2: T) => boolean): boolean {
77
let i = 0;
78
while (i < items.length - 1) {
79
const a = items[i];
80
const b = items[i + 1];
81
if (!predicate(a, b)) {
82
return false;
83
}
84
i++;
85
}
86
return true;
87
}
88
89