Skip to content

Commit b1a0261

Browse files
authored
If declaration emit input is a module, output should be a module (microsoft#20626)
1 parent b2f2610 commit b1a0261

37 files changed

Lines changed: 107 additions & 4 deletions

src/compiler/declarationEmitter.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,8 @@ namespace ts {
148148
moduleElementDeclarationEmitInfo = [];
149149
}
150150

151-
if (!isBundledEmit && isExternalModule(sourceFile) && sourceFile.moduleAugmentations.length && !resultHasExternalModuleIndicator) {
152-
// if file was external module with augmentations - this fact should be preserved in .d.ts as well.
151+
if (!isBundledEmit && isExternalModule(sourceFile) && !resultHasExternalModuleIndicator) {
152+
// if file was external module this fact should be preserved in .d.ts as well.
153153
// in case if we didn't write any external module specifiers in .d.ts we need to emit something
154154
// that will force compiler to think that this file is an external module - 'export {}' is a reasonable choice here.
155155
write("export {};");
@@ -651,6 +651,9 @@ namespace ts {
651651
}
652652

653653
function emitExportAssignment(node: ExportAssignment) {
654+
if (isSourceFile(node.parent)) {
655+
resultHasExternalModuleIndicator = true; // Top-level exports are external module indicators
656+
}
654657
if (node.expression.kind === SyntaxKind.Identifier) {
655658
write(node.isExportEquals ? "export = " : "export default ");
656659
writeTextOfNode(currentText, node.expression);
@@ -745,6 +748,7 @@ namespace ts {
745748
const modifiers = getModifierFlags(node);
746749
// If the node is exported
747750
if (modifiers & ModifierFlags.Export) {
751+
resultHasExternalModuleIndicator = true; // Top-level exports are external module indicators
748752
write("export ");
749753
}
750754

@@ -901,6 +905,7 @@ namespace ts {
901905
}
902906

903907
function emitExportDeclaration(node: ExportDeclaration) {
908+
resultHasExternalModuleIndicator = true; // Top-level exports are external module indicators
904909
emitJsDocComments(node);
905910
write("export ");
906911
if (node.exportClause) {

tests/baselines/reference/bindingPatternOmittedExpressionNesting.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,4 @@ var _b, _c, _d, _e;
99

1010

1111
//// [bindingPatternOmittedExpressionNesting.d.ts]
12+
export {};

tests/baselines/reference/commentsExternalModules.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,3 +169,4 @@ export declare module m4 {
169169
function fooExport(): number;
170170
}
171171
//// [commentsExternalModules_1.d.ts]
172+
export {};

tests/baselines/reference/commonSourceDirectory.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,3 +28,4 @@ foo_1.x + bar_1.y;
2828

2929
//// [/app/bin/index.d.ts]
3030
/// <reference path="../../types/bar.d.ts" />
31+
export {};

tests/baselines/reference/commonjsSafeImport.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,4 @@ _10_lib_1.Foo();
2424
//// [10_lib.d.ts]
2525
export declare function Foo(): void;
2626
//// [main.d.ts]
27+
export {};
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
//// [tests/cases/compiler/emptyDeclarationEmitIsModule.ts] ////
2+
3+
//// [module.ts]
4+
import * as i from "./index";
5+
class Foo {}
6+
//// [index.ts]
7+
import {} from "./module";
8+
export interface Bar {
9+
x: string
10+
}
11+
12+
//// [index.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
//// [module.js]
16+
"use strict";
17+
exports.__esModule = true;
18+
var Foo = /** @class */ (function () {
19+
function Foo() {
20+
}
21+
return Foo;
22+
}());
23+
24+
25+
//// [index.d.ts]
26+
export interface Bar {
27+
x: string;
28+
}
29+
//// [module.d.ts]
30+
export {};
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/module.ts ===
2+
import * as i from "./index";
3+
>i : Symbol(i, Decl(module.ts, 0, 6))
4+
5+
class Foo {}
6+
>Foo : Symbol(Foo, Decl(module.ts, 0, 29))
7+
8+
=== tests/cases/compiler/index.ts ===
9+
import {} from "./module";
10+
export interface Bar {
11+
>Bar : Symbol(Bar, Decl(index.ts, 0, 26))
12+
13+
x: string
14+
>x : Symbol(Bar.x, Decl(index.ts, 1, 22))
15+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
=== tests/cases/compiler/module.ts ===
2+
import * as i from "./index";
3+
>i : typeof i
4+
5+
class Foo {}
6+
>Foo : Foo
7+
8+
=== tests/cases/compiler/index.ts ===
9+
import {} from "./module";
10+
export interface Bar {
11+
>Bar : Bar
12+
13+
x: string
14+
>x : string
15+
}

tests/baselines/reference/es6ImportDefaultBinding.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,3 +22,4 @@ var x = defaultBinding;
2222
declare var a: number;
2323
export default a;
2424
//// [es6ImportDefaultBinding_1.d.ts]
25+
export {};

tests/baselines/reference/es6ImportDefaultBindingAmd.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,4 @@ define(["require", "exports", "es6ImportDefaultBindingAmd_0"], function (require
2929
declare var a: number;
3030
export default a;
3131
//// [es6ImportDefaultBindingAmd_1.d.ts]
32+
export {};

0 commit comments

Comments
 (0)