Skip to content

Add emitter scaffolding #172

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

Merged
merged 10 commits into from
Dec 17, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
17 changes: 16 additions & 1 deletion cmd/tsgo/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ var printTypes = false
var pretty = true
var listFiles = false
var pprofDir = ""
var outDir = ""

func printDiagnostic(d *ast.Diagnostic, level int, comparePathOptions tspath.ComparePathsOptions) {
file := d.File()
Expand Down Expand Up @@ -57,10 +58,16 @@ func main() {
flag.BoolVar(&pretty, "pretty", true, "Get prettier errors")
flag.BoolVar(&listFiles, "listfiles", false, "List files in the program")
flag.StringVar(&pprofDir, "pprofdir", "", "Generate pprof CPU/memory profiles to the given directory")
flag.StringVar(&outDir, "outDir", "", "Emit to the given directory")
flag.Parse()

rootPath := flag.Arg(0)
compilerOptions := &core.CompilerOptions{Strict: core.TSTrue, Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext}
compilerOptions := &core.CompilerOptions{Strict: core.TSTrue, Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext, NoEmit: core.TSTrue}
if len(outDir) > 0 {
compilerOptions.NoEmit = core.TSFalse
compilerOptions.OutDir = outDir
Copy link
Member

Choose a reason for hiding this comment

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

I just ran this; this needs to be something like:

Suggested change
compilerOptions.OutDir = outDir
compilerOptions.OutDir = tspath.ResolvePath(currentDirectory, outDir)

Otherwise I get:

panic: vfs: path "wow/src/vs/platform/utilityProcess/common/utilityProcessWorkerService.js" is not absolute

(so, probably move down below)

}

currentDirectory, err := os.Getwd()
if err != nil {
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
Expand Down Expand Up @@ -99,6 +106,13 @@ func main() {
}
compileTime := time.Since(startTime)

startTime = time.Now()
if len(outDir) > 0 {
result := program.Emit(&ts.EmitOptions{})
diagnostics = append(diagnostics, result.Diagnostics...)
}
emitTime := time.Since(startTime)

var memStats runtime.MemStats
runtime.GC()
runtime.GC()
Expand Down Expand Up @@ -133,6 +147,7 @@ func main() {
fmt.Printf("Files: %v\n", len(program.SourceFiles()))
fmt.Printf("Types: %v\n", program.TypeCount())
fmt.Printf("Compile time: %v\n", compileTime)
fmt.Printf("Emit time: %v\n", emitTime)
fmt.Printf("Memory used: %vK\n", memStats.Alloc/1024)
}

Expand Down
67 changes: 67 additions & 0 deletions internal/compiler/emitHost.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package compiler

import (
"sync"

"github.com/microsoft/typescript-go/internal/ast"
"github.com/microsoft/typescript-go/internal/core"
)

type WriteFileData struct {
SourceMapUrlPos int
// BuildInfo BuildInfo
Diagnostics []*ast.Diagnostic
DiffersOnlyInMap bool
SkippedDtsWrite bool
}

// NOTE: EmitHost operations must be thread-safe
type EmitHost interface {
Copy link
Member

Choose a reason for hiding this comment

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

Are we ever going to have more than one implementation of this?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not 100% certain yet. This was something we allowed users to override when consuming the API, so that all depends on our long term API story (as in, could someone build on top of our go package). I opted to leave it as-is to more faithfully port portions of the emitter.

Options() *core.CompilerOptions
SourceFiles() []*ast.SourceFile
UseCaseSensitiveFileNames() bool
GetCurrentDirectory() string
CommonSourceDirectory() string
IsEmitBlocked(file string) bool
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
}

var _ EmitHost = (*emitHost)(nil)

// NOTE: emitHost operations must be thread-safe
type emitHost struct {
program *Program

commonSourceDirectory string
commonSourceDirectoryMutex sync.Mutex
}

func (host *emitHost) Options() *core.CompilerOptions { return host.program.Options() }
func (host *emitHost) SourceFiles() []*ast.SourceFile { return host.program.SourceFiles() }
func (host *emitHost) GetCurrentDirectory() string { return host.program.host.GetCurrentDirectory() }
func (host *emitHost) CommonSourceDirectory() string {
if len(host.commonSourceDirectory) > 0 {
return host.commonSourceDirectory
}

host.commonSourceDirectoryMutex.Lock()
defer host.commonSourceDirectoryMutex.Unlock()

// double-check after lock
if len(host.commonSourceDirectory) > 0 {
return host.commonSourceDirectory
}

host.commonSourceDirectory = host.program.CommonSourceDirectory()
return host.commonSourceDirectory
}
func (host *emitHost) UseCaseSensitiveFileNames() bool {
return host.program.host.FS().UseCaseSensitiveFileNames()
}
func (host *emitHost) IsEmitBlocked(file string) bool {
// !!!
return false
}
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *WriteFileData) error {
return host.program.host.FS().WriteFile(fileName, text, writeByteOrderMark)
}
Loading
Loading