Skip to content

Commit 61a3e78

Browse files
committed
Add emitter scaffolding
1 parent ac80542 commit 61a3e78

File tree

12 files changed

+669
-122
lines changed

12 files changed

+669
-122
lines changed

cmd/tsgo/main.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ var printTypes = false
2626
var pretty = true
2727
var listFiles = false
2828
var pprofDir = ""
29+
var outDir = ""
2930

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

6264
rootPath := flag.Arg(0)
63-
compilerOptions := &core.CompilerOptions{Strict: core.TSTrue, Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext}
65+
compilerOptions := &core.CompilerOptions{Strict: core.TSTrue, Target: core.ScriptTargetESNext, ModuleKind: core.ModuleKindNodeNext, NoEmit: core.TSTrue}
66+
if len(outDir) > 0 {
67+
compilerOptions.NoEmit = core.TSFalse
68+
compilerOptions.OutDir = outDir
69+
}
70+
6471
currentDirectory, err := os.Getwd()
6572
if err != nil {
6673
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
@@ -99,6 +106,13 @@ func main() {
99106
}
100107
compileTime := time.Since(startTime)
101108

109+
startTime = time.Now()
110+
if len(outDir) > 0 {
111+
result := program.Emit(&ts.EmitOptions{})
112+
diagnostics = append(diagnostics, result.Diagnostics...)
113+
}
114+
emitTime := time.Since(startTime)
115+
102116
var memStats runtime.MemStats
103117
runtime.GC()
104118
runtime.GC()
@@ -133,6 +147,7 @@ func main() {
133147
fmt.Printf("Files: %v\n", len(program.SourceFiles()))
134148
fmt.Printf("Types: %v\n", program.TypeCount())
135149
fmt.Printf("Compile time: %v\n", compileTime)
150+
fmt.Printf("Emit time: %v\n", emitTime)
136151
fmt.Printf("Memory used: %vK\n", memStats.Alloc/1024)
137152
}
138153

internal/compiler/emitHost.go

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
package compiler
2+
3+
import (
4+
"sync"
5+
6+
"github.com/microsoft/typescript-go/internal/ast"
7+
"github.com/microsoft/typescript-go/internal/core"
8+
)
9+
10+
type WriteFileData struct {
11+
SourceMapUrlPos int
12+
// BuildInfo BuildInfo
13+
Diagnostics []*ast.Diagnostic
14+
DiffersOnlyInMap bool
15+
SkippedDtsWrite bool
16+
}
17+
18+
// NOTE: EmitHost operations must be thread-safe
19+
type EmitHost interface {
20+
Options() *core.CompilerOptions
21+
SourceFiles() []*ast.SourceFile
22+
UseCaseSensitiveFileNames() bool
23+
GetCurrentDirectory() string
24+
CommonSourceDirectory() string
25+
IsEmitBlocked(file string) bool
26+
WriteFile(fileName string, text string, writeByteOrderMark bool, relatedSourceFiles []*ast.SourceFile, data *WriteFileData) error
27+
}
28+
29+
var _ EmitHost = (*emitHost)(nil)
30+
31+
// NOTE: emitHost operations must be thread-safe
32+
type emitHost struct {
33+
program *Program
34+
35+
commonSourceDirectory string
36+
commonSourceDirectoryMutex sync.Mutex
37+
}
38+
39+
func (host *emitHost) Options() *core.CompilerOptions { return host.program.Options() }
40+
func (host *emitHost) SourceFiles() []*ast.SourceFile { return host.program.SourceFiles() }
41+
func (host *emitHost) GetCurrentDirectory() string { return host.program.host.GetCurrentDirectory() }
42+
func (host *emitHost) CommonSourceDirectory() string {
43+
if len(host.commonSourceDirectory) > 0 {
44+
return host.commonSourceDirectory
45+
}
46+
47+
host.commonSourceDirectoryMutex.Lock()
48+
defer host.commonSourceDirectoryMutex.Unlock()
49+
50+
// double-check after lock
51+
if len(host.commonSourceDirectory) > 0 {
52+
return host.commonSourceDirectory
53+
}
54+
55+
host.commonSourceDirectory = host.program.CommonSourceDirectory()
56+
return host.commonSourceDirectory
57+
}
58+
func (host *emitHost) UseCaseSensitiveFileNames() bool {
59+
return host.program.host.FS().UseCaseSensitiveFileNames()
60+
}
61+
func (host *emitHost) IsEmitBlocked(file string) bool {
62+
// !!!
63+
return false
64+
}
65+
func (host *emitHost) WriteFile(fileName string, text string, writeByteOrderMark bool, _ []*ast.SourceFile, _ *WriteFileData) error {
66+
return host.program.host.FS().WriteFile(fileName, text, writeByteOrderMark)
67+
}

0 commit comments

Comments
 (0)