Skip to content

Commit 443ea33

Browse files
committed
start porting logic
1 parent ad9559e commit 443ea33

File tree

1 file changed

+58
-7
lines changed

1 file changed

+58
-7
lines changed

internal/compiler/emitter.go

Lines changed: 58 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package compiler
22

33
import (
44
"encoding/base64"
5+
"fmt"
56
"strings"
67

78
"github.com/microsoft/typescript-go/internal/ast"
@@ -297,17 +298,25 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
297298
return stringutil.EncodeURI(sourceMapFile)
298299
}
299300

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.
300304
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+
}
302311

303312
// Declaration files are not emitted
304313
if sourceFile.IsDeclarationFile {
305314
return false
306315
}
307316

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.
311320
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
312321
return false
313322
}
@@ -318,17 +327,57 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, f
318327
}
319328

320329
// 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
321332
if host.GetOutputAndProjectReference(sourceFile.Path()) != nil {
322333
return false
323334
}
324335

325336
// Any non json file should be emitted
326337
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)
327350
return true
328351
}
329352

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
332381
}
333382

334383
func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
@@ -340,7 +389,9 @@ func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFil
340389
sourceFiles = host.SourceFiles()
341390
}
342391
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
344395
})
345396
}
346397

0 commit comments

Comments
 (0)