Skip to content

Commit 8cecfc9

Browse files
Copilotsheetalkamat
andcommitted
Fix: restore sourceFileMayBeEmitted to emitter.go and add program context
Co-authored-by: sheetalkamat <8052792+sheetalkamat@users.noreply.github.com>
1 parent 2ca8702 commit 8cecfc9

File tree

4 files changed

+58
-34
lines changed

4 files changed

+58
-34
lines changed

internal/checker/checker.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"github.com/microsoft/typescript-go/internal/jsnum"
2323
"github.com/microsoft/typescript-go/internal/module"
2424
"github.com/microsoft/typescript-go/internal/modulespecifiers"
25-
"github.com/microsoft/typescript-go/internal/outputpaths"
2625
"github.com/microsoft/typescript-go/internal/printer"
2726
"github.com/microsoft/typescript-go/internal/scanner"
2827
"github.com/microsoft/typescript-go/internal/stringutil"
@@ -14518,7 +14517,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1451814517
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
1451914518
relativeToSourceFile,
1452014519
)
14521-
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && outputpaths.SourceFileMayBeEmitted(sourceFile, c.compilerOptions, false) {
14520+
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
1452214521
c.error(
1452314522
errorNode,
1452414523
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,
@@ -30408,3 +30407,25 @@ func (c *Checker) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) *e
3040830407
func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
3040930408
return c.resolveAlias(symbol)
3041030409
}
30410+
30411+
// sourceFileMayBeEmitted is a simplified version of the function in compiler/emitter.go
30412+
// that handles the basic checks needed for import rewrite diagnostics
30413+
func (c *Checker) sourceFileMayBeEmitted(sourceFile *ast.SourceFile) bool {
30414+
// Declaration files are not emitted
30415+
if sourceFile.IsDeclarationFile {
30416+
return false
30417+
}
30418+
30419+
// Source file from node_modules are not emitted
30420+
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
30421+
return false
30422+
}
30423+
30424+
// Any non json file should be emitted
30425+
if !ast.IsJsonSourceFile(sourceFile) {
30426+
return true
30427+
}
30428+
30429+
// JSON files are generally not emitted
30430+
return false
30431+
}

internal/compiler/emitter.go

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

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

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

300+
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
301+
// !!! Js files are emitted only if option is enabled
302+
303+
// Declaration files are not emitted
304+
if sourceFile.IsDeclarationFile {
305+
return false
306+
}
307+
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.
311+
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
312+
return false
313+
}
314+
315+
// forcing dts emit => file needs to be emitted
316+
if forceDtsEmit {
317+
return true
318+
}
319+
320+
// !!! Source files from referenced projects are not emitted
321+
322+
// Any non json file should be emitted
323+
if !ast.IsJsonSourceFile(sourceFile) {
324+
return true
325+
}
326+
327+
// !!! Should JSON input files be emitted
328+
return false
329+
}
330+
299331
func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
300332
// !!! outFile not yet implemented, may be deprecated
301333
var sourceFiles []*ast.SourceFile
@@ -305,7 +337,7 @@ func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFil
305337
sourceFiles = host.SourceFiles()
306338
}
307339
return core.Filter(sourceFiles, func(sourceFile *ast.SourceFile) bool {
308-
return outputpaths.SourceFileMayBeEmitted(sourceFile, host.Options(), forceDtsEmit)
340+
return sourceFileMayBeEmitted(sourceFile, host, forceDtsEmit)
309341
})
310342
}
311343

internal/compiler/program.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -710,8 +710,9 @@ func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) co
710710
func (p *Program) CommonSourceDirectory() string {
711711
p.commonSourceDirectoryOnce.Do(func() {
712712
var files []string
713+
host := &emitHost{program: p}
713714
for _, file := range p.files {
714-
if outputpaths.SourceFileMayBeEmitted(file, p.Options(), false /*forceDtsEmit*/) {
715+
if sourceFileMayBeEmitted(file, host, false /*forceDtsEmit*/) {
715716
files = append(files, file.FileName())
716717
}
717718
}

internal/outputpaths/outputpaths.go

Lines changed: 0 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -200,34 +200,4 @@ func getDeclarationEmitExtensionForPath(fileName string) string {
200200
return tspath.ExtensionDts
201201
}
202202

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

207-
// Declaration files are not emitted
208-
if sourceFile.IsDeclarationFile {
209-
return false
210-
}
211-
212-
// !!! Source file from node_modules are not emitted. In Strada, this depends on module resolution and uses
213-
// `sourceFilesFoundSearchingNodeModules` in `createProgram`. For now, we will just check for `/node_modules/` in
214-
// the file name.
215-
if strings.Contains(sourceFile.FileName(), "/node_modules/") {
216-
return false
217-
}
218-
219-
// forcing dts emit => file needs to be emitted
220-
if forceDtsEmit {
221-
return true
222-
}
223-
224-
// !!! Source files from referenced projects are not emitted
225-
226-
// Any non json file should be emitted
227-
if !ast.IsJsonSourceFile(sourceFile) {
228-
return true
229-
}
230-
231-
// !!! Should JSON input files be emitted
232-
return false
233-
}

0 commit comments

Comments
 (0)