1
1
namespace ts {
2
- const enum TestExpectation {
3
- Normal ,
4
- NoDiagnostic ,
5
- NoAction
6
- }
7
-
8
2
const libFile : TestFSWithWatch . File = {
9
3
path : "/a/lib/lib.d.ts" ,
10
4
content : `/// <reference no-default-lib="true"/>
@@ -261,14 +255,14 @@ interface String { charAt: any; }
261
255
interface Array<T> {}`
262
256
} ;
263
257
264
- function testConvertToAsyncFunction ( caption : string , text : string , baselineFolder : string , includeLib ?: boolean , expectedResult : TestExpectation = TestExpectation . Normal ) {
258
+ function testConvertToAsyncFunction ( caption : string , text : string , baselineFolder : string , includeLib ?: boolean , expectFailure = false ) {
265
259
const t = extractTest ( text ) ;
266
260
const selectionRange = t . ranges . get ( "selection" ) ! ;
267
261
if ( ! selectionRange ) {
268
262
throw new Error ( `Test ${ caption } does not specify selection range` ) ;
269
263
}
270
264
271
- const extensions = expectedResult === TestExpectation . Normal ? [ Extension . Ts , Extension . Js ] : [ Extension . Ts ] ;
265
+ const extensions = expectFailure ? [ Extension . Ts ] : [ Extension . Ts , Extension . Js ] ;
272
266
273
267
extensions . forEach ( extension =>
274
268
it ( `${ caption } [${ extension } ]` , ( ) => runBaseline ( extension ) ) ) ;
@@ -304,21 +298,21 @@ interface Array<T> {}`
304
298
const diagnostics = languageService . getSuggestionDiagnostics ( f . path ) ;
305
299
const diagnostic = find ( diagnostics , diagnostic => diagnostic . messageText === Diagnostics . This_may_be_converted_to_an_async_function . message &&
306
300
diagnostic . start === context . span . start && diagnostic . length === context . span . length ) ;
307
- if ( expectedResult === TestExpectation . NoDiagnostic ) {
301
+ if ( expectFailure ) {
308
302
assert . isUndefined ( diagnostic ) ;
309
- return ;
310
303
}
311
-
312
- assert . exists ( diagnostic ) ;
304
+ else {
305
+ assert . exists ( diagnostic ) ;
306
+ }
313
307
314
308
const actions = codefix . getFixes ( context ) ;
315
309
const action = find ( actions , action => action . description === Diagnostics . Convert_to_async_function . message ) ;
316
- if ( expectedResult === TestExpectation . NoAction ) {
317
- assert . isUndefined ( action ) ;
310
+ if ( expectFailure ) {
311
+ assert . isNotTrue ( action && action . changes . length > 0 ) ;
318
312
return ;
319
313
}
320
314
321
- assert . exists ( action ) ;
315
+ assert . isTrue ( action && action . changes . length > 0 ) ;
322
316
323
317
const data : string [ ] = [ ] ;
324
318
data . push ( `// ==ORIGINAL==` ) ;
@@ -481,7 +475,7 @@ function [#|f|]() {
481
475
}
482
476
`
483
477
) ;
484
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_NoSuggestion" , `
478
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_NoSuggestion" , `
485
479
function [#|f|]():Promise<Response> {
486
480
return fetch('https://typescriptlang.org');
487
481
}
@@ -495,7 +489,7 @@ function [#|f|]():Promise<void>{
495
489
}
496
490
`
497
491
) ;
498
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_NoSuggestionNoPromise" , `
492
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_NoSuggestionNoPromise" , `
499
493
function [#|f|]():void{
500
494
}
501
495
`
@@ -548,21 +542,21 @@ function [#|f|]():Promise<void> {
548
542
}
549
543
`
550
544
) ;
551
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_Finally1" , `
545
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_Finally1" , `
552
546
function [#|finallyTest|](): Promise<void> {
553
547
return fetch("https://typescriptlang.org").then(res => console.log(res)).catch(rej => console.log("error", rej)).finally(console.log("finally!"));
554
548
}
555
549
`
556
550
) ;
557
551
558
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_Finally2" , `
552
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_Finally2" , `
559
553
function [#|finallyTest|](): Promise<void> {
560
554
return fetch("https://typescriptlang.org").then(res => console.log(res)).finally(console.log("finally!"));
561
555
}
562
556
`
563
557
) ;
564
558
565
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_Finally3" , `
559
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_Finally3" , `
566
560
function [#|finallyTest|](): Promise<void> {
567
561
return fetch("https://typescriptlang.org").finally(console.log("finally!"));
568
562
}
@@ -590,22 +584,22 @@ function [#|innerPromise|](): Promise<string> {
590
584
`
591
585
) ;
592
586
593
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn01" , `
587
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn01" , `
594
588
function [#|f|]() {
595
589
let blob = fetch("https://typescriptlang.org").then(resp => console.log(resp));
596
590
return blob;
597
591
}
598
592
`
599
593
) ;
600
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn02" , `
594
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn02" , `
601
595
function [#|f|]() {
602
596
let blob = fetch("https://typescriptlang.org");
603
597
blob.then(resp => console.log(resp));
604
598
return blob;
605
599
}
606
600
`
607
601
) ;
608
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn03" , `
602
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn03" , `
609
603
function [#|f|]() {
610
604
let blob = fetch("https://typescriptlang.org")
611
605
let blob2 = blob.then(resp => console.log(resp));
@@ -618,7 +612,7 @@ function err (rej) {
618
612
}
619
613
`
620
614
) ;
621
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn04" , `
615
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn04" , `
622
616
function [#|f|]() {
623
617
var blob = fetch("https://typescriptlang.org").then(res => console.log(res)), blob2 = fetch("https://microsoft.com").then(res => res.ok).catch(err);
624
618
return blob;
@@ -629,7 +623,7 @@ function err (rej) {
629
623
`
630
624
) ;
631
625
632
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn05" , `
626
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn05" , `
633
627
function [#|f|]() {
634
628
var blob = fetch("https://typescriptlang.org").then(res => console.log(res));
635
629
blob.then(x => x);
@@ -638,15 +632,15 @@ function [#|f|]() {
638
632
`
639
633
) ;
640
634
641
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn06" , `
635
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn06" , `
642
636
function [#|f|]() {
643
637
var blob = fetch("https://typescriptlang.org");
644
638
return blob;
645
639
}
646
640
`
647
641
) ;
648
642
649
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn07" , `
643
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn07" , `
650
644
function [#|f|]() {
651
645
let blob = fetch("https://typescriptlang.org");
652
646
let blob2 = fetch("https://microsoft.com");
@@ -657,7 +651,7 @@ function [#|f|]() {
657
651
`
658
652
) ;
659
653
660
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn08" , `
654
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn08" , `
661
655
function [#|f|]() {
662
656
let blob = fetch("https://typescriptlang.org");
663
657
if (!blob.ok){
@@ -669,7 +663,7 @@ function [#|f|]() {
669
663
`
670
664
) ;
671
665
672
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn09" , `
666
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn09" , `
673
667
function [#|f|]() {
674
668
let blob3;
675
669
let blob = fetch("https://typescriptlang.org");
@@ -683,7 +677,7 @@ function [#|f|]() {
683
677
) ;
684
678
685
679
686
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn10" , `
680
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn10" , `
687
681
function [#|f|]() {
688
682
let blob3;
689
683
let blob = fetch("https://typescriptlang.org");
@@ -697,7 +691,7 @@ function [#|f|]() {
697
691
`
698
692
) ;
699
693
700
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_VarReturn11" , `
694
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_VarReturn11" , `
701
695
function [#|f|]() {
702
696
let blob;
703
697
return blob;
@@ -707,7 +701,7 @@ function [#|f|]() {
707
701
708
702
709
703
710
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_Param1" , `
704
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_Param1" , `
711
705
function [#|f|]() {
712
706
return my_print(fetch("https://typescriptlang.org").then(res => console.log(res)));
713
707
}
@@ -764,7 +758,7 @@ function [#|f|](): Promise<void> {
764
758
) ;
765
759
766
760
767
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_SeperateLines" , `
761
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_SeperateLines" , `
768
762
function [#|f|](): Promise<string> {
769
763
var blob = fetch("https://typescriptlang.org")
770
764
blob.then(resp => {
@@ -1027,7 +1021,7 @@ function [#|f|]() {
1027
1021
}
1028
1022
1029
1023
` ) ;
1030
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_CatchFollowedByCall" , `
1024
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_CatchFollowedByCall" , `
1031
1025
function [#|f|](){
1032
1026
return fetch("https://typescriptlang.org").then(res).catch(rej).toString();
1033
1027
}
@@ -1091,7 +1085,7 @@ function [#|f|]() {
1091
1085
`
1092
1086
) ;
1093
1087
1094
- _testConvertToAsyncFunctionNoDiagnostic ( "convertToAsyncFunction_NestedFunctionWrongLocation" , `
1088
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_NestedFunctionWrongLocation" , `
1095
1089
function [#|f|]() {
1096
1090
function fn2(){
1097
1091
function fn3(){
@@ -1140,13 +1134,13 @@ const [#|foo|] = function () {
1140
1134
}
1141
1135
` ) ;
1142
1136
1143
- _testConvertToAsyncFunctionNoAction ( "convertToAsyncFunction_thenArgumentNotFunction" , `
1137
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_thenArgumentNotFunction" , `
1144
1138
function [#|f|]() {
1145
1139
return Promise.resolve().then(f ? (x => x) : (y => y));
1146
1140
}
1147
1141
` ) ;
1148
1142
1149
- _testConvertToAsyncFunctionNoAction ( "convertToAsyncFunction_thenArgumentNotFunctionNotLastInChain" , `
1143
+ _testConvertToAsyncFunctionFailed ( "convertToAsyncFunction_thenArgumentNotFunctionNotLastInChain" , `
1150
1144
function [#|f|]() {
1151
1145
return Promise.resolve().then(f ? (x => x) : (y => y)).then(q => q);
1152
1146
}
@@ -1159,11 +1153,7 @@ function [#|f|]() {
1159
1153
testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true ) ;
1160
1154
}
1161
1155
1162
- function _testConvertToAsyncFunctionNoDiagnostic ( caption : string , text : string ) {
1163
- testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true , TestExpectation . NoDiagnostic ) ;
1164
- }
1165
-
1166
- function _testConvertToAsyncFunctionNoAction ( caption : string , text : string ) {
1167
- testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true , TestExpectation . NoAction ) ;
1156
+ function _testConvertToAsyncFunctionFailed ( caption : string , text : string ) {
1157
+ testConvertToAsyncFunction ( caption , text , "convertToAsyncFunction" , /*includeLib*/ true , /*expectFailure*/ true ) ;
1168
1158
}
1169
1159
}
0 commit comments