@@ -1345,4 +1345,123 @@ namespace ts.tscWatch {
13451345 verifyAffectedFiles ( [ referenceFile1Emit , moduleFile2 ] , [ libFile , moduleFile2 , referenceFile1Emit , configFile ] ) ;
13461346 } ) ;
13471347 } ) ;
1348+
1349+ describe ( "tsc-watch emit file content" , ( ) => {
1350+ interface EmittedFile extends FileOrFolder {
1351+ shouldBeWritten : boolean ;
1352+ }
1353+ function getEmittedFiles ( files : FileOrFolderEmit [ ] , contents : string [ ] ) : EmittedFile [ ] {
1354+ return map ( contents , ( content , index ) => {
1355+ return {
1356+ content,
1357+ path : changeExtension ( files [ index ] . path , Extension . Js ) ,
1358+ shouldBeWritten : true
1359+ } ;
1360+ }
1361+ ) ;
1362+ }
1363+ function verifyEmittedFiles ( host : WatchedSystem , emittedFiles : EmittedFile [ ] ) {
1364+ for ( const { path, content, shouldBeWritten } of emittedFiles ) {
1365+ if ( shouldBeWritten ) {
1366+ assert . isTrue ( host . fileExists ( path ) , `Expected file ${ path } to be present` ) ;
1367+ assert . equal ( host . readFile ( path ) , content , `Contents of file ${ path } do not match` ) ;
1368+ }
1369+ else {
1370+ assert . isNotTrue ( host . fileExists ( path ) , `Expected file ${ path } to be absent` ) ;
1371+ }
1372+ }
1373+ }
1374+
1375+ function verifyEmittedFileContents ( newLine : string , inputFiles : FileOrFolder [ ] , initialEmittedFileContents : string [ ] ,
1376+ modifyFiles : ( files : FileOrFolderEmit [ ] , emitedFiles : EmittedFile [ ] ) => FileOrFolderEmit [ ] , configFile ?: FileOrFolder ) {
1377+ const host = createWatchedSystem ( [ ] , { newLine } ) ;
1378+ const files = concatenate (
1379+ map ( inputFiles , file => getFileOrFolderEmit ( file , fileToConvert => getEmittedLineForMultiFileOutput ( fileToConvert , host ) ) ) ,
1380+ configFile ? [ libFile , configFile ] : [ libFile ]
1381+ ) ;
1382+ const allEmittedFiles = getEmittedLines ( files ) ;
1383+ host . reloadFS ( files ) ;
1384+
1385+ // Initial compile
1386+ if ( configFile ) {
1387+ createWatchWithConfig ( configFile . path , host ) ;
1388+ }
1389+ else {
1390+ // First file as the root
1391+ createWatchModeWithoutConfigFile ( [ files [ 0 ] . path ] , { listEmittedFiles : true } , host ) ;
1392+ }
1393+ checkOutputContains ( host , allEmittedFiles ) ;
1394+
1395+ const emittedFiles = getEmittedFiles ( files , initialEmittedFileContents ) ;
1396+ verifyEmittedFiles ( host , emittedFiles ) ;
1397+ host . clearOutput ( ) ;
1398+
1399+ const affectedFiles = modifyFiles ( files , emittedFiles ) ;
1400+ host . reloadFS ( files ) ;
1401+ host . checkTimeoutQueueLengthAndRun ( 1 ) ;
1402+ checkAffectedLines ( host , affectedFiles , allEmittedFiles ) ;
1403+
1404+ verifyEmittedFiles ( host , emittedFiles ) ;
1405+ }
1406+
1407+ function verifyNewLine ( newLine : string ) {
1408+ const lines = [ "var x = 1;" , "var y = 2;" ] ;
1409+ const fileContent = lines . join ( newLine ) ;
1410+ const f = {
1411+ path : "/a/app.ts" ,
1412+ content : fileContent
1413+ } ;
1414+
1415+ verifyEmittedFileContents ( newLine , [ f ] , [ fileContent + newLine ] , modifyFiles ) ;
1416+
1417+ function modifyFiles ( files : FileOrFolderEmit [ ] , emittedFiles : EmittedFile [ ] ) {
1418+ files [ 0 ] . content = fileContent + newLine + "var z = 3;" ;
1419+ emittedFiles [ 0 ] . content = files [ 0 ] . content + newLine ;
1420+ return [ files [ 0 ] ] ;
1421+ }
1422+ }
1423+
1424+ it ( "handles new lines: \\n" , ( ) => {
1425+ verifyNewLine ( "\n" ) ;
1426+ } ) ;
1427+
1428+ it ( "handles new lines: \\r\\n" , ( ) => {
1429+ verifyNewLine ( "\r\n" ) ;
1430+ } ) ;
1431+
1432+ it ( "should emit specified file" , ( ) => {
1433+ const file1 = {
1434+ path : "/a/b/f1.ts" ,
1435+ content : `export function Foo() { return 10; }`
1436+ } ;
1437+
1438+ const file2 = {
1439+ path : "/a/b/f2.ts" ,
1440+ content : `import {Foo} from "./f1"; export let y = Foo();`
1441+ } ;
1442+
1443+ const file3 = {
1444+ path : "/a/b/f3.ts" ,
1445+ content : `import {y} from "./f2"; let x = y;`
1446+ } ;
1447+
1448+ const configFile = {
1449+ path : "/a/b/tsconfig.json" ,
1450+ content : JSON . stringify ( { compilerOptions : { listEmittedFiles : true } } )
1451+ } ;
1452+
1453+ verifyEmittedFileContents ( "\r\n" , [ file1 , file2 , file3 ] , [
1454+ `"use strict";\r\nexports.__esModule = true;\r\nfunction Foo() { return 10; }\r\nexports.Foo = Foo;\r\n` ,
1455+ `"use strict";\r\nexports.__esModule = true;\r\nvar f1_1 = require("./f1");\r\nexports.y = f1_1.Foo();\r\n` ,
1456+ `"use strict";\r\nexports.__esModule = true;\r\nvar f2_1 = require("./f2");\r\nvar x = f2_1.y;\r\n`
1457+ ] , modifyFiles , configFile ) ;
1458+
1459+ function modifyFiles ( files : FileOrFolderEmit [ ] , emittedFiles : EmittedFile [ ] ) {
1460+ files [ 0 ] . content += `export function foo2() { return 2; }` ;
1461+ emittedFiles [ 0 ] . content += `function foo2() { return 2; }\r\nexports.foo2 = foo2;\r\n` ;
1462+ emittedFiles [ 2 ] . shouldBeWritten = false ;
1463+ return files . slice ( 0 , 2 ) ;
1464+ }
1465+ } ) ;
1466+ } ) ;
13481467}
0 commit comments