@@ -6,6 +6,7 @@ import defaultDeprecate from './lib/deprecate';
6
6
import { isTesting } from './lib/testing' ;
7
7
import type { WarnFunc } from './lib/warn' ;
8
8
import _warn from './lib/warn' ;
9
+ import { assert , setAssert } from './lib/assert' ;
9
10
10
11
export { registerHandler as registerWarnHandler } from './lib/warn' ;
11
12
export {
@@ -27,10 +28,6 @@ export type DebugFunctionType =
27
28
| 'runInDebug'
28
29
| 'deprecateFunc' ;
29
30
30
- export interface AssertFunc {
31
- ( desc : string , condition : unknown ) : asserts condition ;
32
- ( desc : string ) : never ;
33
- }
34
31
export type DebugFunc = ( message : string ) => void ;
35
32
export type DebugSealFunc = ( obj : object ) => void ;
36
33
export type DebugFreezeFunc = ( obj : object ) => void ;
@@ -43,7 +40,7 @@ export type DeprecateFuncFunc = (
43
40
) => Function ;
44
41
45
42
export type GetDebugFunction = {
46
- ( type : 'assert' ) : AssertFunc ;
43
+ ( type : 'assert' ) : typeof assert ;
47
44
( type : 'info' ) : InfoFunc ;
48
45
( type : 'warn' ) : WarnFunc ;
49
46
( type : 'debug' ) : DebugFunc ;
@@ -55,7 +52,7 @@ export type GetDebugFunction = {
55
52
} ;
56
53
57
54
export type SetDebugFunction = {
58
- ( type : 'assert' , func : AssertFunc ) : AssertFunc ;
55
+ ( type : 'assert' , func : typeof assert ) : typeof assert ;
59
56
( type : 'info' , func : InfoFunc ) : InfoFunc ;
60
57
( type : 'warn' , func : WarnFunc ) : WarnFunc ;
61
58
( type : 'debug' , func : DebugFunc ) : DebugFunc ;
@@ -71,7 +68,6 @@ const noop = () => {};
71
68
72
69
// SAFETY: these casts are just straight-up lies, but the point is that they do
73
70
// not do anything in production builds.
74
- let assert : AssertFunc = noop as unknown as AssertFunc ;
75
71
let info : InfoFunc = noop ;
76
72
let warn : WarnFunc = noop ;
77
73
let debug : DebugFunc = noop ;
@@ -94,7 +90,7 @@ if (DEBUG) {
94
90
setDebugFunction = function ( type : DebugFunctionType , callback : Function ) {
95
91
switch ( type ) {
96
92
case 'assert' :
97
- return ( assert = callback as AssertFunc ) ;
93
+ return setAssert ( callback as typeof assert ) ;
98
94
case 'info' :
99
95
return ( info = callback as InfoFunc ) ;
100
96
case 'warn' :
@@ -143,51 +139,6 @@ if (DEBUG) {
143
139
*/
144
140
145
141
if ( DEBUG ) {
146
- /**
147
- Verify that a certain expectation is met, or throw a exception otherwise.
148
-
149
- This is useful for communicating assumptions in the code to other human
150
- readers as well as catching bugs that accidentally violates these
151
- expectations.
152
-
153
- Assertions are removed from production builds, so they can be freely added
154
- for documentation and debugging purposes without worries of incuring any
155
- performance penalty. However, because of that, they should not be used for
156
- checks that could reasonably fail during normal usage. Furthermore, care
157
- should be taken to avoid accidentally relying on side-effects produced from
158
- evaluating the condition itself, since the code will not run in production.
159
-
160
- ```javascript
161
- import { assert } from '@ember/debug';
162
-
163
- // Test for truthiness
164
- assert('Must pass a string', typeof str === 'string');
165
-
166
- // Fail unconditionally
167
- assert('This code path should never be run');
168
- ```
169
-
170
- @method assert
171
- @static
172
- @for @ember /debug
173
- @param {String } description Describes the expectation. This will become the
174
- text of the Error thrown if the assertion fails.
175
- @param {any } condition Must be truthy for the assertion to pass. If
176
- falsy, an exception will be thrown.
177
- @public
178
- @since 1.0.0
179
- */
180
- function assert ( desc : string ) : never ;
181
- function assert ( desc : string , test : unknown ) : asserts test ;
182
- // eslint-disable-next-line no-inner-declarations
183
- function assert ( desc : string , test ?: unknown ) : asserts test {
184
- if ( ! test ) {
185
- throw new Error ( `Assertion Failed: ${ desc } ` ) ;
186
- }
187
- }
188
-
189
- setDebugFunction ( 'assert' , assert ) ;
190
-
191
142
/**
192
143
Display a debug notice.
193
144
0 commit comments