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