Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Fix: restore sourceFileMayBeEmitted to emitter.go and add program con…
…text

Co-authored-by: sheetalkamat <[email protected]>
  • Loading branch information
Copilot and sheetalkamat committed Jun 10, 2025
commit 8cecfc9b50fd34ab6b04b2216e005c4792c0071f
25 changes: 23 additions & 2 deletions internal/checker/checker.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ import (
"github.com/microsoft/typescript-go/internal/jsnum"
"github.com/microsoft/typescript-go/internal/module"
"github.com/microsoft/typescript-go/internal/modulespecifiers"
"github.com/microsoft/typescript-go/internal/outputpaths"
"github.com/microsoft/typescript-go/internal/printer"
"github.com/microsoft/typescript-go/internal/scanner"
"github.com/microsoft/typescript-go/internal/stringutil"
Expand Down Expand Up @@ -14518,7 +14517,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
relativeToSourceFile,
)
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && outputpaths.SourceFileMayBeEmitted(sourceFile, c.compilerOptions, false) {
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
c.error(
errorNode,
diagnostics.This_import_uses_a_0_extension_to_resolve_to_an_input_TypeScript_file_but_will_not_be_rewritten_during_emit_because_it_is_not_a_relative_path,
Expand Down Expand Up @@ -30408,3 +30407,25 @@ func (c *Checker) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) *e
func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
return c.resolveAlias(symbol)
}

// sourceFileMayBeEmitted is a simplified version of the function in compiler/emitter.go
// that handles the basic checks needed for import rewrite diagnostics
func (c *Checker) sourceFileMayBeEmitted(sourceFile *ast.SourceFile) bool {
// Declaration files are not emitted
if sourceFile.IsDeclarationFile {
return false
}

// Source file from node_modules are not emitted
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
return false
}

// Any non json file should be emitted
if !ast.IsJsonSourceFile(sourceFile) {
return true
}

// JSON files are generally not emitted
return false
}
34 changes: 33 additions & 1 deletion internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package compiler

import (
"encoding/base64"
"strings"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
Expand Down Expand Up @@ -296,6 +297,37 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
return stringutil.EncodeURI(sourceMapFile)
}

func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
// !!! Js files are emitted only if option is enabled

// Declaration files are not emitted
if sourceFile.IsDeclarationFile {
return false
}

// !!! Source file from node_modules are not emitted. In Strada, this depends on module resolution and uses
// `sourceFilesFoundSearchingNodeModules` in `createProgram`. For now, we will just check for `/node_modules/` in
// the file name.
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
return false
}

// forcing dts emit => file needs to be emitted
if forceDtsEmit {
return true
}

// !!! Source files from referenced projects are not emitted

// Any non json file should be emitted
if !ast.IsJsonSourceFile(sourceFile) {
return true
}

// !!! Should JSON input files be emitted
return false
}

func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
// !!! outFile not yet implemented, may be deprecated
var sourceFiles []*ast.SourceFile
Expand All @@ -305,7 +337,7 @@ func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFil
sourceFiles = host.SourceFiles()
}
return core.Filter(sourceFiles, func(sourceFile *ast.SourceFile) bool {
return outputpaths.SourceFileMayBeEmitted(sourceFile, host.Options(), forceDtsEmit)
return sourceFileMayBeEmitted(sourceFile, host, forceDtsEmit)
})
}

Expand Down
3 changes: 2 additions & 1 deletion internal/compiler/program.go
Original file line number Diff line number Diff line change
Expand Up @@ -710,8 +710,9 @@ func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) co
func (p *Program) CommonSourceDirectory() string {
p.commonSourceDirectoryOnce.Do(func() {
var files []string
host := &emitHost{program: p}
for _, file := range p.files {
if outputpaths.SourceFileMayBeEmitted(file, p.Options(), false /*forceDtsEmit*/) {
if sourceFileMayBeEmitted(file, host, false /*forceDtsEmit*/) {
files = append(files, file.FileName())
}
}
Expand Down
30 changes: 0 additions & 30 deletions internal/outputpaths/outputpaths.go
Original file line number Diff line number Diff line change
Expand Up @@ -200,34 +200,4 @@ func getDeclarationEmitExtensionForPath(fileName string) string {
return tspath.ExtensionDts
}

// SourceFileMayBeEmitted checks if a source file may be emitted by the compiler
func SourceFileMayBeEmitted(sourceFile *ast.SourceFile, options *core.CompilerOptions, forceDtsEmit bool) bool {
// !!! Js files are emitted only if option is enabled

// Declaration files are not emitted
if sourceFile.IsDeclarationFile {
return false
}

// !!! Source file from node_modules are not emitted. In Strada, this depends on module resolution and uses
// `sourceFilesFoundSearchingNodeModules` in `createProgram`. For now, we will just check for `/node_modules/` in
// the file name.
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
return false
}

// forcing dts emit => file needs to be emitted
if forceDtsEmit {
return true
}

// !!! Source files from referenced projects are not emitted

// Any non json file should be emitted
if !ast.IsJsonSourceFile(sourceFile) {
return true
}

// !!! Should JSON input files be emitted
return false
}