@@ -276,6 +276,9 @@ export class AssignmentTests {
276276 @TestCase ( "Foo.staticLambdaProp" , "foo+staticLambdaProp" )
277277 @TestCase ( "foo.voidMethod" , "foo+voidMethod" )
278278 @TestCase ( "foo.voidLambdaProp" , "foo+voidLambdaProp" )
279+ @TestCase ( "s => s" , "foo" )
280+ @TestCase ( "function(s) { return s; }" , "foo" )
281+ @TestCase ( "function(this: void, s: string) { return s; }" , "foo" )
279282 @Test ( "Valid function argument" )
280283 public validFunctionArgument ( func : string , expectResult : string ) : void {
281284 const code = `${ AssignmentTests . funcAssignTestCode }
@@ -285,6 +288,25 @@ export class AssignmentTests {
285288 Expect ( result ) . toBe ( expectResult ) ;
286289 }
287290
291+ @TestCase ( "func" , "foo+func" )
292+ @TestCase ( "lambda" , "foo+lambda" )
293+ @TestCase ( "Foo.staticMethod" , "foo+staticMethod" )
294+ @TestCase ( "Foo.staticLambdaProp" , "foo+staticLambdaProp" )
295+ @TestCase ( "foo.voidMethod" , "foo+voidMethod" )
296+ @TestCase ( "foo.voidLambdaProp" , "foo+voidLambdaProp" )
297+ @TestCase ( "s => s" , "foo" )
298+ @TestCase ( "function(s) { return s; }" , "foo" )
299+ @TestCase ( "function(this: void, s: string) { return s; }" , "foo" )
300+ @Test ( "Valid function return" )
301+ public validFunctionReturn ( func : string , expectResult : string ) : void {
302+ const code = `${ AssignmentTests . funcAssignTestCode }
303+ function returnsFunc(): (s: string) => string { return ${ func } ; }
304+ const fn = returnsFunc();
305+ return fn("foo");` ;
306+ const result = util . transpileAndExecute ( code ) ;
307+ Expect ( result ) . toBe ( expectResult ) ;
308+ }
309+
288310 @TestCase ( "foo.method" , "foo.lambdaProp" , "foo+lambdaProp" )
289311 @TestCase ( "foo.method" , "s => s" , "foo" )
290312 @TestCase ( "foo.method" , "function(s) { return s; }" , "foo" )
@@ -358,17 +380,38 @@ export class AssignmentTests {
358380 @TestCase ( "Foo.thisStaticLambdaProp" , "foo+thisStaticLambdaProp" )
359381 @TestCase ( "thisFunc" , "foo+thisFunc" )
360382 @TestCase ( "thisLambda" , "foo+thisLambda" )
383+ @TestCase ( "s => s" , "foo" )
384+ @TestCase ( "function(s) { return s; }" , "foo" )
385+ @TestCase ( "function(this: Foo, s: string) { return s; }" , "foo" )
361386 @Test ( "Valid method argument" )
362387 public validMethodArgument ( func : string , expectResult : string ) : void {
363388 const code = `${ AssignmentTests . funcAssignTestCode }
364- const foo = new Foo();
365- function takesMethod(meth: (this: Foo, s: string) => s) { foo.method = meth; }
389+ function takesMethod(meth: (this: Foo, s: string) => string) { foo.method = meth; }
366390 takesMethod(${ func } );
367391 return foo.method("foo");` ;
368392 const result = util . transpileAndExecute ( code ) ;
369393 Expect ( result ) . toBe ( expectResult ) ;
370394 }
371395
396+ @TestCase ( "foo.method" , "foo+method" )
397+ @TestCase ( "foo.lambdaProp" , "foo+lambdaProp" )
398+ @TestCase ( "Foo.thisStaticMethod" , "foo+thisStaticMethod" )
399+ @TestCase ( "Foo.thisStaticLambdaProp" , "foo+thisStaticLambdaProp" )
400+ @TestCase ( "thisFunc" , "foo+thisFunc" )
401+ @TestCase ( "thisLambda" , "foo+thisLambda" )
402+ @TestCase ( "s => s" , "foo" )
403+ @TestCase ( "function(s) { return s; }" , "foo" )
404+ @TestCase ( "function(this: Foo, s: string) { return s; }" , "foo" )
405+ @Test ( "Valid method return" )
406+ public validMethodReturn ( func : string , expectResult : string ) : void {
407+ const code = `${ AssignmentTests . funcAssignTestCode }
408+ function returnMethod(): (this: Foo, s: string) => string { return ${ func } ; }
409+ foo.method = returnMethod();
410+ return foo.method("foo");` ;
411+ const result = util . transpileAndExecute ( code ) ;
412+ Expect ( result ) . toBe ( expectResult ) ;
413+ }
414+
372415 @TestCase ( "func" , "foo.method" )
373416 @TestCase ( "func" , "foo.lambdaProp" )
374417 @TestCase ( "func" , "Foo.thisStaticMethod" )
@@ -413,6 +456,7 @@ export class AssignmentTests {
413456 @TestCase ( "Foo.thisStaticLambdaProp" )
414457 @TestCase ( "thisFunc" )
415458 @TestCase ( "thisLambda" )
459+ @TestCase ( "function(this: Foo, s: string) { return s; }" )
416460 @Test ( "Invalid function argument" )
417461 public invalidFunctionArgument ( func : string ) : void {
418462 const code = `${ AssignmentTests . funcAssignTestCode }
@@ -423,6 +467,22 @@ export class AssignmentTests {
423467 "Unsupported conversion from method to function \"fn\". To fix, wrap the method in an arrow function." ) ;
424468 }
425469
470+ @TestCase ( "foo.method" )
471+ @TestCase ( "foo.lambdaProp" )
472+ @TestCase ( "Foo.thisStaticMethod" )
473+ @TestCase ( "Foo.thisStaticLambdaProp" )
474+ @TestCase ( "thisFunc" )
475+ @TestCase ( "thisLambda" )
476+ @TestCase ( "function(this: Foo, s: string) { return s; }" )
477+ @Test ( "Invalid function return" )
478+ public invalidFunctionReturn ( func : string ) : void {
479+ const code = `${ AssignmentTests . funcAssignTestCode }
480+ function returnsFunc(): (s: string) => string { return ${ func } ; }` ;
481+ Expect ( ( ) => util . transpileString ( code ) ) . toThrowError (
482+ TranspileError ,
483+ "Unsupported conversion from method to function. To fix, wrap the method in an arrow function." ) ;
484+ }
485+
426486 @TestCase ( "foo.method" , "func" )
427487 @TestCase ( "foo.method" , "lambda" )
428488 @TestCase ( "foo.method" , "Foo.staticMethod" )
@@ -470,7 +530,8 @@ export class AssignmentTests {
470530 const code = `${ AssignmentTests . funcAssignTestCode } ${ func } = ${ assignTo } ;` ;
471531 Expect ( ( ) => util . transpileString ( code ) ) . toThrowError (
472532 TranspileError ,
473- "Unsupported conversion from function to method. To fix, wrap the function in an arrow function." ) ;
533+ "Unsupported conversion from function to method. To fix, wrap the function in an arrow function or declare"
534+ + " the function with an explicit 'this' parameter." ) ;
474535 }
475536
476537 @TestCase ( "func" )
@@ -479,14 +540,33 @@ export class AssignmentTests {
479540 @TestCase ( "Foo.staticLambdaProp" )
480541 @TestCase ( "foo.voidMethod" )
481542 @TestCase ( "foo.voidLambdaProp" )
543+ @TestCase ( "function(this: void, s: string) { return s; }" )
482544 @Test ( "Invalid method argument" )
483545 public invalidMethodArgument ( func : string ) : void {
484546 const code = `${ AssignmentTests . funcAssignTestCode }
485547 declare function takesMethod(meth: (this: Foo, s: string) => s);
486548 takesMethod(${ func } );` ;
487549 Expect ( ( ) => util . transpileString ( code ) ) . toThrowError (
488550 TranspileError ,
489- "Unsupported conversion from function to method \"meth\". To fix, wrap the function in an arrow function." ) ;
551+ "Unsupported conversion from function to method \"meth\". To fix, wrap the function in an arrow function "
552+ + "or declare the function with an explicit 'this' parameter." ) ;
553+ }
554+
555+ @TestCase ( "func" )
556+ @TestCase ( "lambda" )
557+ @TestCase ( "Foo.staticMethod" )
558+ @TestCase ( "Foo.staticLambdaProp" )
559+ @TestCase ( "foo.voidMethod" )
560+ @TestCase ( "foo.voidLambdaProp" )
561+ @TestCase ( "function(this: void, s: string) { return s; }" )
562+ @Test ( "Invalid method return" )
563+ public invalidMethodReturn ( func : string ) : void {
564+ const code = `${ AssignmentTests . funcAssignTestCode }
565+ function returnsMethod(): (this: Foo, s: string) => s { return ${ func } ; }` ;
566+ Expect ( ( ) => util . transpileString ( code ) ) . toThrowError (
567+ TranspileError ,
568+ "Unsupported conversion from function to method. To fix, wrap the function in an arrow function "
569+ + "or declare the function with an explicit 'this' parameter." ) ;
490570 }
491571
492572 @Test ( "Interface method assignment" )
@@ -547,7 +627,8 @@ export class AssignmentTests {
547627 let [i, f]: [number, Meth] = getTuple();` ;
548628 Expect ( ( ) => util . transpileString ( code ) ) . toThrowError (
549629 TranspileError ,
550- "Unsupported conversion from function to method. To fix, wrap the function in an arrow function." ) ;
630+ "Unsupported conversion from function to method. To fix, wrap the function in an arrow function or declare"
631+ + " the function with an explicit 'this' parameter." ) ;
551632 }
552633
553634 @Test ( "Valid interface method assignment" )
0 commit comments