Skip to content

Commit 081778a

Browse files
committed
WIP
1 parent 3718fca commit 081778a

File tree

414 files changed

+1864
-4726
lines changed

Some content is hidden

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

414 files changed

+1864
-4726
lines changed

internal/checker/checker.go

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -535,7 +535,6 @@ type Program interface {
535535
GetJSXRuntimeImportSpecifier(path tspath.Path) (moduleReference string, specifier *ast.Node)
536536
GetImportHelpersImportSpecifier(path tspath.Path) *ast.Node
537537
GetModeForUsageLocation(sourceFile *ast.SourceFile, location *ast.Node) core.ResolutionMode
538-
GetDefaultResolutionModeForFile(sourceFile *ast.SourceFile) core.ResolutionMode
539538
}
540539

541540
type Host interface {
@@ -5115,11 +5114,13 @@ func (c *Checker) checkImportAttributes(declaration *ast.Node) {
51155114
message = diagnostics.Import_assertions_are_only_supported_when_the_module_option_is_set_to_esnext_nodenext_or_preserve
51165115
}
51175116
c.grammarErrorOnNode(node, message)
5117+
return
51185118
}
51195119
if isTypeOnly {
51205120
c.grammarErrorOnNode(node, core.IfElse(isImportAttributes,
51215121
diagnostics.Import_attributes_cannot_be_used_with_type_only_imports_or_exports,
51225122
diagnostics.Import_assertions_cannot_be_used_with_type_only_imports_or_exports))
5123+
return
51235124
}
51245125
if override != core.ResolutionModeNone {
51255126
c.grammarErrorOnNode(node, diagnostics.X_resolution_mode_can_only_be_set_for_type_only_imports)
@@ -14315,7 +14316,7 @@ func (c *Checker) resolveExternalModule(location *ast.Node, moduleReference stri
1431514316
mode core.ResolutionMode
1431614317
)
1431714318

14318-
if ast.IsStringLiteralLike(location) || ast.IsModuleDeclaration(location.Parent) && location.Parent.AsModuleDeclaration().Name() == location {
14319+
if ast.IsStringLiteralLike(location) || location.Parent != nil && ast.IsModuleDeclaration(location.Parent) && location.Parent.AsModuleDeclaration().Name() == location {
1431914320
contextSpecifier = location
1432014321
} else if ast.IsModuleDeclaration(location) {
1432114322
contextSpecifier = location.AsModuleDeclaration().Name()
@@ -14930,7 +14931,7 @@ func (c *Checker) getFullyQualifiedName(symbol *ast.Symbol, containingLocation *
1493014931
if symbol.Parent != nil {
1493114932
return c.getFullyQualifiedName(symbol.Parent, containingLocation) + "." + c.symbolToString(symbol)
1493214933
}
14933-
return c.symbolToString(symbol) // !!!
14934+
return c.symbolToStringEx(symbol, containingLocation, ast.SymbolFlagsAll, SymbolFormatFlagsDoNotIncludeSymbolChain|SymbolFormatFlagsAllowAnyNodeKind, nil /*writer*/)
1493414935
}
1493514936

1493614937
func (c *Checker) getExportsOfSymbol(symbol *ast.Symbol) ast.SymbolTable {

internal/checker/nodebuilderimpl.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1074,11 +1074,9 @@ func (b *nodeBuilderImpl) getSpecifierForModuleSymbol(symbol *ast.Symbol, overri
10741074
contextFile := b.ctx.enclosingFile
10751075
resolutionMode := overrideImportMode
10761076
if resolutionMode == core.ResolutionModeNone && originalModuleSpecifier != nil {
1077-
// !!! import resolution mode support
1078-
// resolutionMode = b.ch.host.GetModeForUsageLocation(contextFile, originalModuleSpecifier)
1077+
resolutionMode = b.ch.program.GetModeForUsageLocation(contextFile, originalModuleSpecifier)
10791078
} else if resolutionMode == core.ResolutionModeNone && contextFile != nil {
1080-
// !!! import resolution mode support
1081-
// resolutionMode = b.ch.host.GetDefaultResolutionModeForFile(contextFile)
1079+
resolutionMode = b.ch.program.GetDefaultResolutionModeForFile(contextFile)
10821080
}
10831081
cacheKey := module.ModeAwareCacheKey{Name: string(contextFile.Path()), Mode: resolutionMode}
10841082
links := b.symbolLinks.Get(symbol)

internal/compiler/emitHost.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,10 @@ type emitHost struct {
4040
program *Program
4141
}
4242

43+
func (host *emitHost) GetDefaultResolutionModeForFile(file modulespecifiers.SourceFileForSpecifierGeneration) core.ResolutionMode {
44+
return host.program.GetDefaultResolutionModeForFile(file)
45+
}
46+
4347
func (host *emitHost) FileExists(path string) bool {
4448
return host.program.FileExists(path)
4549
}

internal/compiler/fileloader.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ import (
1212
"github.com/microsoft/typescript-go/internal/collections"
1313
"github.com/microsoft/typescript-go/internal/core"
1414
"github.com/microsoft/typescript-go/internal/module"
15+
"github.com/microsoft/typescript-go/internal/modulespecifiers"
1516
"github.com/microsoft/typescript-go/internal/tsoptions"
1617
"github.com/microsoft/typescript-go/internal/tspath"
1718
)
@@ -467,7 +468,7 @@ func getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, file *ast.So
467468
}
468469
}
469470

470-
func getDefaultResolutionModeForFile(file *ast.SourceFile, meta *ast.SourceFileMetaData, options *core.CompilerOptions) core.ResolutionMode {
471+
func getDefaultResolutionModeForFile(file modulespecifiers.SourceFileForSpecifierGeneration, meta *ast.SourceFileMetaData, options *core.CompilerOptions) core.ResolutionMode {
471472
if importSyntaxAffectsModuleResolution(options) {
472473
return ast.GetImpliedNodeFormatForEmitWorker(file.FileName(), options, meta)
473474
} else {

internal/compiler/program.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ type Program struct {
7272
unsupportedExtensions []string
7373
}
7474

75+
// GetCommonSourceDirectory implements checker.Program.
76+
func (p *Program) GetCommonSourceDirectory() string {
77+
return getCommonSourceDirectory(p.compilerOptions, p.files)
78+
}
79+
7580
// FileExists implements checker.Program.
7681
func (p *Program) FileExists(path string) bool {
7782
return p.host.FS().FileExists(path)
@@ -749,7 +754,7 @@ func (p *Program) GetModeForUsageLocation(sourceFile *ast.SourceFile, location *
749754
return getModeForUsageLocation(sourceFile, p.sourceFileMetaDatas[sourceFile.Path()], location, p.compilerOptions)
750755
}
751756

752-
func (p *Program) GetDefaultResolutionModeForFile(sourceFile *ast.SourceFile) core.ResolutionMode {
757+
func (p *Program) GetDefaultResolutionModeForFile(sourceFile modulespecifiers.SourceFileForSpecifierGeneration) core.ResolutionMode {
753758
return getDefaultResolutionModeForFile(sourceFile, p.sourceFileMetaDatas[sourceFile.Path()], p.compilerOptions)
754759
}
755760

internal/modulespecifiers/specifiers.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ func processEnding(
563563
if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionMts, tspath.ExtensionCts}) && tsPriority < jsPriority {
564564
return fileName
565565
}
566-
if tspath.IsDeclarationFileName(fileName) {
566+
if tspath.FileExtensionIsOneOf(fileName, []string{tspath.ExtensionDmts, tspath.ExtensionMts, tspath.ExtensionDcts, tspath.ExtensionCts}) {
567567
inputExt := tspath.GetDeclarationFileExtension(fileName)
568568
ext := getJsExtensionForDeclarationFileExtension(inputExt)
569569
return tspath.RemoveExtension(fileName, inputExt) + ext
@@ -660,6 +660,7 @@ func tryGetModuleNameAsNodeModule(
660660
pkgJsonResults := tryDirectoryWithPackageJson(
661661
*parts,
662662
pathObj,
663+
importingSourceFile,
663664
host,
664665
overrideMode,
665666
options,
@@ -669,8 +670,6 @@ func tryGetModuleNameAsNodeModule(
669670
packageRootPath := pkgJsonResults.packageRootPath
670671
blockedByExports := pkgJsonResults.blockedByExports
671672
verbatimFromExports := pkgJsonResults.verbatimFromExports
672-
// !!! classic resolution is dead?
673-
// if options.GetModuleResolutionKind() != core.ModuleResolutionKindClassic {
674673
if blockedByExports {
675674
return "" // File is under this package.json, but is not publicly exported - there's no way to name it via `node_modules` resolution
676675
}
@@ -729,6 +728,7 @@ type pkgJsonDirAttemptResult struct {
729728
func tryDirectoryWithPackageJson(
730729
parts NodeModulePathParts,
731730
pathObj ModulePath,
731+
importingSourceFile SourceFileForSpecifierGeneration,
732732
host ModuleSpecifierGenerationHost,
733733
overrideMode core.ResolutionMode,
734734
options *core.CompilerOptions,
@@ -752,10 +752,9 @@ func tryDirectoryWithPackageJson(
752752
}
753753

754754
importMode := overrideMode
755-
// !!! TODO: real resolutionMode support
756-
// if importMode == core.ResolutionModeNone {
757-
// importMode = getDefaultResolutionModeForFile(importingSourceFile, host, options);
758-
// }
755+
if importMode == core.ResolutionModeNone {
756+
importMode = host.GetDefaultResolutionModeForFile(importingSourceFile)
757+
}
759758

760759
var packageJsonContent *packagejson.PackageJson
761760
if packageJson != nil {
@@ -868,7 +867,7 @@ func tryGetModuleNameFromExports(
868867
exports packagejson.ExportsOrImports,
869868
conditions []string,
870869
) string {
871-
if exports.Type == packagejson.JSONValueTypeObject && allKeysStartWithDot(exports.AsObject()) {
870+
if exports.IsSubpaths() {
872871
// sub-mappings
873872
// 3 cases:
874873
// * directory mappings (legacyish, key ends with / (technically allows index/extension resolution under cjs mode))

internal/modulespecifiers/types.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ type ModuleSpecifierGenerationHost interface {
4646
// GetModuleResolutionCache() any // !!! TODO: adapt new resolution cache model
4747
// GetSymlinkCache() any // !!! TODO: adapt new resolution cache model
4848
// GetFileIncludeReasons() any // !!! TODO: adapt new resolution cache model
49+
GetCommonSourceDirectory() string
4950
GetGlobalTypingsCacheLocation() string
5051
UseCaseSensitiveFileNames() bool
5152
GetCurrentDirectory() string
@@ -58,6 +59,7 @@ type ModuleSpecifierGenerationHost interface {
5859

5960
GetNearestAncestorDirectoryWithPackageJson(dirname string) string
6061
GetPackageJsonInfo(pkgJsonPath string) PackageJsonInfo
62+
GetDefaultResolutionModeForFile(file SourceFileForSpecifierGeneration) core.ResolutionMode
6163
}
6264

6365
type ImportModuleSpecifierPreference string

internal/transformers/importelision_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ type fakeProgram struct {
1919
singleThreaded bool
2020
compilerOptions *core.CompilerOptions
2121
files []*ast.SourceFile
22-
getEmitModuleFormatOfFile func(sourceFile *ast.SourceFile) core.ModuleKind
22+
getEmitModuleFormatOfFile func(sourceFile modulespecifiers.SourceFileForSpecifierGeneration) core.ModuleKind
2323
getImpliedNodeFormatForEmit func(sourceFile *ast.SourceFile) core.ModuleKind
2424
getResolvedModule func(currentSourceFile *ast.SourceFile, moduleReference string, mode core.ResolutionMode) *module.ResolvedModule
2525
getSourceFile func(FileName string) *ast.SourceFile
@@ -89,7 +89,7 @@ func (p *fakeProgram) GetImpliedNodeFormatForEmit(sourceFile *ast.SourceFile) co
8989
return p.getImpliedNodeFormatForEmit(sourceFile)
9090
}
9191

92-
func (p *fakeProgram) GetDefaultResolutionModeForFile(sourceFile *ast.SourceFile) core.ResolutionMode {
92+
func (p *fakeProgram) GetDefaultResolutionModeForFile(sourceFile modulespecifiers.SourceFileForSpecifierGeneration) core.ResolutionMode {
9393
return p.getEmitModuleFormatOfFile(sourceFile)
9494
}
9595

@@ -173,7 +173,7 @@ func TestImportElision(t *testing.T) {
173173
singleThreaded: true,
174174
compilerOptions: compilerOptions,
175175
files: files,
176-
getEmitModuleFormatOfFile: func(sourceFile *ast.SourceFile) core.ModuleKind {
176+
getEmitModuleFormatOfFile: func(sourceFile modulespecifiers.SourceFileForSpecifierGeneration) core.ModuleKind {
177177
return core.ModuleKindESNext
178178
},
179179
getImpliedNodeFormatForEmit: func(sourceFile *ast.SourceFile) core.ModuleKind {

testdata/baselines/reference/submodule/compiler/allowSyntheticDefaultImports8.errors.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
a.ts(1,10): error TS2305: Module '"b"' has no exported member 'default'.
1+
a.ts(1,10): error TS2305: Module '"./b"' has no exported member 'default'.
22

33

44
==== b.d.ts (0 errors) ====
@@ -9,6 +9,6 @@ a.ts(1,10): error TS2305: Module '"b"' has no exported member 'default'.
99
==== a.ts (1 errors) ====
1010
import { default as Foo } from "./b";
1111
~~~~~~~
12-
!!! error TS2305: Module '"b"' has no exported member 'default'.
12+
!!! error TS2305: Module '"./b"' has no exported member 'default'.
1313
Foo.bar();
1414
Foo.foo();

testdata/baselines/reference/submodule/compiler/ambientExportDefaultErrors.types

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ export default 2 + 2;
1515
>2 : 2
1616

1717
export as namespace Foo;
18-
>Foo : typeof import("./foo.js")
18+
>Foo : typeof import("./foo")
1919

2020
=== foo2.d.ts ===
2121
export = 2 + 2;

0 commit comments

Comments
 (0)