@@ -2,7 +2,10 @@ package incremental
22
33import (
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
218224func (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