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