Skip to content

Commit ce48eb7

Browse files
authoredMar 19, 2025
Add extra diagnostics to tsgo (microsoft#581)
1 parent 10aed3e commit ce48eb7

File tree

5 files changed

+57
-6
lines changed

5 files changed

+57
-6
lines changed
 

‎cmd/tsgo/main.go

+14-2
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,13 @@ func main() {
248248
var stats table
249249

250250
stats.add("Files", len(program.SourceFiles()))
251+
stats.add("Lines", program.LineCount())
252+
stats.add("Identifiers", program.IdentifierCount())
253+
stats.add("Symbols", program.SymbolCount())
251254
stats.add("Types", program.TypeCount())
255+
stats.add("Instantiations", program.InstantiationCount())
256+
stats.add("Memory used", fmt.Sprintf("%vK", memStats.Alloc/1024))
257+
stats.add("Memory allocs", strconv.FormatUint(memStats.Mallocs, 10))
252258
stats.add("Parse time", parseTime)
253259
if bindTime != 0 {
254260
stats.add("Bind time", bindTime)
@@ -260,8 +266,6 @@ func main() {
260266
stats.add("Emit time", emitTime)
261267
}
262268
stats.add("Total time", totalTime)
263-
stats.add("Memory used", fmt.Sprintf("%vK", memStats.Alloc/1024))
264-
stats.add("Memory allocs", strconv.FormatUint(memStats.Mallocs, 10))
265269

266270
stats.print()
267271
}
@@ -299,6 +303,14 @@ func formatDuration(d time.Duration) string {
299303
return fmt.Sprintf("%.3fs", d.Seconds())
300304
}
301305

306+
func identifierCount(p *ts.Program) int {
307+
count := 0
308+
for _, file := range p.SourceFiles() {
309+
count += file.IdentifierCount
310+
}
311+
return count
312+
}
313+
302314
func listFiles(p *ts.Program) {
303315
for _, file := range p.SourceFiles() {
304316
fmt.Println(file.FileName())

‎internal/ast/ast.go

+1
Original file line numberDiff line numberDiff line change
@@ -8689,6 +8689,7 @@ type SourceFile struct {
86898689
HasNoDefaultLib bool
86908690
UsesUriStyleNodeCoreModules core.Tristate
86918691
Identifiers map[string]string
8692+
IdentifierCount int
86928693
Imports []*LiteralLikeNode // []LiteralLikeNode
86938694
ModuleAugmentations []*ModuleName // []ModuleName
86948695
AmbientModuleNames []string

‎internal/checker/checker.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -531,8 +531,8 @@ type Checker struct {
531531
fileIndexMap map[*ast.SourceFile]int
532532
compareSymbols func(*ast.Symbol, *ast.Symbol) int
533533
TypeCount uint32
534-
symbolCount uint32
535-
totalInstantiationCount uint32
534+
SymbolCount uint32
535+
TotalInstantiationCount uint32
536536
instantiationCount uint32
537537
instantiationDepth uint32
538538
inlineLevel int
@@ -13041,7 +13041,7 @@ func (c *Checker) hasParseDiagnostics(sourceFile *ast.SourceFile) bool {
1304113041
}
1304213042

1304313043
func (c *Checker) newSymbol(flags ast.SymbolFlags, name string) *ast.Symbol {
13044-
c.symbolCount++
13044+
c.SymbolCount++
1304513045
result := c.symbolPool.New()
1304613046
result.Flags = flags | ast.SymbolFlagsTransient
1304713047
result.Name = name
@@ -20255,7 +20255,7 @@ func (c *Checker) instantiateTypeWithAlias(t *Type, m *TypeMapper, alias *TypeAl
2025520255
c.error(c.currentNode, diagnostics.Type_instantiation_is_excessively_deep_and_possibly_infinite)
2025620256
return c.errorType
2025720257
}
20258-
c.totalInstantiationCount++
20258+
c.TotalInstantiationCount++
2025920259
c.instantiationCount++
2026020260
c.instantiationDepth++
2026120261
result := c.instantiateTypeWorker(t, m, alias)

‎internal/compiler/program.go

+35
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,33 @@ func (p *Program) getDiagnosticsHelper(sourceFile *ast.SourceFile, ensureBound b
400400
return SortAndDeduplicateDiagnostics(result)
401401
}
402402

403+
func (p *Program) LineCount() int {
404+
var count int
405+
for _, file := range p.files {
406+
count += len(file.LineMap())
407+
}
408+
return count
409+
}
410+
411+
func (p *Program) IdentifierCount() int {
412+
var count int
413+
for _, file := range p.files {
414+
count += file.IdentifierCount
415+
}
416+
return count
417+
}
418+
419+
func (p *Program) SymbolCount() int {
420+
var count int
421+
for _, file := range p.files {
422+
count += file.SymbolCount
423+
}
424+
for _, checker := range p.checkers {
425+
count += int(checker.SymbolCount)
426+
}
427+
return count
428+
}
429+
403430
func (p *Program) TypeCount() int {
404431
var count int
405432
for _, checker := range p.checkers {
@@ -408,6 +435,14 @@ func (p *Program) TypeCount() int {
408435
return count
409436
}
410437

438+
func (p *Program) InstantiationCount() int {
439+
var count int
440+
for _, checker := range p.checkers {
441+
count += int(checker.TotalInstantiationCount)
442+
}
443+
return count
444+
}
445+
411446
func (p *Program) PrintSourceFileWithTypes() {
412447
for _, file := range p.files {
413448
if tspath.GetBaseFileName(file.FileName()) == "main.ts" {

‎internal/parser/parser.go

+3
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ type Parser struct {
6868
hasDeprecatedTag bool
6969

7070
identifiers map[string]string
71+
identifierCount int
7172
notParenthesizedArrow core.Set[int]
7273
nodeSlicePool core.Pool[*ast.Node]
7374
jsdocCache map[*ast.Node][]*ast.Node
@@ -326,6 +327,7 @@ func (p *Parser) finishSourceFile(result *ast.SourceFile, isDeclarationFile bool
326327
result.ScriptKind = p.scriptKind
327328
result.Flags |= p.sourceFlags
328329
result.Identifiers = p.identifiers
330+
result.IdentifierCount = p.identifierCount
329331
result.SetJSDocCache(p.jsdocCache)
330332
p.jsdocCache = nil
331333
p.identifiers = nil
@@ -2812,6 +2814,7 @@ func (p *Parser) parseRightSideOfDot(allowIdentifierNames bool, allowPrivateIden
28122814
}
28132815

28142816
func (p *Parser) newIdentifier(text string) *ast.Node {
2817+
p.identifierCount++
28152818
id := p.factory.NewIdentifier(text)
28162819
if text == "await" {
28172820
p.statementHasAwaitIdentifier = true

0 commit comments

Comments
 (0)
Please sign in to comment.