Skip to content

Commit 50e2fd8

Browse files
authored
only emit /// types reference for a symbol in d.ts file if all declarations of a symbol come from type reference directives (#11872)
* only emit /// types reference for a symbol in d.ts file if all declarations of a symbol come from type reference directives * pass proper value for current directory when compiling .d.ts files
1 parent 3e18aba commit 50e2fd8

18 files changed

+293
-1
lines changed

src/compiler/checker.ts

+4
Original file line numberDiff line numberDiff line change
@@ -19505,6 +19505,10 @@ namespace ts {
1950519505
if (typeReferenceDirective) {
1950619506
(typeReferenceDirectives || (typeReferenceDirectives = [])).push(typeReferenceDirective);
1950719507
}
19508+
else {
19509+
// found at least one entry that does not originate from type reference directive
19510+
return undefined;
19511+
}
1950819512
}
1950919513
}
1951019514
return typeReferenceDirectives;

src/harness/harness.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -1238,7 +1238,7 @@ namespace Harness {
12381238
if (options.declaration && result.errors.length === 0 && result.declFilesCode.length > 0) {
12391239
ts.forEach(inputFiles, file => addDtsFile(file, declInputFiles));
12401240
ts.forEach(otherFiles, file => addDtsFile(file, declOtherFiles));
1241-
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory);
1241+
const output = compileFiles(declInputFiles, declOtherFiles, harnessSettings, options, currentDirectory || harnessSettings["currentDirectory"]);
12421242
return { declInputFiles, declOtherFiles, declResult: output.result };
12431243
}
12441244

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/declarationFilesWithTypeReferences1.ts] ////
2+
3+
//// [index.d.ts]
4+
5+
interface Error {
6+
stack2: string;
7+
}
8+
9+
//// [app.ts]
10+
11+
function foo(): Error {
12+
return undefined;
13+
}
14+
15+
//// [app.js]
16+
function foo() {
17+
return undefined;
18+
}
19+
20+
21+
//// [app.d.ts]
22+
declare function foo(): Error;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /node_modules/@types/node/index.d.ts ===
2+
3+
interface Error {
4+
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(index.d.ts, 0, 0))
5+
6+
stack2: string;
7+
>stack2 : Symbol(Error.stack2, Decl(index.d.ts, 1, 17))
8+
}
9+
10+
=== /app.ts ===
11+
12+
function foo(): Error {
13+
>foo : Symbol(foo, Decl(app.ts, 0, 0))
14+
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(index.d.ts, 0, 0))
15+
16+
return undefined;
17+
>undefined : Symbol(undefined)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /node_modules/@types/node/index.d.ts ===
2+
3+
interface Error {
4+
>Error : Error
5+
6+
stack2: string;
7+
>stack2 : string
8+
}
9+
10+
=== /app.ts ===
11+
12+
function foo(): Error {
13+
>foo : () => Error
14+
>Error : Error
15+
16+
return undefined;
17+
>undefined : undefined
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/declarationFilesWithTypeReferences2.ts] ////
2+
3+
//// [index.d.ts]
4+
5+
interface Error2 {
6+
stack2: string;
7+
}
8+
9+
//// [app.ts]
10+
11+
function foo(): Error2 {
12+
return undefined;
13+
}
14+
15+
//// [app.js]
16+
function foo() {
17+
return undefined;
18+
}
19+
20+
21+
//// [app.d.ts]
22+
/// <reference types="node" />
23+
declare function foo(): Error2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /node_modules/@types/node/index.d.ts ===
2+
3+
interface Error2 {
4+
>Error2 : Symbol(Error2, Decl(index.d.ts, 0, 0))
5+
6+
stack2: string;
7+
>stack2 : Symbol(Error2.stack2, Decl(index.d.ts, 1, 18))
8+
}
9+
10+
=== /app.ts ===
11+
12+
function foo(): Error2 {
13+
>foo : Symbol(foo, Decl(app.ts, 0, 0))
14+
>Error2 : Symbol(Error2, Decl(index.d.ts, 0, 0))
15+
16+
return undefined;
17+
>undefined : Symbol(undefined)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /node_modules/@types/node/index.d.ts ===
2+
3+
interface Error2 {
4+
>Error2 : Error2
5+
6+
stack2: string;
7+
>stack2 : string
8+
}
9+
10+
=== /app.ts ===
11+
12+
function foo(): Error2 {
13+
>foo : () => Error2
14+
>Error2 : Error2
15+
16+
return undefined;
17+
>undefined : undefined
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//// [tests/cases/compiler/declarationFilesWithTypeReferences3.ts] ////
2+
3+
//// [index.d.ts]
4+
5+
interface Error2 {
6+
stack2: string;
7+
}
8+
9+
//// [app.ts]
10+
/// <reference types="node"/>
11+
function foo(): Error2 {
12+
return undefined;
13+
}
14+
15+
//// [app.js]
16+
/// <reference types="node"/>
17+
function foo() {
18+
return undefined;
19+
}
20+
21+
22+
//// [app.d.ts]
23+
/// <reference types="node" />
24+
declare function foo(): Error2;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /a/node_modules/@types/node/index.d.ts ===
2+
3+
interface Error2 {
4+
>Error2 : Symbol(Error2, Decl(index.d.ts, 0, 0))
5+
6+
stack2: string;
7+
>stack2 : Symbol(Error2.stack2, Decl(index.d.ts, 1, 18))
8+
}
9+
10+
=== /a/app.ts ===
11+
/// <reference types="node"/>
12+
function foo(): Error2 {
13+
>foo : Symbol(foo, Decl(app.ts, 0, 0))
14+
>Error2 : Symbol(Error2, Decl(index.d.ts, 0, 0))
15+
16+
return undefined;
17+
>undefined : Symbol(undefined)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /a/node_modules/@types/node/index.d.ts ===
2+
3+
interface Error2 {
4+
>Error2 : Error2
5+
6+
stack2: string;
7+
>stack2 : string
8+
}
9+
10+
=== /a/app.ts ===
11+
/// <reference types="node"/>
12+
function foo(): Error2 {
13+
>foo : () => Error2
14+
>Error2 : Error2
15+
16+
return undefined;
17+
>undefined : undefined
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
//// [tests/cases/compiler/declarationFilesWithTypeReferences4.ts] ////
2+
3+
//// [index.d.ts]
4+
5+
interface Error {
6+
stack2: string;
7+
}
8+
9+
//// [app.ts]
10+
/// <reference types="node"/>
11+
function foo(): Error {
12+
return undefined;
13+
}
14+
15+
//// [app.js]
16+
/// <reference types="node"/>
17+
function foo() {
18+
return undefined;
19+
}
20+
21+
22+
//// [app.d.ts]
23+
declare function foo(): Error;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /a/node_modules/@types/node/index.d.ts ===
2+
3+
interface Error {
4+
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(index.d.ts, 0, 0))
5+
6+
stack2: string;
7+
>stack2 : Symbol(Error.stack2, Decl(index.d.ts, 1, 17))
8+
}
9+
10+
=== /a/app.ts ===
11+
/// <reference types="node"/>
12+
function foo(): Error {
13+
>foo : Symbol(foo, Decl(app.ts, 0, 0))
14+
>Error : Symbol(Error, Decl(lib.d.ts, --, --), Decl(lib.d.ts, --, --), Decl(index.d.ts, 0, 0))
15+
16+
return undefined;
17+
>undefined : Symbol(undefined)
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
=== /a/node_modules/@types/node/index.d.ts ===
2+
3+
interface Error {
4+
>Error : Error
5+
6+
stack2: string;
7+
>stack2 : string
8+
}
9+
10+
=== /a/app.ts ===
11+
/// <reference types="node"/>
12+
function foo(): Error {
13+
>foo : () => Error
14+
>Error : Error
15+
16+
return undefined;
17+
>undefined : undefined
18+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @types: node
2+
// @declaration: true
3+
// @currentDirectory: /
4+
5+
// @filename: /node_modules/@types/node/index.d.ts
6+
interface Error {
7+
stack2: string;
8+
}
9+
10+
// @filename: /app.ts
11+
12+
function foo(): Error {
13+
return undefined;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// @types: node
2+
// @declaration: true
3+
// @currentDirectory: /
4+
5+
// @filename: /node_modules/@types/node/index.d.ts
6+
interface Error2 {
7+
stack2: string;
8+
}
9+
10+
// @filename: /app.ts
11+
12+
function foo(): Error2 {
13+
return undefined;
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @declaration: true
2+
3+
// @filename: /a/node_modules/@types/node/index.d.ts
4+
interface Error2 {
5+
stack2: string;
6+
}
7+
8+
// @filename: /a/app.ts
9+
/// <reference types="node"/>
10+
function foo(): Error2 {
11+
return undefined;
12+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
// @declaration: true
2+
3+
// @filename: /a/node_modules/@types/node/index.d.ts
4+
interface Error {
5+
stack2: string;
6+
}
7+
8+
// @filename: /a/app.ts
9+
/// <reference types="node"/>
10+
function foo(): Error {
11+
return undefined;
12+
}

0 commit comments

Comments
 (0)