Skip to content

feature: Port emitter functions #600

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions internal/compiler/buildInfo.go
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can I keep it for future use and scaling, or should I delete it?

Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
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, error) {
data, err := json.MarshalIndent(buildInfo, "", " ")
if err != nil {
return "", err
}
return string(data), nil
}
21 changes: 19 additions & 2 deletions internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
@@ -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"
Expand Down Expand Up @@ -33,6 +35,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() }
Expand All @@ -44,8 +50,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 {
Expand Down
34 changes: 30 additions & 4 deletions internal/compiler/emitter.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,13 +196,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 {
Expand Down
27 changes: 14 additions & 13 deletions internal/tspath/extension.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -154,3 +141,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
}
}