Skip to content

Commit 08d918e

Browse files
Copilotandrewbranch
andcommitted
Refactor: move sourceFileMayBeEmitted to outputpaths and add forceDtsEmit parameter
Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
1 parent ba765e4 commit 08d918e

File tree

4 files changed

+30
-47
lines changed

4 files changed

+30
-47
lines changed

internal/checker/checker.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14518,7 +14518,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1451814518
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
1451914519
relativeToSourceFile,
1452014520
)
14521-
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && outputpaths.SourceFileMayBeEmitted(sourceFile, c.compilerOptions) {
14521+
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && outputpaths.SourceFileMayBeEmitted(sourceFile, c.compilerOptions, false) {
1452214522
c.error(
1452314523
errorNode,
1452414524
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,
@@ -30410,8 +30410,8 @@ func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
3041030410
}
3041130411

3041230412
func (c *Checker) shouldRewriteModuleSpecifier(specifier string) bool {
30413-
return c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
30414-
tspath.PathIsRelative(specifier) &&
30415-
!tspath.IsDeclarationFileName(specifier) &&
30413+
return c.compilerOptions.RewriteRelativeImportExtensions.IsTrue() &&
30414+
tspath.PathIsRelative(specifier) &&
30415+
!tspath.IsDeclarationFileName(specifier) &&
3041630416
tspath.HasTSFileExtension(specifier)
3041730417
}

internal/compiler/emitter.go

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

33
import (
44
"encoding/base64"
5-
"strings"
65

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

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-
331299
func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFile, forceDtsEmit bool) []*ast.SourceFile {
332300
// !!! outFile not yet implemented, may be deprecated
333301
var sourceFiles []*ast.SourceFile
@@ -337,7 +305,7 @@ func getSourceFilesToEmit(host printer.EmitHost, targetSourceFile *ast.SourceFil
337305
sourceFiles = host.SourceFiles()
338306
}
339307
return core.Filter(sourceFiles, func(sourceFile *ast.SourceFile) bool {
340-
return sourceFileMayBeEmitted(sourceFile, host, forceDtsEmit)
308+
return outputpaths.SourceFileMayBeEmitted(sourceFile, host.Options(), forceDtsEmit)
341309
})
342310
}
343311

internal/compiler/program.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -710,9 +710,8 @@ 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}
714713
for _, file := range p.files {
715-
if sourceFileMayBeEmitted(file, host, false /*forceDtsEmit*/) {
714+
if outputpaths.SourceFileMayBeEmitted(file, p.Options(), false /*forceDtsEmit*/) {
716715
files = append(files, file.FileName())
717716
}
718717
}

internal/outputpaths/outputpaths.go

Lines changed: 24 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -201,17 +201,33 @@ func getDeclarationEmitExtensionForPath(fileName string) string {
201201
}
202202

203203
// SourceFileMayBeEmitted checks if a source file may be emitted by the compiler
204-
func SourceFileMayBeEmitted(sourceFile *ast.SourceFile, options *core.CompilerOptions) bool {
205-
if options.NoEmit.IsTrue() || options.EmitDeclarationOnly.IsTrue() {
204+
func SourceFileMayBeEmitted(sourceFile *ast.SourceFile, options *core.CompilerOptions, forceDtsEmit bool) bool {
205+
// !!! Js files are emitted only if option is enabled
206+
207+
// Declaration files are not emitted
208+
if sourceFile.IsDeclarationFile {
206209
return false
207210
}
208-
// Check if this source file is a declaration file
209-
if tspath.IsDeclarationFileName(sourceFile.FileName()) {
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/") {
210216
return false
211217
}
212-
// Check if this is a JS file and allowJs is disabled
213-
if tspath.HasJSFileExtension(sourceFile.FileName()) && !options.AllowJs.IsTrue() {
214-
return false
218+
219+
// forcing dts emit => file needs to be emitted
220+
if forceDtsEmit {
221+
return true
215222
}
216-
return true
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
217233
}

0 commit comments

Comments
 (0)