Skip to content

Commit 16953b7

Browse files
Fix for dts emit for tripple slash directives in d.ts files (#1519)
Co-authored-by: Copilot <[email protected]>
1 parent 807ee7f commit 16953b7

File tree

71 files changed

+102
-1865
lines changed

Some content is hidden

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

71 files changed

+102
-1865
lines changed

internal/printer/printer.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4403,6 +4403,9 @@ func (p *Printer) emitSourceFile(node *ast.SourceFile) {
44034403
p.emitShebangIfNeeded(node)
44044404
index = p.emitPrologueDirectives(node.Statements)
44054405
p.emitHelpers(node.AsNode())
4406+
if node.IsDeclarationFile {
4407+
p.emitTripleSlashDirectives(node)
4408+
}
44064409
}
44074410

44084411
// !!! Emit triple-slash directives
@@ -4420,6 +4423,23 @@ func (p *Printer) emitSourceFile(node *ast.SourceFile) {
44204423
p.commentsDisabled = savedCommentsDisabled
44214424
}
44224425

4426+
func (p *Printer) emitTripleSlashDirectives(node *ast.SourceFile) {
4427+
p.emitDirective("path", node.ReferencedFiles)
4428+
p.emitDirective("types", node.TypeReferenceDirectives)
4429+
p.emitDirective("lib", node.LibReferenceDirectives)
4430+
}
4431+
4432+
func (p *Printer) emitDirective(kind string, refs []*ast.FileReference) {
4433+
for _, ref := range refs {
4434+
var resolutionMode string
4435+
if ref.ResolutionMode != core.ResolutionModeNone {
4436+
resolutionMode = fmt.Sprintf(`resolution-mode="%s" `, core.IfElse(ref.ResolutionMode == core.ResolutionModeESM, "import", "require"))
4437+
}
4438+
p.writeComment(fmt.Sprintf("/// <reference %s=\"%s\" %s%s/>", kind, ref.FileName, resolutionMode, core.IfElse(ref.Preserve, `preserve="true" `, "")))
4439+
p.writeLine()
4440+
}
4441+
}
4442+
44234443
//
44244444
// Lists
44254445
//

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,5 @@ foo_1.x + bar_1.y;
2525
//# sourceMappingURL=../myMapRoot/index.js.map
2626

2727
//// [/app/bin/index.d.ts]
28+
/// <reference path="../../types/bar.d.ts" preserve="true" />
2829
export {};

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

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,3 @@
1010
+const bar_1 = require("bar");
1111
foo_1.x + bar_1.y;
1212
//# sourceMappingURL=../myMapRoot/index.js.map
13-
14-
//// [/app/bin/index.d.ts]
15-
-/// <reference path="../../types/bar.d.ts" preserve="true" />
16-
export {};

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,4 +17,5 @@ exports.x = y;
1717
//# sourceMappingURL=../src/myMapRoot/index.js.map
1818

1919
//// [/app/bin/index.d.ts]
20+
/// <reference path="../lib/bar.d.ts" preserve="true" />
2021
export declare const x: number;

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

Lines changed: 0 additions & 8 deletions
This file was deleted.

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

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -34,29 +34,6 @@ declare module "SubModule" {
3434
}
3535
}
3636
//// [declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts]
37+
/// <reference path="declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts" preserve="true" />
3738
import SubModule = require('SubModule');
3839
export declare var x: SubModule.m.m3.c;
39-
40-
41-
//// [DtsFileErrors]
42-
43-
44-
declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts(1,28): error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.
45-
46-
47-
==== declFileAmbientExternalModuleWithSingleExportedModule_1.d.ts (1 errors) ====
48-
import SubModule = require('SubModule');
49-
~~~~~~~~~~~
50-
!!! error TS2307: Cannot find module 'SubModule' or its corresponding type declarations.
51-
export declare var x: SubModule.m.m3.c;
52-
53-
==== declFileAmbientExternalModuleWithSingleExportedModule_0.d.ts (0 errors) ====
54-
declare module "SubModule" {
55-
namespace m {
56-
namespace m3 {
57-
interface c {
58-
}
59-
}
60-
}
61-
}
62-

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

Lines changed: 0 additions & 33 deletions
This file was deleted.

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,5 @@ var x = new M.C(); // Declaration file wont get emitted because there are errors
2121

2222

2323
//// [client.d.ts]
24+
/// <reference path="declFile.d.ts" preserve="true" />
2425
declare var x: M.C; // Declaration file wont get emitted because there are errors in declaration file
Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
--- old.declFileWithErrorsInInputDeclarationFile.js
22
+++ new.declFileWithErrorsInInputDeclarationFile.js
3-
@@= skipped -20, +20 lines =@@
4-
3+
@@= skipped -21, +21 lines =@@
54

65
//// [client.d.ts]
7-
-/// <reference path="declFile.d.ts" preserve="true" />
6+
/// <reference path="declFile.d.ts" preserve="true" />
87
-declare var x: M.C;
98
+declare var x: M.C; // Declaration file wont get emitted because there are errors in declaration file

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

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -24,42 +24,5 @@ export async function drainStream(stream: NodeJS.ReadableStream): Promise<void>
2424

2525

2626
//// [app.d.mts]
27+
/// <reference types="node" preserve="true" />
2728
export declare function drainStream(stream: NodeJS.ReadableStream): Promise<void>;
28-
29-
30-
//// [DtsFileErrors]
31-
32-
33-
/app.d.mts(1,45): error TS2503: Cannot find namespace 'NodeJS'.
34-
35-
36-
==== /tsconfig.json (0 errors) ====
37-
{
38-
"compilerOptions": {
39-
"module": "nodenext",
40-
"types": [],
41-
"declaration": true,
42-
"emitDeclarationOnly": true,
43-
}
44-
}
45-
46-
==== /app.d.mts (1 errors) ====
47-
export declare function drainStream(stream: NodeJS.ReadableStream): Promise<void>;
48-
~~~~~~
49-
!!! error TS2503: Cannot find namespace 'NodeJS'.
50-
51-
==== /node_modules/@types/node/package.json (0 errors) ====
52-
{
53-
"name": "@types/node",
54-
"version": "1.0.0",
55-
"types": "index.d.ts"
56-
}
57-
58-
==== /node_modules/@types/node/globals.d.ts (0 errors) ====
59-
declare namespace NodeJS {
60-
interface ReadableStream {}
61-
}
62-
63-
==== /node_modules/@types/node/index.d.ts (0 errors) ====
64-
/// <reference path="globals.d.ts" />
65-

0 commit comments

Comments
 (0)