Skip to content

Commit 289344a

Browse files
authored
Don’t emit file references that are already referenced via imports (#56614)
1 parent 369eeb1 commit 289344a

6 files changed

Lines changed: 69 additions & 5 deletions

File tree

src/compiler/transformers/declarations.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -617,6 +617,11 @@ export function transformDeclarations(context: TransformationContext) {
617617

618618
function mapReferencesIntoArray(references: FileReference[], outputFilePath: string): (file: SourceFile) => void {
619619
return file => {
620+
if (exportedModulesFromDeclarationEmit?.includes(file.symbol)) {
621+
// Already have an import declaration resolving to this file
622+
return;
623+
}
624+
620625
let declFileName: string;
621626
if (file.isDeclarationFile) { // Neither decl files or js should have their refs changed
622627
declFileName = file.fileName;

tests/baselines/reference/declFileForExportedImport.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,5 @@ var z = exports.b.x;
2929
//// [declFileForExportedImport_0.d.ts]
3030
export declare var x: number;
3131
//// [declFileForExportedImport_1.d.ts]
32-
/// <reference path="declFileForExportedImport_0.d.ts" />
3332
export import a = require('./declFileForExportedImport_0');
3433
export import b = a;
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
//// [tests/cases/compiler/declarationEmitRedundantTripleSlashModuleAugmentation.ts] ////
2+
3+
//// [index.d.ts]
4+
declare module "foo" {
5+
export interface Original {}
6+
}
7+
8+
//// [augmentation.ts]
9+
export interface FooOptions {}
10+
declare module "foo" {
11+
export interface Augmentation {}
12+
}
13+
14+
//// [index.ts]
15+
import { Original, Augmentation } from "foo";
16+
import type { FooOptions } from "./augmentation";
17+
export interface _ {
18+
original: Original;
19+
augmentation: Augmentation;
20+
options: FooOptions;
21+
}
22+
23+
24+
25+
26+
//// [augmentation.d.ts]
27+
export interface FooOptions {
28+
}
29+
declare module "foo" {
30+
interface Augmentation {
31+
}
32+
}
33+
//// [index.d.ts]
34+
import { Original, Augmentation } from "foo";
35+
import type { FooOptions } from "./augmentation";
36+
export interface _ {
37+
original: Original;
38+
augmentation: Augmentation;
39+
options: FooOptions;
40+
}

tests/baselines/reference/importDecl.js

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,6 @@ export declare function foo(): d;
210210
import m4 = require("./importDecl_require");
211211
export declare function foo2(): m4.d;
212212
//// [importDecl_1.d.ts]
213-
/// <reference path="importDecl_require.d.ts" />
214-
/// <reference path="importDecl_require1.d.ts" />
215-
/// <reference path="importDecl_require2.d.ts" />
216213
/// <reference path="importDecl_require3.d.ts" />
217214
/// <reference path="importDecl_require4.d.ts" />
218215
import m4 = require("./importDecl_require");

tests/baselines/reference/importDeclarationUsedAsTypeQuery.js

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,5 @@ export declare class B {
3232
id: number;
3333
}
3434
//// [importDeclarationUsedAsTypeQuery_1.d.ts]
35-
/// <reference path="importDeclarationUsedAsTypeQuery_require.d.ts" />
3635
import a = require('./importDeclarationUsedAsTypeQuery_require');
3736
export declare var x: typeof a;
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
// @declaration: true
2+
// @emitDeclarationOnly: true
3+
// @noTypesAndSymbols: true
4+
// @module: nodenext
5+
6+
// @Filename: /node_modules/foo/index.d.ts
7+
declare module "foo" {
8+
export interface Original {}
9+
}
10+
11+
// @Filename: /augmentation.ts
12+
export interface FooOptions {}
13+
declare module "foo" {
14+
export interface Augmentation {}
15+
}
16+
17+
// @Filename: /index.ts
18+
import { Original, Augmentation } from "foo";
19+
import type { FooOptions } from "./augmentation";
20+
export interface _ {
21+
original: Original;
22+
augmentation: Augmentation;
23+
options: FooOptions;
24+
}

0 commit comments

Comments
 (0)