diff --git a/Herebyfile.mjs b/Herebyfile.mjs index 5143cfe8692..c05fd8f1b9d 100644 --- a/Herebyfile.mjs +++ b/Herebyfile.mjs @@ -355,7 +355,7 @@ const enumDefs = [ { name: "TokenFlags", goPrefix: "TokenFlags", goFile: "internal/ast/tokenflags.go", outDir: "_packages/native-preview/src/enums" }, { name: "NodeBuilderFlags", goPrefix: "Flags", goFile: "internal/nodebuilder/types.go", outDir: "_packages/native-preview/src/enums" }, { name: "CompletionItemKind", goPrefix: "CompletionItemKind", goFile: "internal/lsp/lsproto/lsp_generated.go", outDir: "_packages/native-preview/src/enums" }, - { name: "EmitOnly", goPrefix: "Emit", goFile: "internal/compiler/emitter.go", outDir: "_packages/native-preview/src/enums", excludeMembers: ["OnlyForcedDts"] }, + { name: "EmitOnly", goPrefix: "Emit", goFile: "internal/compiler/emitter.go", outDir: "_packages/native-preview/src/enums", excludeMembers: ["OnlyBuilderSignature"] }, // String enum: Go stores internal names with a "\xFE" sentinel prefix, but the escaped // form sent over the wire uses "__" (see EscapeSymbolName), so map the sentinel accordingly. { name: "InternalSymbolName", goPrefix: "InternalSymbolName", goFile: "internal/ast/symbol.go", outDir: "_packages/native-preview/src/enums", stringEnum: true, valueReplacements: { InternalSymbolNamePrefix: "__" } }, diff --git a/_packages/native-preview/src/api/async/api.ts b/_packages/native-preview/src/api/async/api.ts index 77e19bf93f9..f63d500fb3a 100644 --- a/_packages/native-preview/src/api/async/api.ts +++ b/_packages/native-preview/src/api/async/api.ts @@ -137,6 +137,18 @@ interface EmitOutputResponse { readonly outputFiles: readonly (EmitOutputFile & { readonly fileName: string; })[]; } +export interface TranspileOptions { + compilerOptions?: CompilerOptions; + fileName?: string; + reportDiagnostics?: boolean; +} + +export interface TranspileOutput { + outputText: string; + diagnostics?: readonly Diagnostic[]; + sourceMapText?: string; +} + export class API { private client: Client; private sourceFileCache: SourceFileCache; @@ -177,6 +189,26 @@ export class API { return this.client.apiRequest("parseConfigFile", { file }); } + async transpileModule(input: string, options: TranspileOptions = {}): Promise { + await this.ensureInitialized(); + return this.client.apiRequest("transpileModule", { input, options }); + } + + async transpileModuleFromFile(fileName: string, options: TranspileOptions = {}): Promise { + await this.ensureInitialized(); + return this.client.apiRequest("transpileModuleFromFile", { fileName, options }); + } + + async transpileDeclaration(input: string, options: TranspileOptions = {}): Promise { + await this.ensureInitialized(); + return this.client.apiRequest("transpileDeclaration", { input, options }); + } + + async transpileDeclarationFromFile(fileName: string, options: TranspileOptions = {}): Promise { + await this.ensureInitialized(); + return this.client.apiRequest("transpileDeclarationFromFile", { fileName, options }); + } + async updateSnapshot(params?: FromLSP extends true ? LSPUpdateSnapshotParams : UpdateSnapshotParams): Promise { await this.ensureInitialized(); diff --git a/_packages/native-preview/src/api/sync/api.ts b/_packages/native-preview/src/api/sync/api.ts index 447656f2a57..22aacc48737 100644 --- a/_packages/native-preview/src/api/sync/api.ts +++ b/_packages/native-preview/src/api/sync/api.ts @@ -145,6 +145,18 @@ interface EmitOutputResponse { readonly outputFiles: readonly (EmitOutputFile & { readonly fileName: string; })[]; } +export interface TranspileOptions { + compilerOptions?: CompilerOptions; + fileName?: string; + reportDiagnostics?: boolean; +} + +export interface TranspileOutput { + outputText: string; + diagnostics?: readonly Diagnostic[]; + sourceMapText?: string; +} + export class API { private client: Client; private sourceFileCache: SourceFileCache; @@ -185,6 +197,26 @@ export class API { return this.client.apiRequest("parseConfigFile", { file }); } + transpileModule(input: string, options: TranspileOptions = {}): TranspileOutput { + this.ensureInitialized(); + return this.client.apiRequest("transpileModule", { input, options }); + } + + transpileModuleFromFile(fileName: string, options: TranspileOptions = {}): TranspileOutput { + this.ensureInitialized(); + return this.client.apiRequest("transpileModuleFromFile", { fileName, options }); + } + + transpileDeclaration(input: string, options: TranspileOptions = {}): TranspileOutput { + this.ensureInitialized(); + return this.client.apiRequest("transpileDeclaration", { input, options }); + } + + transpileDeclarationFromFile(fileName: string, options: TranspileOptions = {}): TranspileOutput { + this.ensureInitialized(); + return this.client.apiRequest("transpileDeclarationFromFile", { fileName, options }); + } + updateSnapshot(params?: FromLSP extends true ? LSPUpdateSnapshotParams : UpdateSnapshotParams): Snapshot { this.ensureInitialized(); diff --git a/_packages/native-preview/test/async/api.test.ts b/_packages/native-preview/test/async/api.test.ts index e084ebc2731..ce4e6f53af3 100644 --- a/_packages/native-preview/test/async/api.test.ts +++ b/_packages/native-preview/test/async/api.test.ts @@ -89,6 +89,32 @@ const defaultFiles = { }; describe("API", () => { + test("transpile", async () => { + const api = spawnAPI({ + "/input.ts": "export const x: number = 1;", + }); + try { + const moduleOutput = await api.transpileModule("export const x: number = 1;", { + compilerOptions: { module: ModuleKind.CommonJS }, + }); + assert.match(moduleOutput.outputText, /exports\.x = 1/); + + const moduleFileOutput = await api.transpileModuleFromFile("/input.ts", { + compilerOptions: { module: ModuleKind.CommonJS }, + }); + assert.match(moduleFileOutput.outputText, /exports\.x = 1/); + + const declarationOutput = await api.transpileDeclaration("export const x: number = 1;"); + assert.equal(declarationOutput.outputText, "export declare const x: number;\n"); + + const declarationFileOutput = await api.transpileDeclarationFromFile("/input.ts"); + assert.equal(declarationFileOutput.outputText, "export declare const x: number;\n"); + } + finally { + await api.close(); + } + }); + test("parseConfigFile", async () => { const api = spawnAPI(); try { diff --git a/_packages/native-preview/test/sync/api.test.ts b/_packages/native-preview/test/sync/api.test.ts index 0203aa28ffe..9fe20fb9192 100644 --- a/_packages/native-preview/test/sync/api.test.ts +++ b/_packages/native-preview/test/sync/api.test.ts @@ -97,6 +97,32 @@ const defaultFiles = { }; describe("API", () => { + test("transpile", () => { + const api = spawnAPI({ + "/input.ts": "export const x: number = 1;", + }); + try { + const moduleOutput = api.transpileModule("export const x: number = 1;", { + compilerOptions: { module: ModuleKind.CommonJS }, + }); + assert.match(moduleOutput.outputText, /exports\.x = 1/); + + const moduleFileOutput = api.transpileModuleFromFile("/input.ts", { + compilerOptions: { module: ModuleKind.CommonJS }, + }); + assert.match(moduleFileOutput.outputText, /exports\.x = 1/); + + const declarationOutput = api.transpileDeclaration("export const x: number = 1;"); + assert.equal(declarationOutput.outputText, "export declare const x: number;\n"); + + const declarationFileOutput = api.transpileDeclarationFromFile("/input.ts"); + assert.equal(declarationFileOutput.outputText, "export declare const x: number;\n"); + } + finally { + api.close(); + } + }); + test("parseConfigFile", () => { const api = spawnAPI(); try { diff --git a/internal/api/proto.go b/internal/api/proto.go index fd064deec70..67bf8dc19ca 100644 --- a/internal/api/proto.go +++ b/internal/api/proto.go @@ -69,30 +69,34 @@ const ( // the connection itself and is not recorded. MethodResetServerTiming Method = "resetServerTiming" - MethodInitialize Method = "initialize" - MethodUpdateSnapshot Method = "updateSnapshot" - MethodUpdateTemporarySnapshot Method = "updateTemporarySnapshot" - MethodParseConfigFile Method = "parseConfigFile" - MethodGetDefaultProjectForFile Method = "getDefaultProjectForFile" - MethodGetSymbolAtPosition Method = "getSymbolAtPosition" - MethodGetSymbolsAtPositions Method = "getSymbolsAtPositions" - MethodGetSymbolAtLocation Method = "getSymbolAtLocation" - MethodGetSymbolsAtLocations Method = "getSymbolsAtLocations" - MethodGetTypeOfSymbol Method = "getTypeOfSymbol" - MethodGetTypesOfSymbols Method = "getTypesOfSymbols" - MethodGetDeclaredTypeOfSymbol Method = "getDeclaredTypeOfSymbol" - MethodGetSourceFile Method = "getSourceFile" - MethodGetSourceFileNames Method = "getSourceFileNames" - MethodGetSourceFileMetadata Method = "getSourceFileMetadata" - MethodGetConfigFileNames Method = "getConfigFileNames" - MethodGetConfigSourceFile Method = "getConfigSourceFile" - MethodResolveName Method = "resolveName" - MethodGetSignaturesOfType Method = "getSignaturesOfType" - MethodGetResolvedSignature Method = "getResolvedSignature" - MethodGetTypeAtLocation Method = "getTypeAtLocation" - MethodGetTypeAtLocations Method = "getTypeAtLocations" - MethodGetTypeAtPosition Method = "getTypeAtPosition" - MethodGetTypesAtPositions Method = "getTypesAtPositions" + MethodInitialize Method = "initialize" + MethodUpdateSnapshot Method = "updateSnapshot" + MethodUpdateTemporarySnapshot Method = "updateTemporarySnapshot" + MethodParseConfigFile Method = "parseConfigFile" + MethodTranspileModule Method = "transpileModule" + MethodTranspileModuleFromFile Method = "transpileModuleFromFile" + MethodTranspileDeclaration Method = "transpileDeclaration" + MethodTranspileDeclarationFromFile Method = "transpileDeclarationFromFile" + MethodGetDefaultProjectForFile Method = "getDefaultProjectForFile" + MethodGetSymbolAtPosition Method = "getSymbolAtPosition" + MethodGetSymbolsAtPositions Method = "getSymbolsAtPositions" + MethodGetSymbolAtLocation Method = "getSymbolAtLocation" + MethodGetSymbolsAtLocations Method = "getSymbolsAtLocations" + MethodGetTypeOfSymbol Method = "getTypeOfSymbol" + MethodGetTypesOfSymbols Method = "getTypesOfSymbols" + MethodGetDeclaredTypeOfSymbol Method = "getDeclaredTypeOfSymbol" + MethodGetSourceFile Method = "getSourceFile" + MethodGetSourceFileNames Method = "getSourceFileNames" + MethodGetSourceFileMetadata Method = "getSourceFileMetadata" + MethodGetConfigFileNames Method = "getConfigFileNames" + MethodGetConfigSourceFile Method = "getConfigSourceFile" + MethodResolveName Method = "resolveName" + MethodGetSignaturesOfType Method = "getSignaturesOfType" + MethodGetResolvedSignature Method = "getResolvedSignature" + MethodGetTypeAtLocation Method = "getTypeAtLocation" + MethodGetTypeAtLocations Method = "getTypeAtLocations" + MethodGetTypeAtPosition Method = "getTypeAtPosition" + MethodGetTypesAtPositions Method = "getTypesAtPositions" // Symbol sub-property methods MethodGetParentOfSymbol Method = "getParentOfSymbol" @@ -385,31 +389,35 @@ type UpdateSnapshotResponse struct { } var unmarshalers = map[Method]func([]byte) (any, error){ - MethodRelease: unmarshallerFor[ReleaseParams], - MethodInitialize: noParams, - MethodUpdateSnapshot: unmarshallerFor[UpdateSnapshotParams], - MethodUpdateTemporarySnapshot: unmarshallerFor[UpdateTemporarySnapshotParams], - MethodParseConfigFile: unmarshallerFor[ParseConfigFileParams], - MethodGetDefaultProjectForFile: unmarshallerFor[GetDefaultProjectForFileParams], - MethodGetSourceFile: unmarshallerFor[GetSourceFileParams], - MethodGetSourceFileNames: unmarshallerFor[GetSourceFileNamesParams], - MethodGetSourceFileMetadata: unmarshallerFor[GetSourceFileParams], - MethodGetConfigFileNames: unmarshallerFor[GetProjectDiagnosticsParams], - MethodGetConfigSourceFile: unmarshallerFor[GetSourceFileParams], - MethodGetSymbolAtPosition: unmarshallerFor[GetSymbolAtPositionParams], - MethodGetSymbolsAtPositions: unmarshallerFor[GetSymbolsAtPositionsParams], - MethodGetSymbolAtLocation: unmarshallerFor[GetSymbolAtLocationParams], - MethodGetSymbolsAtLocations: unmarshallerFor[GetSymbolsAtLocationsParams], - MethodGetTypeOfSymbol: unmarshallerFor[GetTypeOfSymbolParams], - MethodGetTypesOfSymbols: unmarshallerFor[GetTypesOfSymbolsParams], - MethodGetDeclaredTypeOfSymbol: unmarshallerFor[GetTypeOfSymbolParams], - MethodResolveName: unmarshallerFor[ResolveNameParams], - MethodGetSignaturesOfType: unmarshallerFor[GetSignaturesOfTypeParams], - MethodGetResolvedSignature: unmarshallerFor[GetResolvedSignatureParams], - MethodGetTypeAtLocation: unmarshallerFor[GetTypeAtLocationParams], - MethodGetTypeAtLocations: unmarshallerFor[GetTypeAtLocationsParams], - MethodGetTypeAtPosition: unmarshallerFor[GetTypeAtPositionParams], - MethodGetTypesAtPositions: unmarshallerFor[GetTypesAtPositionsParams], + MethodRelease: unmarshallerFor[ReleaseParams], + MethodInitialize: noParams, + MethodUpdateSnapshot: unmarshallerFor[UpdateSnapshotParams], + MethodUpdateTemporarySnapshot: unmarshallerFor[UpdateTemporarySnapshotParams], + MethodParseConfigFile: unmarshallerFor[ParseConfigFileParams], + MethodTranspileModule: unmarshallerFor[TranspileParams], + MethodTranspileModuleFromFile: unmarshallerFor[TranspileFromFileParams], + MethodTranspileDeclaration: unmarshallerFor[TranspileParams], + MethodTranspileDeclarationFromFile: unmarshallerFor[TranspileFromFileParams], + MethodGetDefaultProjectForFile: unmarshallerFor[GetDefaultProjectForFileParams], + MethodGetSourceFile: unmarshallerFor[GetSourceFileParams], + MethodGetSourceFileNames: unmarshallerFor[GetSourceFileNamesParams], + MethodGetSourceFileMetadata: unmarshallerFor[GetSourceFileParams], + MethodGetConfigFileNames: unmarshallerFor[GetProjectDiagnosticsParams], + MethodGetConfigSourceFile: unmarshallerFor[GetSourceFileParams], + MethodGetSymbolAtPosition: unmarshallerFor[GetSymbolAtPositionParams], + MethodGetSymbolsAtPositions: unmarshallerFor[GetSymbolsAtPositionsParams], + MethodGetSymbolAtLocation: unmarshallerFor[GetSymbolAtLocationParams], + MethodGetSymbolsAtLocations: unmarshallerFor[GetSymbolsAtLocationsParams], + MethodGetTypeOfSymbol: unmarshallerFor[GetTypeOfSymbolParams], + MethodGetTypesOfSymbols: unmarshallerFor[GetTypesOfSymbolsParams], + MethodGetDeclaredTypeOfSymbol: unmarshallerFor[GetTypeOfSymbolParams], + MethodResolveName: unmarshallerFor[ResolveNameParams], + MethodGetSignaturesOfType: unmarshallerFor[GetSignaturesOfTypeParams], + MethodGetResolvedSignature: unmarshallerFor[GetResolvedSignatureParams], + MethodGetTypeAtLocation: unmarshallerFor[GetTypeAtLocationParams], + MethodGetTypeAtLocations: unmarshallerFor[GetTypeAtLocationsParams], + MethodGetTypeAtPosition: unmarshallerFor[GetTypeAtPositionParams], + MethodGetTypesAtPositions: unmarshallerFor[GetTypesAtPositionsParams], MethodGetParentOfSymbol: unmarshallerFor[GetSymbolPropertyParams], MethodGetMembersOfSymbol: unmarshallerFor[GetSymbolPropertyParams], @@ -519,6 +527,28 @@ type ParseConfigFileParams struct { File DocumentIdentifier `json:"file"` } +type TranspileOptions struct { + CompilerOptions *core.CompilerOptions `json:"compilerOptions,omitempty"` + FileName string `json:"fileName,omitempty"` + ReportDiagnostics bool `json:"reportDiagnostics,omitempty"` +} + +type TranspileParams struct { + Input string `json:"input"` + Options TranspileOptions `json:"options"` +} + +type TranspileFromFileParams struct { + FileName string `json:"fileName"` + Options TranspileOptions `json:"options"` +} + +type TranspileOutputResponse struct { + OutputText string `json:"outputText"` + Diagnostics []*DiagnosticResponse `json:"diagnostics,omitempty"` + SourceMapText string `json:"sourceMapText,omitempty"` +} + // ReleaseParams are the parameters for the release method. type ReleaseParams struct { Snapshot SnapshotID `json:"snapshot"` diff --git a/internal/api/session.go b/internal/api/session.go index 21060a91cbe..80b114c3748 100644 --- a/internal/api/session.go +++ b/internal/api/session.go @@ -27,6 +27,7 @@ import ( "github.com/microsoft/typescript-go/internal/pprof" "github.com/microsoft/typescript-go/internal/printer" "github.com/microsoft/typescript-go/internal/project" + "github.com/microsoft/typescript-go/internal/transpile" "github.com/microsoft/typescript-go/internal/tsoptions" "github.com/microsoft/typescript-go/internal/tspath" ) @@ -589,6 +590,14 @@ func (s *Session) HandleRequest(ctx context.Context, method string, params json. return s.handleUpdateTemporarySnapshot(ctx, parsed.(*UpdateTemporarySnapshotParams)) case string(MethodParseConfigFile): return s.handleParseConfigFile(ctx, parsed.(*ParseConfigFileParams)) + case string(MethodTranspileModule): + return s.handleTranspile(ctx, parsed.(*TranspileParams), false) + case string(MethodTranspileModuleFromFile): + return s.handleTranspileFromFile(ctx, parsed.(*TranspileFromFileParams), false) + case string(MethodTranspileDeclaration): + return s.handleTranspile(ctx, parsed.(*TranspileParams), true) + case string(MethodTranspileDeclarationFromFile): + return s.handleTranspileFromFile(ctx, parsed.(*TranspileFromFileParams), true) case string(MethodGetDefaultProjectForFile): return s.handleGetDefaultProjectForFile(ctx, parsed.(*GetDefaultProjectForFileParams)) case string(MethodGetSourceFile): @@ -1138,6 +1147,46 @@ func (s *Session) handleParseConfigFile(ctx context.Context, params *ParseConfig return NewConfigFileResponse(parsedCommandLine), nil } +func (s *Session) handleTranspile(ctx context.Context, params *TranspileParams, declaration bool) (*TranspileOutputResponse, error) { + return transpileOutput(ctx, params.Input, params.Options, declaration) +} + +func (s *Session) handleTranspileFromFile(ctx context.Context, params *TranspileFromFileParams, declaration bool) (*TranspileOutputResponse, error) { + fileName := tspath.GetNormalizedAbsolutePath(params.FileName, s.projectSession.GetCurrentDirectory()) + input, ok := s.projectSession.FS().ReadFile(fileName) + if !ok { + return nil, fmt.Errorf("%w: could not read file %q", ErrClientError, fileName) + } + options := params.Options + options.FileName = fileName + return transpileOutput(ctx, input, options, declaration) +} + +func transpileOutput(ctx context.Context, input string, options TranspileOptions, declaration bool) (*TranspileOutputResponse, error) { + transpileOptions := transpile.Options{ + CompilerOptions: options.CompilerOptions, + FileName: options.FileName, + ReportDiagnostics: options.ReportDiagnostics, + } + var output *transpile.Output + if declaration { + output = transpile.TranspileDeclaration(ctx, input, transpileOptions) + } else { + output = transpile.TranspileModule(ctx, input, transpileOptions) + } + if output == nil { + if err := ctx.Err(); err != nil { + return nil, err + } + return nil, errors.New("transpilation produced no output") + } + return &TranspileOutputResponse{ + OutputText: output.OutputText, + Diagnostics: NewDiagnosticResponses(output.Diagnostics), + SourceMapText: output.SourceMapText, + }, nil +} + // handleGetSourceFile returns a source file from a project within a snapshot. func (s *Session) handleGetSourceFile(ctx context.Context, params *GetSourceFileParams) (any, error) { sd, err := s.getSnapshotData(params.Snapshot) diff --git a/internal/compiler/emitter.go b/internal/compiler/emitter.go index d1464af3756..f6de252bac4 100644 --- a/internal/compiler/emitter.go +++ b/internal/compiler/emitter.go @@ -27,7 +27,7 @@ const ( EmitAll EmitOnly = iota EmitOnlyJs EmitOnlyDts - EmitOnlyForcedDts + EmitOnlyBuilderSignature ) type emitter struct { @@ -230,12 +230,12 @@ func (e *emitter) emitDeclarationFile(sourceFile *ast.SourceFile, declarationFil e.emitterDiagnostics.Add(elem) } - if !e.forceEmit && e.emitOnly != EmitOnlyForcedDts && (options.NoEmit == core.TSTrue || e.host.IsEmitBlocked(declarationFilePath)) { + if !e.forceEmit && e.emitOnly != EmitOnlyBuilderSignature && (options.NoEmit == core.TSTrue || e.host.IsEmitBlocked(declarationFilePath)) { e.emitResult.EmitSkipped = true return } - declBlocked := len(diags) > 0 && !e.forceEmit && e.emitOnly != EmitOnlyForcedDts + declBlocked := len(diags) > 0 && !e.forceEmit && e.emitOnly != EmitOnlyBuilderSignature if declBlocked { e.emitResult.EmitSkipped = true return @@ -248,7 +248,7 @@ func (e *emitter) emitDeclarationFile(sourceFile *ast.SourceFile, declarationFil // Module: options.Module, // NYI // ModuleResolution: options.ModuleResolution, // NYI Target: options.GetEmitScriptTarget(), - SourceMap: e.emitOnly != EmitOnlyForcedDts && options.DeclarationMap.IsTrue(), + SourceMap: e.emitOnly != EmitOnlyBuilderSignature && options.DeclarationMap.IsTrue(), InlineSourceMap: options.InlineSourceMap.IsTrue(), // InlineSources: options.InlineSources.IsTrue(), // ignored, per strada // ExtendedDiagnostics: options.ExtendedDiagnostics.IsTrue(), // NYI @@ -262,7 +262,7 @@ func (e *emitter) emitDeclarationFile(sourceFile *ast.SourceFile, declarationFil }, emitContext) declarationMapOptions := &core.CompilerOptions{ - SourceMap: core.IfElse(e.emitOnly != EmitOnlyForcedDts && options.DeclarationMap.IsTrue(), core.TSTrue, core.TSFalse), + SourceMap: core.IfElse(e.emitOnly != EmitOnlyBuilderSignature && options.DeclarationMap.IsTrue(), core.TSTrue, core.TSFalse), SourceRoot: options.SourceRoot, MapRoot: options.MapRoot, // Explicitly do not pass through either inline option. diff --git a/internal/compiler/program.go b/internal/compiler/program.go index 2a91845230d..3299b6a3acb 100644 --- a/internal/compiler/program.go +++ b/internal/compiler/program.go @@ -1655,7 +1655,7 @@ func (p *Program) Emit(ctx context.Context, options EmitOptions) *EmitResult { defer tr.Push(tracing.PhaseEmit, "emit", nil, true)() } - if !options.ForceEmit && options.EmitOnly != EmitOnlyForcedDts { + if !options.ForceEmit && options.EmitOnly != EmitOnlyBuilderSignature { result := HandleNoEmitOnError( ctx, p, @@ -1674,7 +1674,7 @@ func (p *Program) Emit(ctx context.Context, options EmitOptions) *EmitResult { } wg := core.NewWorkGroup(p.SingleThreaded()) var emitters []*emitter - forceDtsEmit := options.EmitOnly == EmitOnlyForcedDts || options.ForceEmit && options.EmitOnly == EmitOnlyDts + forceDtsEmit := options.EmitOnly == EmitOnlyBuilderSignature || options.ForceEmit && options.EmitOnly == EmitOnlyDts forceJsEmit := options.ForceEmit && options.EmitOnly == EmitOnlyJs sourceFiles := p.getSourceFilesToEmit(options.TargetSourceFiles, forceDtsEmit, forceJsEmit) diff --git a/internal/execute/incremental/affectedfileshandler.go b/internal/execute/incremental/affectedfileshandler.go index 1d62206fdf7..dc6ecc98505 100644 --- a/internal/execute/incremental/affectedfileshandler.go +++ b/internal/execute/incremental/affectedfileshandler.go @@ -70,7 +70,7 @@ func (h *affectedFilesHandler) computeDtsSignature(file *ast.SourceFile) string var signature string h.program.program.Emit(h.ctx, compiler.EmitOptions{ TargetSourceFiles: core.SingleElementSlice(file), - EmitOnly: compiler.EmitOnlyForcedDts, + EmitOnly: compiler.EmitOnlyBuilderSignature, WriteFile: func(fileName string, text string, data *compiler.WriteFileData) error { if !tspath.IsDeclarationFileName(fileName) { panic("File extension for signature expected to be dts, got : " + fileName) diff --git a/internal/testrunner/transpile_runner.go b/internal/testrunner/transpile_runner.go new file mode 100644 index 00000000000..fe2c499f5c4 --- /dev/null +++ b/internal/testrunner/transpile_runner.go @@ -0,0 +1,191 @@ +package testrunner + +import ( + "fmt" + "os" + "path/filepath" + "regexp" + "strings" + "testing" + + "github.com/microsoft/typescript-go/internal/core" + "github.com/microsoft/typescript-go/internal/diagnosticwriter" + "github.com/microsoft/typescript-go/internal/outputpaths" + "github.com/microsoft/typescript-go/internal/repo" + "github.com/microsoft/typescript-go/internal/testutil/baseline" + "github.com/microsoft/typescript-go/internal/testutil/harnessutil" + "github.com/microsoft/typescript-go/internal/testutil/tsbaseline" + "github.com/microsoft/typescript-go/internal/transpile" + "github.com/microsoft/typescript-go/internal/tspath" + "github.com/microsoft/typescript-go/internal/vfs/osvfs" +) + +var transpileBaselineRegex = regexp.MustCompile(`\.[cm]?[tj]sx?$`) + +var transpileVaryBy = map[string]struct{}{ + "declarationmap": {}, + "sourcemap": {}, + "inlinesourcemap": {}, +} + +type TranspileBaselineRunner struct { + testFiles []string + basePath string +} + +var _ Runner = (*TranspileBaselineRunner)(nil) + +func NewTranspileBaselineRunner() *TranspileBaselineRunner { + return &TranspileBaselineRunner{ + basePath: "../_submodules/TypeScript/tests/cases/transpile", + } +} + +func (r *TranspileBaselineRunner) EnumerateTestFiles() []string { + if len(r.testFiles) > 0 { + return r.testFiles + } + files, err := harnessutil.EnumerateFiles(r.basePath, transpileBaselineRegex, true) + if err != nil { + panic("Could not read transpile test files: " + err.Error()) + } + r.testFiles = files + return files +} + +func (r *TranspileBaselineRunner) RunTests(t *testing.T) { + for _, fileName := range r.EnumerateTestFiles() { + r.runTest(t, fileName) + } +} + +func (r *TranspileBaselineRunner) runTest(t *testing.T, fileName string) { + content, ok := osvfs.FS().ReadFile(fileName) + if !ok { + panic("Could not read transpile test file: " + fileName) + } + settings := extractCompilerSettings(content) + configurations := harnessutil.GetFileBasedTestConfigurations(t, settings, transpileVaryBy) + if len(configurations) == 0 { + configurations = []*harnessutil.NamedTestConfiguration{{Config: settings}} + } + + extension := tspath.GetAnyExtensionFromPath(fileName, nil, false) + baseName := tspath.GetBaseFileName(fileName) + justName := strings.TrimSuffix(baseName, extension) + units := makeUnitsFromTest(content, baseName).testUnitData + + for _, configuration := range configurations { + configuredName := justName + if configuration.Name != "" { + configuredName += "(" + formatTranspileConfigurationName(configuration.Name) + ")" + } + t.Run(configuredName, func(t *testing.T) { + options := &core.CompilerOptions{} + harnessOptions := &harnessutil.HarnessOptions{} + harnessutil.SetOptionsFromTestConfig(t, configuration.Config, options, harnessOptions, srcFolder, false) + + if !options.EmitDeclarationOnly.IsTrue() { + r.runKind(t, configuredName, extension, units, options, harnessOptions, false) + } + if options.Declaration.IsTrue() { + r.runKind(t, configuredName, extension, units, options, harnessOptions, true) + } + }) + } +} + +func formatTranspileConfigurationName(name string) string { + name = strings.ReplaceAll(name, "declarationmap=", "declarationMap=") + name = strings.ReplaceAll(name, "inlinesourcemap=", "inlineSourceMap=") + return strings.ReplaceAll(name, "sourcemap=", "sourceMap=") +} + +func (r *TranspileBaselineRunner) runKind( + t *testing.T, + configuredName string, + extension string, + units []*testUnit, + options *core.CompilerOptions, + harnessOptions *harnessutil.HarnessOptions, + declaration bool, +) { + var result strings.Builder + for _, unit := range units { + appendTranspileSection(&result, unit.name, unit.content) + } + + for _, unit := range units { + transpileOptions := transpile.Options{ + CompilerOptions: options, + FileName: unit.name, + ReportDiagnostics: harnessOptions.ReportDiagnostics, + } + var output *transpile.Output + if declaration { + output = transpile.TranspileDeclaration(t.Context(), unit.content, transpileOptions) + } else { + output = transpile.TranspileModule(t.Context(), unit.content, transpileOptions) + } + if output == nil { + t.Fatal("transpilation was canceled") + } + + outputExtension := outputpaths.GetOutputExtension(unit.name, options.Jsx) + if declaration { + outputExtension = tspath.GetDeclarationEmitExtensionForPath(unit.name) + } + outputFileName := tspath.ChangeExtension(unit.name, outputExtension) + appendTranspileSection(&result, outputFileName, output.OutputText) + if output.SourceMapText != "" { + appendTranspileSection(&result, outputFileName+".map", output.SourceMapText) + } + if len(output.Diagnostics) > 0 { + result.WriteString("\r\n\r\n//// [Diagnostics reported]\r\n") + diagnosticFileName := unit.name + if file := output.Diagnostics[0].File(); file != nil { + diagnosticFileName = file.FileName() + } + errorBaseline := tsbaseline.GetErrorBaseline( + t, + []*harnessutil.TestFile{{UnitName: diagnosticFileName, Content: unit.content}}, + diagnosticwriter.WrapASTDiagnostics(output.Diagnostics), + diagnosticwriter.CompareASTDiagnostics, + options.Pretty.IsTrue(), + ) + result.WriteString(strings.ReplaceAll(errorBaseline, diagnosticFileName, unit.name)) + if !strings.HasSuffix(result.String(), "\n") { + result.WriteString("\r\n") + } + } + } + + baselineExtension := outputpaths.GetOutputExtension(configuredName+extension, options.Jsx) + if declaration { + baselineExtension = tspath.GetDeclarationEmitExtensionForPath(configuredName + extension) + } + baselineName := configuredName + baselineExtension + baseline.Run(t, "transpile/"+baselineName, result.String(), baseline.Options{IsSubmodule: true}) +} + +func appendTranspileSection(result *strings.Builder, fileName string, content string) { + fmt.Fprintf(result, "//// [%s] ////\r\n", fileName) + result.WriteString(content) + if !strings.HasSuffix(content, "\n") { + result.WriteString("\r\n") + } +} + +func cleanTranspileBaselines() { + for _, folder := range []string{"submodule", "submoduleAccepted", "submoduleTriaged"} { + if err := os.RemoveAll(filepath.Join(localBasePath, folder, "transpile")); err != nil { + panic("Could not clean up transpile baselines: " + err.Error()) + } + } +} + +func RunTranspileTests(t *testing.T) { + repo.SkipIfNoTypeScriptSubmodule(t) + cleanTranspileBaselines() + NewTranspileBaselineRunner().RunTests(t) +} diff --git a/internal/testrunner/transpile_runner_test.go b/internal/testrunner/transpile_runner_test.go new file mode 100644 index 00000000000..dda8db93996 --- /dev/null +++ b/internal/testrunner/transpile_runner_test.go @@ -0,0 +1,8 @@ +package testrunner + +import "testing" + +func TestTranspile(t *testing.T) { + t.Parallel() + RunTranspileTests(t) +} diff --git a/internal/transpile/transpile.go b/internal/transpile/transpile.go new file mode 100644 index 00000000000..7b5c8ab5904 --- /dev/null +++ b/internal/transpile/transpile.go @@ -0,0 +1,257 @@ +// Package transpile implements single-file JavaScript and declaration emit. +package transpile + +import ( + "context" + "strings" + + "github.com/microsoft/typescript-go/internal/ast" + "github.com/microsoft/typescript-go/internal/compiler" + "github.com/microsoft/typescript-go/internal/core" + "github.com/microsoft/typescript-go/internal/debug" + "github.com/microsoft/typescript-go/internal/tsoptions" + "github.com/microsoft/typescript-go/internal/tspath" + "github.com/microsoft/typescript-go/internal/vfs/vfstest" +) + +// Options configures single-file transpilation. +type Options struct { + // CompilerOptions are the base compiler options to use for the transpilation. + // If nil, a default set of compiler options is used. Regardless of what is + // provided, a number of options are unconditionally overridden; see + // [TranspileModule] and [TranspileDeclaration]. + CompilerOptions *core.CompilerOptions + + // FileName is the name given to the synthesized input file. It only needs to + // be provided if the source text relies on characteristics implied by the + // file's extension or path, e.g. its extension controls whether the file is + // parsed as a script or module, whether JSX syntax is allowed, etc. + // Defaults to "module.ts", or "module.tsx" if CompilerOptions.Jsx is set. + FileName string + + // ReportDiagnostics indicates whether syntactic and compiler option + // diagnostics should be included in the result. Regardless of this setting, + // diagnostics produced while emitting (including declaration emit errors + // such as those produced by isolated declarations) are always included. + ReportDiagnostics bool +} + +// Output contains the emitted text and any requested diagnostics. +type Output struct { + OutputText string + Diagnostics []*ast.Diagnostic + SourceMapText string +} + +// inputDirectory is the synthetic current directory used to root the +// single input file created for transpilation. +const inputDirectory = "/" + +// libDirectory is the synthetic directory that the barebones default library +// file is placed in for declaration transpilation. See [barebonesLibContent]. +const libDirectory = "/lib" + +// Declaration emit works without a `lib`, but some local inferences you'd +// expect to work won't without at least a minimal `lib` available, since the +// checker will type inferred declarations as `any` without these defined. +// Late bound symbol names, in particular, are impossible to define without +// `Symbol` at least partially defined. +// TODO: This should *probably* just load the full, real `lib` for the target. +const barebonesLibContent = `interface Boolean {} +interface Function {} +interface CallableFunction {} +interface NewableFunction {} +interface IArguments {} +interface Number {} +interface Object {} +interface RegExp {} +interface String {} +interface Array { length: number; [n: number]: T; } +interface SymbolConstructor { + (desc?: string | number): symbol; + for(name: string): symbol; + readonly toStringTag: symbol; +} +declare var Symbol: SymbolConstructor; +interface Symbol { + readonly [Symbol.toStringTag]: string; +}` + +// TranspileModule transpiles a single file of source text to JavaScript +// using the specified options. If no compiler options are provided, a +// default set of compiler options is used. It returns nil if the context is +// canceled before emission completes. +// +// Extra compiler options that are unconditionally used by this function are: +// - IsolatedModules = true (unless VerbatimModuleSyntax is set, which makes +// this option redundant) +// - NoCheck = true +// - NoResolve = true +// - NoLib = true +// - Declaration = false +// - DeclarationMap = false +func TranspileModule(ctx context.Context, input string, options Options) *Output { + return transpileWorker(ctx, input, options, false /*declaration*/) +} + +// TranspileDeclaration creates a declaration (.d.ts) file from a single file +// of source text using the specified options. If no compiler options are +// provided, a default set of compiler options is used. +// +// Note that, because only the single input file is available, the resulting +// declaration file may differ from the one a full program type-check and +// emit would produce. +// +// Extra compiler options that are unconditionally used by this function are: +// - IsolatedModules = true (unless VerbatimModuleSyntax is set, which makes +// this option redundant) +// - NoCheck = true +// - NoResolve = true +// - NoLib = false +// - Declaration = true +// - EmitDeclarationOnly = true +// - IsolatedDeclarations = true +func TranspileDeclaration(ctx context.Context, input string, options Options) *Output { + return transpileWorker(ctx, input, options, true /*declaration*/) +} + +func transpileWorker(ctx context.Context, input string, options Options, declaration bool) *Output { + var opts *core.CompilerOptions + if options.CompilerOptions != nil { + opts = options.CompilerOptions.Clone() + } else { + opts = &core.CompilerOptions{} + } + + // Clear options that do not apply to single-file transpilation. + opts.Incremental = core.TSUnknown + opts.Declaration = core.TSUnknown + opts.EmitDeclarationOnly = core.TSUnknown + opts.NoEmit = core.TSUnknown + opts.Lib = nil + opts.OutFile = "" + opts.Composite = core.TSUnknown + opts.TsBuildInfoFile = "" + opts.Paths = nil + opts.RootDirs = nil + opts.Types = nil + opts.AllowImportingTsExtensions = core.TSUnknown + opts.NoEmitOnError = core.TSUnknown + opts.DeclarationDir = "" + + // Do not set `isolatedModules` if `verbatimModuleSyntax` was supplied, since + // it would be redundant. + if !opts.VerbatimModuleSyntax.IsTrue() { + opts.IsolatedModules = core.TSTrue + } + opts.NoCheck = core.TSTrue + opts.NoResolve = core.TSTrue + + // transpileModule/transpileDeclaration do not write anything to disk, so + // there's no need to verify there are no conflicts between input and + // output paths. + opts.SuppressOutputPathCheck = core.TSTrue + + // FileName can be a non-ts file. + opts.AllowNonTsExtensions = core.TSTrue + + if declaration { + opts.Declaration = core.TSTrue + opts.EmitDeclarationOnly = core.TSTrue + opts.IsolatedDeclarations = core.TSTrue + } else { + opts.Declaration = core.TSFalse + opts.DeclarationMap = core.TSFalse + } + + // When transpiling declarations, we need a lib. GetDefaultLibFileName will + // cause the barebones lib below to be used instead of a real lib. + if declaration { + opts.NoLib = core.TSFalse + } else { + opts.NoLib = core.TSTrue + } + + // If jsx is specified, then treat the file as .tsx. + fileName := options.FileName + if fileName == "" { + if opts.Jsx != core.JsxEmitNone { + fileName = "module.tsx" + } else { + fileName = "module.ts" + } + } + inputFileName := tspath.GetNormalizedAbsolutePath(fileName, inputDirectory) + + files := map[string]string{ + inputFileName: input, + } + + // Declaration emit needs a default lib to resolve global types (e.g. + // `Array`, `Symbol`); plain transpilation sets NoLib so none is read. + // The default lib name depends on the configured target. + if declaration { + libFileName := tsoptions.GetDefaultLibFileName(opts) + files[tspath.CombinePaths(libDirectory, libFileName)] = barebonesLibContent + } + + fs := vfstest.FromMap(files, true /*useCaseSensitiveFileNames*/) + host := compiler.NewCompilerHost(inputDirectory, fs, libDirectory, nil, nil) + + program := compiler.NewProgram(compiler.ProgramOptions{ + Config: &tsoptions.ParsedCommandLine{ + ParsedConfig: &core.ParsedOptions{ + FileNames: []string{inputFileName}, + CompilerOptions: opts, + }, + }, + Host: host, + }) + + var allDiagnostics []*ast.Diagnostic + if options.ReportDiagnostics { + sourceFile := program.GetSourceFile(inputFileName) + allDiagnostics = append(allDiagnostics, program.GetSyntacticDiagnostics(ctx, sourceFile)...) + allDiagnostics = append(allDiagnostics, program.GetConfigFileParsingDiagnostics()...) + allDiagnostics = append(allDiagnostics, program.GetProgramDiagnostics()...) + } + + emitOnly := compiler.EmitAll + if declaration { + emitOnly = compiler.EmitOnlyDts + } + + var outputText, sourceMapText string + var hasOutputText, hasSourceMapText bool + result := program.Emit(ctx, compiler.EmitOptions{ + EmitOnly: emitOnly, + ForceEmit: declaration, + WriteFile: func(fileName string, text string, data *compiler.WriteFileData) error { + if strings.HasSuffix(fileName, ".map") { + debug.Assert(!hasSourceMapText, "Unexpected multiple source map outputs, file: "+fileName) + sourceMapText = text + hasSourceMapText = true + } else { + debug.Assert(!hasOutputText, "Unexpected multiple outputs, file: "+fileName) + outputText = text + hasOutputText = true + } + return nil + }, + }) + if result == nil { + return nil + } + + // Diagnostics produced during emit (e.g. isolated declaration errors) are + // always included, regardless of ReportDiagnostics. + allDiagnostics = append(allDiagnostics, result.Diagnostics...) + + debug.Assert(hasOutputText, "Output generation failed") + + return &Output{ + OutputText: outputText, + Diagnostics: allDiagnostics, + SourceMapText: sourceMapText, + } +} diff --git a/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.d.ts b/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.d.ts new file mode 100644 index 00000000000..c7acdac65cc --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.d.ts @@ -0,0 +1,71 @@ +//// [declarationAsyncAndGeneratorFunctions.ts] //// +export async function asyncFn() { + return {} as Promise +} + +export async function asyncFn2() { + return {} as number +} + +export async function asyncFn3() { + return (await 42) as number; + } + +export function* generatorFn() { + return {} as number +} + +export async function* asyncGeneratorFn() { + return {} as number +} +//// [declarationAsyncAndGeneratorFunctions.d.ts] //// +export declare function asyncFn(): unknown; +export declare function asyncFn2(): unknown; +export declare function asyncFn3(): unknown; +export declare function generatorFn(): {}; +export declare function asyncGeneratorFn(): {}; + + +//// [Diagnostics reported] +declarationAsyncAndGeneratorFunctions.ts(1,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationAsyncAndGeneratorFunctions.ts(5,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationAsyncAndGeneratorFunctions.ts(9,23): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationAsyncAndGeneratorFunctions.ts(13,18): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +declarationAsyncAndGeneratorFunctions.ts(17,24): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. + + +==== declarationAsyncAndGeneratorFunctions.ts (5 errors) ==== + export async function asyncFn() { + ~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:1:23: Add a return type to the function declaration. + return {} as Promise + } + + export async function asyncFn2() { + ~~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:5:23: Add a return type to the function declaration. + return {} as number + } + + export async function asyncFn3() { + ~~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:9:23: Add a return type to the function declaration. + return (await 42) as number; + } + + export function* generatorFn() { + ~~~~~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:13:18: Add a return type to the function declaration. + return {} as number + } + + export async function* asyncGeneratorFn() { + ~~~~~~~~~~~~~~~~ +!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +!!! related TS9031 declarationAsyncAndGeneratorFunctions.ts:17:24: Add a return type to the function declaration. + return {} as number + } diff --git a/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.js b/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.js new file mode 100644 index 00000000000..9f0704cfd8a --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationAsyncAndGeneratorFunctions.js @@ -0,0 +1,36 @@ +//// [declarationAsyncAndGeneratorFunctions.ts] //// +export async function asyncFn() { + return {} as Promise +} + +export async function asyncFn2() { + return {} as number +} + +export async function asyncFn3() { + return (await 42) as number; + } + +export function* generatorFn() { + return {} as number +} + +export async function* asyncGeneratorFn() { + return {} as number +} +//// [declarationAsyncAndGeneratorFunctions.js] //// +export async function asyncFn() { + return {}; +} +export async function asyncFn2() { + return {}; +} +export async function asyncFn3() { + return (await 42); +} +export function* generatorFn() { + return {}; +} +export async function* asyncGeneratorFn() { + return {}; +} diff --git a/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).d.ts b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).d.ts new file mode 100644 index 00000000000..921b3bd3220 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).d.ts @@ -0,0 +1,113 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.d.ts] //// +export declare const a = 1; +export declare let b: number; +export declare var c: number; +declare const d: undefined; +export { d }; +declare const e: undefined; +export { e }; +//// [interface.d.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.d.ts] //// +export declare class Bar { + #private; + a: string; + b?: string; + c: string; + e: string; + protected f: string; + private g; + ["h"]: string; +} +export declare abstract class Baz { + abstract a: string; + abstract method(): void; +} + + +//// [Diagnostics reported] +class.ts(11,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + + +==== class.ts (1 errors) ==== + const i = Symbol(); + export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; + ~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + } + + export abstract class Baz { + abstract a: string; + abstract method(): void; + } +//// [namespace.d.ts] //// +export declare namespace ns { + namespace internal { + class Foo { + } + } + export namespace nested { + export import inner = internal; + } + export {}; +} +//// [alias.d.ts] //// +export type A = { + x: T; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).js b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).js new file mode 100644 index 00000000000..dd7a762bf28 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=false).js @@ -0,0 +1,145 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//// [interface.js] //// +export {}; +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//// [alias.js] //// +export {}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts new file mode 100644 index 00000000000..eee9a38470f --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts @@ -0,0 +1,128 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.d.ts] //// +export declare const a = 1; +export declare let b: number; +export declare var c: number; +declare const d: undefined; +export { d }; +declare const e: undefined; +export { e }; +//# sourceMappingURL=variables.d.ts.map +//// [variables.d.ts.map] //// +{"version":3,"file":"variables.d.ts","sourceRoot":"","sources":["variables.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,CAAC,IAAI,CAAC;AACnB,eAAO,IAAI,CAAC,QAAI,CAAC;AACjB,eAAO,IAAI,CAAC,QAAI,CAAC;AACjB,QAAA,MAAM,CAAC,WAAY,CAAC;AACpB,OAAO,EAAE,CAAC,EAAE,CAAC;AACb,QAAA,MAAY,CAAC,WAAY,CAAC;AAC1B,OAAO,EAAE,CAAC,EAAE,CAAC"} +//// [interface.d.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//# sourceMappingURL=interface.d.ts.map +//// [interface.d.ts.map] //// +{"version":3,"file":"interface.d.ts","sourceRoot":"","sources":["interface.ts"],"names":[],"mappings":"AAAA,MAAM,WAAW,GAAG;IAChB,CAAC,EAAE,MAAM,CAAC;IACV,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,CAAC,CAAC,EAAE,MAAM,CAAC;CACd"} +//// [class.d.ts] //// +export declare class Bar { + #private; + a: string; + b?: string; + c: string; + e: string; + protected f: string; + private g; + ["h"]: string; +} +export declare abstract class Baz { + abstract a: string; + abstract method(): void; +} +//# sourceMappingURL=class.d.ts.map +//// [class.d.ts.map] //// +{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["class.ts"],"names":[],"mappings":"AACA,qBAAa,GAAG;;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,CAAC,EAAE,MAAM,CAAC;IACH,CAAC,EAAE,MAAM,CAAC;IAEX,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,CAAC,CAAS;IAClB,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;CAEjB;AAED,8BAAsB,GAAG;IACrB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,MAAM,IAAI,IAAI,CAAC;CAC3B"} + + +//// [Diagnostics reported] +class.ts(11,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + + +==== class.ts (1 errors) ==== + const i = Symbol(); + export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; + ~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + } + + export abstract class Baz { + abstract a: string; + abstract method(): void; + } +//// [namespace.d.ts] //// +export declare namespace ns { + namespace internal { + class Foo { + } + } + export namespace nested { + export import inner = internal; + } + export {}; +} +//# sourceMappingURL=namespace.d.ts.map +//// [namespace.d.ts.map] //// +{"version":3,"file":"namespace.d.ts","sourceRoot":"","sources":["namespace.ts"],"names":[],"mappings":"AAAA,yBAAiB,EAAE,CAAC;IAChB,UAAU,QAAQ,CAAC;QACf,MAAa,GAAG;SAAG;KACtB;IACD,MAAM,WAAW,MAAM,CAAC;QACpB,MAAM,QAAQ,KAAK,GAAG,QAAQ,CAAC;KAClC;;CACJ"} +//// [alias.d.ts] //// +export type A = { + x: T; +}; +//# sourceMappingURL=alias.d.ts.map +//// [alias.d.ts.map] //// +{"version":3,"file":"alias.d.ts","sourceRoot":"","sources":["alias.ts"],"names":[],"mappings":"AAAA,MAAM,MAAM,CAAC,CAAC,CAAC,IAAI;IAAE,CAAC,EAAE,CAAC,CAAA;CAAE,CAAC"} diff --git a/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts.diff b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts.diff new file mode 100644 index 00000000000..01cfa8c1b23 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).d.ts.diff @@ -0,0 +1,11 @@ +--- old.transpile/declarationBasicSyntax(declarationMap=true).d.ts ++++ new.transpile/declarationBasicSyntax(declarationMap=true).d.ts +@@= skipped -77, +77 lines =@@ + } + //# sourceMappingURL=class.d.ts.map + //// [class.d.ts.map] //// +-{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["class.ts"],"names":[],"mappings":"AACA,qBAAa,GAAG;;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,CAAC,EAAE,MAAM,CAAC;IACH,CAAC,EAAE,MAAM,CAAC;IAEX,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,CAAC,CAAS;IAClB,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;CAEjB;AAED,8BAAsB,GAAG;IACrB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,MAAM,IAAI,IAAI;CAC1B"} ++{"version":3,"file":"class.d.ts","sourceRoot":"","sources":["class.ts"],"names":[],"mappings":"AACA,qBAAa,GAAG;;IACZ,CAAC,EAAE,MAAM,CAAC;IACV,CAAC,CAAC,EAAE,MAAM,CAAC;IACH,CAAC,EAAE,MAAM,CAAC;IAEX,CAAC,EAAE,MAAM,CAAC;IACjB,SAAS,CAAC,CAAC,EAAE,MAAM,CAAC;IACpB,OAAO,CAAC,CAAC,CAAS;IAClB,CAAC,GAAG,CAAC,EAAE,MAAM,CAAC;CAEjB;AAED,8BAAsB,GAAG;IACrB,QAAQ,CAAC,CAAC,EAAE,MAAM,CAAC;IACnB,QAAQ,CAAC,MAAM,IAAI,IAAI,CAAC;CAC3B"} + + + //// [Diagnostics reported] \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).js b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).js new file mode 100644 index 00000000000..dd7a762bf28 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationBasicSyntax(declarationMap=true).js @@ -0,0 +1,145 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//// [interface.js] //// +export {}; +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//// [alias.js] //// +export {}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts b/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts new file mode 100644 index 00000000000..c1259abf658 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts @@ -0,0 +1,254 @@ +//// [declarationComputedPropertyNames.ts] //// +export namespace presentNs { + export const a = Symbol(); +} + +const aliasing = Symbol; + +export type A = { + [missing]: number, + [ns.missing]: number, + [presentNs.a]: number, + [Symbol.iterator]: number, + [globalThis.Symbol.toStringTag]: number, + [(globalThis.Symbol).unscopables]: number, + [aliasing.isConcatSpreadable]: number, + [1]: number, + ["2"]: number, + [(missing2)]: number, + [Math.random() > 0.5 ? "f1" : "f2"]: number, +}; + +export interface B { + [missing]: number, + [ns.missing]: number, + [presentNs.a]: number, + [Symbol.iterator]: number, + [globalThis.Symbol.toStringTag]: number, + [(globalThis.Symbol).unscopables]: number, + [aliasing.isConcatSpreadable]: number, + [1]: number, + ["2"]: number, + [(missing2)]: number, + [Math.random() > 0.5 ? "f1" : "f2"]: number, +} + +export class C { + [missing]: number = 1; + [ns.missing]: number = 1; + [presentNs.a]: number = 1; + [Symbol.iterator]: number = 1; + [globalThis.Symbol.toStringTag]: number = 1; + [(globalThis.Symbol).unscopables]: number = 1; + [aliasing.isConcatSpreadable]: number = 1; + [1]: number = 1; + ["2"]: number = 1; + [(missing2)]: number = 1; + [Math.random() > 0.5 ? "f1" : "f2"]: number = 1; +} + +export const D = { + [missing]: 1, + [ns.missing]: 1, + [presentNs.a]: 1, + [Symbol.iterator]: 1, + [globalThis.Symbol.toStringTag]: 1, + [(globalThis.Symbol).unscopables]: 1, + [aliasing.isConcatSpreadable]: 1, + [1]: 1, + ["2"]: 1, + [(missing2)]: 1, + [Math.random() > 0.5 ? "f1" : "f2"]: 1, +}; +//// [declarationComputedPropertyNames.d.ts] //// +export declare namespace presentNs { + const a: unique symbol; +} +declare const aliasing: SymbolConstructor; +export type A = { + [missing]: number; + [ns.missing]: number; + [presentNs.a]: number; + [Symbol.iterator]: number; + [globalThis.Symbol.toStringTag]: number; + [aliasing.isConcatSpreadable]: number; + [1]: number; + ["2"]: number; +}; +export interface B { + [missing]: number; + [ns.missing]: number; + [presentNs.a]: number; + [Symbol.iterator]: number; + [globalThis.Symbol.toStringTag]: number; + [aliasing.isConcatSpreadable]: number; + [1]: number; + ["2"]: number; +} +export declare class C { + [x: number]: number; + [Symbol.iterator]: number; + [globalThis.Symbol.toStringTag]: number; + [1]: number; + ["2"]: number; +} +export declare const D: { + [x: string]: number; + [x: number]: number; + [presentNs.a]: number; + [SymbolConstructor.toStringTag]: number; + 1: number; + "2": number; +}; +export {}; + + +//// [Diagnostics reported] +declarationComputedPropertyNames.ts(2,18): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationComputedPropertyNames.ts(5,7): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationComputedPropertyNames.ts(13,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(17,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(18,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(27,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(31,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(32,5): error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. +declarationComputedPropertyNames.ts(36,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(37,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(38,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(41,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(42,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(45,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(46,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(50,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(51,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(52,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(53,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(54,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(55,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(56,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(59,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +declarationComputedPropertyNames.ts(60,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + + +==== declarationComputedPropertyNames.ts (24 errors) ==== + export namespace presentNs { + export const a = Symbol(); + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:2:18: Add a type annotation to the variable a. + } + + const aliasing = Symbol; + ~~~~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:5:7: Add a type annotation to the variable aliasing. + + export type A = { + [missing]: number, + [ns.missing]: number, + [presentNs.a]: number, + [Symbol.iterator]: number, + [globalThis.Symbol.toStringTag]: number, + [(globalThis.Symbol).unscopables]: number, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + [aliasing.isConcatSpreadable]: number, + [1]: number, + ["2"]: number, + [(missing2)]: number, + ~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + [Math.random() > 0.5 ? "f1" : "f2"]: number, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + }; + + export interface B { + [missing]: number, + [ns.missing]: number, + [presentNs.a]: number, + [Symbol.iterator]: number, + [globalThis.Symbol.toStringTag]: number, + [(globalThis.Symbol).unscopables]: number, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + [aliasing.isConcatSpreadable]: number, + [1]: number, + ["2"]: number, + [(missing2)]: number, + ~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + [Math.random() > 0.5 ? "f1" : "f2"]: number, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9014: Computed properties must be number or string literals, variables or dotted expressions with --isolatedDeclarations. + } + + export class C { + [missing]: number = 1; + ~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [ns.missing]: number = 1; + ~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [presentNs.a]: number = 1; + ~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [Symbol.iterator]: number = 1; + [globalThis.Symbol.toStringTag]: number = 1; + [(globalThis.Symbol).unscopables]: number = 1; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [aliasing.isConcatSpreadable]: number = 1; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [1]: number = 1; + ["2"]: number = 1; + [(missing2)]: number = 1; + ~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + [Math.random() > 0.5 ? "f1" : "f2"]: number = 1; + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + } + + export const D = { + [missing]: 1, + ~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [ns.missing]: 1, + ~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [presentNs.a]: 1, + ~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [Symbol.iterator]: 1, + ~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [globalThis.Symbol.toStringTag]: 1, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [(globalThis.Symbol).unscopables]: 1, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [aliasing.isConcatSpreadable]: 1, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [1]: 1, + ["2"]: 1, + [(missing2)]: 1, + ~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [Math.random() > 0.5 ? "f1" : "f2"]: 1, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. +!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + }; + diff --git a/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts.diff b/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts.diff new file mode 100644 index 00000000000..c41b72bbb80 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationComputedPropertyNames.d.ts.diff @@ -0,0 +1,42 @@ +--- old.transpile/declarationComputedPropertyNames.d.ts ++++ new.transpile/declarationComputedPropertyNames.d.ts +@@= skipped -95, +95 lines =@@ + [x: string]: number; + [x: number]: number; + [presentNs.a]: number; +- [aliasing.toStringTag]: number; ++ [SymbolConstructor.toStringTag]: number; + 1: number; + "2": number; + }; +@@= skipped -26, +26 lines =@@ + declarationComputedPropertyNames.ts(50,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(51,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(52,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. ++declarationComputedPropertyNames.ts(53,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. ++declarationComputedPropertyNames.ts(54,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(55,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(56,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(59,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + declarationComputedPropertyNames.ts(60,5): error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + + +-==== declarationComputedPropertyNames.ts (22 errors) ==== ++==== declarationComputedPropertyNames.ts (24 errors) ==== + export namespace presentNs { + export const a = Symbol(); + ~ +@@= skipped -101, +103 lines =@@ + !!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. + !!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [Symbol.iterator]: 1, ++ ~~~~~~~~~~~~~~~~~ ++!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. ++!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [globalThis.Symbol.toStringTag]: 1, ++ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ++!!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. ++!!! related TS9027 declarationComputedPropertyNames.ts:49:14: Add a type annotation to the variable D. + [(globalThis.Symbol).unscopables]: 1, + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + !!! error TS9038: Computed property names on class or object literals cannot be inferred with --isolatedDeclarations. \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts new file mode 100644 index 00000000000..c01d9c948c5 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts @@ -0,0 +1,49 @@ +//// [defines.ts] //// +export class A { + field = { x: 1 } +} +//// [consumes.ts] //// +import {A} from "./defines.js"; +export function create() { + return new A(); +} +//// [exposes.ts] //// +import {create} from "./consumes.js"; +export const value = create(); +//// [defines.d.ts] //// +export declare class A { + field: { + x: number; + }; +} +//// [consumes.d.ts] //// +export declare function create(): any; + + +//// [Diagnostics reported] +consumes.ts(3,12): error TS9013: Expression type can't be inferred with --isolatedDeclarations. + + +==== consumes.ts (1 errors) ==== + import {A} from "./defines.js"; + export function create() { + return new A(); + ~~~~~~~ +!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. +!!! related TS9031 consumes.ts:2:17: Add a return type to the function declaration. +!!! related TS9035 consumes.ts:3:12: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + } +//// [exposes.d.ts] //// +export declare const value: any; + + +//// [Diagnostics reported] +exposes.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== exposes.ts (1 errors) ==== + import {create} from "./consumes.js"; + export const value = create(); + ~~~~~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 exposes.ts:2:14: Add a type annotation to the variable value. diff --git a/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts.diff b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts.diff new file mode 100644 index 00000000000..b3b7bb96627 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.d.ts.diff @@ -0,0 +1,24 @@ +--- old.transpile/declarationCrossFileInferences.d.ts ++++ new.transpile/declarationCrossFileInferences.d.ts +@@= skipped -20, +20 lines =@@ + + + //// [Diagnostics reported] +-consumes.ts(2,17): error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. ++consumes.ts(3,12): error TS9013: Expression type can't be inferred with --isolatedDeclarations. + + + ==== consumes.ts (1 errors) ==== + import {A} from "./defines.js"; + export function create() { +- ~~~~~~ +-!!! error TS9007: Function must have an explicit return type annotation with --isolatedDeclarations. +-!!! related TS9031 consumes.ts:2:17: Add a return type to the function declaration. + return new A(); ++ ~~~~~~~ ++!!! error TS9013: Expression type can't be inferred with --isolatedDeclarations. ++!!! related TS9031 consumes.ts:2:17: Add a return type to the function declaration. ++!!! related TS9035 consumes.ts:3:12: Add satisfies and a type assertion to this expression (satisfies T as T) to make the type explicit. + } + //// [exposes.d.ts] //// + export declare const value: any; \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.js b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.js new file mode 100644 index 00000000000..cc055fa857d --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationCrossFileInferences.js @@ -0,0 +1,36 @@ +//// [defines.ts] //// +export class A { + field = { x: 1 } +} +//// [consumes.ts] //// +import {A} from "./defines.js"; +export function create() { + return new A(); +} +//// [exposes.ts] //// +import {create} from "./consumes.js"; +export const value = create(); +//// [defines.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.A = void 0; +class A { + constructor() { + this.field = { x: 1 }; + } +} +exports.A = A; +//// [consumes.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.create = create; +const defines_js_1 = require("./defines.js"); +function create() { + return new defines_js_1.A(); +} +//// [exposes.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.value = void 0; +const consumes_js_1 = require("./consumes.js"); +exports.value = (0, consumes_js_1.create)(); diff --git a/testdata/baselines/reference/submodule/transpile/declarationEmitPartialNodeReuse.d.ts b/testdata/baselines/reference/submodule/transpile/declarationEmitPartialNodeReuse.d.ts new file mode 100644 index 00000000000..178ad0da29d --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationEmitPartialNodeReuse.d.ts @@ -0,0 +1,55 @@ +//// [a.ts] //// +export type SpecialString = string; +type PrivateSpecialString = string; + +export namespace N { + export type SpecialString = string; +} +export const o = (p1: SpecialString, p2: PrivateSpecialString, p3: N.SpecialString) => null! as { foo: SpecialString, bar: PrivateSpecialString, baz: N.SpecialString }; +//// [b.ts] //// +import * as a from "./a"; +export const g = a.o +//// [c.ts] //// +import { o, SpecialString } from "./a"; +export const g = o +//// [a.d.ts] //// +export type SpecialString = string; +type PrivateSpecialString = string; +export declare namespace N { + type SpecialString = string; +} +export declare const o: (p1: SpecialString, p2: PrivateSpecialString, p3: N.SpecialString) => { + foo: SpecialString; + bar: PrivateSpecialString; + baz: N.SpecialString; +}; +export {}; +//// [b.d.ts] //// +export declare const g: any; + + +//// [Diagnostics reported] +b.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== b.ts (1 errors) ==== + import * as a from "./a"; + export const g = a.o + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 b.ts:2:14: Add a type annotation to the variable g. + +//// [c.d.ts] //// +export declare const g: any; + + +//// [Diagnostics reported] +c.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== c.ts (1 errors) ==== + import { o, SpecialString } from "./a"; + export const g = o + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 c.ts:2:14: Add a type annotation to the variable g. diff --git a/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts new file mode 100644 index 00000000000..3aacba0baa2 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts @@ -0,0 +1,227 @@ +//// [fnDecl.ts] //// +type T = number[] +export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { }; +export function fnDeclBasic2(p: (n: T) => T = () => null!, rParam: string): void { }; +export function fnDeclBasic3(p: new () => any = class {}, rParam: string): void { }; +export function fnDeclBasic4(p: [T] = [[]], rParam: string): void { }; +export function fnDeclBasic5(p: { a: T } = { a: [] }, rParam: string): void { }; +export function fnDeclBasic6(p: `_${string}` = "_", rParam: string): void { }; +export function fnDeclBasic7(p: { a?: string } & number[] = [], rParam: string): void { }; +export function fnDeclBasic8(p: (number[] | string[]) | number = [], rParam: string): void { }; + +export function fnDeclHasUndefined(p: T | undefined = [], rParam: string): void { }; +export function fnDeclBad(p: T = [], rParam: string): void { }; + +export const fnExprOk1 = function (array: number[] = [], rParam: string): void { }; +export const fnExprOk2 = function (array: T | undefined = [], rParam: string): void { }; +export const fnExprBad = function (array: T = [], rParam: string): void { }; + +export const arrowOk1 = (array: number[] = [], rParam: string): void => { }; +export const arrowOk2 = (array: T | undefined = [], rParam: string): void => { }; +export const arrowBad = (array: T = [], rParam: string): void => { }; + +export const inObjectLiteralFnExprOk1 = { o: function (array: number[] = [], rParam: string): void { } }; +export const inObjectLiteralFnExprOk2 = { o: function (array: T | undefined = [], rParam: string): void { } }; +export const inObjectLiteralFnExprBad = { o: function (array: T = [], rParam: string): void { } }; + +export const inObjectLiteralArrowOk1 = { o: (array: number[] = [], rParam: string): void => { } }; +export const inObjectLiteralArrowOk2 = { o: (array: T | undefined = [], rParam: string): void => { } }; +export const inObjectLiteralArrowBad = { o: (array: T = [], rParam: string): void => { } }; + +export const inObjectLiteralMethodOk1 = { o(array: number[] = [], rParam: string): void { } }; +export const inObjectLiteralMethodOk2 = { o(array: T | undefined = [], rParam: string): void { } }; +export const inObjectLiteralMethodBad = { o(array: T = [], rParam: string): void { } }; + + +export class InClassFnExprOk1 { o = function (array: number[] = [], rParam: string): void { } }; +export class InClassFnExprOk2 { o = function (array: T | undefined = [], rParam: string): void { } }; +export class InClassFnExprBad { o = function (array: T = [], rParam: string): void { } }; + +export class InClassArrowOk1 { o = (array: number[] = [], rParam: string): void => { } }; +export class InClassArrowOk2 { o = (array: T | undefined = [], rParam: string): void => { } }; +export class InClassArrowBad { o = (array: T = [], rParam: string): void => { } }; + +export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { } }; +export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; +export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; + +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar {} +export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} +} +//// [fnDecl.d.ts] //// +type T = number[]; +export declare function fnDeclBasic1(p: number[] | string[] | [T] | undefined, rParam: string): void; +export declare function fnDeclBasic2(p: ((n: T) => T) | undefined, rParam: string): void; +export declare function fnDeclBasic3(p: (new () => any) | undefined, rParam: string): void; +export declare function fnDeclBasic4(p: [T] | undefined, rParam: string): void; +export declare function fnDeclBasic5(p: { + a: T; +} | undefined, rParam: string): void; +export declare function fnDeclBasic6(p: `_${string}` | undefined, rParam: string): void; +export declare function fnDeclBasic7(p: ({ + a?: string; +} & number[]) | undefined, rParam: string): void; +export declare function fnDeclBasic8(p: (number[] | string[]) | number | undefined, rParam: string): void; +export declare function fnDeclHasUndefined(p: T | undefined, rParam: string): void; +export declare function fnDeclBad(p: T | undefined, rParam: string): void; +export declare const fnExprOk1: (array: number[] | undefined, rParam: string) => void; +export declare const fnExprOk2: (array: T | undefined, rParam: string) => void; +export declare const fnExprBad: (array: T | undefined, rParam: string) => void; +export declare const arrowOk1: (array: number[] | undefined, rParam: string) => void; +export declare const arrowOk2: (array: T | undefined, rParam: string) => void; +export declare const arrowBad: (array: T | undefined, rParam: string) => void; +export declare const inObjectLiteralFnExprOk1: { + o: (array: number[] | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralFnExprOk2: { + o: (array: T | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralFnExprBad: { + o: (array: T | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralArrowOk1: { + o: (array: number[] | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralArrowOk2: { + o: (array: T | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralArrowBad: { + o: (array: T | undefined, rParam: string) => void; +}; +export declare const inObjectLiteralMethodOk1: { + o(array: number[] | undefined, rParam: string): void; +}; +export declare const inObjectLiteralMethodOk2: { + o(array: T | undefined, rParam: string): void; +}; +export declare const inObjectLiteralMethodBad: { + o(array: T | undefined, rParam: string): void; +}; +export declare class InClassFnExprOk1 { + o: (array: number[] | undefined, rParam: string) => void; +} +export declare class InClassFnExprOk2 { + o: (array: T | undefined, rParam: string) => void; +} +export declare class InClassFnExprBad { + o: (array: T | undefined, rParam: string) => void; +} +export declare class InClassArrowOk1 { + o: (array: number[] | undefined, rParam: string) => void; +} +export declare class InClassArrowOk2 { + o: (array: T | undefined, rParam: string) => void; +} +export declare class InClassArrowBad { + o: (array: T | undefined, rParam: string) => void; +} +export declare class InClassMethodOk1 { + o(array: number[] | undefined, rParam: string): void; +} +export declare class InClassMethodOk2 { + o(array: T | undefined, rParam: string): void; +} +export declare class InClassMethodBad { + o(array: T | undefined, rParam: string): void; +} +declare class Bar { +} +export declare class ClsWithRequiredInitializedParameter { + private arr; + private bool; + constructor(arr: Bar | undefined, bool: boolean); +} +export {}; + + +//// [Diagnostics reported] +fnDecl.ts(16,36): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(20,26): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(24,56): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(28,46): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(32,45): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(37,47): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +fnDecl.ts(41,37): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + + +==== fnDecl.ts (7 errors) ==== + type T = number[] + export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { }; + export function fnDeclBasic2(p: (n: T) => T = () => null!, rParam: string): void { }; + export function fnDeclBasic3(p: new () => any = class {}, rParam: string): void { }; + export function fnDeclBasic4(p: [T] = [[]], rParam: string): void { }; + export function fnDeclBasic5(p: { a: T } = { a: [] }, rParam: string): void { }; + export function fnDeclBasic6(p: `_${string}` = "_", rParam: string): void { }; + export function fnDeclBasic7(p: { a?: string } & number[] = [], rParam: string): void { }; + export function fnDeclBasic8(p: (number[] | string[]) | number = [], rParam: string): void { }; + + export function fnDeclHasUndefined(p: T | undefined = [], rParam: string): void { }; + export function fnDeclBad(p: T = [], rParam: string): void { }; + + export const fnExprOk1 = function (array: number[] = [], rParam: string): void { }; + export const fnExprOk2 = function (array: T | undefined = [], rParam: string): void { }; + export const fnExprBad = function (array: T = [], rParam: string): void { }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:16:36: Add a type annotation to the parameter array. + + export const arrowOk1 = (array: number[] = [], rParam: string): void => { }; + export const arrowOk2 = (array: T | undefined = [], rParam: string): void => { }; + export const arrowBad = (array: T = [], rParam: string): void => { }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:20:26: Add a type annotation to the parameter array. + + export const inObjectLiteralFnExprOk1 = { o: function (array: number[] = [], rParam: string): void { } }; + export const inObjectLiteralFnExprOk2 = { o: function (array: T | undefined = [], rParam: string): void { } }; + export const inObjectLiteralFnExprBad = { o: function (array: T = [], rParam: string): void { } }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:24:56: Add a type annotation to the parameter array. + + export const inObjectLiteralArrowOk1 = { o: (array: number[] = [], rParam: string): void => { } }; + export const inObjectLiteralArrowOk2 = { o: (array: T | undefined = [], rParam: string): void => { } }; + export const inObjectLiteralArrowBad = { o: (array: T = [], rParam: string): void => { } }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:28:46: Add a type annotation to the parameter array. + + export const inObjectLiteralMethodOk1 = { o(array: number[] = [], rParam: string): void { } }; + export const inObjectLiteralMethodOk2 = { o(array: T | undefined = [], rParam: string): void { } }; + export const inObjectLiteralMethodBad = { o(array: T = [], rParam: string): void { } }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:32:45: Add a type annotation to the parameter array. + + + export class InClassFnExprOk1 { o = function (array: number[] = [], rParam: string): void { } }; + export class InClassFnExprOk2 { o = function (array: T | undefined = [], rParam: string): void { } }; + export class InClassFnExprBad { o = function (array: T = [], rParam: string): void { } }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:37:47: Add a type annotation to the parameter array. + + export class InClassArrowOk1 { o = (array: number[] = [], rParam: string): void => { } }; + export class InClassArrowOk2 { o = (array: T | undefined = [], rParam: string): void => { } }; + export class InClassArrowBad { o = (array: T = [], rParam: string): void => { } }; + ~~~~~~~~~~~~~ +!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +!!! related TS9028 fnDecl.ts:41:37: Add a type annotation to the parameter array. + + export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { } }; + export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; + export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; + + // https://github.com/microsoft/TypeScript/issues/60976 + class Bar {} + export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} + } diff --git a/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts.diff b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts.diff new file mode 100644 index 00000000000..ca84e1dc0bc --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.d.ts.diff @@ -0,0 +1,54 @@ +--- old.transpile/declarationFunctionDeclarations.d.ts ++++ new.transpile/declarationFunctionDeclarations.d.ts +@@= skipped -139, +139 lines =@@ + + + //// [Diagnostics reported] +-fnDecl.ts(12,27): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + fnDecl.ts(16,36): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + fnDecl.ts(20,26): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + fnDecl.ts(24,56): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +@@= skipped -8, +7 lines =@@ + fnDecl.ts(32,45): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + fnDecl.ts(37,47): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. + fnDecl.ts(41,37): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +-fnDecl.ts(45,35): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +-fnDecl.ts(51,5): error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +- +- +-==== fnDecl.ts (10 errors) ==== ++ ++ ++==== fnDecl.ts (7 errors) ==== + type T = number[] + export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { }; + export function fnDeclBasic2(p: (n: T) => T = () => null!, rParam: string): void { }; +@@= skipped -17, +15 lines =@@ + + export function fnDeclHasUndefined(p: T | undefined = [], rParam: string): void { }; + export function fnDeclBad(p: T = [], rParam: string): void { }; +- ~~~~~~~~~ +-!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +-!!! related TS9028 fnDecl.ts:12:27: Add a type annotation to the parameter p. + + export const fnExprOk1 = function (array: number[] = [], rParam: string): void { }; + export const fnExprOk2 = function (array: T | undefined = [], rParam: string): void { }; +@@= skipped -57, +54 lines =@@ + export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { } }; + export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; + export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; +- ~~~~~~~~~~~~~ +-!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +-!!! related TS9028 fnDecl.ts:45:35: Add a type annotation to the parameter array. + + // https://github.com/microsoft/TypeScript/issues/60976 + class Bar {} + export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), +- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~ +-!!! error TS9025: Declaration emit for this parameter requires implicitly adding undefined to its type. This is not supported with --isolatedDeclarations. +-!!! related TS9028 fnDecl.ts:51:5: Add a type annotation to the parameter arr. + private bool: boolean, + ) {} + } \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.js b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.js new file mode 100644 index 00000000000..0e32cdf3471 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationFunctionDeclarations.js @@ -0,0 +1,139 @@ +//// [fnDecl.ts] //// +type T = number[] +export function fnDeclBasic1(p: number[] | string[] | [T] = [], rParam: string): void { }; +export function fnDeclBasic2(p: (n: T) => T = () => null!, rParam: string): void { }; +export function fnDeclBasic3(p: new () => any = class {}, rParam: string): void { }; +export function fnDeclBasic4(p: [T] = [[]], rParam: string): void { }; +export function fnDeclBasic5(p: { a: T } = { a: [] }, rParam: string): void { }; +export function fnDeclBasic6(p: `_${string}` = "_", rParam: string): void { }; +export function fnDeclBasic7(p: { a?: string } & number[] = [], rParam: string): void { }; +export function fnDeclBasic8(p: (number[] | string[]) | number = [], rParam: string): void { }; + +export function fnDeclHasUndefined(p: T | undefined = [], rParam: string): void { }; +export function fnDeclBad(p: T = [], rParam: string): void { }; + +export const fnExprOk1 = function (array: number[] = [], rParam: string): void { }; +export const fnExprOk2 = function (array: T | undefined = [], rParam: string): void { }; +export const fnExprBad = function (array: T = [], rParam: string): void { }; + +export const arrowOk1 = (array: number[] = [], rParam: string): void => { }; +export const arrowOk2 = (array: T | undefined = [], rParam: string): void => { }; +export const arrowBad = (array: T = [], rParam: string): void => { }; + +export const inObjectLiteralFnExprOk1 = { o: function (array: number[] = [], rParam: string): void { } }; +export const inObjectLiteralFnExprOk2 = { o: function (array: T | undefined = [], rParam: string): void { } }; +export const inObjectLiteralFnExprBad = { o: function (array: T = [], rParam: string): void { } }; + +export const inObjectLiteralArrowOk1 = { o: (array: number[] = [], rParam: string): void => { } }; +export const inObjectLiteralArrowOk2 = { o: (array: T | undefined = [], rParam: string): void => { } }; +export const inObjectLiteralArrowBad = { o: (array: T = [], rParam: string): void => { } }; + +export const inObjectLiteralMethodOk1 = { o(array: number[] = [], rParam: string): void { } }; +export const inObjectLiteralMethodOk2 = { o(array: T | undefined = [], rParam: string): void { } }; +export const inObjectLiteralMethodBad = { o(array: T = [], rParam: string): void { } }; + + +export class InClassFnExprOk1 { o = function (array: number[] = [], rParam: string): void { } }; +export class InClassFnExprOk2 { o = function (array: T | undefined = [], rParam: string): void { } }; +export class InClassFnExprBad { o = function (array: T = [], rParam: string): void { } }; + +export class InClassArrowOk1 { o = (array: number[] = [], rParam: string): void => { } }; +export class InClassArrowOk2 { o = (array: T | undefined = [], rParam: string): void => { } }; +export class InClassArrowBad { o = (array: T = [], rParam: string): void => { } }; + +export class InClassMethodOk1 { o(array: number[] = [], rParam: string): void { } }; +export class InClassMethodOk2 { o(array: T | undefined = [], rParam: string): void { } }; +export class InClassMethodBad { o(array: T = [], rParam: string): void { } }; + +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar {} +export class ClsWithRequiredInitializedParameter { + constructor( + private arr: Bar = new Bar(), + private bool: boolean, + ) {} +} +//// [fnDecl.js] //// +export function fnDeclBasic1(p = [], rParam) { } +; +export function fnDeclBasic2(p = () => null, rParam) { } +; +export function fnDeclBasic3(p = class { +}, rParam) { } +; +export function fnDeclBasic4(p = [[]], rParam) { } +; +export function fnDeclBasic5(p = { a: [] }, rParam) { } +; +export function fnDeclBasic6(p = "_", rParam) { } +; +export function fnDeclBasic7(p = [], rParam) { } +; +export function fnDeclBasic8(p = [], rParam) { } +; +export function fnDeclHasUndefined(p = [], rParam) { } +; +export function fnDeclBad(p = [], rParam) { } +; +export const fnExprOk1 = function (array = [], rParam) { }; +export const fnExprOk2 = function (array = [], rParam) { }; +export const fnExprBad = function (array = [], rParam) { }; +export const arrowOk1 = (array = [], rParam) => { }; +export const arrowOk2 = (array = [], rParam) => { }; +export const arrowBad = (array = [], rParam) => { }; +export const inObjectLiteralFnExprOk1 = { o: function (array = [], rParam) { } }; +export const inObjectLiteralFnExprOk2 = { o: function (array = [], rParam) { } }; +export const inObjectLiteralFnExprBad = { o: function (array = [], rParam) { } }; +export const inObjectLiteralArrowOk1 = { o: (array = [], rParam) => { } }; +export const inObjectLiteralArrowOk2 = { o: (array = [], rParam) => { } }; +export const inObjectLiteralArrowBad = { o: (array = [], rParam) => { } }; +export const inObjectLiteralMethodOk1 = { o(array = [], rParam) { } }; +export const inObjectLiteralMethodOk2 = { o(array = [], rParam) { } }; +export const inObjectLiteralMethodBad = { o(array = [], rParam) { } }; +export class InClassFnExprOk1 { + o = function (array = [], rParam) { }; +} +; +export class InClassFnExprOk2 { + o = function (array = [], rParam) { }; +} +; +export class InClassFnExprBad { + o = function (array = [], rParam) { }; +} +; +export class InClassArrowOk1 { + o = (array = [], rParam) => { }; +} +; +export class InClassArrowOk2 { + o = (array = [], rParam) => { }; +} +; +export class InClassArrowBad { + o = (array = [], rParam) => { }; +} +; +export class InClassMethodOk1 { + o(array = [], rParam) { } +} +; +export class InClassMethodOk2 { + o(array = [], rParam) { } +} +; +export class InClassMethodBad { + o(array = [], rParam) { } +} +; +// https://github.com/microsoft/TypeScript/issues/60976 +class Bar { +} +export class ClsWithRequiredInitializedParameter { + arr; + bool; + constructor(arr = new Bar(), bool) { + this.arr = arr; + this.bool = bool; + } +} diff --git a/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.d.ts b/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.d.ts new file mode 100644 index 00000000000..f32860fc576 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.d.ts @@ -0,0 +1,8 @@ +//// [declarationLinkedAliases.ts] //// +import { A } from "mod"; +import B = A.C; +export { B }; +//// [declarationLinkedAliases.d.ts] //// +import { A } from "mod"; +import B = A.C; +export { B }; diff --git a/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.js b/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.js new file mode 100644 index 00000000000..10d49bf6a7d --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationLinkedAliases.js @@ -0,0 +1,11 @@ +//// [declarationLinkedAliases.ts] //// +import { A } from "mod"; +import B = A.C; +export { B }; +//// [declarationLinkedAliases.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.B = void 0; +const mod_1 = require("mod"); +var B = mod_1.A.C; +exports.B = B; diff --git a/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.d.ts b/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.d.ts new file mode 100644 index 00000000000..d7e1a23992d --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.d.ts @@ -0,0 +1,15 @@ +//// [declarationLocalAliasOfImportAlias.ts] //// +import { Record } from "./a"; +export type Foo = Record; + +export const obj = { + doThing(_k: K): Foo { + return {} as any; + }, +}; +//// [declarationLocalAliasOfImportAlias.d.ts] //// +import { Record } from "./a"; +export type Foo = Record; +export declare const obj: { + doThing(_k: K): Foo; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.js b/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.js new file mode 100644 index 00000000000..d903e835cd6 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationLocalAliasOfImportAlias.js @@ -0,0 +1,18 @@ +//// [declarationLocalAliasOfImportAlias.ts] //// +import { Record } from "./a"; +export type Foo = Record; + +export const obj = { + doThing(_k: K): Foo { + return {} as any; + }, +}; +//// [declarationLocalAliasOfImportAlias.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.obj = void 0; +exports.obj = { + doThing(_k) { + return {}; + }, +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.d.ts b/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.d.ts new file mode 100644 index 00000000000..99742621419 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.d.ts @@ -0,0 +1,49 @@ +//// [variables.ts] //// +const x = ""; +export function one() { + return {} as typeof x; +} + +export function two() { + const y = ""; + return {} as typeof y; +} + +export function three() { + type Z = string; + return {} as Z; +} +//// [variables.d.ts] //// +declare const x = ""; +export declare function one(): typeof x; +export declare function two(): ""; +export declare function three(): string; +export {}; + + +//// [Diagnostics reported] +variables.ts(8,25): error TS9039: Type containing private name 'y' can't be used with --isolatedDeclarations. +variables.ts(13,18): error TS9039: Type containing private name 'Z' can't be used with --isolatedDeclarations. + + +==== variables.ts (2 errors) ==== + const x = ""; + export function one() { + return {} as typeof x; + } + + export function two() { + const y = ""; + return {} as typeof y; + ~ +!!! error TS9039: Type containing private name 'y' can't be used with --isolatedDeclarations. +!!! related TS9031 variables.ts:6:17: Add a return type to the function declaration. + } + + export function three() { + type Z = string; + return {} as Z; + ~ +!!! error TS9039: Type containing private name 'Z' can't be used with --isolatedDeclarations. +!!! related TS9031 variables.ts:11:17: Add a return type to the function declaration. + } diff --git a/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.js b/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.js new file mode 100644 index 00000000000..0cdd7d39fd1 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationNotInScopeTypes.js @@ -0,0 +1,27 @@ +//// [variables.ts] //// +const x = ""; +export function one() { + return {} as typeof x; +} + +export function two() { + const y = ""; + return {} as typeof y; +} + +export function three() { + type Z = string; + return {} as Z; +} +//// [variables.js] //// +const x = ""; +export function one() { + return {}; +} +export function two() { + const y = ""; + return {}; +} +export function three() { + return {}; +} diff --git a/testdata/baselines/reference/submodule/transpile/declarationPartialNodeReuseTypeOf.d.ts b/testdata/baselines/reference/submodule/transpile/declarationPartialNodeReuseTypeOf.d.ts new file mode 100644 index 00000000000..3a64b75b948 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationPartialNodeReuseTypeOf.d.ts @@ -0,0 +1,53 @@ +//// [a.ts] //// +export const nImported = "nImported" +export const nNotImported = "nNotImported" +const nPrivate = "private" +export const o = (p1: typeof nImported, p2: typeof nNotImported, p3: typeof nPrivate) => null! as { foo: typeof nImported, bar: typeof nPrivate, baz: typeof nNotImported } +//// [b.ts] //// +import { o, nImported } from "./a"; +export const g = o +console.log(nImported); +//// [c.ts] //// +import * as a from "./a"; +export const g = a.o +//// [a.d.ts] //// +export declare const nImported = "nImported"; +export declare const nNotImported = "nNotImported"; +declare const nPrivate = "private"; +export declare const o: (p1: typeof nImported, p2: typeof nNotImported, p3: typeof nPrivate) => { + foo: typeof nImported; + bar: typeof nPrivate; + baz: typeof nNotImported; +}; +export {}; +//// [b.d.ts] //// +export declare const g: any; + + +//// [Diagnostics reported] +b.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== b.ts (1 errors) ==== + import { o, nImported } from "./a"; + export const g = o + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 b.ts:2:14: Add a type annotation to the variable g. + console.log(nImported); + +//// [c.d.ts] //// +export declare const g: any; + + +//// [Diagnostics reported] +c.ts(2,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== c.ts (1 errors) ==== + import * as a from "./a"; + export const g = a.o + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 c.ts:2:14: Add a type annotation to the variable g. + diff --git a/testdata/baselines/reference/submodule/transpile/declarationRestParameters.d.ts b/testdata/baselines/reference/submodule/transpile/declarationRestParameters.d.ts new file mode 100644 index 00000000000..eb411b39bc3 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationRestParameters.d.ts @@ -0,0 +1,50 @@ +//// [v1.ts] //// +export const v1 = (...a: [n: "n", a: "a"]): { + /** r rest param */ + a: typeof a, +} => { + return null! +} +//// [v2.ts] //// +const n = Symbol(); +export const v2 = (...a: [n: "n", a: "a"]): { + /** r rest param */ + a: typeof a, + /** module var */ + n: typeof n, +} => { + return null! +} +//// [v1.d.ts] //// +export declare const v1: (...a: [n: "n", a: "a"]) => { + /** r rest param */ + a: typeof a; +}; +//// [v2.d.ts] //// +declare const n: unique symbol; +export declare const v2: (...a: [n: "n", a: "a"]) => { + /** r rest param */ + a: typeof a; + /** module var */ + n: typeof n; +}; +export {}; + + +//// [Diagnostics reported] +v2.ts(1,7): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== v2.ts (1 errors) ==== + const n = Symbol(); + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 v2.ts:1:7: Add a type annotation to the variable n. + export const v2 = (...a: [n: "n", a: "a"]): { + /** r rest param */ + a: typeof a, + /** module var */ + n: typeof n, + } => { + return null! + } diff --git a/testdata/baselines/reference/submodule/transpile/declarationSelfReferentialConstraint.d.ts b/testdata/baselines/reference/submodule/transpile/declarationSelfReferentialConstraint.d.ts new file mode 100644 index 00000000000..7ac9bb45e67 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationSelfReferentialConstraint.d.ts @@ -0,0 +1,8 @@ +//// [declarationSelfReferentialConstraint.ts] //// +export const object = { + foo: | []>(): void => { }, +}; +//// [declarationSelfReferentialConstraint.d.ts] //// +export declare const object: { + foo: | []>() => void; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.d.ts b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.d.ts new file mode 100644 index 00000000000..6952247996e --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.d.ts @@ -0,0 +1,15 @@ +//// [declarationSingleFileHasErrors.ts] //// +export const a number = "missing colon"; +//// [declarationSingleFileHasErrors.d.ts] //// +export declare const a: any, number = "missing colon"; + + +//// [Diagnostics reported] +declarationSingleFileHasErrors.ts(1,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. + + +==== declarationSingleFileHasErrors.ts (1 errors) ==== + export const a number = "missing colon"; + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationSingleFileHasErrors.ts:1:14: Add a type annotation to the variable a. diff --git a/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.js b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.js new file mode 100644 index 00000000000..3168dbe6f4c --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrors.js @@ -0,0 +1,7 @@ +//// [declarationSingleFileHasErrors.ts] //// +export const a number = "missing colon"; +//// [declarationSingleFileHasErrors.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.number = exports.a = void 0; +exports.number = "missing colon"; diff --git a/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.d.ts b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.d.ts new file mode 100644 index 00000000000..46cadeff0df --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.d.ts @@ -0,0 +1,18 @@ +//// [declarationSingleFileHasErrorsReported.ts] //// +export const a string = "missing colon"; +//// [declarationSingleFileHasErrorsReported.d.ts] //// +export declare const a: any, string = "missing colon"; + + +//// [Diagnostics reported] +declarationSingleFileHasErrorsReported.ts(1,14): error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +declarationSingleFileHasErrorsReported.ts(1,16): error TS1005: ',' expected. + + +==== declarationSingleFileHasErrorsReported.ts (2 errors) ==== + export const a string = "missing colon"; + ~ +!!! error TS9010: Variable must have an explicit type annotation with --isolatedDeclarations. +!!! related TS9027 declarationSingleFileHasErrorsReported.ts:1:14: Add a type annotation to the variable a. + ~~~~~~ +!!! error TS1005: ',' expected. diff --git a/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.js b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.js new file mode 100644 index 00000000000..1e1cddc8258 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationSingleFileHasErrorsReported.js @@ -0,0 +1,17 @@ +//// [declarationSingleFileHasErrorsReported.ts] //// +export const a string = "missing colon"; +//// [declarationSingleFileHasErrorsReported.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.string = exports.a = void 0; +exports.string = "missing colon"; + + +//// [Diagnostics reported] +declarationSingleFileHasErrorsReported.ts(1,16): error TS1005: ',' expected. + + +==== declarationSingleFileHasErrorsReported.ts (1 errors) ==== + export const a string = "missing colon"; + ~~~~~~ +!!! error TS1005: ',' expected. diff --git a/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.d.ts b/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.d.ts new file mode 100644 index 00000000000..b20c8b3da16 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.d.ts @@ -0,0 +1,13 @@ +//// [declarationTypeParameterConstraint.ts] //// +import { type In, type Out, type Base } from "./a"; + +export const object = { + doThing(_t: T, _in: In[T]): Out[T] { + return; + }, +}; +//// [declarationTypeParameterConstraint.d.ts] //// +import { type In, type Out, type Base } from "./a"; +export declare const object: { + doThing(_t: T, _in: In[T]): Out[T]; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.js b/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.js new file mode 100644 index 00000000000..004440aa743 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationTypeParameterConstraint.js @@ -0,0 +1,17 @@ +//// [declarationTypeParameterConstraint.ts] //// +import { type In, type Out, type Base } from "./a"; + +export const object = { + doThing(_t: T, _in: In[T]): Out[T] { + return; + }, +}; +//// [declarationTypeParameterConstraint.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.object = void 0; +exports.object = { + doThing(_t, _in) { + return; + }, +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationTypeWithComputedName.d.ts b/testdata/baselines/reference/submodule/transpile/declarationTypeWithComputedName.d.ts new file mode 100644 index 00000000000..b1cdfdf8b7b --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationTypeWithComputedName.d.ts @@ -0,0 +1,22 @@ +//// [declarationTypeWithComputedName.ts] //// +import {Foo} from './a'; + +export type Bar = { + [Foo.A]: 1; + [Foo.B]: 2; +} + +export const valBar = null as any as { + [Foo.A]: 1; + [Foo.B]: 2; +}; +//// [declarationTypeWithComputedName.d.ts] //// +import { Foo } from './a'; +export type Bar = { + [Foo.A]: 1; + [Foo.B]: 2; +}; +export declare const valBar: { + [Foo.A]: 1; + [Foo.B]: 2; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationUnresolvedGlobalReferencesNoErrors.d.ts b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedGlobalReferencesNoErrors.d.ts new file mode 100644 index 00000000000..843cd829a99 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedGlobalReferencesNoErrors.d.ts @@ -0,0 +1,23 @@ +//// [declarationUnresolvedGlobalReferencesNoErrors.ts] //// +export const x: MissingGlobalType = null!; +export const fn = (a: MissingGlobalType): MissingGlobalType => null!; +export const fn2 = (a: MissingGlobalType) => null! as MissingGlobalType; + +export const x2: typeof missingGlobalValue = null!; +export const fn3 = (a: typeof missingGlobalValue): typeof missingGlobalValue => null!; +export const fn4 = (a: typeof missingGlobalValue) => null! as typeof missingGlobalValue; + + +export const o : { + [missingGlobalValue]: string +} = null!; +//// [declarationUnresolvedGlobalReferencesNoErrors.d.ts] //// +export declare const x: MissingGlobalType; +export declare const fn: (a: MissingGlobalType) => MissingGlobalType; +export declare const fn2: (a: MissingGlobalType) => MissingGlobalType; +export declare const x2: typeof missingGlobalValue; +export declare const fn3: (a: typeof missingGlobalValue) => typeof missingGlobalValue; +export declare const fn4: (a: typeof missingGlobalValue) => typeof missingGlobalValue; +export declare const o: { + [missingGlobalValue]: string; +}; diff --git a/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference.d.ts b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference.d.ts new file mode 100644 index 00000000000..93b978ccfef --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference.d.ts @@ -0,0 +1,9 @@ +//// [declarationUnresolvedTypeReference.ts] //// +import { type Type } from "./a"; + +export const foo = (_: Type): void => {}; +export const bar = (_: import("./a").Type): void => {}; +//// [declarationUnresolvedTypeReference.d.ts] //// +import { type Type } from "./a"; +export declare const foo: (_: Type) => void; +export declare const bar: (_: import("./a").Type) => void; diff --git a/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference2.d.ts b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference2.d.ts new file mode 100644 index 00000000000..3b1b91ced83 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationUnresolvedTypeReference2.d.ts @@ -0,0 +1,19 @@ +//// [globals.ts] //// +type MissingGlobalType = "global"; +declare const missingGlobalValue: "A"; +//// [index.ts] //// +// this test assumes there is some global definitions for MissingGlobalType and missingGlobalValue that are not available to transpileDeclaration +export const fn = (a: MissingGlobalType): MissingGlobalType => null!; +export const fn2 = (a: MissingGlobalType) => null! as MissingGlobalType; + +export const fn3 = (a: typeof missingGlobalValue): typeof missingGlobalValue => null!; +export const fn4 = (a: typeof missingGlobalValue) => null! as typeof missingGlobalValue; + +//// [globals.d.ts] //// +type MissingGlobalType = "global"; +declare const missingGlobalValue: "A"; +//// [index.d.ts] //// +export declare const fn: (a: MissingGlobalType) => MissingGlobalType; +export declare const fn2: (a: MissingGlobalType) => MissingGlobalType; +export declare const fn3: (a: typeof missingGlobalValue) => typeof missingGlobalValue; +export declare const fn4: (a: typeof missingGlobalValue) => typeof missingGlobalValue; diff --git a/testdata/baselines/reference/submodule/transpile/declarationsSimple.d.ts b/testdata/baselines/reference/submodule/transpile/declarationsSimple.d.ts new file mode 100644 index 00000000000..3d9a2d99779 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationsSimple.d.ts @@ -0,0 +1,23 @@ +//// [declarationsSimple.ts] //// +export const c: number = 1; + +export interface A { + x: number; +} + +let expr: { x: number; }; + +expr = { + x: 12, +} + +export default expr; +//// [declarationsSimple.d.ts] //// +export declare const c: number; +export interface A { + x: number; +} +declare let expr: { + x: number; +}; +export default expr; diff --git a/testdata/baselines/reference/submodule/transpile/declarationsSimple.js b/testdata/baselines/reference/submodule/transpile/declarationsSimple.js new file mode 100644 index 00000000000..56ea163de58 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/declarationsSimple.js @@ -0,0 +1,24 @@ +//// [declarationsSimple.ts] //// +export const c: number = 1; + +export interface A { + x: number; +} + +let expr: { x: number; }; + +expr = { + x: 12, +} + +export default expr; +//// [declarationsSimple.js] //// +"use strict"; +Object.defineProperty(exports, "__esModule", { value: true }); +exports.c = void 0; +exports.c = 1; +let expr; +expr = { + x: 12, +}; +exports.default = expr; diff --git a/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=false).js b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=false).js new file mode 100644 index 00000000000..dd7a762bf28 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=false).js @@ -0,0 +1,145 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//// [interface.js] //// +export {}; +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//// [alias.js] //// +export {}; diff --git a/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js new file mode 100644 index 00000000000..af74c6acd50 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js @@ -0,0 +1,150 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFyaWFibGVzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidmFyaWFibGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxNQUFNLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ25CLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUM7QUFDakIsTUFBTSxDQUFDLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUVqQixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7QUFFYixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7Ozs7SUFIUCxDQUFDLGtDQUFHLFNBQVMsUUFBQSxDQUFDO0lBRVIsQ0FBQyxrQ0FBRyxTQUFTLE9BQUEsQ0FBQyJ9 +//// [interface.js] //// +export {}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW50ZXJmYWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiaW50ZXJmYWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiIifQ== +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiY2xhc3MuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJjbGFzcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiO0FBQUEsTUFBTSxDQUFDLEdBQUcsTUFBTSxFQUFFLENBQUM7QUFDbkIsTUFBTSxPQUFPLEdBQUc7SUFBaEI7UUFJSSx5QkFBVztJQU1mLENBQUM7Q0FBQTs7QUFFRCxNQUFNLE9BQWdCLEdBQUc7Q0FHeEIifQ== +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmFtZXNwYWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsibmFtZXNwYWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sS0FBVyxFQUFFLENBT2xCO0FBUEQsV0FBaUIsRUFBRTtJQUNmLElBQVUsUUFBUSxDQUVqQjtJQUZELFdBQVUsUUFBUTtRQUNkLE1BQWEsR0FBRztTQUFHO1FBQU4sU0FBQSxHQUFHLE1BQUcsQ0FBQTtJQUN2QixDQUFDLEVBRlMsUUFBUSxLQUFSLFFBQVEsUUFFakI7SUFDRCxJQUFpQixNQUFNLENBRXRCO0lBRkQsV0FBaUIsTUFBTTtRQUNMLE9BQUEsS0FBSyxHQUFHLFFBQVMsQ0FBQTtJQUNuQyxDQUFDLEVBRmdCLE1BQU0sR0FBTixHQUFBLE1BQU0sS0FBTixHQUFBLE1BQU0sUUFFdEI7QUFDTCxDQUFDLEVBUGdCLEVBQUUsS0FBRixFQUFFLFFBT2xCIn0= +//// [alias.js] //// +export {}; +//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWxpYXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJhbGlhcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0= diff --git a/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js.diff b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js.diff new file mode 100644 index 00000000000..baa91e07113 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js.diff @@ -0,0 +1,20 @@ +--- old.transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js ++++ new.transpile/jsWithInlineSourceMapBasic(inlineSourceMap=true).js +@@= skipped -113, +113 lines =@@ + if (result_1) + await result_1; + } +-//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFyaWFibGVzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidmFyaWFibGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxNQUFNLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ25CLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUM7QUFDakIsTUFBTSxDQUFDLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUVqQixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7QUFFYixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7Ozs7SUFIUCxtQ0FBSSxTQUFTLFFBQUEsQ0FBQztJQUVSLG1DQUFJLFNBQVMsT0FBQSxDQUFDIn0= ++//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoidmFyaWFibGVzLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsidmFyaWFibGVzLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiI7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7Ozs7QUFBQSxNQUFNLENBQUMsTUFBTSxDQUFDLEdBQUcsQ0FBQyxDQUFDO0FBQ25CLE1BQU0sQ0FBQyxJQUFJLENBQUMsR0FBRyxDQUFDLENBQUM7QUFDakIsTUFBTSxDQUFDLElBQUksQ0FBQyxHQUFHLENBQUMsQ0FBQztBQUVqQixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7QUFFYixPQUFPLEVBQUUsQ0FBQyxFQUFFLENBQUM7Ozs7SUFIUCxDQUFDLGtDQUFHLFNBQVMsUUFBQSxDQUFDO0lBRVIsQ0FBQyxrQ0FBRyxTQUFTLE9BQUEsQ0FBQyJ9 + //// [interface.js] //// + export {}; + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiaW50ZXJmYWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsiaW50ZXJmYWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiIifQ== +@@= skipped -30, +30 lines =@@ + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); + })(ns || (ns = {})); +-//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmFtZXNwYWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsibmFtZXNwYWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sS0FBVyxFQUFFLENBT2xCO0FBUEQsV0FBaUIsRUFBRTtJQUNmLElBQVUsUUFBUSxDQUVqQjtJQUZELFdBQVUsUUFBUTtRQUNkLE1BQWEsR0FBRztTQUFHO1FBQU4sWUFBRyxNQUFHLENBQUE7SUFDdkIsQ0FBQyxFQUZTLFFBQVEsS0FBUixRQUFRLFFBRWpCO0lBQ0QsSUFBaUIsTUFBTSxDQUV0QjtJQUZELFdBQWlCLE1BQU07UUFDTCxZQUFLLEdBQUcsUUFBUSxDQUFDO0lBQ25DLENBQUMsRUFGZ0IsTUFBTSxHQUFOLFNBQU0sS0FBTixTQUFNLFFBRXRCO0FBQ0wsQ0FBQyxFQVBnQixFQUFFLEtBQUYsRUFBRSxRQU9sQiJ9 ++//# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoibmFtZXNwYWNlLmpzIiwic291cmNlUm9vdCI6IiIsInNvdXJjZXMiOlsibmFtZXNwYWNlLnRzIl0sIm5hbWVzIjpbXSwibWFwcGluZ3MiOiJBQUFBLE1BQU0sS0FBVyxFQUFFLENBT2xCO0FBUEQsV0FBaUIsRUFBRTtJQUNmLElBQVUsUUFBUSxDQUVqQjtJQUZELFdBQVUsUUFBUTtRQUNkLE1BQWEsR0FBRztTQUFHO1FBQU4sU0FBQSxHQUFHLE1BQUcsQ0FBQTtJQUN2QixDQUFDLEVBRlMsUUFBUSxLQUFSLFFBQVEsUUFFakI7SUFDRCxJQUFpQixNQUFNLENBRXRCO0lBRkQsV0FBaUIsTUFBTTtRQUNMLE9BQUEsS0FBSyxHQUFHLFFBQVMsQ0FBQTtJQUNuQyxDQUFDLEVBRmdCLE1BQU0sR0FBTixHQUFBLE1BQU0sS0FBTixHQUFBLE1BQU0sUUFFdEI7QUFDTCxDQUFDLEVBUGdCLEVBQUUsS0FBRixFQUFFLFFBT2xCIn0= + //// [alias.js] //// + export {}; + //# sourceMappingURL=data:application/json;base64,eyJ2ZXJzaW9uIjozLCJmaWxlIjoiYWxpYXMuanMiLCJzb3VyY2VSb290IjoiIiwic291cmNlcyI6WyJhbGlhcy50cyJdLCJuYW1lcyI6W10sIm1hcHBpbmdzIjoiIn0= \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=false).js b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=false).js new file mode 100644 index 00000000000..dd7a762bf28 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=false).js @@ -0,0 +1,145 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//// [interface.js] //// +export {}; +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//// [alias.js] //// +export {}; diff --git a/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js new file mode 100644 index 00000000000..b9c4b97ab02 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js @@ -0,0 +1,160 @@ +//// [variables.ts] //// +export const a = 1; +export let b = 2; +export var c = 3; +using d = undefined; +export { d }; +await using e = undefined; +export { e }; +//// [interface.ts] //// +export interface Foo { + a: string; + readonly b: string; + c?: string; +} +//// [class.ts] //// +const i = Symbol(); +export class Bar { + a: string; + b?: string; + declare c: string; + #d: string; + public e: string; + protected f: string; + private g: string; + ["h"]: string; + [i]: string; +} + +export abstract class Baz { + abstract a: string; + abstract method(): void; +} +//// [namespace.ts] //// +export namespace ns { + namespace internal { + export class Foo {} + } + export namespace nested { + export import inner = internal; + } +} +//// [alias.ts] //// +export type A = { x: T }; +//// [variables.js] //// +var __addDisposableResource = (this && this.__addDisposableResource) || function (env, value, async) { + if (value !== null && value !== void 0) { + if (typeof value !== "object" && typeof value !== "function") throw new TypeError("Object expected."); + var dispose, inner; + if (async) { + if (!Symbol.asyncDispose) throw new TypeError("Symbol.asyncDispose is not defined."); + dispose = value[Symbol.asyncDispose]; + } + if (dispose === void 0) { + if (!Symbol.dispose) throw new TypeError("Symbol.dispose is not defined."); + dispose = value[Symbol.dispose]; + if (async) inner = dispose; + } + if (typeof dispose !== "function") throw new TypeError("Object not disposable."); + if (inner) dispose = function() { try { inner.call(this); } catch (e) { return Promise.reject(e); } }; + env.stack.push({ value: value, dispose: dispose, async: async }); + } + else if (async) { + env.stack.push({ async: true }); + } + return value; +}; +var __disposeResources = (this && this.__disposeResources) || (function (SuppressedError) { + return function (env) { + function fail(e) { + env.error = env.hasError ? new SuppressedError(e, env.error, "An error was suppressed during disposal.") : e; + env.hasError = true; + } + var r, s = 0; + function next() { + while (r = env.stack.pop()) { + try { + if (!r.async && s === 1) return s = 0, env.stack.push(r), Promise.resolve().then(next); + if (r.dispose) { + var result = r.dispose.call(r.value); + if (r.async) return s |= 2, Promise.resolve(result).then(next, function(e) { fail(e); return next(); }); + } + else s |= 1; + } + catch (e) { + fail(e); + } + } + if (s === 1) return env.hasError ? Promise.reject(env.error) : Promise.resolve(); + if (env.hasError) throw env.error; + } + return next(); + }; +})(typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) { + var e = new Error(message); + return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e; +}); +export const a = 1; +export let b = 2; +export var c = 3; +export { d }; +export { e }; +var d, e; +const env_1 = { stack: [], error: void 0, hasError: false }; +try { + d = __addDisposableResource(env_1, undefined, false); + e = __addDisposableResource(env_1, undefined, true); +} +catch (e_1) { + env_1.error = e_1; + env_1.hasError = true; +} +finally { + const result_1 = __disposeResources(env_1); + if (result_1) + await result_1; +} +//# sourceMappingURL=variables.js.map +//// [variables.js.map] //// +{"version":3,"file":"variables.js","sourceRoot":"","sources":["variables.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACnB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAEjB,OAAO,EAAE,CAAC,EAAE,CAAC;AAEb,OAAO,EAAE,CAAC,EAAE,CAAC;;;;IAHP,CAAC,kCAAG,SAAS,QAAA,CAAC;IAER,CAAC,kCAAG,SAAS,OAAA,CAAC"} +//// [interface.js] //// +export {}; +//# sourceMappingURL=interface.js.map +//// [interface.js.map] //// +{"version":3,"file":"interface.js","sourceRoot":"","sources":["interface.ts"],"names":[],"mappings":""} +//// [class.js] //// +var _Bar_d; +const i = Symbol(); +export class Bar { + constructor() { + _Bar_d.set(this, void 0); + } +} +_Bar_d = new WeakMap(); +export class Baz { +} +//# sourceMappingURL=class.js.map +//// [class.js.map] //// +{"version":3,"file":"class.js","sourceRoot":"","sources":["class.ts"],"names":[],"mappings":";AAAA,MAAM,CAAC,GAAG,MAAM,EAAE,CAAC;AACnB,MAAM,OAAO,GAAG;IAAhB;QAII,yBAAW;IAMf,CAAC;CAAA;;AAED,MAAM,OAAgB,GAAG;CAGxB"} +//// [namespace.js] //// +export var ns; +(function (ns) { + let internal; + (function (internal) { + class Foo { + } + internal.Foo = Foo; + })(internal || (internal = {})); + let nested; + (function (nested) { + nested.inner = internal; + })(nested = ns.nested || (ns.nested = {})); +})(ns || (ns = {})); +//# sourceMappingURL=namespace.js.map +//// [namespace.js.map] //// +{"version":3,"file":"namespace.js","sourceRoot":"","sources":["namespace.ts"],"names":[],"mappings":"AAAA,MAAM,KAAW,EAAE,CAOlB;AAPD,WAAiB,EAAE;IACf,IAAU,QAAQ,CAEjB;IAFD,WAAU,QAAQ;QACd,MAAa,GAAG;SAAG;QAAN,SAAA,GAAG,MAAG,CAAA;IACvB,CAAC,EAFS,QAAQ,KAAR,QAAQ,QAEjB;IACD,IAAiB,MAAM,CAEtB;IAFD,WAAiB,MAAM;QACL,OAAA,KAAK,GAAG,QAAS,CAAA;IACnC,CAAC,EAFgB,MAAM,GAAN,GAAA,MAAM,KAAN,GAAA,MAAM,QAEtB;AACL,CAAC,EAPgB,EAAE,KAAF,EAAE,QAOlB"} +//// [alias.js] //// +export {}; +//# sourceMappingURL=alias.js.map +//// [alias.js.map] //// +{"version":3,"file":"alias.js","sourceRoot":"","sources":["alias.ts"],"names":[],"mappings":""} diff --git a/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js.diff b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js.diff new file mode 100644 index 00000000000..589aa7b92d1 --- /dev/null +++ b/testdata/baselines/reference/submodule/transpile/jsWithSourceMapBasic(sourceMap=true).js.diff @@ -0,0 +1,20 @@ +--- old.transpile/jsWithSourceMapBasic(sourceMap=true).js ++++ new.transpile/jsWithSourceMapBasic(sourceMap=true).js +@@= skipped -115, +115 lines =@@ + } + //# sourceMappingURL=variables.js.map + //// [variables.js.map] //// +-{"version":3,"file":"variables.js","sourceRoot":"","sources":["variables.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACnB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAEjB,OAAO,EAAE,CAAC,EAAE,CAAC;AAEb,OAAO,EAAE,CAAC,EAAE,CAAC;;;;IAHP,mCAAI,SAAS,QAAA,CAAC;IAER,mCAAI,SAAS,OAAA,CAAC"} ++{"version":3,"file":"variables.js","sourceRoot":"","sources":["variables.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AAAA,MAAM,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC;AACnB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AACjB,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,CAAC;AAEjB,OAAO,EAAE,CAAC,EAAE,CAAC;AAEb,OAAO,EAAE,CAAC,EAAE,CAAC;;;;IAHP,CAAC,kCAAG,SAAS,QAAA,CAAC;IAER,CAAC,kCAAG,SAAS,OAAA,CAAC"} + //// [interface.js] //// + export {}; + //# sourceMappingURL=interface.js.map +@@= skipped -36, +36 lines =@@ + })(ns || (ns = {})); + //# sourceMappingURL=namespace.js.map + //// [namespace.js.map] //// +-{"version":3,"file":"namespace.js","sourceRoot":"","sources":["namespace.ts"],"names":[],"mappings":"AAAA,MAAM,KAAW,EAAE,CAOlB;AAPD,WAAiB,EAAE;IACf,IAAU,QAAQ,CAEjB;IAFD,WAAU,QAAQ;QACd,MAAa,GAAG;SAAG;QAAN,YAAG,MAAG,CAAA;IACvB,CAAC,EAFS,QAAQ,KAAR,QAAQ,QAEjB;IACD,IAAiB,MAAM,CAEtB;IAFD,WAAiB,MAAM;QACL,YAAK,GAAG,QAAQ,CAAC;IACnC,CAAC,EAFgB,MAAM,GAAN,SAAM,KAAN,SAAM,QAEtB;AACL,CAAC,EAPgB,EAAE,KAAF,EAAE,QAOlB"} ++{"version":3,"file":"namespace.js","sourceRoot":"","sources":["namespace.ts"],"names":[],"mappings":"AAAA,MAAM,KAAW,EAAE,CAOlB;AAPD,WAAiB,EAAE;IACf,IAAU,QAAQ,CAEjB;IAFD,WAAU,QAAQ;QACd,MAAa,GAAG;SAAG;QAAN,SAAA,GAAG,MAAG,CAAA;IACvB,CAAC,EAFS,QAAQ,KAAR,QAAQ,QAEjB;IACD,IAAiB,MAAM,CAEtB;IAFD,WAAiB,MAAM;QACL,OAAA,KAAK,GAAG,QAAS,CAAA;IACnC,CAAC,EAFgB,MAAM,GAAN,GAAA,MAAM,KAAN,GAAA,MAAM,QAEtB;AACL,CAAC,EAPgB,EAAE,KAAF,EAAE,QAOlB"} + //// [alias.js] //// + export {}; + //# sourceMappingURL=alias.js.map \ No newline at end of file