diff --git a/tsc/internal/compiler/contentmapper_test.go b/tsc/internal/compiler/contentmapper_test.go index 31b8f831870e8..d477022346274 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") @@ -87,6 +94,282 @@ 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 := 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 + }}, + 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, + }, + map[string]string{".vue": tt.virtualExtension}, + ) + + 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 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": "