@@ -2,6 +2,7 @@ package compiler
2
2
3
3
import (
4
4
"encoding/base64"
5
+ "fmt"
5
6
"strings"
6
7
7
8
"github.com/microsoft/typescript-go/internal/ast"
@@ -297,17 +298,25 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
297
298
return stringutil .EncodeURI (sourceMapFile )
298
299
}
299
300
301
+ // sourceFileMayBeEmitted determines whether a source file should be emitted based on TypeScript compiler rules.
302
+ // This implementation now closely matches the TypeScript utilities.ts sourceFileMayBeEmitted function,
303
+ // including proper handling of JS files, external libraries, project references, and JSON file emission logic.
300
304
func sourceFileMayBeEmitted (sourceFile * ast.SourceFile , host printer.EmitHost , forceDtsEmit bool ) bool {
301
- // !!! Js files are emitted only if option is enabled
305
+ options := host .Options ()
306
+
307
+ // Js files are emitted only if option is enabled
308
+ if options .NoEmitForJsFiles == core .TSTrue && ast .IsSourceFileJS (sourceFile ) {
309
+ return false
310
+ }
302
311
303
312
// Declaration files are not emitted
304
313
if sourceFile .IsDeclarationFile {
305
314
return false
306
315
}
307
316
308
- // !!! Source file from node_modules are not emitted. In Strada, this depends on module resolution and uses
309
- // `sourceFilesFoundSearchingNodeModules` in `createProgram`. For now, we will just check for `/node_modules/` in
310
- // the file name.
317
+ // Source file from node_modules are not emitted
318
+ // In the TypeScript implementation, this uses host.isSourceFileFromExternalLibrary(sourceFile)
319
+ // For now, we will just check for `/node_modules/` in the file name.
311
320
if strings .Contains (sourceFile .FileName (), "/node_modules/" ) {
312
321
return false
313
322
}
@@ -318,17 +327,57 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
318
327
}
319
328
320
329
// Source files from referenced projects are not emitted
330
+ // In TypeScript this uses host.isSourceOfProjectReferenceRedirect(sourceFile.fileName)
331
+ // We're using the available GetOutputAndProjectReference method as an approximation
321
332
if host .GetOutputAndProjectReference (sourceFile .Path ()) != nil {
322
333
return false
323
334
}
324
335
325
336
// Any non json file should be emitted
326
337
if ! ast .IsJsonSourceFile (sourceFile ) {
338
+ fmt .Println ("sourceFile.FileName()" , sourceFile .FileName (), "IsJsonSourceFile" , true )
339
+ return true
340
+ }
341
+
342
+ // The following logic matches the TypeScript implementation for JSON files
343
+
344
+ // In TypeScript this would be: if (host.getResolvedProjectReferenceToRedirect(sourceFile.fileName)) return false;
345
+ // We don't have this method available yet, so skip this check for now
346
+
347
+ // Emit json file if outFile is specified
348
+ if options .OutFile != "" {
349
+ fmt .Println ("sourceFile.FileName()" , sourceFile .FileName (), "options.OutFile" , true )
327
350
return true
328
351
}
329
352
330
- // !!! Should JSON input files be emitted
331
- return false
353
+ // Json file is not emitted if outDir is not specified
354
+ if options .OutDir == "" {
355
+ fmt .Println ("sourceFile.FileName()" , sourceFile .FileName (), "options.OutDir" , true )
356
+ return false
357
+ }
358
+
359
+ // Otherwise if rootDir or composite config file, we know common sourceDir and can check if file would be emitted in same location
360
+ if options .RootDir != "" || (options .Composite == core .TSTrue && options .ConfigFilePath != "" ) {
361
+ // Get normalized absolute paths
362
+ commonDir := host .CommonSourceDirectory ()
363
+ currentDir := host .GetCurrentDirectory ()
364
+
365
+ // Calculate output path
366
+ outputPath := outputpaths .GetSourceFilePathInNewDir (sourceFile .FileName (), options .OutDir , currentDir , commonDir , host .UseCaseSensitiveFileNames ())
367
+
368
+ // Compare paths - if they're the same, don't emit
369
+ compareResult := tspath .ComparePaths (sourceFile .FileName (), outputPath , tspath.ComparePathsOptions {
370
+ CurrentDirectory : currentDir ,
371
+ UseCaseSensitiveFileNames : host .UseCaseSensitiveFileNames (),
372
+ })
373
+ if compareResult == 0 { // Comparison.EqualTo
374
+ return false
375
+ }
376
+ }
377
+
378
+ fmt .Println ("sourceFile.FileName()" , sourceFile .FileName (), "fully fell through" , true )
379
+
380
+ return true
332
381
}
333
382
334
383
func getSourceFilesToEmit (host printer.EmitHost , targetSourceFile * ast.SourceFile , forceDtsEmit bool ) []* ast.SourceFile {
@@ -340,7 +389,9 @@ func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFil
340
389
sourceFiles = host .SourceFiles ()
341
390
}
342
391
return core .Filter (sourceFiles , func (sourceFile * ast.SourceFile ) bool {
343
- return sourceFileMayBeEmitted (sourceFile , host , forceDtsEmit )
392
+ var result bool = sourceFileMayBeEmitted (sourceFile , host , forceDtsEmit )
393
+ fmt .Println ("sourceFile.FileName()" , sourceFile .FileName (), "result" , result )
394
+ return result
344
395
})
345
396
}
346
397
0 commit comments