From b1ea1f389076de8d323c51aa2d07ea67606fcb41 Mon Sep 17 00:00:00 2001 From: KeoFoxy Date: Fri, 14 Mar 2025 12:59:46 +0300 Subject: [PATCH 1/3] Port emitter functions --- internal/compiler/buildInfo.go | 26 ++++++++++++++++++++++++++ internal/compiler/emitHost.go | 21 +++++++++++++++++++-- internal/compiler/emitter.go | 34 ++++++++++++++++++++++++++++++---- internal/tspath/extension.go | 14 ++++++++++++++ 4 files changed, 89 insertions(+), 6 deletions(-) create mode 100644 internal/compiler/buildInfo.go diff --git a/internal/compiler/buildInfo.go b/internal/compiler/buildInfo.go new file mode 100644 index 0000000000..2ccd2f7c2d --- /dev/null +++ b/internal/compiler/buildInfo.go @@ -0,0 +1,26 @@ +package compiler + +import ( + "encoding/json" + "errors" +) + +type BuildInfo struct { + Version string `json:"version"` +} + +func GetBuildInfo(buildInfoFile string, buildInfoText string) (*BuildInfo, error) { + if buildInfoText == "" { + return nil, errors.New("empty buildInfoText") + } + var buildInfo BuildInfo + if err := json.Unmarshal([]byte(buildInfoText), &buildInfo); err != nil { + return nil, err + } + return &buildInfo, nil +} + +func GetBuildInfoText(buildInfo BuildInfo) string { + data, _ := json.MarshalIndent(buildInfo, "", " ") + return string(data) +} diff --git a/internal/compiler/emitHost.go b/internal/compiler/emitHost.go index 875afd34c0..ad2054313b 100644 --- a/internal/compiler/emitHost.go +++ b/internal/compiler/emitHost.go @@ -1,6 +1,8 @@ package compiler import ( + "sync" + "github.com/microsoft/typescript-go/internal/ast" "github.com/microsoft/typescript-go/internal/core" "github.com/microsoft/typescript-go/internal/printer" @@ -31,6 +33,10 @@ var _ EmitHost = (*emitHost)(nil) // NOTE: emitHost operations must be thread-safe type emitHost struct { program *Program + + mu sync.RWMutex + // Map storing if there is emit blocking diagnostics for given input + hasEmitBlockingDiagnostics map[string]bool } func (host *emitHost) Options() *core.CompilerOptions { return host.program.Options() } @@ -42,8 +48,19 @@ func (host *emitHost) UseCaseSensitiveFileNames() bool { } func (host *emitHost) IsEmitBlocked(file string) bool { - // !!! - return false + host.mu.RLock() + blocked := host.hasEmitBlockingDiagnostics[file] + host.mu.RUnlock() + return blocked +} + +func (host *emitHost) SetEmitBlocked(file string, blocked bool) { + host.mu.Lock() + defer host.mu.Unlock() + if host.hasEmitBlockingDiagnostics == nil { + host.hasEmitBlockingDiagnostics = make(map[string]bool) + } + host.hasEmitBlockingDiagnostics[file] = blocked } func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *WriteFileData) error { diff --git a/internal/compiler/emitter.go b/internal/compiler/emitter.go index 784b3c72f7..ddfdd5df1c 100644 --- a/internal/compiler/emitter.go +++ b/internal/compiler/emitter.go @@ -195,13 +195,39 @@ func getOwnEmitOutputFilePath(fileName string, host EmitHost, extension string) } func getSourceMapFilePath(jsFilePath string, options *core.CompilerOptions) string { - // !!! - return "" + if options.SourceMap.IsTrue() && options.InlineSourceMap.IsFalse() { + return jsFilePath + ".map" + } else { + return "" + } } func getDeclarationEmitOutputFilePath(file string, host EmitHost) string { - // !!! - return "" + return getDeclarationEmitOutputFilePathWorker(file, &core.CompilerOptions{}, host) +} + +func getDeclarationEmitOutputFilePathWorker(file string, options *core.CompilerOptions, host EmitHost) string { + // Prefer declaration folder if specified + outputDir := options.DeclarationDir + if outputDir == "" { + outputDir = options.OutDir + } + + var path string + if outputDir != "" { + path = getSourceFilePathInNewDir( + file, + outputDir, + host.GetCurrentDirectory(), + host.CommonSourceDirectory(), + host.UseCaseSensitiveFileNames(), + ) + } else { + path = file + } + + declarationExtension := tspath.GetDeclarationEmitExtensionForPath(path) + return tspath.RemoveFileExtension(path) + declarationExtension } type outputPaths struct { diff --git a/internal/tspath/extension.go b/internal/tspath/extension.go index c9110a6667..40d93bfaf9 100644 --- a/internal/tspath/extension.go +++ b/internal/tspath/extension.go @@ -133,3 +133,17 @@ func changeAnyExtension(path string, ext string, extensions []string, ignoreCase func ChangeExtension(path string, newExtension string) string { return changeAnyExtension(path, newExtension, extensionsToRemove /*ignoreCase*/, false) } + +func GetDeclarationEmitExtensionForPath(path string) string { + switch { + case FileExtensionIsOneOf(path, []string{ExtensionMjs, ExtensionMts}): + return ExtensionDmts + case FileExtensionIsOneOf(path, []string{ExtensionCjs, ExtensionCts}): + return ExtensionDcts + case FileExtensionIsOneOf(path, []string{ExtensionJson}): + // Drive-by redefinition of json declaration file output name so if it's ever enabled, it behaves well + return ".d.json.ts" + default: + return ExtensionDts + } +} From 35debe2527bb08f0263c17056341f320773d26e3 Mon Sep 17 00:00:00 2001 From: KeoFoxy Date: Fri, 14 Mar 2025 13:15:06 +0300 Subject: [PATCH 2/3] fix lint issue --- internal/compiler/buildInfo.go | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/internal/compiler/buildInfo.go b/internal/compiler/buildInfo.go index 2ccd2f7c2d..9d919072d5 100644 --- a/internal/compiler/buildInfo.go +++ b/internal/compiler/buildInfo.go @@ -20,7 +20,10 @@ func GetBuildInfo(buildInfoFile string, buildInfoText string) (*BuildInfo, error return &buildInfo, nil } -func GetBuildInfoText(buildInfo BuildInfo) string { - data, _ := json.MarshalIndent(buildInfo, "", " ") - return string(data) +func GetBuildInfoText(buildInfo BuildInfo) (string, error) { + data, err := json.MarshalIndent(buildInfo, "", " ") + if err != nil { + return "", err + } + return string(data), nil } From f496c6dfd372951478c78d29333b463fa6933ce8 Mon Sep 17 00:00:00 2001 From: KeoFoxy Date: Thu, 20 Mar 2025 12:43:21 +0300 Subject: [PATCH 3/3] Fix build issue --- internal/tspath/extension.go | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/internal/tspath/extension.go b/internal/tspath/extension.go index 28caf87441..046fc15207 100644 --- a/internal/tspath/extension.go +++ b/internal/tspath/extension.go @@ -120,19 +120,6 @@ func GetDeclarationFileExtension(fileName string) string { return "" } -func GetDeclarationEmitExtensionForPath(path string) string { - switch { - case FileExtensionIsOneOf(path, []string{ExtensionMjs, ExtensionMts}): - return ExtensionDmts - case FileExtensionIsOneOf(path, []string{ExtensionCjs, ExtensionCts}): - return ExtensionDcts - case FileExtensionIsOneOf(path, []string{ExtensionJson}): - return `.d.json.ts` // Drive-by redefinition of json declaration file output name so if it's ever enabled, it behaves well - default: - return ExtensionDts - } -} - // changeAnyExtension changes the extension of a path to the provided extension if it has one of the provided extensions. // // changeAnyExtension("/path/to/file.ext", ".js", ".ext") === "/path/to/file.js"