Skip to content

Commit f4d57f5

Browse files
committed
Write signature and its text for testing
1 parent 7d761cb commit f4d57f5

28 files changed

+1124
-1116
lines changed

internal/incremental/affectedfileshandler.go

Lines changed: 1 addition & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,8 @@ package incremental
22

33
import (
44
"context"
5-
"crypto/sha256"
6-
"fmt"
75
"maps"
86
"slices"
9-
"strings"
107
"sync"
118
"sync/atomic"
129

@@ -71,7 +68,7 @@ func (h *affectedFilesHandler) computeDtsSignature(file *ast.SourceFile) string
7168
if !tspath.IsDeclarationFileName(fileName) {
7269
panic("File extension for signature expected to be dts, got : " + fileName)
7370
}
74-
signature = computeSignatureWithDiagnostics(file, text, data)
71+
signature = h.program.snapshot.computeSignatureWithDiagnostics(file, text, data)
7572
return nil
7673
},
7774
})
@@ -394,50 +391,3 @@ func collectAllAffectedFiles(ctx context.Context, program *Program) {
394391
// Update the snapshot with the new state
395392
handler.updateSnapshot()
396393
}
397-
398-
func getTextHandlingSourceMapForSignature(text string, data *compiler.WriteFileData) string {
399-
if data.SourceMapUrlPos != -1 {
400-
return text[:data.SourceMapUrlPos]
401-
}
402-
return text
403-
}
404-
405-
func computeSignatureWithDiagnostics(file *ast.SourceFile, text string, data *compiler.WriteFileData) string {
406-
var builder strings.Builder
407-
builder.WriteString(getTextHandlingSourceMapForSignature(text, data))
408-
for _, diag := range data.Diagnostics {
409-
diagnosticToStringBuilder(diag, file, &builder)
410-
}
411-
return computeHash(builder.String())
412-
}
413-
414-
func diagnosticToStringBuilder(diagnostic *ast.Diagnostic, file *ast.SourceFile, builder *strings.Builder) string {
415-
if diagnostic == nil {
416-
return ""
417-
}
418-
builder.WriteString("\n")
419-
if diagnostic.File() != file {
420-
builder.WriteString(tspath.EnsurePathIsNonModuleName(tspath.GetRelativePathFromDirectory(
421-
tspath.GetDirectoryPath(string(file.Path())),
422-
string(diagnostic.File().Path()),
423-
tspath.ComparePathsOptions{},
424-
)))
425-
}
426-
if diagnostic.File() != nil {
427-
builder.WriteString(fmt.Sprintf("(%d,%d): ", diagnostic.Pos(), diagnostic.Len()))
428-
}
429-
builder.WriteString(diagnostic.Category().Name())
430-
builder.WriteString(fmt.Sprintf("%d: ", diagnostic.Code()))
431-
builder.WriteString(diagnostic.Message())
432-
for _, chain := range diagnostic.MessageChain() {
433-
diagnosticToStringBuilder(chain, file, builder)
434-
}
435-
for _, info := range diagnostic.RelatedInformation() {
436-
diagnosticToStringBuilder(info, file, builder)
437-
}
438-
return builder.String()
439-
}
440-
441-
func computeHash(text string) string {
442-
return fmt.Sprintf("%x", sha256.Sum256([]byte(text)))
443-
}

internal/incremental/emitfileshandler.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,7 @@ func (h *emitFilesHandler) getEmitOptions(options compiler.EmitOptions) compiler
151151
var emitSignature string
152152
info := h.program.snapshot.fileInfos[options.TargetSourceFile.Path()]
153153
if info.signature == info.version {
154-
signature := computeSignatureWithDiagnostics(options.TargetSourceFile, text, data)
154+
signature := h.program.snapshot.computeSignatureWithDiagnostics(options.TargetSourceFile, text, data)
155155
// With d.ts diagnostics they are also part of the signature so emitSignature will be different from it since its just hash of d.ts
156156
if len(data.Diagnostics) == 0 {
157157
emitSignature = signature
@@ -193,7 +193,7 @@ func (h *emitFilesHandler) skipDtsOutputOfComposite(file *ast.SourceFile, output
193193
}
194194
}
195195
if newSignature == "" {
196-
newSignature = computeHash(getTextHandlingSourceMapForSignature(text, data))
196+
newSignature = h.program.snapshot.computeHash(getTextHandlingSourceMapForSignature(text, data))
197197
}
198198
// Dont write dts files if they didn't change
199199
if newSignature == oldSignature {

internal/incremental/program.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ var _ compiler.AnyProgram = (*Program)(nil)
3434

3535
func NewProgram(program *compiler.Program, oldProgram *Program, testing bool) *Program {
3636
incrementalProgram := &Program{
37-
snapshot: newSnapshotForProgram(program, oldProgram),
37+
snapshot: newSnapshotForProgram(program, oldProgram, testing),
3838
program: program,
3939
}
4040

internal/incremental/snapshot.go

Lines changed: 60 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,10 @@ package incremental
22

33
import (
44
"context"
5+
"crypto/sha256"
6+
"fmt"
57
"maps"
8+
"strings"
69
"sync"
710

811
"github.com/microsoft/typescript-go/internal/ast"
@@ -213,6 +216,9 @@ type snapshot struct {
213216
allFilesExcludingDefaultLibraryFileOnce sync.Once
214217
// Cache of all files excluding default library file for the current program
215218
allFilesExcludingDefaultLibraryFile []*ast.SourceFile
219+
220+
// Used with testing to add text of hash for better comparison
221+
hashWithText bool
216222
}
217223

218224
func (s *snapshot) createEmitSignaturesMap() {
@@ -257,14 +263,66 @@ func (s *snapshot) getAllFilesExcludingDefaultLibraryFile(program *compiler.Prog
257263
return s.allFilesExcludingDefaultLibraryFile
258264
}
259265

260-
func newSnapshotForProgram(program *compiler.Program, oldProgram *Program) *snapshot {
266+
func getTextHandlingSourceMapForSignature(text string, data *compiler.WriteFileData) string {
267+
if data.SourceMapUrlPos != -1 {
268+
return text[:data.SourceMapUrlPos]
269+
}
270+
return text
271+
}
272+
273+
func (s *snapshot) computeSignatureWithDiagnostics(file *ast.SourceFile, text string, data *compiler.WriteFileData) string {
274+
var builder strings.Builder
275+
builder.WriteString(getTextHandlingSourceMapForSignature(text, data))
276+
for _, diag := range data.Diagnostics {
277+
diagnosticToStringBuilder(diag, file, &builder)
278+
}
279+
return s.computeHash(builder.String())
280+
}
281+
282+
func diagnosticToStringBuilder(diagnostic *ast.Diagnostic, file *ast.SourceFile, builder *strings.Builder) string {
283+
if diagnostic == nil {
284+
return ""
285+
}
286+
builder.WriteString("\n")
287+
if diagnostic.File() != file {
288+
builder.WriteString(tspath.EnsurePathIsNonModuleName(tspath.GetRelativePathFromDirectory(
289+
tspath.GetDirectoryPath(string(file.Path())),
290+
string(diagnostic.File().Path()),
291+
tspath.ComparePathsOptions{},
292+
)))
293+
}
294+
if diagnostic.File() != nil {
295+
builder.WriteString(fmt.Sprintf("(%d,%d): ", diagnostic.Pos(), diagnostic.Len()))
296+
}
297+
builder.WriteString(diagnostic.Category().Name())
298+
builder.WriteString(fmt.Sprintf("%d: ", diagnostic.Code()))
299+
builder.WriteString(diagnostic.Message())
300+
for _, chain := range diagnostic.MessageChain() {
301+
diagnosticToStringBuilder(chain, file, builder)
302+
}
303+
for _, info := range diagnostic.RelatedInformation() {
304+
diagnosticToStringBuilder(info, file, builder)
305+
}
306+
return builder.String()
307+
}
308+
309+
func (s *snapshot) computeHash(text string) string {
310+
hash := fmt.Sprintf("%x", sha256.Sum256([]byte(text)))
311+
if s.hashWithText {
312+
hash += "-" + text
313+
}
314+
return hash
315+
}
316+
317+
func newSnapshotForProgram(program *compiler.Program, oldProgram *Program, hashWithText bool) *snapshot {
261318
if oldProgram != nil && oldProgram.program == program {
262319
return oldProgram.snapshot
263320
}
264321
files := program.GetSourceFiles()
265322
snapshot := &snapshot{
266323
options: program.Options(),
267324
semanticDiagnosticsPerFile: make(map[tspath.Path]*diagnosticsOrBuildInfoDiagnosticsWithFileName, len(files)),
325+
hashWithText: hashWithText,
268326
}
269327
if oldProgram != nil && snapshot.options.Composite.IsTrue() {
270328
snapshot.latestChangedDtsFile = oldProgram.snapshot.latestChangedDtsFile
@@ -304,7 +362,7 @@ func newSnapshotForProgram(program *compiler.Program, oldProgram *Program) *snap
304362
snapshot.options.SkipDefaultLibCheck.IsTrue() == oldProgram.snapshot.options.SkipDefaultLibCheck.IsTrue()
305363
snapshot.fileInfos = make(map[tspath.Path]*fileInfo, len(files))
306364
for _, file := range files {
307-
version := computeHash(file.Text())
365+
version := snapshot.computeHash(file.Text())
308366
impliedNodeFormat := program.GetSourceFileMetaData(file.Path()).ImpliedNodeFormat
309367
affectsGlobalScope := fileAffectsGlobalScope(file)
310368
var signature string

0 commit comments

Comments
 (0)