Skip to content

Commit 7fbbe04

Browse files
authored
Build mode (#1484)
1 parent a6bad90 commit 7fbbe04

File tree

432 files changed

+119474
-2901
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

432 files changed

+119474
-2901
lines changed

cmd/tsgo/sys.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"time"
88

99
"github.com/microsoft/typescript-go/internal/bundled"
10-
"github.com/microsoft/typescript-go/internal/execute"
10+
"github.com/microsoft/typescript-go/internal/execute/tsc"
1111
"github.com/microsoft/typescript-go/internal/tspath"
1212
"github.com/microsoft/typescript-go/internal/vfs"
1313
"github.com/microsoft/typescript-go/internal/vfs/osvfs"
@@ -63,7 +63,7 @@ func newSystem() *osSys {
6363
cwd, err := os.Getwd()
6464
if err != nil {
6565
fmt.Fprintf(os.Stderr, "Error getting current directory: %v\n", err)
66-
os.Exit(int(execute.ExitStatusInvalidProject_OutputsSkipped))
66+
os.Exit(int(tsc.ExitStatusInvalidProject_OutputsSkipped))
6767
}
6868

6969
return &osSys{

internal/api/server.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import (
99
"runtime/debug"
1010
"strconv"
1111
"sync"
12+
"time"
1213

1314
"github.com/go-json-experiment/json"
1415
"github.com/microsoft/typescript-go/internal/bundled"
@@ -489,3 +490,8 @@ func (s *Server) Stat(path string) vfs.FileInfo {
489490
func (s *Server) Remove(path string) error {
490491
panic("unimplemented")
491492
}
493+
494+
// Chtimes implements vfs.FS.
495+
func (s *Server) Chtimes(path string, aTime time.Time, mTime time.Time) error {
496+
panic("unimplemented")
497+
}

internal/bundled/embed.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -161,6 +161,13 @@ func (vfs *wrappedFS) Remove(path string) error {
161161
return vfs.fs.Remove(path)
162162
}
163163

164+
func (vfs *wrappedFS) Chtimes(path string, aTime time.Time, mTime time.Time) error {
165+
if _, ok := splitPath(path); ok {
166+
panic("cannot change times on embedded file system")
167+
}
168+
return vfs.fs.Chtimes(path, aTime, mTime)
169+
}
170+
164171
type fileInfo struct {
165172
mode fs.FileMode
166173
name string

internal/checker/checker.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -540,7 +540,7 @@ type Program interface {
540540
SourceFileMayBeEmitted(sourceFile *ast.SourceFile, forceDtsEmit bool) bool
541541
IsSourceFromProjectReference(path tspath.Path) bool
542542
IsSourceFileDefaultLibrary(path tspath.Path) bool
543-
GetSourceAndProjectReference(path tspath.Path) *tsoptions.SourceAndProjectReference
543+
GetProjectReferenceFromOutputDts(path tspath.Path) *tsoptions.SourceOutputAndProjectReference
544544
GetRedirectForResolution(file ast.HasFileName) *tsoptions.ParsedCommandLine
545545
CommonSourceDirectory() string
546546
}
@@ -6483,7 +6483,7 @@ func (c *Checker) checkAliasSymbol(node *ast.Node) {
64836483
}
64846484
if c.compilerOptions.VerbatimModuleSyntax.IsTrue() && !ast.IsTypeOnlyImportOrExportDeclaration(node) && node.Flags&ast.NodeFlagsAmbient == 0 && targetFlags&ast.SymbolFlagsConstEnum != 0 {
64856485
constEnumDeclaration := target.ValueDeclaration
6486-
redirect := c.program.GetSourceAndProjectReference(ast.GetSourceFileOfNode(constEnumDeclaration).Path())
6486+
redirect := c.program.GetProjectReferenceFromOutputDts(ast.GetSourceFileOfNode(constEnumDeclaration).Path())
64876487
if constEnumDeclaration.Flags&ast.NodeFlagsAmbient != 0 && (redirect == nil || !redirect.Resolved.CompilerOptions().ShouldPreserveConstEnums()) {
64886488
c.error(node, diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, c.getIsolatedModulesLikeFlagName())
64896489
}
@@ -7204,7 +7204,7 @@ func (c *Checker) checkConstEnumAccess(node *ast.Node, t *Type) {
72047204
if c.compilerOptions.IsolatedModules.IsTrue() || c.compilerOptions.VerbatimModuleSyntax.IsTrue() && ok && c.resolveName(node, ast.GetFirstIdentifier(node).Text(), ast.SymbolFlagsAlias, nil, false, true) == nil {
72057205
debug.Assert(t.symbol.Flags&ast.SymbolFlagsConstEnum != 0)
72067206
constEnumDeclaration := t.symbol.ValueDeclaration
7207-
redirect := c.program.GetSourceAndProjectReference(ast.GetSourceFileOfNode(constEnumDeclaration).Path())
7207+
redirect := c.program.GetProjectReferenceFromOutputDts(ast.GetSourceFileOfNode(constEnumDeclaration).Path())
72087208
if constEnumDeclaration.Flags&ast.NodeFlagsAmbient != 0 && !ast.IsValidTypeOnlyAliasUseSite(node) && (redirect == nil || !redirect.Resolved.CompilerOptions().ShouldPreserveConstEnums()) {
72097209
c.error(node, diagnostics.Cannot_access_ambient_const_enums_when_0_is_enabled, c.getIsolatedModulesLikeFlagName())
72107210
}
@@ -14636,7 +14636,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1463614636
}
1463714637

1463814638
var message *diagnostics.Message
14639-
if overrideHost != nil && overrideHost.Kind == ast.KindImportDeclaration && overrideHost.AsImportDeclaration().ImportClause.IsTypeOnly() {
14639+
if overrideHost != nil && overrideHost.Kind == ast.KindImportDeclaration && overrideHost.AsImportDeclaration().ImportClause != nil && overrideHost.AsImportDeclaration().ImportClause.IsTypeOnly() {
1464014640
message = diagnostics.Type_only_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute
1464114641
} else if overrideHost != nil && overrideHost.Kind == ast.KindImportType {
1464214642
message = diagnostics.Type_import_of_an_ECMAScript_module_from_a_CommonJS_module_must_have_a_resolution_mode_attribute
@@ -14689,7 +14689,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1468914689
if moduleNotFoundError != nil {
1469014690
// See if this was possibly a projectReference redirect
1469114691
if resolvedModule.IsResolved() {
14692-
redirect := c.program.GetOutputAndProjectReference(tspath.ToPath(resolvedModule.ResolvedFileName, c.program.GetCurrentDirectory(), c.program.UseCaseSensitiveFileNames()))
14692+
redirect := c.program.GetProjectReferenceFromSource(tspath.ToPath(resolvedModule.ResolvedFileName, c.program.GetCurrentDirectory(), c.program.UseCaseSensitiveFileNames()))
1469314693
if redirect != nil && redirect.OutputDts != "" {
1469414694
c.error(
1469514695
errorNode,

internal/collections/syncset.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ func (s *SyncSet[T]) Has(key T) bool {
1212
}
1313

1414
func (s *SyncSet[T]) Add(key T) {
15-
s.m.Store(key, struct{}{})
15+
s.AddIfAbsent(key)
1616
}
1717

1818
// AddIfAbsent adds the key to the set if it is not already present

internal/compiler/emitHost.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,8 @@ func (host *emitHost) GetSourceOfProjectReferenceIfOutputIncluded(file ast.HasFi
7878
return host.program.GetSourceOfProjectReferenceIfOutputIncluded(file)
7979
}
8080

81-
func (host *emitHost) GetOutputAndProjectReference(path tspath.Path) *tsoptions.OutputDtsAndProjectReference {
82-
return host.program.GetOutputAndProjectReference(path)
81+
func (host *emitHost) GetProjectReferenceFromSource(path tspath.Path) *tsoptions.SourceOutputAndProjectReference {
82+
return host.program.GetProjectReferenceFromSource(path)
8383
}
8484

8585
func (host *emitHost) GetRedirectTargets(path tspath.Path) []string {

internal/compiler/emitter.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,6 @@ type emitter struct {
4343
}
4444

4545
func (e *emitter) emit() {
46-
if e.host.Options().ListEmittedFiles.IsTrue() {
47-
e.emitResult.EmittedFiles = []string{}
48-
}
4946
// !!! tracing
5047
e.emitJSFile(e.sourceFile, e.paths.JsFilePath(), e.paths.SourceMapFilePath())
5148
e.emitDeclarationFile(e.sourceFile, e.paths.DeclarationFilePath(), e.paths.DeclarationMapPath())
@@ -268,7 +265,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
268265
err := e.host.WriteFile(sourceMapFilePath, sourceMap, false /*writeByteOrderMark*/)
269266
if err != nil {
270267
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
271-
} else if e.emitResult.EmittedFiles != nil {
268+
} else {
272269
e.emitResult.EmittedFiles = append(e.emitResult.EmittedFiles, sourceMapFilePath)
273270
}
274271
}
@@ -292,7 +289,7 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
292289
}
293290
if err != nil {
294291
e.emitterDiagnostics.Add(ast.NewCompilerDiagnostic(diagnostics.Could_not_write_file_0_Colon_1, jsFilePath, err.Error()))
295-
} else if e.emitResult.EmittedFiles != nil && !skippedDtsWrite {
292+
} else if !skippedDtsWrite {
296293
e.emitResult.EmittedFiles = append(e.emitResult.EmittedFiles, jsFilePath)
297294
}
298295

@@ -391,7 +388,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
391388

392389
type SourceFileMayBeEmittedHost interface {
393390
Options() *core.CompilerOptions
394-
GetOutputAndProjectReference(path tspath.Path) *tsoptions.OutputDtsAndProjectReference
391+
GetProjectReferenceFromSource(path tspath.Path) *tsoptions.SourceOutputAndProjectReference
395392
IsSourceFileFromExternalLibrary(file *ast.SourceFile) bool
396393
GetCurrentDirectory() string
397394
UseCaseSensitiveFileNames() bool
@@ -424,7 +421,7 @@ func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host SourceFileMayBeEmit
424421

425422
// Check other conditions for file emit
426423
// Source files from referenced projects are not emitted
427-
if host.GetOutputAndProjectReference(sourceFile.Path()) != nil {
424+
if host.GetProjectReferenceFromSource(sourceFile.Path()) != nil {
428425
return false
429426
}
430427

internal/compiler/fileInclude.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"github.com/microsoft/typescript-go/internal/core"
99
"github.com/microsoft/typescript-go/internal/diagnostics"
1010
"github.com/microsoft/typescript-go/internal/module"
11+
"github.com/microsoft/typescript-go/internal/scanner"
1112
"github.com/microsoft/typescript-go/internal/tsoptions"
1213
"github.com/microsoft/typescript-go/internal/tspath"
1314
)
@@ -57,7 +58,11 @@ type referenceFileLocation struct {
5758

5859
func (r *referenceFileLocation) text() string {
5960
if r.node != nil {
60-
return r.node.Text()
61+
if !ast.NodeIsSynthesized(r.node) {
62+
return r.file.Text()[scanner.SkipTrivia(r.file.Text(), r.node.Loc.Pos()):r.node.End()]
63+
} else {
64+
return fmt.Sprintf(`"%s"`, r.node.Text())
65+
}
6166
} else {
6267
return r.file.Text()[r.ref.Pos():r.ref.End()]
6368
}

internal/compiler/fileloader.go

Lines changed: 15 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -143,7 +143,10 @@ func processAllProgramFiles(
143143

144144
filesByPath := make(map[tspath.Path]*ast.SourceFile, totalFileCount)
145145
loader.includeProcessor.fileIncludeReasons = make(map[tspath.Path][]*fileIncludeReason, totalFileCount)
146-
outputFileToProjectReferenceSource := make(map[tspath.Path]string, totalFileCount)
146+
var outputFileToProjectReferenceSource map[tspath.Path]string
147+
if !opts.canUseProjectReferenceSource() {
148+
outputFileToProjectReferenceSource = make(map[tspath.Path]string, totalFileCount)
149+
}
147150
resolvedModules := make(map[tspath.Path]module.ModeAwareCache[*module.ResolvedModule], totalFileCount+1)
148151
typeResolutionsInFile := make(map[tspath.Path]module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], totalFileCount)
149152
sourceFileMetaDatas := make(map[tspath.Path]ast.SourceFileMetaData, totalFileCount)
@@ -454,9 +457,9 @@ func (p *fileLoader) resolveTypeReferenceDirectives(t *parseTask) {
454457
typeResolutionsInFile := make(module.ModeAwareCache[*module.ResolvedTypeReferenceDirective], len(file.TypeReferenceDirectives))
455458
var typeResolutionsTrace []string
456459
for index, ref := range file.TypeReferenceDirectives {
457-
redirect := p.projectReferenceFileMapper.getRedirectForResolution(file)
460+
redirect, fileName := p.projectReferenceFileMapper.getRedirectForResolution(file)
458461
resolutionMode := getModeForTypeReferenceDirectiveInFile(ref, file, meta, module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect))
459-
resolved, trace := p.resolver.ResolveTypeReferenceDirective(ref.FileName, file.FileName(), resolutionMode, redirect)
462+
resolved, trace := p.resolver.ResolveTypeReferenceDirective(ref.FileName, fileName, resolutionMode, redirect)
460463
typeResolutionsInFile[module.ModeAwareCacheKey{Name: ref.FileName, Mode: resolutionMode}] = resolved
461464
includeReason := &fileIncludeReason{
462465
kind: fileIncludeKindTypeReferenceDirective,
@@ -498,7 +501,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
498501
isJavaScriptFile := ast.IsSourceFileJS(file)
499502
isExternalModuleFile := ast.IsExternalModule(file)
500503

501-
redirect := p.projectReferenceFileMapper.getRedirectForResolution(file)
504+
redirect, fileName := p.projectReferenceFileMapper.getRedirectForResolution(file)
502505
optionsForFile := module.GetCompilerOptionsWithRedirect(p.opts.Config.CompilerOptions(), redirect)
503506
if isJavaScriptFile || (!file.IsDeclarationFile && (optionsForFile.GetIsolatedModules() || isExternalModuleFile)) {
504507
if optionsForFile.ImportHelpers.IsTrue() {
@@ -539,7 +542,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
539542
}
540543

541544
mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile)
542-
resolvedModule, trace := p.resolver.ResolveModuleName(moduleName, file.FileName(), mode, redirect)
545+
resolvedModule, trace := p.resolver.ResolveModuleName(moduleName, fileName, mode, redirect)
543546
resolutionsInFile[module.ModeAwareCacheKey{Name: moduleName, Mode: mode}] = resolvedModule
544547
resolutionsTrace = append(resolutionsTrace, trace...)
545548

@@ -550,7 +553,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) {
550553
resolvedFileName := resolvedModule.ResolvedFileName
551554
isFromNodeModulesSearch := resolvedModule.IsExternalLibraryImport
552555
// Don't treat redirected files as JS files.
553-
isJsFile := !tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsWithJsonFlat) && p.projectReferenceFileMapper.getRedirectForResolution(ast.NewHasFileName(resolvedFileName, p.toPath(resolvedFileName))) == nil
556+
isJsFile := !tspath.FileExtensionIsOneOf(resolvedFileName, tspath.SupportedTSExtensionsWithJsonFlat) && p.projectReferenceFileMapper.getRedirectParsedCommandLineForResolution(ast.NewHasFileName(resolvedFileName, p.toPath(resolvedFileName))) == nil
554557
isJsFileFromNodeModules := isFromNodeModulesSearch && isJsFile && strings.Contains(resolvedFileName, "/node_modules/")
555558

556559
// add file to program only if:
@@ -610,19 +613,19 @@ func (p *fileLoader) pathForLibFile(name string) *LibFile {
610613

611614
path := tspath.CombinePaths(p.defaultLibraryPath, name)
612615
replaced := false
613-
if p.opts.Config.CompilerOptions().LibReplacement.IsTrue() {
616+
if p.opts.Config.CompilerOptions().LibReplacement.IsTrue() && name != "lib.d.ts" {
614617
libraryName := getLibraryNameFromLibFileName(name)
615618
resolveFrom := getInferredLibraryNameResolveFrom(p.opts.Config.CompilerOptions(), p.opts.Host.GetCurrentDirectory(), name)
616619
resolution, trace := p.resolver.ResolveModuleName(libraryName, resolveFrom, core.ModuleKindCommonJS, nil)
617620
if resolution.IsResolved() {
618621
path = resolution.ResolvedFileName
619622
replaced = true
620-
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), &libResolution{
621-
libraryName: libraryName,
622-
resolution: resolution,
623-
trace: trace,
624-
})
625623
}
624+
p.pathForLibFileResolutions.LoadOrStore(p.toPath(resolveFrom), &libResolution{
625+
libraryName: libraryName,
626+
resolution: resolution,
627+
trace: trace,
628+
})
626629
}
627630

628631
libPath, _ := p.pathForLibFileCache.LoadOrStore(name, &LibFile{name, path, replaced})

internal/compiler/processingDiagnostic.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ func (d *processingDiagnostic) toDiagnostic(program *Program) *ast.Diagnostic {
6767

6868
func (d *processingDiagnostic) createDiagnosticExplainingFile(program *Program) *ast.Diagnostic {
6969
diag := d.asIncludeExplainingDiagnostic()
70-
var chain []*ast.Diagnostic
70+
var includeDetails []*ast.Diagnostic
7171
var relatedInfo []*ast.Diagnostic
7272
var redirectInfo []*ast.Diagnostic
7373
var preferredLocation *fileIncludeReason
@@ -90,15 +90,15 @@ func (d *processingDiagnostic) createDiagnosticExplainingFile(program *Program)
9090
if !seenReasons.AddIfAbsent(includeReason) {
9191
return
9292
}
93-
chain = append(chain, includeReason.toDiagnostic(program, false))
93+
includeDetails = append(includeDetails, includeReason.toDiagnostic(program, false))
9494
processRelatedInfo(includeReason)
9595
}
9696

9797
// !!! todo sheetal caching
9898

9999
if diag.file != "" {
100100
reasons := program.includeProcessor.fileIncludeReasons[diag.file]
101-
chain = make([]*ast.Diagnostic, 0, len(reasons))
101+
includeDetails = make([]*ast.Diagnostic, 0, len(reasons))
102102
for _, reason := range reasons {
103103
processInclude(reason)
104104
}
@@ -107,9 +107,10 @@ func (d *processingDiagnostic) createDiagnosticExplainingFile(program *Program)
107107
if diag.diagnosticReason != nil {
108108
processInclude(diag.diagnosticReason)
109109
}
110-
if chain != nil && (preferredLocation == nil || seenReasons.Len() != 1) {
110+
var chain []*ast.Diagnostic
111+
if includeDetails != nil && (preferredLocation == nil || seenReasons.Len() != 1) {
111112
fileReason := ast.NewCompilerDiagnostic(diagnostics.The_file_is_in_the_program_because_Colon)
112-
fileReason.SetMessageChain(chain)
113+
fileReason.SetMessageChain(includeDetails)
113114
chain = []*ast.Diagnostic{fileReason}
114115
}
115116
if redirectInfo != nil {

0 commit comments

Comments
 (0)