/*---------------------------------------------------------------------------------------------1* Copyright (c) Microsoft Corporation. All rights reserved.2* Licensed under the MIT License. See License.txt in the project root for license information.3*--------------------------------------------------------------------------------------------*/45import { BugIndicatingError, onUnexpectedError } from './errors.js';67/**8* Throws an error with the provided message if the provided value does not evaluate to a true Javascript value.9*10* @deprecated Use `assert(...)` instead.11* This method is usually used like this:12* ```ts13* import * as assert from 'vs/base/common/assert';14* assert.ok(...);15* ```16*17* However, `assert` in that example is a user chosen name.18* There is no tooling for generating such an import statement.19* Thus, the `assert(...)` function should be used instead.20*/21export function ok(value?: unknown, message?: string) {22if (!value) {23throw new Error(message ? `Assertion failed (${message})` : 'Assertion Failed');24}25}2627export function assertNever(value: never, message = 'Unreachable'): never {28throw new Error(message);29}3031/**32* Asserts that a condition is `truthy`.33*34* @throws provided {@linkcode messageOrError} if the {@linkcode condition} is `falsy`.35*36* @param condition The condition to assert.37* @param messageOrError An error message or error object to throw if condition is `falsy`.38*/39export function assert(40condition: boolean,41messageOrError: string | Error = 'unexpected state',42): asserts condition {43if (!condition) {44// if error instance is provided, use it, otherwise create a new one45const errorToThrow = typeof messageOrError === 'string'46? new BugIndicatingError(`Assertion Failed: ${messageOrError}`)47: messageOrError;4849throw errorToThrow;50}51}5253/**54* Like assert, but doesn't throw.55*/56export function softAssert(condition: boolean, message = 'Soft Assertion Failed'): void {57if (!condition) {58onUnexpectedError(new BugIndicatingError(message));59}60}6162/**63* condition must be side-effect free!64*/65export function assertFn(condition: () => boolean): void {66if (!condition()) {67// eslint-disable-next-line no-debugger68debugger;69// Reevaluate `condition` again to make debugging easier70condition();71onUnexpectedError(new BugIndicatingError('Assertion Failed'));72}73}7475export function checkAdjacentItems<T>(items: readonly T[], predicate: (item1: T, item2: T) => boolean): boolean {76let i = 0;77while (i < items.length - 1) {78const a = items[i];79const b = items[i + 1];80if (!predicate(a, b)) {81return false;82}83i++;84}85return true;86}878889