src/vs/base/common/assert.ts

91 LOC · 75 covered · 16 uncovered · 22 ranges · 20961 concepts · 10 introducers · 12741 tests

File neighbourhood

The centred file is linked to every concept that introduces one of its ranges, every test that runs code from the file, and the gray connector concepts standing between those tests and the file's own introducer concepts. Undirected links join concepts to every file where they introduce source and concepts to the tests they introduce; arrows show specialization between the displayed concepts and bridge only concepts omitted from this view. Concept colors match the source ranges below; connector concepts have no source color and are shown in gray.

Focused file, its introducer and connector concepts, their introduced files, and tests that run code from the file

In the embedded map, ordinary wheel input scrolls the page; use the visible controls to zoom and drag to pan. Open the full-screen map for canvas navigation: wheel pans, Ctrl/Command plus wheel zooms, and arrow keys pan when this region is focused. On touch screens, open the full-screen map to pan or pinch. If JavaScript or WebGL is unavailable, use the related-file, concept, and source links on this page.

Graph controls are ready.

Interactive rendering requires JavaScript and WebGL. Use the related-file, concept, and source links on this page while the interactive map is unavailable.

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