-
-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathassert.ts
More file actions
49 lines (41 loc) · 1.43 KB
/
assert.ts
File metadata and controls
49 lines (41 loc) · 1.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
// import Logger from './logger';
import { DEBUG } from '@glimmer/env';
import { LOCAL_DEBUG } from '@glimmer/local-debug-flags';
export default function assert(test: unknown, msg: string): asserts test {
if (LOCAL_DEBUG) {
if (!test) {
throw new Error(msg || 'assertion failure');
}
}
}
export function debugAssert(test: unknown, msg: string | (() => string)): asserts test {
if (DEBUG && !test) {
throw new Error(typeof msg === 'string' ? msg : msg());
}
}
export function deprecate(desc: string) {
if (LOCAL_DEBUG) {
LOCAL_LOGGER.warn(`DEPRECATION: ${desc}`);
}
}
/**
* This constant exists to make it easier to differentiate normal logs from
* errant console.logs. LOCAL_LOGGER should only be used inside a
* LOCAL_TRACE_LOGGING check.
*FF
* It does not alleviate the need to check LOCAL_TRACE_LOGGING, which is used
* for stripping.
*/
export const LOCAL_LOGGER = console;
/**
* This constant exists to make it easier to differentiate normal logs from
* errant console.logs. LOGGER can be used outside of LOCAL_TRACE_LOGGING checks,
* and is meant to be used in the rare situation where a console.* call is
* actually appropriate.
*/
export const LOGGER = console;
export function assertNever(value: never, desc = 'unexpected unreachable branch'): never {
LOGGER.log('unreachable', value);
LOGGER.log(`${desc} :: ${JSON.stringify(value)} (${value})`);
throw new Error(`code reached unreachable`);
}