Skip to content

Commit 33bfd92

Browse files
committed
Merge pull request #6444 from Microsoft/errorOnReexportingGlobals
report errors when re-exporting globals
2 parents 869b811 + b811b9f commit 33bfd92

23 files changed

+365
-97
lines changed

src/compiler/checker.ts

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14364,7 +14364,16 @@ namespace ts {
1436414364
function checkExportSpecifier(node: ExportSpecifier) {
1436514365
checkAliasSymbol(node);
1436614366
if (!(<ExportDeclaration>node.parent.parent).moduleSpecifier) {
14367-
markExportAsReferenced(node);
14367+
const exportedName = node.propertyName || node.name;
14368+
// find immediate value referenced by exported name (SymbolFlags.Alias is set so we don't chase down aliases)
14369+
const symbol = resolveName(exportedName, exportedName.text, SymbolFlags.Value | SymbolFlags.Type | SymbolFlags.Namespace | SymbolFlags.Alias,
14370+
/*nameNotFoundMessage*/ undefined, /*nameArg*/ undefined);
14371+
if (symbol && isGlobalSourceFile(getDeclarationContainer(symbol.declarations[0]))) {
14372+
error(exportedName, Diagnostics.Cannot_re_export_name_that_is_not_defined_in_the_module);
14373+
}
14374+
else {
14375+
markExportAsReferenced(node);
14376+
}
1436814377
}
1436914378
}
1437014379

src/compiler/diagnosticMessages.json

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1771,6 +1771,10 @@
17711771
"category": "Error",
17721772
"code": 2660
17731773
},
1774+
"Cannot re-export name that is not defined in the module.": {
1775+
"category": "Error",
1776+
"code": 2661
1777+
},
17741778
"Import declaration '{0}' is using private name '{1}'.": {
17751779
"category": "Error",
17761780
"code": 4000
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
tests/cases/compiler/b.ts(1,9): error TS2661: Cannot re-export name that is not defined in the module.
2+
3+
4+
==== tests/cases/compiler/a.d.ts (0 errors) ====
5+
6+
declare class X { }
7+
8+
==== tests/cases/compiler/b.ts (1 errors) ====
9+
export {X};
10+
~
11+
!!! error TS2661: Cannot re-export name that is not defined in the module.
12+
export function f() {
13+
var x: X;
14+
return x;
15+
}
16+

tests/baselines/reference/exportSpecifierForAGlobal.symbols

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

tests/baselines/reference/exportSpecifierForAGlobal.types

Lines changed: 0 additions & 20 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/exportSpecifierReferencingOuterDeclaration1.ts(3,14): error TS2661: Cannot re-export name that is not defined in the module.
2+
3+
4+
==== tests/cases/compiler/exportSpecifierReferencingOuterDeclaration1.ts (1 errors) ====
5+
declare module X { export interface bar { } }
6+
declare module "m" {
7+
export { X };
8+
~
9+
!!! error TS2661: Cannot re-export name that is not defined in the module.
10+
export function foo(): X.bar;
11+
}

tests/baselines/reference/exportSpecifierReferencingOuterDeclaration1.symbols

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

tests/baselines/reference/exportSpecifierReferencingOuterDeclaration1.types

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
tests/cases/compiler/exportSpecifierReferencingOuterDeclaration2_B.ts(1,10): error TS2661: Cannot re-export name that is not defined in the module.
2+
3+
4+
==== tests/cases/compiler/exportSpecifierReferencingOuterDeclaration2_A.ts (0 errors) ====
5+
declare module X { export interface bar { } }
6+
7+
==== tests/cases/compiler/exportSpecifierReferencingOuterDeclaration2_B.ts (1 errors) ====
8+
export { X };
9+
~
10+
!!! error TS2661: Cannot re-export name that is not defined in the module.
11+
export declare function foo(): X.bar;

tests/baselines/reference/exportSpecifierReferencingOuterDeclaration2.symbols

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

0 commit comments

Comments
 (0)