Skip to content

Commit 9a3814a

Browse files
Copilotandrewbranch
andcommitted
Refactor: move sourceFileMayBeEmitted to Program interface as requested
Co-authored-by: andrewbranch <3277153+andrewbranch@users.noreply.github.com>
1 parent 8cecfc9 commit 9a3814a

File tree

2 files changed

+6
-21
lines changed

2 files changed

+6
-21
lines changed

internal/checker/checker.go

Lines changed: 2 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -535,6 +535,7 @@ type Program interface {
535535
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
536536
GetJSXRuntimeImportSpecifier(path tspath.Path) (moduleReference string, specifier *ast.Node)
537537
GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node
538+
SourceFileMayBeEmitted(sourceFile *ast.SourceFile, forceDtsEmit bool) bool
538539
}
539540

540541
type Host interface {
@@ -14517,7 +14518,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1451714518
diagnostics.This_relative_import_path_is_unsafe_to_rewrite_because_it_looks_like_a_file_name_but_actually_resolves_to_0,
1451814519
relativeToSourceFile,
1451914520
)
14520-
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.sourceFileMayBeEmitted(sourceFile) {
14521+
} else if resolvedModule.ResolvedUsingTsExtension && !shouldRewrite && c.program.SourceFileMayBeEmitted(sourceFile, false) {
1452114522
c.error(
1452214523
errorNode,
1452314524
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,24 +30409,4 @@ func (c *Checker) GetAliasedSymbol(symbol *ast.Symbol) *ast.Symbol {
3040830409
return c.resolveAlias(symbol)
3040930410
}
3041030411

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-
}
3042330412

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/program.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -946,6 +946,10 @@ func (p *Program) GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node {
946946
return p.importHelpersImportSpecifiers[path]
947947
}
948948

949+
func (p *Program) SourceFileMayBeEmitted(sourceFile *ast.SourceFile, forceDtsEmit bool) bool {
950+
return sourceFileMayBeEmitted(sourceFile, &emitHost{program: p}, forceDtsEmit)
951+
}
952+
949953
var plainJSErrors = core.NewSetFromItems(
950954
// binder errors
951955
diagnostics.Cannot_redeclare_block_scoped_variable_0.Code(),

0 commit comments

Comments
 (0)