@@ -152,11 +152,28 @@ describe('BabelAstFactory', () => {
152152 { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
153153 ] ,
154154 stmts ,
155+ null ,
155156 ) ;
156157 expect ( generate ( fn ) . code ) . toEqual (
157158 [ 'function foo(arg1, arg2: number) {' , ' x = 10;' , ' y = 20;' , '}' ] . join ( '\n' ) ,
158159 ) ;
159160 } ) ;
161+
162+ it ( 'should create a function declaration with a return type' , ( ) => {
163+ const stmts = statement . ast `{x = 10; y = 20;}` ;
164+ const fn = factory . createFunctionDeclaration (
165+ 'foo' ,
166+ [
167+ { name : 'arg1' , type : null } ,
168+ { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
169+ ] ,
170+ stmts ,
171+ factory . createBuiltInType ( 'boolean' ) ,
172+ ) ;
173+ expect ( generate ( fn ) . code ) . toEqual (
174+ [ 'function foo(arg1, arg2: number): boolean {' , ' x = 10;' , ' y = 20;' , '}' ] . join ( '\n' ) ,
175+ ) ;
176+ } ) ;
160177 } ) ;
161178
162179 describe ( 'createFunctionExpression()' , ( ) => {
@@ -169,13 +186,30 @@ describe('BabelAstFactory', () => {
169186 { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
170187 ] ,
171188 stmts ,
189+ null ,
172190 ) ;
173191 expect ( t . isStatement ( fn ) ) . toBe ( false ) ;
174192 expect ( generate ( fn ) . code ) . toEqual (
175193 [ 'function foo(arg1, arg2: number) {' , ' x = 10;' , ' y = 20;' , '}' ] . join ( '\n' ) ,
176194 ) ;
177195 } ) ;
178196
197+ it ( 'should create a function expression with a return type' , ( ) => {
198+ const stmts = statement . ast `{x = 10; y = 20;}` ;
199+ const fn = factory . createFunctionExpression (
200+ 'foo' ,
201+ [
202+ { name : 'arg1' , type : null } ,
203+ { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
204+ ] ,
205+ stmts ,
206+ factory . createBuiltInType ( 'boolean' ) ,
207+ ) ;
208+ expect ( generate ( fn ) . code ) . toEqual (
209+ [ 'function foo(arg1, arg2: number): boolean {' , ' x = 10;' , ' y = 20;' , '}' ] . join ( '\n' ) ,
210+ ) ;
211+ } ) ;
212+
179213 it ( 'should create an anonymous function expression node if the name is null' , ( ) => {
180214 const stmts = statement . ast `{x = 10; y = 20;}` ;
181215 const fn = factory . createFunctionExpression (
@@ -185,6 +219,7 @@ describe('BabelAstFactory', () => {
185219 { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
186220 ] ,
187221 stmts ,
222+ null ,
188223 ) ;
189224 expect ( generate ( fn ) . code ) . toEqual (
190225 [ 'function (arg1, arg2: number) {' , ' x = 10;' , ' y = 20;' , '}' ] . join ( '\n' ) ,
@@ -201,13 +236,27 @@ describe('BabelAstFactory', () => {
201236 { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
202237 ] ,
203238 expr ,
239+ null ,
204240 ) ;
205241 expect ( generate ( fn ) . code ) . toEqual ( '(arg1, arg2: number) => arg2 + arg1' ) ;
206242 } ) ;
207243
244+ it ( 'should create an arrow function with a return type' , ( ) => {
245+ const expr = expression . ast `arg2 + arg1` ;
246+ const fn = factory . createArrowFunctionExpression (
247+ [
248+ { name : 'arg1' , type : null } ,
249+ { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
250+ ] ,
251+ expr ,
252+ factory . createBuiltInType ( 'number' ) ,
253+ ) ;
254+ expect ( generate ( fn ) . code ) . toEqual ( '(arg1, arg2: number): number => arg2 + arg1' ) ;
255+ } ) ;
256+
208257 it ( 'should create an arrow function with an implicit return object literal' , ( ) => {
209258 const expr = expression . ast `{a: 1, b: 2}` ;
210- const fn = factory . createArrowFunctionExpression ( [ ] , expr ) ;
259+ const fn = factory . createArrowFunctionExpression ( [ ] , expr , null ) ;
211260 expect ( generate ( fn ) . code ) . toEqual ( [ '() => ({' , ' a: 1,' , ' b: 2' , '})' ] . join ( '\n' ) ) ;
212261 } ) ;
213262
@@ -219,6 +268,7 @@ describe('BabelAstFactory', () => {
219268 { name : 'arg2' , type : factory . createBuiltInType ( 'number' ) } ,
220269 ] ,
221270 stmts ,
271+ null ,
222272 ) ;
223273 expect ( generate ( fn ) . code ) . toEqual (
224274 [ '(arg1, arg2: number) => {' , ' x = 10;' , ' y = 20;' , ' return x + y;' , '}' ] . join ( '\n' ) ,
0 commit comments