Skip to content

Commit 405d19f

Browse files
committed
Feedback
1 parent 953be0d commit 405d19f

File tree

13 files changed

+287
-271
lines changed

13 files changed

+287
-271
lines changed

cmd/tsgo/main.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,6 @@ func runMain() int {
2020
return runAPI(args[1:])
2121
}
2222
}
23-
status, _, _ := execute.CommandLine(newSystem(), args, false)
24-
return int(status)
23+
result := execute.CommandLine(newSystem(), args, false)
24+
return int(result.Status)
2525
}

internal/collections/set.go

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,7 @@ func (s *Set[T]) Equals(other *Set[T]) bool {
6565
if s == nil || other == nil {
6666
return false
6767
}
68-
if s.Len() != other.Len() {
69-
return false
70-
}
71-
for key := range s.M {
72-
if !other.Has(key) {
73-
return false
74-
}
75-
}
76-
return true
68+
return maps.Equal(s.M, other.M)
7769
}
7870

7971
func NewSetFromItems[T comparable](items ...T) *Set[T] {

internal/collections/syncset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ func (s *SyncSet[T]) Range(fn func(key T) bool) {
2323
})
2424
}
2525

26-
func (s *SyncSet[T]) ToArray() []T {
26+
func (s *SyncSet[T]) ToSlice() []T {
2727
var arr []T
2828
arr = make([]T, 0, s.m.Size())
2929
s.m.Range(func(key T, value struct{}) bool {

internal/compiler/emitter.go

Lines changed: 0 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,7 @@
11
package compiler
22

33
import (
4-
"context"
54
"encoding/base64"
6-
"slices"
7-
"time"
85

96
"github.com/microsoft/typescript-go/internal/ast"
107
"github.com/microsoft/typescript-go/internal/binder"
@@ -469,93 +466,3 @@ func getDeclarationDiagnostics(host EmitHost, file *ast.SourceFile) []*ast.Diagn
469466
transform.TransformSourceFile(file)
470467
return transform.GetDiagnostics()
471468
}
472-
473-
type AnyProgram interface {
474-
Options() *core.CompilerOptions
475-
GetSourceFiles() []*ast.SourceFile
476-
GetConfigFileParsingDiagnostics() []*ast.Diagnostic
477-
GetSyntacticDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
478-
GetBindDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
479-
GetOptionsDiagnostics(ctx context.Context) []*ast.Diagnostic
480-
GetProgramDiagnostics() []*ast.Diagnostic
481-
GetGlobalDiagnostics(ctx context.Context) []*ast.Diagnostic
482-
GetSemanticDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
483-
GetDeclarationDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
484-
Emit(ctx context.Context, options EmitOptions) *EmitResult
485-
}
486-
487-
func HandleNoEmitOnError(ctx context.Context, program AnyProgram, file *ast.SourceFile) *EmitResult {
488-
if !program.Options().NoEmitOnError.IsTrue() {
489-
return nil // No emit on error is not set, so we can proceed with emitting
490-
}
491-
492-
diagnostics := GetDiagnosticsOfAnyProgram(ctx, program, file, true, func(name string, start bool, nameStart time.Time) time.Time { return time.Time{} })
493-
if len(diagnostics) == 0 {
494-
return nil // No diagnostics, so we can proceed with emitting
495-
}
496-
return &EmitResult{
497-
Diagnostics: diagnostics,
498-
EmitSkipped: true,
499-
}
500-
}
501-
502-
func GetDiagnosticsOfAnyProgram(
503-
ctx context.Context,
504-
program AnyProgram,
505-
file *ast.SourceFile,
506-
skipNoEmitCheckForDtsDiagnostics bool,
507-
recordTime func(name string, start bool, nameStart time.Time) time.Time,
508-
) []*ast.Diagnostic {
509-
allDiagnostics := slices.Clip(program.GetConfigFileParsingDiagnostics())
510-
configFileParsingDiagnosticsLength := len(allDiagnostics)
511-
512-
allDiagnostics = append(allDiagnostics, program.GetSyntacticDiagnostics(ctx, file)...)
513-
allDiagnostics = append(allDiagnostics, program.GetProgramDiagnostics()...)
514-
515-
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
516-
// Options diagnostics include global diagnostics (even though we collect them separately),
517-
// and global diagnostics create checkers, which then bind all of the files. Do this binding
518-
// early so we can track the time.
519-
bindStart := recordTime("bind", true, time.Time{})
520-
_ = program.GetBindDiagnostics(ctx, file)
521-
recordTime("bind", false, bindStart)
522-
523-
allDiagnostics = append(allDiagnostics, program.GetOptionsDiagnostics(ctx)...)
524-
525-
if program.Options().ListFilesOnly.IsFalseOrUnknown() {
526-
allDiagnostics = append(allDiagnostics, program.GetGlobalDiagnostics(ctx)...)
527-
528-
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
529-
// !!! add program diagnostics here instead of merging with the semantic diagnostics for better api usage with with incremental and
530-
checkStart := recordTime("check", true, time.Time{})
531-
allDiagnostics = append(allDiagnostics, program.GetSemanticDiagnostics(ctx, file)...)
532-
recordTime("check", false, checkStart)
533-
}
534-
535-
if (skipNoEmitCheckForDtsDiagnostics || program.Options().NoEmit.IsTrue()) && program.Options().GetEmitDeclarations() && len(allDiagnostics) == configFileParsingDiagnosticsLength {
536-
allDiagnostics = append(allDiagnostics, program.GetDeclarationDiagnostics(ctx, file)...)
537-
}
538-
}
539-
}
540-
return allDiagnostics
541-
}
542-
543-
func CombineEmitResults(results []*EmitResult) *EmitResult {
544-
result := &EmitResult{}
545-
for _, emitResult := range results {
546-
if emitResult == nil {
547-
continue // Skip nil results
548-
}
549-
if emitResult.EmitSkipped {
550-
result.EmitSkipped = true
551-
}
552-
result.Diagnostics = append(result.Diagnostics, emitResult.Diagnostics...)
553-
if emitResult.EmittedFiles != nil {
554-
result.EmittedFiles = append(result.EmittedFiles, emitResult.EmittedFiles...)
555-
}
556-
if emitResult.SourceMaps != nil {
557-
result.SourceMaps = append(result.SourceMaps, emitResult.SourceMaps...)
558-
}
559-
}
560-
return result
561-
}

internal/compiler/host.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package compiler
22

33
import (
4+
"sync"
5+
46
"github.com/microsoft/typescript-go/internal/ast"
57
"github.com/microsoft/typescript-go/internal/collections"
68
"github.com/microsoft/typescript-go/internal/core"
@@ -23,10 +25,11 @@ type CompilerHost interface {
2325
var _ CompilerHost = (*compilerHost)(nil)
2426

2527
type compilerHost struct {
26-
currentDirectory string
27-
fs vfs.FS
28-
defaultLibraryPath string
29-
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]
28+
currentDirectory string
29+
fs vfs.FS
30+
defaultLibraryPath string
31+
extendedConfigCache *collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]
32+
extendedConfigCacheOnce sync.Once
3033
}
3134

3235
func NewCachedFSCompilerHost(
@@ -77,9 +80,11 @@ func (h *compilerHost) GetSourceFile(opts ast.SourceFileParseOptions) *ast.Sourc
7780
}
7881

7982
func (h *compilerHost) GetResolvedProjectReference(fileName string, path tspath.Path) *tsoptions.ParsedCommandLine {
80-
if h.extendedConfigCache == nil {
81-
h.extendedConfigCache = &collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]{}
82-
}
83+
h.extendedConfigCacheOnce.Do(func() {
84+
if h.extendedConfigCache == nil {
85+
h.extendedConfigCache = &collections.SyncMap[tspath.Path, *tsoptions.ExtendedConfigCacheEntry]{}
86+
}
87+
})
8388
commandLine, _ := tsoptions.GetParsedCommandLineOfConfigFilePath(fileName, path, nil, h, h.extendedConfigCache)
8489
return commandLine
8590
}

internal/compiler/program.go

Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1292,6 +1292,99 @@ func (p *Program) Emit(ctx context.Context, options EmitOptions) *EmitResult {
12921292
}))
12931293
}
12941294

1295+
func CombineEmitResults(results []*EmitResult) *EmitResult {
1296+
result := &EmitResult{}
1297+
for _, emitResult := range results {
1298+
if emitResult == nil {
1299+
continue // Skip nil results
1300+
}
1301+
if emitResult.EmitSkipped {
1302+
result.EmitSkipped = true
1303+
}
1304+
result.Diagnostics = append(result.Diagnostics, emitResult.Diagnostics...)
1305+
if emitResult.EmittedFiles != nil {
1306+
result.EmittedFiles = append(result.EmittedFiles, emitResult.EmittedFiles...)
1307+
}
1308+
if emitResult.SourceMaps != nil {
1309+
result.SourceMaps = append(result.SourceMaps, emitResult.SourceMaps...)
1310+
}
1311+
}
1312+
return result
1313+
}
1314+
1315+
type ProgramLike interface {
1316+
Options() *core.CompilerOptions
1317+
GetSourceFiles() []*ast.SourceFile
1318+
GetConfigFileParsingDiagnostics() []*ast.Diagnostic
1319+
GetSyntacticDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
1320+
GetBindDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
1321+
GetOptionsDiagnostics(ctx context.Context) []*ast.Diagnostic
1322+
GetProgramDiagnostics() []*ast.Diagnostic
1323+
GetGlobalDiagnostics(ctx context.Context) []*ast.Diagnostic
1324+
GetSemanticDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
1325+
GetDeclarationDiagnostics(ctx context.Context, file *ast.SourceFile) []*ast.Diagnostic
1326+
Emit(ctx context.Context, options EmitOptions) *EmitResult
1327+
}
1328+
1329+
func HandleNoEmitOnError(ctx context.Context, program ProgramLike, file *ast.SourceFile) *EmitResult {
1330+
if !program.Options().NoEmitOnError.IsTrue() {
1331+
return nil // No emit on error is not set, so we can proceed with emitting
1332+
}
1333+
1334+
diagnostics := GetDiagnosticsOfAnyProgram(
1335+
ctx,
1336+
program,
1337+
file,
1338+
true,
1339+
program.GetBindDiagnostics,
1340+
program.GetSemanticDiagnostics,
1341+
)
1342+
if len(diagnostics) == 0 {
1343+
return nil // No diagnostics, so we can proceed with emitting
1344+
}
1345+
return &EmitResult{
1346+
Diagnostics: diagnostics,
1347+
EmitSkipped: true,
1348+
}
1349+
}
1350+
1351+
func GetDiagnosticsOfAnyProgram(
1352+
ctx context.Context,
1353+
program ProgramLike,
1354+
file *ast.SourceFile,
1355+
skipNoEmitCheckForDtsDiagnostics bool,
1356+
getBindDiagnostics func(context.Context, *ast.SourceFile) []*ast.Diagnostic,
1357+
getSemanticDiagnostics func(context.Context, *ast.SourceFile) []*ast.Diagnostic,
1358+
) []*ast.Diagnostic {
1359+
allDiagnostics := slices.Clip(program.GetConfigFileParsingDiagnostics())
1360+
configFileParsingDiagnosticsLength := len(allDiagnostics)
1361+
1362+
allDiagnostics = append(allDiagnostics, program.GetSyntacticDiagnostics(ctx, file)...)
1363+
allDiagnostics = append(allDiagnostics, program.GetProgramDiagnostics()...)
1364+
1365+
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
1366+
// Options diagnostics include global diagnostics (even though we collect them separately),
1367+
// and global diagnostics create checkers, which then bind all of the files. Do this binding
1368+
// early so we can track the time.
1369+
getBindDiagnostics(ctx, file)
1370+
1371+
allDiagnostics = append(allDiagnostics, program.GetOptionsDiagnostics(ctx)...)
1372+
1373+
if program.Options().ListFilesOnly.IsFalseOrUnknown() {
1374+
allDiagnostics = append(allDiagnostics, program.GetGlobalDiagnostics(ctx)...)
1375+
1376+
if len(allDiagnostics) == configFileParsingDiagnosticsLength {
1377+
allDiagnostics = append(allDiagnostics, getSemanticDiagnostics(ctx, file)...)
1378+
}
1379+
1380+
if (skipNoEmitCheckForDtsDiagnostics || program.Options().NoEmit.IsTrue()) && program.Options().GetEmitDeclarations() && len(allDiagnostics) == configFileParsingDiagnosticsLength {
1381+
allDiagnostics = append(allDiagnostics, program.GetDeclarationDiagnostics(ctx, file)...)
1382+
}
1383+
}
1384+
}
1385+
return allDiagnostics
1386+
}
1387+
12951388
func (p *Program) toPath(filename string) tspath.Path {
12961389
return tspath.ToPath(filename, p.GetCurrentDirectory(), p.UseCaseSensitiveFileNames())
12971390
}

internal/execute/testsys_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -181,10 +181,10 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
181181
}
182182

183183
baseline.WriteString("SemanticDiagnostics::\n")
184-
semanticDiagnostics, diagnosticsFromOldProgram, updatedSignatureKinds := program.GetTestingData(program.GetProgram())
184+
testingData := program.GetTestingData(program.GetProgram())
185185
for _, file := range program.GetProgram().GetSourceFiles() {
186-
if diagnostics, ok := semanticDiagnostics[file.Path()]; ok {
187-
if oldDiagnostics, ok := diagnosticsFromOldProgram[file.Path()]; !ok || oldDiagnostics != diagnostics {
186+
if diagnostics, ok := testingData.SemanticDiagnosticsPerFile[file.Path()]; ok {
187+
if oldDiagnostics, ok := testingData.OldProgramSemanticDiagnosticsPerFile[file.Path()]; !ok || oldDiagnostics != diagnostics {
188188
baseline.WriteString("*refresh* " + file.FileName() + "\n")
189189
}
190190
} else {
@@ -195,7 +195,7 @@ func (s *testSys) baselineProgram(baseline *strings.Builder, program *incrementa
195195
// Write signature updates
196196
baseline.WriteString("Signatures::\n")
197197
for _, file := range program.GetProgram().GetSourceFiles() {
198-
if kind, loaded := updatedSignatureKinds.Load(file.Path()); loaded {
198+
if kind, ok := testingData.UpdatedSignatureKinds[file.Path()]; ok {
199199
switch kind {
200200
case incremental.SignatureUpdateKindComputedDts:
201201
baseline.WriteString("(computed .d.ts) " + file.FileName() + "\n")

0 commit comments

Comments
 (0)