@@ -4,12 +4,10 @@ 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
-
9
7
const objectIdentifier = ts . createIdentifier ( 'object' ) ;
10
8
const pathIdentifier = ts . createIdentifier ( 'path' ) ;
11
9
12
- function createArrowFunction ( type : ts . Type , optional : boolean , partialVisitorContext : PartialVisitorContext , assertMode : AssertMode ) {
10
+ function createArrowFunction ( type : ts . Type , optional : boolean , partialVisitorContext : PartialVisitorContext ) {
13
11
const functionMap : VisitorContext [ 'functionMap' ] = new Map ( ) ;
14
12
const functionNames : VisitorContext [ 'functionNames' ] = new Set ( ) ;
15
13
const visitorContext = { ...partialVisitorContext , functionNames, functionMap } ;
@@ -23,19 +21,6 @@ function createArrowFunction(type: ts.Type, optional: boolean, partialVisitorCon
23
21
const errorIdentifier = ts . createIdentifier ( 'error' ) ;
24
22
const declarations = sliceMapValues ( functionMap ) ;
25
23
26
- let finalStatement : ts . Statement ;
<
10000
/tr>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
-
39
24
return ts . createArrowFunction (
40
25
undefined ,
41
26
undefined ,
@@ -61,7 +46,7 @@ function createArrowFunction(type: ts.Type, optional: boolean, partialVisitorCon
61
46
[ ts . createModifier ( ts . SyntaxKind . ConstKeyword ) ] ,
62
47
[ ts . createVariableDeclaration ( errorIdentifier , undefined , ts . createCall ( ts . createIdentifier ( functionName ) , undefined , [ objectIdentifier ] ) ) ]
63
48
) ,
64
- finalStatement
49
+ ts . createReturn ( errorIdentifier )
65
50
] )
66
51
) ;
67
52
}
@@ -75,7 +60,7 @@ function transformDecorator(node: ts.Decorator, parameterType: ts.Type, optional
75
60
&& path . resolve ( signature . declaration . getSourceFile ( ) . fileName ) === path . resolve ( path . join ( __dirname , '..' , '..' , 'index.d.ts' ) )
76
61
&& node . expression . arguments . length <= 1
77
62
) {
78
- const arrowFunction : ts . Expression = createArrowFunction ( parameterType , optional , visitorContext , 'decorator' ) ;
63
+ const arrowFunction : ts . Expression = createArrowFunction ( parameterType , optional , visitorContext ) ;
79
64
const expression = ts . updateCall (
80
65
node . expression ,
81
66
node . expression . expression ,
@@ -115,27 +100,19 @@ export function transformNode(node: ts.Node, visitorContext: PartialVisitorConte
115
100
&& node . typeArguments !== undefined
116
101
&& node . typeArguments . length === 1
117
102
) {
118
- const name = visitorContext . checker . getTypeAtLocation ( signature . declaration ) . symbol . name ;
119
- const isCreate = name === 'createIs' || name === 'createAssertType' ;
120
- const isAssert = name === 'assertType' || name === 'createAssertType' ;
121
103
const typeArgument = node . typeArguments [ 0 ] ;
122
104
const type = visitorContext . checker . getTypeFromTypeNode ( typeArgument ) ;
105
+ const arrowFunction = createArrowFunction ( type , false , visitorContext ) ;
123
106
124
- if ( ! ( isCreate && node . arguments . length === 0 ) && ! ( ! isCreate && node . arguments . length === 1 ) ) {
125
- throw new Error ( 'Calls to `is` and `assertType` should have one argument, calls to `createIs` and `createAssertType` should have no arguments.' ) ;
126
- }
127
-
128
- const arrowFunction = createArrowFunction ( type , false , visitorContext , isAssert ? 'assert' : 'boolean' ) ;
129
-
130
- if ( isCreate ) {
131
- return arrowFunction ;
132
- } else {
133
- return ts . createCall (
134
- arrowFunction ,
135
- undefined ,
136
- node . arguments
137
- ) ;
138
- }
107
+ return ts . updateCall (
108
+ node ,
109
+ node . expression ,
110
+ node . typeArguments ,
111
+ [
112
+ ...node . arguments ,
113
+ arrowFunction
114
+ ]
115
+ ) ;
139
116
}
140
117
}
141
118
return node ;
0 commit comments