Skip to content

Commit af2ba73

Browse files
authored
More module specifier generation (#1051)
1 parent a4132f5 commit af2ba73

File tree

308 files changed

+1400
-3880
lines changed

Some content is hidden

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

308 files changed

+1400
-3880
lines changed

internal/ast/ast.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9935,6 +9935,11 @@ type CheckJsDirective struct {
99359935
Range CommentRange
99369936
}
99379937

9938+
type HasFileName interface {
9939+
FileName() string
9940+
Path() tspath.Path
9941+
}
9942+
99389943
type SourceFile struct {
99399944
NodeBase
99409945
DeclarationBase

internal/ast/utilities.go

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2414,15 +2414,15 @@ func GetImpliedNodeFormatForFile(path string, packageJsonType string) core.Modul
24142414
return impliedNodeFormat
24152415
}
24162416

2417-
func GetEmitModuleFormatOfFileWorker(sourceFile *SourceFile, options *core.CompilerOptions, sourceFileMetaData *SourceFileMetaData) core.ModuleKind {
2418-
result := GetImpliedNodeFormatForEmitWorker(sourceFile.FileName(), options, sourceFileMetaData)
2417+
func GetEmitModuleFormatOfFileWorker(fileName string, options *core.CompilerOptions, sourceFileMetaData *SourceFileMetaData) core.ModuleKind {
2418+
result := GetImpliedNodeFormatForEmitWorker(fileName, options, sourceFileMetaData)
24192419
if result != core.ModuleKindNone {
24202420
return result
24212421
}
24222422
return options.GetEmitModuleKind()
24232423
}
24242424

2425-
func GetImpliedNodeFormatForEmitWorker(fileName string, options *core.CompilerOptions, sourceFileMetaData *SourceFileMetaData) core.ModuleKind {
2425+
func GetImpliedNodeFormatForEmitWorker(fileName string, options *core.CompilerOptions, sourceFileMetaData *SourceFileMetaData) core.ResolutionMode {
24262426
moduleKind := options.GetEmitModuleKind()
24272427
if core.ModuleKindNode16 <= moduleKind && moduleKind <= core.ModuleKindNodeNext {
24282428
if sourceFileMetaData == nil {
@@ -3400,6 +3400,14 @@ func IsRightSideOfQualifiedNameOrPropertyAccess(node *Node) bool {
34003400
return false
34013401
}
34023402

3403+
func ShouldTransformImportCall(fileName string, options *core.CompilerOptions, impliedNodeFormatForEmit core.ModuleKind) bool {
3404+
moduleKind := options.GetEmitModuleKind()
3405+
if core.ModuleKindNode16 <= moduleKind && moduleKind <= core.ModuleKindNodeNext || moduleKind == core.ModuleKindPreserve {
3406+
return false
3407+
}
3408+
return impliedNodeFormatForEmit < core.ModuleKindES2015
3409+
}
3410+
34033411
func HasQuestionToken(node *Node) bool {
34043412
switch node.Kind {
34053413
case KindParameter:

internal/checker/checker.go

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -527,14 +527,13 @@ type Program interface {
527527
BindSourceFiles()
528528
FileExists(fileName string) bool
529529
GetSourceFile(fileName string) *ast.SourceFile
530-
GetEmitModuleFormatOfFile(sourceFile *ast.SourceFile) core.ModuleKind
531-
GetImpliedNodeFormatForEmit(sourceFile *ast.SourceFile) core.ModuleKind
532-
GetResolvedModule(currentSourceFile *ast.SourceFile, moduleReference string, mode core.ResolutionMode) *module.ResolvedModule
530+
GetEmitModuleFormatOfFile(sourceFile ast.HasFileName) core.ModuleKind
531+
GetImpliedNodeFormatForEmit(sourceFile ast.HasFileName) core.ModuleKind
532+
GetResolvedModule(currentSourceFile ast.HasFileName, moduleReference string, mode core.ResolutionMode) *module.ResolvedModule
533533
GetResolvedModules() map[tspath.Path]module.ModeAwareCache[*module.ResolvedModule]
534534
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
535535
GetJSXRuntimeImportSpecifier(path tspath.Path) (moduleReference string, specifier *ast.Node)
536536
GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node
537-
GetModeForUsageLocation(sourceFile *ast.SourceFile, location *ast.Node) core.ResolutionMode
538537
}
539538

540539
type Host interface {
@@ -14386,7 +14385,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1438614385

1438714386
var sourceFile *ast.SourceFile
1438814387
resolvedModule := c.program.GetResolvedModule(importingSourceFile, moduleReference, mode)
14389-
if resolvedModule != nil && resolvedModule.IsResolved() {
14388+
if resolvedModule.IsResolved() {
1439014389
sourceFile = c.program.GetSourceFile(resolvedModule.ResolvedFileName)
1439114390
}
1439214391

@@ -14478,7 +14477,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1447814477
return nil
1447914478
}
1448014479

14481-
if resolvedModule != nil && resolvedModule.IsResolved() && !(tspath.ExtensionIsTs(resolvedModule.Extension) || resolvedModule.Extension == tspath.ExtensionJson) {
14480+
if resolvedModule.IsResolved() && !(tspath.ExtensionIsTs(resolvedModule.Extension) || resolvedModule.Extension == tspath.ExtensionJson) {
1448214481
if isForAugmentation {
1448314482
c.error(
1448414483
errorNode,

internal/compiler/emitHost.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@ import (
55

66
"github.com/microsoft/typescript-go/internal/ast"
77
"github.com/microsoft/typescript-go/internal/core"
8+
"github.com/microsoft/typescript-go/internal/module"
89
"github.com/microsoft/typescript-go/internal/modulespecifiers"
10+
"github.com/microsoft/typescript-go/internal/outputpaths"
911
"github.com/microsoft/typescript-go/internal/printer"
1012
"github.com/microsoft/typescript-go/internal/transformers/declarations"
1113
"github.com/microsoft/typescript-go/internal/tspath"
@@ -29,7 +31,6 @@ type EmitHost interface {
2931
GetCurrentDirectory() string
3032
CommonSourceDirectory() string
3133
IsEmitBlocked(file string) bool
32-
GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData
3334
GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool) printer.EmitResolver
3435
}
3536

@@ -40,10 +41,22 @@ type emitHost struct {
4041
program *Program
4142
}
4243

43-
func (host *emitHost) GetDefaultResolutionModeForFile(file modulespecifiers.SourceFileForSpecifierGeneration) core.ResolutionMode {
44+
func (host *emitHost) GetModeForUsageLocation(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) core.ResolutionMode {
45+
return host.program.GetModeForUsageLocation(file, moduleSpecifier)
46+
}
47+
48+
func (host *emitHost) GetResolvedModuleFromModuleSpecifier(file ast.HasFileName, moduleSpecifier *ast.StringLiteralLike) *module.ResolvedModule {
49+
return host.program.GetResolvedModuleFromModuleSpecifier(file, moduleSpecifier)
50+
}
51+
52+
func (host *emitHost) GetDefaultResolutionModeForFile(file ast.HasFileName) core.ResolutionMode {
4453
return host.program.GetDefaultResolutionModeForFile(file)
4554
}
4655

56+
func (host *emitHost) GetEmitModuleFormatOfFile(file ast.HasFileName) core.ModuleKind {
57+
return host.program.GetEmitModuleFormatOfFile(file)
58+
}
59+
4760
func (host *emitHost) FileExists(path string) bool {
4861
return host.program.FileExists(path)
4962
}
@@ -78,7 +91,7 @@ func (host *emitHost) GetEffectiveDeclarationFlags(node *ast.Node, flags ast.Mod
7891

7992
func (host *emitHost) GetOutputPathsFor(file *ast.SourceFile, forceDtsPaths bool) declarations.OutputPaths {
8093
// TODO: cache
81-
return getOutputPathsFor(file, host, forceDtsPaths)
94+
return outputpaths.GetOutputPathsFor(file, host.Options(), host, forceDtsPaths)
8295
}
8396

8497
func (host *emitHost) GetResolutionModeOverride(node *ast.Node) core.ResolutionMode {
@@ -114,7 +127,3 @@ func (host *emitHost) GetEmitResolver(file *ast.SourceFile, skipDiagnostics bool
114127
defer done()
115128
return checker.GetEmitResolver(file, skipDiagnostics)
116129
}
117-
118-
func (host *emitHost) GetSourceFileMetaData(path tspath.Path) *ast.SourceFileMetaData {
119-
return host.program.GetSourceFileMetaData(path)
120-
}

internal/compiler/emitter.go

Lines changed: 7 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"github.com/microsoft/typescript-go/internal/ast"
88
"github.com/microsoft/typescript-go/internal/core"
99
"github.com/microsoft/typescript-go/internal/diagnostics"
10+
"github.com/microsoft/typescript-go/internal/outputpaths"
1011
"github.com/microsoft/typescript-go/internal/printer"
1112
"github.com/microsoft/typescript-go/internal/sourcemap"
1213
"github.com/microsoft/typescript-go/internal/stringutil"
@@ -32,15 +33,15 @@ type emitter struct {
3233
emitSkipped bool
3334
sourceMapDataList []*SourceMapEmitResult
3435
writer printer.EmitTextWriter
35-
paths *outputPaths
36+
paths *outputpaths.OutputPaths
3637
sourceFile *ast.SourceFile
3738
}
3839

3940
func (e *emitter) emit() {
4041
// !!! tracing
41-
e.emitJSFile(e.sourceFile, e.paths.jsFilePath, e.paths.sourceMapFilePath)
42-
e.emitDeclarationFile(e.sourceFile, e.paths.declarationFilePath, e.paths.declarationMapPath)
43-
e.emitBuildInfo(e.paths.buildInfoPath)
42+
e.emitJSFile(e.sourceFile, e.paths.JsFilePath(), e.paths.SourceMapFilePath())
43+
e.emitDeclarationFile(e.sourceFile, e.paths.DeclarationFilePath(), e.paths.DeclarationMapPath())
44+
e.emitBuildInfo(e.paths.BuildInfoPath())
4445
}
4546

4647
func (e *emitter) getDeclarationTransformers(emitContext *printer.EmitContext, sourceFile *ast.SourceFile, declarationFilePath string, declarationMapPath string) []*declarations.DeclarationTransformer {
@@ -207,44 +208,6 @@ func (e *emitter) printSourceFile(jsFilePath string, sourceMapFilePath string, s
207208
return !data.SkippedDtsWrite
208209
}
209210

210-
func getSourceFilePathInNewDir(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
211-
sourceFilePath := tspath.GetNormalizedAbsolutePath(fileName, currentDirectory)
212-
commonSourceDirectory = tspath.EnsureTrailingDirectorySeparator(commonSourceDirectory)
213-
isSourceFileInCommonSourceDirectory := tspath.ContainsPath(commonSourceDirectory, sourceFilePath, tspath.ComparePathsOptions{
214-
UseCaseSensitiveFileNames: useCaseSensitiveFileNames,
215-
CurrentDirectory: currentDirectory,
216-
})
217-
if isSourceFileInCommonSourceDirectory {
218-
sourceFilePath = sourceFilePath[len(commonSourceDirectory):]
219-
}
220-
return tspath.CombinePaths(newDirPath, sourceFilePath)
221-
}
222-
223-
func getOwnEmitOutputFilePath(fileName string, host printer.EmitHost, extension string) string {
224-
compilerOptions := host.Options()
225-
var emitOutputFilePathWithoutExtension string
226-
if len(compilerOptions.OutDir) > 0 {
227-
currentDirectory := host.GetCurrentDirectory()
228-
emitOutputFilePathWithoutExtension = tspath.RemoveFileExtension(getSourceFilePathInNewDir(
229-
fileName,
230-
compilerOptions.OutDir,
231-
currentDirectory,
232-
host.CommonSourceDirectory(),
233-
host.UseCaseSensitiveFileNames(),
234-
))
235-
} else {
236-
emitOutputFilePathWithoutExtension = tspath.RemoveFileExtension(fileName)
237-
}
238-
return emitOutputFilePathWithoutExtension + extension
239-
}
240-
241-
func getSourceMapFilePath(jsFilePath string, options *core.CompilerOptions) string {
242-
if options.SourceMap.IsTrue() && !options.InlineSourceMap.IsTrue() {
243-
return jsFilePath + ".map"
244-
}
245-
return ""
246-
}
247-
248211
func shouldEmitSourceMaps(mapOptions *core.CompilerOptions, sourceFile *ast.SourceFile) bool {
249212
return (mapOptions.SourceMap.IsTrue() || mapOptions.InlineSourceMap.IsTrue()) &&
250213
!tspath.FileExtensionIs(sourceFile.FileName(), tspath.ExtensionJson)
@@ -274,7 +237,7 @@ func (e *emitter) getSourceMapDirectory(mapOptions *core.CompilerOptions, filePa
274237
if sourceFile != nil {
275238
// For modules or multiple emit files the mapRoot will have directory structure like the sources
276239
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
277-
sourceMapDir = tspath.GetDirectoryPath(getSourceFilePathInNewDir(
240+
sourceMapDir = tspath.GetDirectoryPath(outputpaths.GetSourceFilePathInNewDir(
278241
sourceFile.FileName(),
279242
sourceMapDir,
280243
e.host.GetCurrentDirectory(),
@@ -305,7 +268,7 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
305268
if sourceFile != nil {
306269
// For modules or multiple emit files the mapRoot will have directory structure like the sources
307270
// So if src\a.ts and src\lib\b.ts are compiled together user would be moving the maps into mapRoot\a.js.map and mapRoot\lib\b.js.map
308-
sourceMapDir = tspath.GetDirectoryPath(getSourceFilePathInNewDir(
271+
sourceMapDir = tspath.GetDirectoryPath(outputpaths.GetSourceFilePathInNewDir(
309272
sourceFile.FileName(),
310273
sourceMapDir,
311274
e.host.GetCurrentDirectory(),
@@ -334,91 +297,6 @@ func (e *emitter) getSourceMappingURL(mapOptions *core.CompilerOptions, sourceMa
334297
return stringutil.EncodeURI(sourceMapFile)
335298
}
336299

337-
func getDeclarationEmitOutputFilePath(file string, host EmitHost) string {
338-
options := host.Options()
339-
var outputDir *string
340-
if len(options.DeclarationDir) > 0 {
341-
outputDir = &options.DeclarationDir
342-
} else if len(options.OutDir) > 0 {
343-
outputDir = &options.OutDir
344-
}
345-
346-
var path string
347-
if outputDir != nil {
348-
path = getSourceFilePathInNewDirWorker(file, *outputDir, host.GetCurrentDirectory(), host.CommonSourceDirectory(), host.UseCaseSensitiveFileNames())
349-
} else {
350-
path = file
351-
}
352-
declarationExtension := tspath.GetDeclarationEmitExtensionForPath(path)
353-
return tspath.RemoveFileExtension(path) + declarationExtension
354-
}
355-
356-
func getSourceFilePathInNewDirWorker(fileName string, newDirPath string, currentDirectory string, commonSourceDirectory string, useCaseSensitiveFileNames bool) string {
357-
sourceFilePath := tspath.GetNormalizedAbsolutePath(fileName, currentDirectory)
358-
commonDir := tspath.GetCanonicalFileName(commonSourceDirectory, useCaseSensitiveFileNames)
359-
canonFile := tspath.GetCanonicalFileName(sourceFilePath, useCaseSensitiveFileNames)
360-
isSourceFileInCommonSourceDirectory := strings.HasPrefix(canonFile, commonDir)
361-
if isSourceFileInCommonSourceDirectory {
362-
sourceFilePath = sourceFilePath[len(commonSourceDirectory):]
363-
}
364-
return tspath.CombinePaths(newDirPath, sourceFilePath)
365-
}
366-
367-
type outputPaths struct {
368-
jsFilePath string
369-
sourceMapFilePath string
370-
declarationFilePath string
371-
declarationMapPath string
372-
buildInfoPath string
373-
}
374-
375-
// DeclarationFilePath implements declarations.OutputPaths.
376-
func (o *outputPaths) DeclarationFilePath() string {
377-
return o.declarationFilePath
378-
}
379-
380-
// JsFilePath implements declarations.OutputPaths.
381-
func (o *outputPaths) JsFilePath() string {
382-
return o.jsFilePath
383-
}
384-
385-
func getOutputPathsFor(sourceFile *ast.SourceFile, host EmitHost, forceDtsEmit bool) *outputPaths {
386-
options := host.Options()
387-
// !!! bundle not implemented, may be deprecated
388-
ownOutputFilePath := getOwnEmitOutputFilePath(sourceFile.FileName(), host, core.GetOutputExtension(sourceFile.FileName(), options.Jsx))
389-
isJsonFile := ast.IsJsonSourceFile(sourceFile)
390-
// If json file emits to the same location skip writing it, if emitDeclarationOnly skip writing it
391-
isJsonEmittedToSameLocation := isJsonFile &&
392-
tspath.ComparePaths(sourceFile.FileName(), ownOutputFilePath, tspath.ComparePathsOptions{
393-
CurrentDirectory: host.GetCurrentDirectory(),
394-
UseCaseSensitiveFileNames: host.UseCaseSensitiveFileNames(),
395-
}) == 0
396-
paths := &outputPaths{}
397-
if options.EmitDeclarationOnly != core.TSTrue && !isJsonEmittedToSameLocation {
398-
paths.jsFilePath = ownOutputFilePath
399-
if !ast.IsJsonSourceFile(sourceFile) {
400-
paths.sourceMapFilePath = getSourceMapFilePath(paths.jsFilePath, options)
401-
}
402-
}
403-
if forceDtsEmit || options.GetEmitDeclarations() && !isJsonFile {
404-
paths.declarationFilePath = getDeclarationEmitOutputFilePath(sourceFile.FileName(), host)
405-
if options.GetAreDeclarationMapsEnabled() {
406-
paths.declarationMapPath = paths.declarationFilePath + ".map"
407-
}
408-
}
409-
return paths
410-
}
411-
412-
func forEachEmittedFile(host EmitHost, action func(emitFileNames *outputPaths, sourceFile *ast.SourceFile) bool, sourceFiles []*ast.SourceFile, options *EmitOptions) bool {
413-
// !!! outFile not yet implemented, may be deprecated
414-
for _, sourceFile := range sourceFiles {
415-
if action(getOutputPathsFor(sourceFile, host, options.forceDtsEmit), sourceFile) {
416-
return true
417-
}
418-
}
419-
return false
420-
}
421-
422300
func sourceFileMayBeEmitted(sourceFile *ast.SourceFile, host printer.EmitHost, forceDtsEmit bool) bool {
423301
// !!! Js files are emitted only if option is enabled
424302

0 commit comments

Comments
 (0)