MCPcopy Index your code
hub / github.com/microsoft/typescript-go / CreateProgram

Method CreateProgram

internal/project/project.go:351–415  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

349}
350
351func (p *Project) CreateProgram() CreateProgramResult {
352 updateKind := ProgramUpdateKindNewFiles
353 var programCloned bool
354 var newProgram *compiler.Program
355
356 // Define a fresh CreateCheckerPool closure for this call. Each invocation of
357 // CreateProgram must use its own closure so that concurrent goroutines cloning
358 // the same project never share a captured variable through a stale closure
359 // stored in the old program's options.
360 createCheckerPool := func(program *compiler.Program) compiler.CheckerPool {
361 return newCheckerPool(p.host.sessionOptions.CheckerPoolOptions, program, p.log)
362 }
363
364 // Create the command line, potentially augmented with typing files
365 commandLine := p.getCommandLineWithTypingsFiles()
366
367 if p.dirtyFilePath != "" && p.Program != nil && p.Program.CommandLine() == commandLine {
368 var dirtyFile *ast.SourceFile
369 newProgram, dirtyFile, programCloned = p.Program.UpdateProgram(p.dirtyFilePath, p.host, createCheckerPool)
370 if programCloned {
371 updateKind = ProgramUpdateKindCloned
372 for _, file := range newProgram.SourceFiles() {
373 // Use pointer identity: dirtyFile is the exact instance UpdateProgram acquired,
374 // and it is the only file whose refcount is already accounted for.
375 if file != dirtyFile {
376 // UpdateProgram acquired the changed file only, so we need to ref everything else
377 p.host.builder.parseCache.Ref(NewParseCacheKey(file.ParseOptions(), file.Hash, file.ScriptKind))
378 }
379 }
380 for _, file := range newProgram.DuplicateSourceFiles() {
381 p.host.builder.parseCache.Ref(NewParseCacheKey(file.ParseOptions, file.Hash, file.ScriptKind))
382 }
383 } else if dirtyFile != nil {
384 // UpdateProgram always acquires the dirty file before deciding whether it can
385 // reuse the old program. If it falls back to a full rebuild, release that
386 // speculative acquire so the rebuilt program is the only remaining owner.
387 p.host.builder.parseCache.Deref(NewParseCacheKey(dirtyFile.ParseOptions(), dirtyFile.Hash, dirtyFile.ScriptKind))
388 }
389 } else {
390 var typingsLocation string
391 if p.GetTypeAcquisition().Enable.IsTrue() {
392 typingsLocation = p.host.sessionOptions.TypingsLocation
393 }
394 newProgram = compiler.NewProgram(
395 compiler.ProgramOptions{
396 Host: p.host,
397 Config: commandLine,
398 UseSourceOfProjectReference: true,
399 TypingsLocation: typingsLocation,
400 CreateCheckerPool: createCheckerPool,
401 },
402 )
403 }
404
405 if !programCloned && p.Program != nil && p.Program.HasSameFileNames(newProgram) {
406 updateKind = ProgramUpdateKindSameFileNames
407 }
408

Callers 1

updateProgramMethod · 0.80

Implementers 1

Projectinternal/project/project.go

Calls 15

SourceFilesMethod · 0.95
ParseOptionsMethod · 0.95
DuplicateSourceFilesMethod · 0.95
GetTypeAcquisitionMethod · 0.95
BindSourceFilesMethod · 0.95
NewProgramFunction · 0.92
NewParseCacheKeyFunction · 0.85
CommandLineMethod · 0.80
UpdateProgramMethod · 0.80
IsTrueMethod · 0.80
HasSameFileNamesMethod · 0.80

Tested by

no test coverage detected