@@ -4,10 +4,12 @@ import { VisitorContext, PartialVisitorContext } from './visitor-context';
4
4
import { visitType , visitUndefinedOrType , visitShortCircuit } from './visitor-type-check' ;
5
5
import { sliceMapValues } from './utils' ;
6
6
7
+ type AssertMode = 'assert' | 'decorator' | 'boolean' ;
8
+
7
9
const objectIdentifier = ts . createIdentifier ( 'object' ) ;
8
10
const pathIdentifier = ts . createIdentifier ( 'path' ) ;
9
11
10
- function createArrowFunction ( type : ts . Type , optional : boolean , partialVisitorContext : PartialVisitorContext , isAssert : boolean ) {
12
+ function createArrowFunction ( type : ts . Type , optional : boolean , partialVisitorContext : PartialVisitorContext , assertMode : AssertMode ) {
11
13
const functionMap : VisitorContext [ 'functionMap' ] = new Map ( ) ;
12
14
const functionNames : VisitorContext [ 'functionNames' ] = new Set ( ) ;
13
15
const visitorContext = { ...partialVisitorContext , functionNames, functionMap } ;
@@ -21,6 +23,19 @@ function createArrowFunction(type: ts.Type, optional: boolean, partialVisitorCon
21
23
const errorIdentifier = ts . createIdentifier ( 'error' ) ;
22
24
const declarations = sliceMapValues ( functionMap ) ;
23
25
26
+ let finalStatement : ts . Statement ;
27
+ if ( assertMode === 'assert' ) {
28
+ finalStatement = ts . createIf (
29
+ errorIdentifier ,
30
+ ts . createThrow ( ts . createNew ( ts . createIdentifier ( 'Error' ) , undefined , [ errorIdentifier ] ) ) ,
31
+ ts . createReturn ( objectIdentifier )
32
+ ) ;
33
+ } else if ( assertMode === 'boolean' ) {
34
+ finalStatement = ts . createReturn ( ts . createStrictEquality ( errorIdentifier , ts . createNull ( ) ) ) ;
35
+ } else {
36
+ finalStatement = ts . createReturn ( errorIdentifier ) ;
37
+ }
38
+
24
39
return ts . createArrowFunction (
25
40
undefined ,
26
41
undefined ,
@@ -46,13 +61,7 @@ function createArrowFunction(type: ts.Type, optional: boolean, partialVisitorCon
46
61
[ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
47
62
[ ts . createVariableDeclaration ( errorIdentifier , undefined , ts . createCall ( ts . createIdentifier ( functionName ) , undefined , [ objectIdentifier ] ) ) ]
48
63
) ,
49
- isAssert
50
- ? ts . createIf (
51
- errorIdentifier ,
52
- ts . createThrow ( ts . createNew ( ts . createIdentifier ( 'Error' ) , undefined , [ errorIdentifier ] ) ) ,
53
- ts . createReturn ( objectIdentifier )
54
- )
55
- : ts . createReturn ( ts . createStrictEquality ( errorIdentifier , ts . createNull ( ) ) )
64
+ finalStatement
56
65
] )
57
66
) ;
58
67
}
@@ -66,7 +75,7 @@ function transformDecorator(node: ts.Decorator, parameterType: ts.Type, optional
66
75
&& path . resolve ( signature . declaration . getSourceFile ( ) . fileName ) === path . resolve ( path . join ( __dirname , '..' , '..' , 'index.d.ts' ) )
67
76
&& node . expression . arguments . length <= 1
68
77
) {
69
- const arrowFunction : ts . Expression = createArrowFunction ( parameterType , optional , visitorContext , false ) ;
78
+ const arrowFunction : ts . Expression = createArrowFunction ( parameterType , optional , visitorContext , 'decorator' ) ;
70
79
const expression = ts . updateCall (
71
80
node . expression ,
72
81
node . expression . expression ,
@@ -116,7 +125,7 @@ export function transformNode(node: ts.Node, visitorContext: PartialVisitorConte
116
125
throw new Error ( 'Calls to `is` and `assertType` should have one argument, calls to `createIs` and `createAssertType` should have no arguments.' ) ;
117
126
}
118
127
119
- const arrowFunction = createArrowFunction ( type , false , visitorContext , isAssert ) ;
128
+ const arrowFunction = createArrowFunction ( type , false , visitorContext , isAssert ? 'assert' : 'boolean' ) ;
120
129
121
130
if ( isCreate ) {
122
131
return arrowFunction ;
0 commit comments