@@ -80,20 +80,47 @@ namespace ts.tscWatch {
80
80
checkOutputDoesNotContain ( host , expectedNonAffectedFiles ) ;
81
81
}
82
82
83
- function checkOutputErrors ( host : WatchedSystem , errors : ReadonlyArray < Diagnostic > , isInitial ?: true , skipWaiting ?: true ) {
83
+ enum ExpectedOutputErrorsPosition {
84
+ BeforeCompilationStarts ,
85
+ AfterCompilationStarting ,
86
+ AfterFileChangeDetected
87
+ }
88
+
89
+ function checkOutputErrors (
90
+ host : WatchedSystem ,
91
+ errors : ReadonlyArray < Diagnostic > ,
92
+ errorsPosition : ExpectedOutputErrorsPosition ,
93
+ skipWaiting ?: true
94
+ ) {
84
95
const outputs = host . getOutput ( ) ;
85
- const expectedOutputCount = ( isInitial ? 0 : 1 ) + errors . length + ( skipWaiting ? 0 : 1 ) ;
96
+ const expectedOutputCount = errors . length + ( skipWaiting ? 0 : 1 ) + 1 ;
86
97
assert . equal ( outputs . length , expectedOutputCount , "Outputs = " + outputs . toString ( ) ) ;
87
- let index = 0 ;
88
- if ( ! isInitial ) {
89
- assertWatchDiagnosticAt ( host , index , Diagnostics . File_change_detected_Starting_incremental_compilation ) ;
90
- index ++ ;
98
+ let index : number ;
99
+
100
+ switch ( errorsPosition ) {
101
+ case ExpectedOutputErrorsPosition . AfterCompilationStarting :
102
+ assertWatchDiagnosticAt ( host , 0 , Diagnostics . Starting_compilation_in_watch_mode ) ;
103
+ index = 1 ;
104
+ break ;
105
+ case ExpectedOutputErrorsPosition . AfterFileChangeDetected :
106
+ assertWatchDiagnosticAt ( host , 0 , Diagnostics . File_change_detected_Starting_incremental_compilation ) ;
107
+ index = 1 ;
108
+ break ;
109
+ case ExpectedOutputErrorsPosition . BeforeCompilationStarts :
110
+ assertWatchDiagnosticAt ( host , errors . length , Diagnostics . Starting_compilation_in_watch_mode ) ;
111
+ index = 0 ;
112
+ break ;
91
113
}
114
+
92
115
forEach ( errors , error => {
93
116
assertDiagnosticAt ( host , index , error ) ;
94
117
index ++ ;
95
118
} ) ;
96
119
if ( ! skipWaiting ) {
120
+ if ( errorsPosition === ExpectedOutputErrorsPosition . BeforeCompilationStarts ) {
121
+ assertWatchDiagnosticAt ( host , index , ts . Diagnostics . Starting_compilation_in_watch_mode ) ;
122
+ index += 1 ;
123
+ }
97
124
assertWatchDiagnosticAt ( host , index , Diagnostics . Compilation_complete_Watching_for_file_changes ) ;
98
125
}
99
126
host . clearOutput ( ) ;
@@ -333,13 +360,13 @@ namespace ts.tscWatch {
333
360
checkOutputErrors ( host , [
334
361
getDiagnosticOfFileFromProgram ( watch ( ) , file1 . path , file1 . content . indexOf ( commonFile2Name ) , commonFile2Name . length , Diagnostics . File_0_not_found , commonFile2 . path ) ,
335
362
getDiagnosticOfFileFromProgram ( watch ( ) , file1 . path , file1 . content . indexOf ( "y" ) , 1 , Diagnostics . Cannot_find_name_0 , "y" )
336
- ] , /*isInitial */ true ) ;
363
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
337
364
338
365
host . reloadFS ( [ file1 , commonFile2 , libFile ] ) ;
339
366
host . runQueuedTimeoutCallbacks ( ) ;
340
367
checkProgramRootFiles ( watch ( ) , [ file1 . path ] ) ;
341
368
checkProgramActualFiles ( watch ( ) , [ file1 . path , libFile . path , commonFile2 . path ] ) ;
342
- checkOutputErrors ( host , emptyArray ) ;
369
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
343
370
} ) ;
344
371
345
372
it ( "should reflect change in config file" , ( ) => {
@@ -667,15 +694,15 @@ namespace ts.tscWatch {
667
694
const watch = createWatchModeWithConfigFile ( config . path , host ) ;
668
695
669
696
checkProgramActualFiles ( watch ( ) , [ file1 . path , file2 . path , libFile . path ] ) ;
670
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
697
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
671
698
672
699
host . reloadFS ( [ file1 , file2 , libFile ] ) ;
673
700
host . checkTimeoutQueueLengthAndRun ( 1 ) ;
674
701
675
702
assert . equal ( host . exitCode , ExitStatus . DiagnosticsPresent_OutputsSkipped ) ;
676
703
checkOutputErrors ( host , [
677
704
getDiagnosticWithoutFile ( Diagnostics . File_0_not_found , config . path )
678
- ] , /*isInitial */ undefined , /*skipWaiting*/ true ) ;
705
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterFileChangeDetected , /*skipWaiting*/ true ) ;
679
706
} ) ;
680
707
681
708
it ( "Proper errors: document is not contained in project" , ( ) => {
@@ -778,7 +805,7 @@ namespace ts.tscWatch {
778
805
} ;
779
806
const host = createWatchedSystem ( [ moduleFile , file1 , libFile ] ) ;
780
807
const watch = createWatchModeWithoutConfigFile ( [ file1 . path ] , host ) ;
781
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
808
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
782
809
783
810
const moduleFileOldPath = moduleFile . path ;
784
811
const moduleFileNewPath = "/a/b/moduleFile1.ts" ;
@@ -787,12 +814,12 @@ namespace ts.tscWatch {
787
814
host . runQueuedTimeoutCallbacks ( ) ;
788
815
checkOutputErrors ( host , [
789
816
getDiagnosticModuleNotFoundOfFile ( watch ( ) , file1 , "./moduleFile" )
790
- ] ) ;
817
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
791
818
792
819
moduleFile . path = moduleFileOldPath ;
793
820
host . reloadFS ( [ moduleFile , file1 , libFile ] ) ;
794
821
host . runQueuedTimeoutCallbacks ( ) ;
795
- checkOutputErrors ( host , emptyArray ) ;
822
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
796
823
} ) ;
797
824
798
825
it ( "rename a module file and rename back should restore the states for configured projects" , ( ) => {
@@ -810,7 +837,7 @@ namespace ts.tscWatch {
810
837
} ;
811
838
const host = createWatchedSystem ( [ moduleFile , file1 , configFile , libFile ] ) ;
812
839
const watch = createWatchModeWithConfigFile ( configFile . path , host ) ;
813
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
840
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
814
841
815
842
const moduleFileOldPath = moduleFile . path ;
816
843
const moduleFileNewPath = "/a/b/moduleFile1.ts" ;
@@ -819,12 +846,12 @@ namespace ts.tscWatch {
819
846
host . runQueuedTimeoutCallbacks ( ) ;
820
847
checkOutputErrors ( host , [
821
848
getDiagnosticModuleNotFoundOfFile ( watch ( ) , file1 , "./moduleFile" )
822
- ] ) ;
849
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
823
850
824
851
moduleFile . path = moduleFileOldPath ;
825
852
host . reloadFS ( [ moduleFile , file1 , configFile , libFile ] ) ;
826
853
host . runQueuedTimeoutCallbacks ( ) ;
827
- checkOutputErrors ( host , emptyArray ) ;
854
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
828
855
} ) ;
829
856
830
857
it ( "types should load from config file path if config exists" , ( ) => {
@@ -863,11 +890,11 @@ namespace ts.tscWatch {
863
890
864
891
checkOutputErrors ( host , [
865
892
getDiagnosticModuleNotFoundOfFile ( watch ( ) , file1 , "./moduleFile" )
866
- ] , /*isInitial */ true ) ;
893
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
867
894
868
895
host . reloadFS ( [ file1 , moduleFile , libFile ] ) ;
869
896
host . runQueuedTimeoutCallbacks ( ) ;
870
- checkOutputErrors ( host , emptyArray ) ;
897
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
871
898
} ) ;
872
899
873
900
it ( "Configure file diagnostics events are generated when the config file has errors" , ( ) => {
@@ -890,7 +917,7 @@ namespace ts.tscWatch {
890
917
checkOutputErrors ( host , [
891
918
getUnknownCompilerOption ( watch ( ) , configFile , "foo" ) ,
892
919
getUnknownCompilerOption ( watch ( ) , configFile , "allowJS" )
893
- ] , /*isInitial */ true ) ;
920
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . BeforeCompilationStarts ) ;
894
921
} ) ;
895
922
896
923
it ( "If config file doesnt have errors, they are not reported" , ( ) => {
@@ -907,7 +934,7 @@ namespace ts.tscWatch {
907
934
908
935
const host = createWatchedSystem ( [ file , configFile , libFile ] ) ;
909
936
createWatchModeWithConfigFile ( configFile . path , host ) ;
910
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
937
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
911
938
} ) ;
912
939
913
940
it ( "Reports errors when the config file changes" , ( ) => {
@@ -924,7 +951,7 @@ namespace ts.tscWatch {
924
951
925
952
const host = createWatchedSystem ( [ file , configFile , libFile ] ) ;
926
953
const watch = createWatchModeWithConfigFile ( configFile . path , host ) ;
927
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
954
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
928
955
929
956
configFile . content = `{
930
957
"compilerOptions": {
@@ -935,14 +962,14 @@ namespace ts.tscWatch {
935
962
host . runQueuedTimeoutCallbacks ( ) ;
936
963
checkOutputErrors ( host , [
937
964
getUnknownCompilerOption ( watch ( ) , configFile , "haha" )
938
- ] ) ;
965
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
939
966
940
967
configFile . content = `{
941
968
"compilerOptions": {}
942
969
}` ;
943
970
host . reloadFS ( [ file , configFile , libFile ] ) ;
944
971
host . runQueuedTimeoutCallbacks ( ) ;
945
- checkOutputErrors ( host , emptyArray ) ;
972
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
946
973
} ) ;
947
974
948
975
it ( "non-existing directories listed in config file input array should be tolerated without crashing the server" , ( ) => {
@@ -1030,13 +1057,13 @@ namespace ts.tscWatch {
1030
1057
getDiagnosticOfFile ( watch ( ) . getCompilerOptions ( ) . configFile , configFile . content . indexOf ( '"declaration"' ) , '"declaration"' . length , Diagnostics . Option_0_cannot_be_specified_with_option_1 , "allowJs" , "declaration" )
1031
1058
] ;
1032
1059
const intialErrors = errors ( ) ;
1033
- checkOutputErrors ( host , intialErrors , /*isInitial */ true ) ;
1060
+ checkOutputErrors ( host , intialErrors , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1034
1061
1035
1062
configFile . content = configFileContentWithoutCommentLine ;
1036
1063
host . reloadFS ( files ) ;
1037
1064
host . runQueuedTimeoutCallbacks ( ) ;
1038
1065
const nowErrors = errors ( ) ;
1039
- checkOutputErrors ( host , nowErrors ) ;
1066
+ checkOutputErrors ( host , nowErrors , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1040
1067
assert . equal ( nowErrors [ 0 ] . start , intialErrors [ 0 ] . start - configFileContentComment . length ) ;
1041
1068
assert . equal ( nowErrors [ 1 ] . start , intialErrors [ 1 ] . start - configFileContentComment . length ) ;
1042
1069
} ) ;
@@ -1667,7 +1694,7 @@ namespace ts.tscWatch {
1667
1694
const cannotFindFoo = getDiagnosticOfFileFromProgram ( watch ( ) , imported . path , imported . content . indexOf ( "foo" ) , "foo" . length , Diagnostics . Cannot_find_name_0 , "foo" ) ;
1668
1695
1669
1696
// ensure that imported file was found
1670
- checkOutputErrors ( host , [ f1IsNotModule , cannotFindFoo ] , /*isInitial */ true ) ;
1697
+ checkOutputErrors ( host , [ f1IsNotModule , cannotFindFoo ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1671
1698
1672
1699
const originalFileExists = host . fileExists ;
1673
1700
{
@@ -1687,7 +1714,7 @@ namespace ts.tscWatch {
1687
1714
f1IsNotModule ,
1688
1715
getDiagnosticOfFileFromProgram ( watch ( ) , root . path , newContent . indexOf ( "var x" ) + "var " . length , "x" . length , Diagnostics . Type_0_is_not_assignable_to_type_1 , 1 , "string" ) ,
1689
1716
cannotFindFoo
1690
- ] ) ;
1717
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1691
1718
}
1692
1719
{
1693
1720
let fileExistsIsCalled = false ;
@@ -1709,7 +1736,7 @@ namespace ts.tscWatch {
1709
1736
// ensure file has correct number of errors after edit
1710
1737
checkOutputErrors ( host , [
1711
1738
getDiagnosticModuleNotFoundOfFile ( watch ( ) , root , "f2" )
1712
- ] ) ;
1739
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1713
1740
1714
1741
assert ( fileExistsIsCalled ) ;
1715
1742
}
@@ -1730,7 +1757,7 @@ namespace ts.tscWatch {
1730
1757
host . reloadFS ( files ) ;
1731
1758
host . runQueuedTimeoutCallbacks ( ) ;
1732
1759
1733
- checkOutputErrors ( host , [ f1IsNotModule , cannotFindFoo ] ) ;
1760
+ checkOutputErrors ( host , [ f1IsNotModule , cannotFindFoo ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1734
1761
assert ( fileExistsCalled ) ;
1735
1762
}
1736
1763
} ) ;
@@ -1767,15 +1794,15 @@ namespace ts.tscWatch {
1767
1794
assert ( fileExistsCalledForBar , "'fileExists' should be called" ) ;
1768
1795
checkOutputErrors ( host , [
1769
1796
getDiagnosticModuleNotFoundOfFile ( watch ( ) , root , "bar" )
1770
- ] , /*isInitial */ true ) ;
1797
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1771
1798
1772
1799
fileExistsCalledForBar = false ;
1773
1800
root . content = `import {y} from "bar"` ;
1774
1801
host . reloadFS ( files . concat ( imported ) ) ;
1775
1802
1776
1803
host . runQueuedTimeoutCallbacks ( ) ;
1804
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1777
1805
assert ( fileExistsCalledForBar , "'fileExists' should be called." ) ;
1778
- checkOutputErrors ( host , emptyArray ) ;
1779
1806
} ) ;
1780
1807
1781
1808
it ( "should compile correctly when resolved module goes missing and then comes back (module is not part of the root)" , ( ) => {
@@ -1807,21 +1834,21 @@ namespace ts.tscWatch {
1807
1834
const watch = createWatchModeWithoutConfigFile ( [ root . path ] , host , { module : ModuleKind . AMD } ) ;
1808
1835
1809
1836
assert ( fileExistsCalledForBar , "'fileExists' should be called" ) ;
1810
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
1837
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1811
1838
1812
1839
fileExistsCalledForBar = false ;
1813
1840
host . reloadFS ( files ) ;
1814
1841
host . runQueuedTimeoutCallbacks ( ) ;
1815
1842
assert ( fileExistsCalledForBar , "'fileExists' should be called." ) ;
1816
1843
checkOutputErrors ( host , [
1817
1844
getDiagnosticModuleNotFoundOfFile ( watch ( ) , root , "bar" )
1818
- ] ) ;
1845
+ ] , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1819
1846
1820
1847
fileExistsCalledForBar = false ;
1821
1848
host . reloadFS ( filesWithImported ) ;
1822
1849
host . checkTimeoutQueueLengthAndRun ( 1 ) ;
1850
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1823
1851
assert ( fileExistsCalledForBar , "'fileExists' should be called." ) ;
1824
- checkOutputErrors ( host , emptyArray ) ;
1825
1852
} ) ;
1826
1853
1827
1854
it ( "works when module resolution changes to ambient module" , ( ) => {
@@ -1857,11 +1884,11 @@ declare module "fs" {
1857
1884
1858
1885
checkOutputErrors ( host , [
1859
1886
getDiagnosticModuleNotFoundOfFile ( watch ( ) , root , "fs" )
1860
- ] , /*isInitial */ true ) ;
1887
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1861
1888
1862
1889
host . reloadFS ( filesWithNodeType ) ;
1863
1890
host . runQueuedTimeoutCallbacks ( ) ;
1864
- checkOutputErrors ( host , emptyArray ) ;
1891
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1865
1892
} ) ;
1866
1893
1867
1894
it ( "works when included file with ambient module changes" , ( ) => {
@@ -1899,12 +1926,12 @@ declare module "fs" {
1899
1926
1900
1927
checkOutputErrors ( host , [
1901
1928
getDiagnosticModuleNotFoundOfFile ( watch ( ) , root , "fs" )
1902
- ] , /*isInitial */ true ) ;
1929
+ ] , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1903
1930
1904
1931
file . content += fileContentWithFS ;
1905
1932
host . reloadFS ( files ) ;
1906
1933
host . runQueuedTimeoutCallbacks ( ) ;
1907
- checkOutputErrors ( host , emptyArray ) ;
1934
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1908
1935
} ) ;
1909
1936
1910
1937
it ( "works when reusing program with files from external library" , ( ) => {
@@ -1939,7 +1966,7 @@ declare module "fs" {
1939
1966
const host = createWatchedSystem ( programFiles . concat ( configFile ) , { currentDirectory : "/a/b/projects/myProject/" } ) ;
1940
1967
const watch = createWatchModeWithConfigFile ( configFile . path , host ) ;
1941
1968
checkProgramActualFiles ( watch ( ) , programFiles . map ( f => f . path ) ) ;
1942
- checkOutputErrors ( host , emptyArray , /*isInitial */ true ) ;
1969
+ checkOutputErrors ( host , emptyArray , /*errorsPosition */ ExpectedOutputErrorsPosition . AfterCompilationStarting ) ;
1943
1970
const expectedFiles : ExpectedFile [ ] = [
1944
1971
createExpectedEmittedFile ( file1 ) ,
1945
1972
createExpectedEmittedFile ( file2 ) ,
@@ -1958,7 +1985,7 @@ declare module "fs" {
1958
1985
host . reloadFS ( programFiles . concat ( configFile ) ) ;
1959
1986
host . runQueuedTimeoutCallbacks ( ) ;
1960
1987
checkProgramActualFiles ( watch ( ) , programFiles . map ( f => f . path ) ) ;
1961
- checkOutputErrors ( host , emptyArray ) ;
1988
+ checkOutputErrors ( host , emptyArray , /*errorsPosition*/ ExpectedOutputErrorsPosition . AfterFileChangeDetected ) ;
1962
1989
verifyExpectedFiles ( expectedFiles ) ;
1963
1990
1964
1991
@@ -2029,7 +2056,7 @@ declare module "fs" {
2029
2056
} ) ;
2030
2057
2031
2058
describe ( "tsc-watch console clearing" , ( ) => {
2032
- it ( "doesn't clear the console when it starts" , ( ) => {
2059
+ it ( "clears the console when it starts" , ( ) => {
2033
2060
const file = {
2034
2061
path : "f.ts" ,
2035
2062
content : ""
@@ -2039,7 +2066,7 @@ declare module "fs" {
2039
2066
createWatchModeWithoutConfigFile ( [ file . path ] , host ) ;
2040
2067
host . runQueuedTimeoutCallbacks ( ) ;
2041
2068
2042
- host . checkScreenClears ( 0 ) ;
2069
+ host . checkScreenClears ( 1 ) ;
2043
2070
} ) ;
2044
2071
2045
2072
it ( "clears the console on recompile" , ( ) => {
@@ -2057,7 +2084,7 @@ declare module "fs" {
2057
2084
host . reloadFS ( [ modifiedFile ] ) ;
2058
2085
host . runQueuedTimeoutCallbacks ( ) ;
2059
2086
2060
- host . checkScreenClears ( 1 ) ;
2087
+ host . checkScreenClears ( 2 ) ;
2061
2088
} ) ;
2062
2089
} ) ;
2063
2090
}
0 commit comments