@@ -2,7 +2,10 @@ package incremental
2
2
3
3
import (
4
4
"context"
5
+ "crypto/sha256"
6
+ "fmt"
5
7
"maps"
8
+ "strings"
6
9
"sync"
7
10
8
11
"github.com/microsoft/typescript-go/internal/ast"
@@ -213,6 +216,9 @@ type snapshot struct {
213
216
allFilesExcludingDefaultLibraryFileOnce sync.Once
214
217
// Cache of all files excluding default library file for the current program
215
218
allFilesExcludingDefaultLibraryFile []* ast.SourceFile
219
+
220
+ // Used with testing to add text of hash for better comparison
221
+ hashWithText bool
216
222
}
217
223
218
224
func (s * snapshot ) createEmitSignaturesMap () {
@@ -257,14 +263,66 @@ func (s *snapshot) getAllFilesExcludingDefaultLibraryFile(program *compiler.Prog
257
263
return s .allFilesExcludingDefaultLibraryFile
258
264
}
259
265
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 {
261
318
if oldProgram != nil && oldProgram .program == program {
262
319
return oldProgram .snapshot
263
320
}
264
321
files := program .GetSourceFiles ()
265
322
snapshot := & snapshot {
266
323
options : program .Options (),
267
324
semanticDiagnosticsPerFile : make (map [tspath.Path ]* diagnosticsOrBuildInfoDiagnosticsWithFileName , len (files )),
325
+ hashWithText : hashWithText ,
268
326
}
269
327
if oldProgram != nil && snapshot .options .Composite .IsTrue () {
270
328
snapshot .latestChangedDtsFile = oldProgram .snapshot .latestChangedDtsFile
@@ -304,7 +362,7 @@ func newSnapshotForProgram(program *compiler.Program, oldProgram *Program) *snap
304
362
snapshot .options .SkipDefaultLibCheck .IsTrue () == oldProgram .snapshot .options .SkipDefaultLibCheck .IsTrue ()
305
363
snapshot .fileInfos = make (map [tspath.Path ]* fileInfo , len (files ))
306
364
for _ , file := range files {
307
- version := computeHash (file .Text ())
365
+ version := snapshot . computeHash (file .Text ())
308
366
impliedNodeFormat := program .GetSourceFileMetaData (file .Path ()).ImpliedNodeFormat
309
367
affectsGlobalScope := fileAffectsGlobalScope (file )
310
368
var signature string
0 commit comments