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;

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export as namespace Foo;
77
->Foo : typeof import("foo")
8-
+>Foo : typeof import("./foo.js")
8+
+>Foo : typeof import("./foo")
99

1010
=== foo2.d.ts ===
1111
export = 2 + 2;

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

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import * as _three from './three';
1313
>_three : typeof _three
1414

1515
export as namespace THREE;
16-
>THREE : typeof import("./global.js")
16+
>THREE : typeof import("./global")
1717

1818
declare global {
1919
>global : typeof global
@@ -25,6 +25,6 @@ declare global {
2525

2626
=== test.ts ===
2727
const m = THREE
28-
>m : typeof import("./three.js")
29-
>THREE : typeof import("./three.js")
28+
>m : typeof import("./three")
29+
>THREE : typeof import("./three")
3030

testdata/baselines/reference/submodule/compiler/checkMergedGlobalUMDSymbol.types.diff

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export as namespace THREE;
77
->THREE : typeof import("global")
8-
+>THREE : typeof import("./global.js")
8+
+>THREE : typeof import("./global")
99

1010
declare global {
1111
>global : typeof global
@@ -15,5 +15,5 @@
1515
const m = THREE
1616
->m : typeof import("three")
1717
->THREE : typeof import("three")
18-
+>m : typeof import("./three.js")
19-
+>THREE : typeof import("./three.js")
18+
+>m : typeof import("./three")
19+
+>THREE : typeof import("./three")

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,8 @@ export default MyConstEnum;
1919

2020
=== ReExport.d.ts ===
2121
export { MyConstEnum as default } from './ConstEnum';
22-
>MyConstEnum : typeof import("./ConstEnum.js").MyConstEnum
23-
>default : typeof import("./ConstEnum.js").MyConstEnum
22+
>MyConstEnum : typeof import("./ConstEnum").MyConstEnum
23+
>default : typeof import("./ConstEnum").MyConstEnum
2424

2525
=== usages.ts ===
2626
import {MyConstEnum} from "./ConstEnum";

testdata/baselines/reference/submodule/compiler/constEnumNoPreserveDeclarationReexport.types.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
export { MyConstEnum as default } from './ConstEnum';
77
->MyConstEnum : typeof import("ConstEnum").MyConstEnum
88
->default : typeof import("ConstEnum").MyConstEnum
9-
+>MyConstEnum : typeof import("./ConstEnum.js").MyConstEnum
10-
+>default : typeof import("./ConstEnum.js").MyConstEnum
9+
+>MyConstEnum : typeof import("./ConstEnum").MyConstEnum
10+
+>default : typeof import("./ConstEnum").MyConstEnum
1111

1212
=== usages.ts ===
1313
import {MyConstEnum} from "./ConstEnum";

testdata/baselines/reference/submodule/compiler/declarationEmitAliasFromIndirectFile.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ exports.default = fp.l10ns;
3636

3737
//// [app.d.ts]
3838
declare const _default: {
39-
ar?: import("./locale.js").CustomLocale;
40-
bg?: import("./locale.js").CustomLocale;
39+
ar?: import("./locale").CustomLocale;
40+
bg?: import("./locale").CustomLocale;
4141
} & {
42-
default: import("./locale.js").Locale;
42+
default: import("./locale").Locale;
4343
};
4444
export default _default;

testdata/baselines/reference/submodule/compiler/declarationEmitAliasFromIndirectFile.js.diff

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,3 @@
88
+const fp = { l10ns: {} };
99
exports.default = fp.l10ns;
1010

11-
12-
//// [app.d.ts]
13-
declare const _default: {
14-
- ar?: import("./locale").CustomLocale;
15-
- bg?: import("./locale").CustomLocale;
16-
+ ar?: import("./locale.js").CustomLocale;
17-
+ bg?: import("./locale.js").CustomLocale;
18-
} & {
19-
- default: import("./locale").Locale;
20-
+ default: import("./locale.js").Locale;
21-
};
22-
export default _default;

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ const fp = { l10ns: {} } as FlatpickrFn;
5757
>{} : {}
5858

5959
export default fp.l10ns;
60-
>fp.l10ns : { ar?: import("./locale.js").CustomLocale; bg?: import("./locale.js").CustomLocale; } & { default: import("./locale.js").Locale; }
60+
>fp.l10ns : { ar?: import("./locale").CustomLocale; bg?: import("./locale").CustomLocale; } & { default: import("./locale").Locale; }
6161
>fp : FlatpickrFn
62-
>l10ns : { ar?: import("./locale.js").CustomLocale; bg?: import("./locale.js").CustomLocale; } & { default: import("./locale.js").Locale; }
62+
>l10ns : { ar?: import("./locale").CustomLocale; bg?: import("./locale").CustomLocale; } & { default: import("./locale").Locale; }
6363

testdata/baselines/reference/submodule/compiler/declarationEmitAliasFromIndirectFile.types.diff

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55

66
export default fp.l10ns;
77
->fp.l10ns : { ar?: import("locale").CustomLocale; bg?: import("locale").CustomLocale; } & { default: import("locale").Locale; }
8-
+>fp.l10ns : { ar?: import("./locale.js").CustomLocale; bg?: import("./locale.js").CustomLocale; } & { default: import("./locale.js").Locale; }
8+
+>fp.l10ns : { ar?: import("./locale").CustomLocale; bg?: import("./locale").CustomLocale; } & { default: import("./locale").Locale; }
99
>fp : FlatpickrFn
1010
->l10ns : { ar?: import("locale").CustomLocale; bg?: import("locale").CustomLocale; } & { default: import("locale").Locale; }
11-
+>l10ns : { ar?: import("./locale.js").CustomLocale; bg?: import("./locale.js").CustomLocale; } & { default: import("./locale.js").Locale; }
11+
+>l10ns : { ar?: import("./locale").CustomLocale; bg?: import("./locale").CustomLocale; } & { default: import("./locale").Locale; }
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/index.ts(2,14): error TS2742: The inferred type of 'c' cannot be named without a reference to './node_modules/pkg'. This is likely not portable. A type annotation is necessary.
2+
3+
4+
==== /node_modules/pkg/package.json (0 errors) ====
5+
{
6+
"name": "pkg",
7+
"type": "module",
8+
"exports": {
9+
".": {
10+
"import": "./index.js",
11+
"require": "./index.cjs"
12+
}
13+
}
14+
}
15+
16+
==== /node_modules/pkg/index.d.ts (0 errors) ====
17+
export declare class C {
18+
private p;
19+
}
20+
21+
==== /node_modules/pkg/index.d.cts (0 errors) ====
22+
export {};
23+
24+
==== /makeC.ts (0 errors) ====
25+
import { C } from "pkg";
26+
export function makeC() {
27+
return new C();
28+
}
29+
30+
==== /index.ts (1 errors) ====
31+
import { makeC } from "./makeC";
32+
export const c = makeC();
33+
~
34+
!!! error TS2742: The inferred type of 'c' cannot be named without a reference to './node_modules/pkg'. This is likely not portable. A type annotation is necessary.
35+

0 commit comments

Comments
 (0)