From ae85c539a10d231e321dc04295c94b613b1ebf94 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Mon, 9 Nov 2026 16:39:35 -0800 Subject: [PATCH 1/5] Use content mapper virtual filename for Node module format detection --- tsc/internal/compiler/contentmapper_test.go | 82 +++++++++++++++++++++ tsc/internal/compiler/fileloader.go | 13 +++- tsc/internal/compiler/program.go | 10 +-- 3 files changed, 98 insertions(+), 7 deletions(-) diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 31b8f831870e8..65d570f6df2e7 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -87,6 +87,88 @@ func TestContentMapperVirtualExtensionSetsImpliedNodeFormat(t *testing.T) { assert.Equal(t, program.GetSourceFileMetaData(file.PathKey()).ImpliedNodeFormat, core.ResolutionModeESM) } +func TestContentMapperVirtualExtensionDeterminesModuleFormat(t *testing.T) { + t.Parallel() + tests := []struct { + name string + virtualExtension string + text string + module core.ModuleKind + moduleResolution core.ModuleResolutionKind + expectedFormat core.ModuleKind + expectedDiagnostic int32 + }{ + { + name: "cts", + virtualExtension: ".cts", + text: `import type { Foo } from "./dep"; declare const styles: { foo: string }; export = styles;`, + module: core.ModuleKindESNext, + moduleResolution: core.ModuleResolutionKindNode10, + expectedFormat: core.ModuleKindCommonJS, + expectedDiagnostic: diagnostics.Export_assignment_cannot_be_used_when_targeting_ECMAScript_modules_Consider_using_export_default_or_another_module_format_instead.Code(), + }, + { + name: "mts", + virtualExtension: ".mts", + text: `import type { Foo } from "./dep"; export const value = 1;`, + module: core.ModuleKindCommonJS, + moduleResolution: core.ModuleResolutionKindNode10, + expectedFormat: core.ModuleKindESNext, + expectedDiagnostic: diagnostics.A_top_level_export_modifier_cannot_be_used_on_value_declarations_in_a_CommonJS_module_when_verbatimModuleSyntax_is_enabled.Code(), + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + physicalFileName := "/src/physical" + tt.virtualExtension + program := newContentMapperProgramWithOptions( + t, + fakeContentMapperHost{transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{Text: content, VirtualExtension: tt.virtualExtension, Mappings: spanmap.New(nil)}, nil + }}, + map[string]string{ + "/src/Component.vue": tt.text, + physicalFileName: tt.text, + "/src/dep.ts": "export type Foo = {};", + }, + []string{"/src/Component.vue", physicalFileName}, + &core.CompilerOptions{ + SkipLibCheck: core.TSTrue, + Module: tt.module, + ModuleResolution: tt.moduleResolution, + ResolvePackageJsonExports: core.TSTrue, + VerbatimModuleSyntax: core.TSTrue, + }, + ) + + mappedFile := program.GetSourceFile("/src/Component.vue") + assert.Assert(t, mappedFile != nil) + assert.Equal(t, mappedFile.VirtualFileName(), tspath.RootedFilePathFromNormalized("/src/Component.vue"+tt.virtualExtension)) + assert.Equal(t, program.GetImpliedNodeFormatForEmit(mappedFile), tt.expectedFormat) + assert.Equal(t, program.GetEmitModuleFormatOfFile(mappedFile), tt.expectedFormat) + assert.Equal(t, program.GetDefaultResolutionModeForFile(mappedFile), tt.expectedFormat) + + imports := mappedFile.Imports() + assert.Equal(t, len(imports), 1) + assert.Equal(t, program.GetModeForUsageLocation(mappedFile, imports[0]), tt.expectedFormat) + assert.Equal(t, program.GetEmitSyntaxForUsageLocation(mappedFile, imports[0]), tt.expectedFormat) + + mappedDiagnosticCodes := core.Map(program.GetSemanticDiagnostics(t.Context(), mappedFile), func(diagnostic *ast.Diagnostic) int32 { + return diagnostic.Code() + }) + assert.Assert(t, !slices.Contains(mappedDiagnosticCodes, tt.expectedDiagnostic), "mapped file has the format diagnostic: %v", mappedDiagnosticCodes) + + physicalFile := program.GetSourceFile(tspath.RootedFilePathFromNormalized(physicalFileName)) + assert.Assert(t, physicalFile != nil) + physicalDiagnosticCodes := core.Map(program.GetSemanticDiagnostics(t.Context(), physicalFile), func(diagnostic *ast.Diagnostic) int32 { + return diagnostic.Code() + }) + assert.Assert(t, !slices.Contains(physicalDiagnosticCodes, tt.expectedDiagnostic), "physical file has the format diagnostic: %v", physicalDiagnosticCodes) + }) + } +} + func TestContentMapperDirectivesPreserveIncrementalGlobals(t *testing.T) { t.Parallel() for _, policy := range []ast.MappedDiagnosticDirectivePolicy{ diff --git a/tsc/internal/compiler/fileloader.go b/tsc/internal/compiler/fileloader.go index e1a623e2f7746..6e430b63e4988 100644 --- a/tsc/internal/compiler/fileloader.go +++ b/tsc/internal/compiler/fileloader.go @@ -870,7 +870,7 @@ func (p *fileLoader) resolveImportsAndModuleAugmentations(t *parseTask) { continue } - mode := getModeForUsageLocation(file.FileName(), meta, entry, optionsForFile) + mode := getModeForUsageLocation(getModuleFormatFileName(file), meta, entry, optionsForFile) // We know moduleName resolves to an ambient module provided that moduleName: // - is in the list of ambient modules locally declared in the current source file. if slices.Contains(file.AmbientModuleNames, moduleName) { @@ -1029,10 +1029,19 @@ func getModeForTypeReferenceDirectiveInFile(ref *ast.FileReference, file *ast.So if ref.ResolutionMode != core.ResolutionModeNone { return ref.ResolutionMode } else { - return getDefaultResolutionModeForFile(file.FileName(), meta, options) + return getDefaultResolutionModeForFile(getModuleFormatFileName(file), meta, options) } } +func getModuleFormatFileName(file ast.HasFileName) tspath.RootedFilePath { + if sourceFile, ok := file.(*ast.SourceFile); ok { + if virtualFileName := sourceFile.VirtualFileName(); virtualFileName != "" { + return virtualFileName + } + } + return file.FileName() +} + func getDefaultResolutionModeForFile(fileName tspath.RootedFilePath, meta ast.SourceFileMetaData, options *core.CompilerOptions) core.ResolutionMode { if importSyntaxAffectsModuleResolution(options) { return ast.GetImpliedNodeFormatForEmitWorker(fileName, options.GetEmitModuleKind(), meta) diff --git a/tsc/internal/compiler/program.go b/tsc/internal/compiler/program.go index 62b9de7ce981c..300dfa86a6128 100644 --- a/tsc/internal/compiler/program.go +++ b/tsc/internal/compiler/program.go @@ -1838,19 +1838,19 @@ func (p *Program) GetSourceFileMetaData(path tspath.PathKey) ast.SourceFileMetaD } func (p *Program) GetEmitModuleFormatOfFile(sourceFile ast.HasFileName) core.ModuleKind { - return ast.GetEmitModuleFormatOfFileWorker(sourceFile.FileName(), p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile), p.GetSourceFileMetaData(sourceFile.PathKey())) + return ast.GetEmitModuleFormatOfFileWorker(getModuleFormatFileName(sourceFile), p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile), p.GetSourceFileMetaData(sourceFile.PathKey())) } func (p *Program) GetEmitSyntaxForUsageLocation(sourceFile ast.HasFileName, location *ast.StringLiteralLike) core.ResolutionMode { - return getEmitSyntaxForUsageLocationWorker(sourceFile.FileName(), p.sourceFileMetaDatas[sourceFile.PathKey()], location, p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) + return getEmitSyntaxForUsageLocationWorker(getModuleFormatFileName(sourceFile), p.sourceFileMetaDatas[sourceFile.PathKey()], location, p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) } func (p *Program) GetImpliedNodeFormatForEmit(sourceFile ast.HasFileName) core.ResolutionMode { - return ast.GetImpliedNodeFormatForEmitWorker(sourceFile.FileName(), p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile).GetEmitModuleKind(), p.GetSourceFileMetaData(sourceFile.PathKey())) + return ast.GetImpliedNodeFormatForEmitWorker(getModuleFormatFileName(sourceFile), p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile).GetEmitModuleKind(), p.GetSourceFileMetaData(sourceFile.PathKey())) } func (p *Program) GetModeForUsageLocation(sourceFile ast.HasFileName, location *ast.StringLiteralLike) core.ResolutionMode { - return getModeForUsageLocation(sourceFile.FileName(), p.sourceFileMetaDatas[sourceFile.PathKey()], location, p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) + return getModeForUsageLocation(getModuleFormatFileName(sourceFile), p.sourceFileMetaDatas[sourceFile.PathKey()], location, p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) } func (p *Program) GetModeForResolutionAtIndex(sourceFile *ast.SourceFile, index int) core.ResolutionMode { @@ -1871,7 +1871,7 @@ func (p *Program) GetModeForResolutionAtIndex(sourceFile *ast.SourceFile, index } func (p *Program) GetDefaultResolutionModeForFile(sourceFile ast.HasFileName) core.ResolutionMode { - return getDefaultResolutionModeForFile(sourceFile.FileName(), p.sourceFileMetaDatas[sourceFile.PathKey()], p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) + return getDefaultResolutionModeForFile(getModuleFormatFileName(sourceFile), p.sourceFileMetaDatas[sourceFile.PathKey()], p.projectReferenceFileMapper.getCompilerOptionsForFile(sourceFile)) } func (p *Program) IsSourceFileDefaultLibrary(path tspath.PathKey) bool { From 4dcfd8c1ed6969ca78076d603db5113cc94e6886 Mon Sep 17 00:00:00 2001 From: Andrew Branch Date: Tue, 10 Nov 2026 09:47:00 -0800 Subject: [PATCH 2/5] =?UTF-8?q?Issue=20diagnostic=20when=20content=20mappe?= =?UTF-8?q?r=E2=80=99s=20`extension`=20response=20implies=20a=20different?= =?UTF-8?q?=20module=20format=20than=20its=20`outputExtensions`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- tsc/internal/compiler/contentmapper_test.go | 121 +++++++++++++++++- tsc/internal/compiler/fileloader.go | 27 ++++ tsc/internal/contentmapper/contentmapper.go | 1 + tsc/internal/contentmapper/host.go | 3 +- .../diagnosticMessages.generated.json | 1 + .../diagnostics/diagnosticMessages.json | 4 + .../diagnostics/diagnostics_generated.go | 3 + .../contentMapperOutputExtensions_test.go | 64 +++++++++ tsc/internal/project/contentmapper_test.go | 18 ++- tsc/internal/testrunner/compiler_runner.go | 9 +- .../contentMapperModuleFormat.contentmapper | 34 +++++ ...ontentMapperOutputExtensions.contentmapper | 2 + .../compiler/contentMapperOutputExtensions.js | 1 + .../contentMapperOutputExtensions.js.map | 2 +- ...ontentMapperOutputExtensions.sourcemap.txt | 25 ++-- .../contentMapperOutputExtensions.symbols | 3 +- .../contentMapperOutputExtensions.types | 1 + ...apperOutputExtensionsOverlap.contentmapper | 2 + .../contentMapperOutputExtensionsOverlap.js | 1 + ...ntentMapperOutputExtensionsOverlap.symbols | 3 +- ...contentMapperOutputExtensionsOverlap.types | 1 + .../compiler/contentMapperModuleFormat.ts | 40 ++++++ .../compiler/contentMapperOutputExtensions.ts | 1 + .../contentMapperOutputExtensionsOverlap.ts | 1 + 24 files changed, 345 insertions(+), 23 deletions(-) create mode 100644 tsc/testdata/baselines/reference/compiler/contentMapperModuleFormat.contentmapper create mode 100644 tsc/testdata/tests/cases/compiler/contentMapperModuleFormat.ts diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 65d570f6df2e7..0ad19059350d7 100644 --- a/tsc/internal/compiler/contentmapper_test.go +++ b/tsc/internal/compiler/contentmapper_test.go @@ -13,6 +13,7 @@ import ( "github.com/microsoft/TypeScript/tsc/internal/core" "github.com/microsoft/TypeScript/tsc/internal/diagnostics" "github.com/microsoft/TypeScript/tsc/internal/locale" + "github.com/microsoft/TypeScript/tsc/internal/outputpaths" "github.com/microsoft/TypeScript/tsc/internal/spanmap" "github.com/microsoft/TypeScript/tsc/internal/tsoptions" "github.com/microsoft/TypeScript/tsc/internal/tspath" @@ -46,6 +47,11 @@ func newContentMapperProgram(t *testing.T, contentMapperProject contentmapper.Pr } func newContentMapperProgramWithOptions(t *testing.T, contentMapperProject contentmapper.Project, files map[string]string, rootFiles []string, options *core.CompilerOptions) *compiler.Program { + t.Helper() + return newContentMapperProgramWithOutputExtensions(t, contentMapperProject, files, rootFiles, options, nil) +} + +func newContentMapperProgramWithOutputExtensions(t *testing.T, contentMapperProject contentmapper.Project, files map[string]string, rootFiles []string, options *core.CompilerOptions, outputExtensions map[string]string) *compiler.Program { t.Helper() if !bundled.Embedded { t.Skip("bundled files are not embedded") @@ -57,7 +63,7 @@ func newContentMapperProgramWithOptions(t *testing.T, contentMapperProject conte } config := tsoptions.NewParsedCommandLine(options, testFileNames(rootFiles...), nil, "/", fs.CaseSensitivity()) - config.ParsedConfig.ContentMappers = []*contentmapper.Mapper{{Package: "vue", Extensions: []string{".vue"}, Name: "vue-mapper", Version: "1.0.0"}} + config.ParsedConfig.ContentMappers = []*contentmapper.Mapper{{Package: "vue", Extensions: []string{".vue"}, OutputExtensions: outputExtensions, Name: "vue-mapper", Version: "1.0.0"}} return compiler.NewProgram(compiler.ProgramOptions{ Config: config, Host: compiler.NewCompilerHost(fs, bundled.LibPath(), nil, nil, contentMapperProject), @@ -68,7 +74,7 @@ func newContentMapperProgramWithOptions(t *testing.T, contentMapperProject conte func TestContentMapperVirtualExtensionSetsImpliedNodeFormat(t *testing.T) { t.Parallel() - program := newContentMapperProgramWithOptions( + program := newContentMapperProgramWithOutputExtensions( t, fakeContentMapperHost{transform: func(fileName string, content string) (contentmapper.Result, error) { return contentmapper.Result{Text: "export {};", VirtualExtension: ".mts", Mappings: spanmap.New(nil)}, nil @@ -80,6 +86,7 @@ func TestContentMapperVirtualExtensionSetsImpliedNodeFormat(t *testing.T) { Module: core.ModuleKindNodeNext, ModuleResolution: core.ModuleResolutionKindNodeNext, }, + map[string]string{".vue": ".mjs"}, ) file := program.GetSourceFile("/src/Component.vue") @@ -122,7 +129,7 @@ func TestContentMapperVirtualExtensionDeterminesModuleFormat(t *testing.T) { t.Run(tt.name, func(t *testing.T) { t.Parallel() physicalFileName := "/src/physical" + tt.virtualExtension - program := newContentMapperProgramWithOptions( + program := newContentMapperProgramWithOutputExtensions( t, fakeContentMapperHost{transform: func(fileName string, content string) (contentmapper.Result, error) { return contentmapper.Result{Text: content, VirtualExtension: tt.virtualExtension, Mappings: spanmap.New(nil)}, nil @@ -140,6 +147,7 @@ func TestContentMapperVirtualExtensionDeterminesModuleFormat(t *testing.T) { ResolvePackageJsonExports: core.TSTrue, VerbatimModuleSyntax: core.TSTrue, }, + map[string]string{".vue": tt.virtualExtension}, ) mappedFile := program.GetSourceFile("/src/Component.vue") @@ -169,6 +177,113 @@ func TestContentMapperVirtualExtensionDeterminesModuleFormat(t *testing.T) { } } +func TestContentMapperDeclarationModuleFormat(t *testing.T) { + t.Parallel() + for _, test := range []struct { + outputExtension string + declarationName string + differentPackageScope bool + }{ + {".cjs", "Component.d.cts", false}, + {".cts", "Component.d.cts", false}, + {".mjs", "Component.d.mts", false}, + {".mts", "Component.d.mts", false}, + {".custom.mjs", "Component.custom.d.mts", false}, + {".custom.cts", "Component.custom.d.cts", false}, + {".js", "Component.d.ts", false}, + {"", "Component.d.vue.ts", false}, + {".js", "Component.d.ts", true}, + {"", "Component.d.vue.ts", true}, + } { + for _, packageType := range []string{"module", "commonjs"} { + for _, virtualExtension := range []string{".cts", ".cjs", ".mts", ".mjs", ".ts", ".tsx", ".js", ".jsx"} { + name := test.outputExtension + "/" + packageType + "/" + virtualExtension + if test.differentPackageScope { + name += "/different-output-package" + } + t.Run(name, func(t *testing.T) { + t.Parallel() + outputPackageType := packageType + if test.differentPackageScope { + outputPackageType = core.IfElse(packageType == "module", "commonjs", "module") + } + outputExtensions := map[string]string{} + if test.outputExtension != "" { + outputExtensions[".vue"] = test.outputExtension + } + program := newContentMapperProgramWithOutputExtensions(t, fakeContentMapperHost{ + transform: func(fileName string, content string) (contentmapper.Result, error) { + return contentmapper.Result{Text: "export const value = 1;", VirtualExtension: virtualExtension, Mappings: spanmap.New(nil)}, nil + }, + }, map[string]string{ + "/src/package.json": `{"type":"` + packageType + `"}`, + "/dist/package.json": `{"type":"` + outputPackageType + `"}`, + "/src/Component.vue": "