/*---------------------------------------------------------------------------------------------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}3031export function softAssertNever(value: never): void {32// no-op33}3435/**36* Asserts that a condition is `truthy`.37*38* @throws provided {@linkcode messageOrError} if the {@linkcode condition} is `falsy`.39*40* @param condition The condition to assert.41* @param messageOrError An error message or error object to throw if condition is `falsy`.42*/43export function assert(44condition: boolean,45messageOrError: string | Error = 'unexpected state',46): asserts condition {47if (!condition) {48// if error instance is provided, use it, otherwise create a new one49const errorToThrow = typeof messageOrError === 'string'50? new BugIndicatingError(`Assertion Failed: ${messageOrError}`)51: messageOrError;5253throw errorToThrow;54}55}5657/**58* Like assert, but doesn't throw.59*/60export function softAssert(condition: boolean, message = 'Soft Assertion Failed'): void {61if (!condition) {62onUnexpectedError(new BugIndicatingError(message));63}64}6566/**67* condition must be side-effect free!68*/69export function assertFn(condition: () => boolean): void {70if (!condition()) {71// eslint-disable-next-line no-debugger72debugger;73// Reevaluate `condition` again to make debugging easier74condition();75onUnexpectedError(new BugIndicatingError('Assertion Failed'));76}77}7879export function checkAdjacentItems<T>(items: readonly T[], predicate: (item1: T, item2: T) => boolean): boolean {80let i = 0;81while (i < items.length - 1) {82const a = items[i];83const b = items[i + 1];84if (!predicate(a, b)) {85return false;86}87i++;88}89return true;90}919293