Skip to content

Commit e315436

Browse files
committed
feat(29624): improve errors for non-exported types
1 parent 76ee021 commit e315436

13 files changed

+172
-5
lines changed

src/compiler/checker.ts

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2355,7 +2355,7 @@ namespace ts {
23552355
);
23562356
}
23572357
else {
2358-
error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
2358+
reportNonExportedMember(name, declarationName, moduleSymbol, moduleName);
23592359
}
23602360
}
23612361
}
@@ -2364,6 +2364,25 @@ namespace ts {
23642364
}
23652365
}
23662366

2367+
function reportNonExportedMember(name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
2368+
const localSymbol = moduleSymbol.valueDeclaration.locals?.get(name.escapedText);
2369+
if (localSymbol) {
2370+
const exportedSymbol = moduleSymbol.exports
2371+
? find(symbolsToArray(moduleSymbol.exports), symbol => !!getSymbolIfSameReference(symbol, localSymbol))
2372+
: undefined;
2373+
const diagnostic = exportedSymbol
2374+
? error(name, Diagnostics.Module_0_declares_1_locally_but_it_is_exported_as_2, moduleName, declarationName, symbolToString(exportedSymbol))
2375+
: error(name, Diagnostics.Module_0_declares_1_locally_but_it_is_not_exported, moduleName, declarationName)
2376+
2377+
addRelatedInfo(diagnostic,
2378+
createDiagnosticForNode(localSymbol.valueDeclaration, Diagnostics._0_is_declared_here, declarationName)
2379+
);
2380+
}
2381+
else {
2382+
error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
2383+
}
2384+
}
2385+
23672386
function getTargetOfImportSpecifier(node: ImportSpecifier, dontResolveAlias: boolean): Symbol | undefined {
23682387
const resolved = getExternalModuleMember(node.parent.parent.parent, node, dontResolveAlias);
23692388
if (resolved && node.parent.parent.isTypeOnly) {

src/compiler/diagnosticMessages.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1765,6 +1765,14 @@
17651765
"category": "Error",
17661766
"code": 2458
17671767
},
1768+
"Module '{0}' declares '{1}' locally, but it is not exported.": {
1769+
"category": "Error",
1770+
"code": 2459
1771+
},
1772+
"Module '{0}' declares '{1}' locally, but it is exported as '{2}'.": {
1773+
"category": "Error",
1774+
"code": 2460
1775+
},
17681776
"Type '{0}' is not an array type.": {
17691777
"category": "Error",
17701778
"code": 2461
Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(1,10): error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
2-
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
1+
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(1,10): error TS2460: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is exported as 'a'.
2+
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2460: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is exported as 'a'.
33

44

55
==== tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts (0 errors) ====
@@ -9,7 +9,9 @@ tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2305
99
==== tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts (2 errors) ====
1010
import { a } from "./es6ImportNamedImportNoNamedExports_0";
1111
~
12-
!!! error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
12+
!!! error TS2460: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is exported as 'a'.
13+
!!! related TS2728 tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts:1:5: 'a' is declared here.
1314
import { a as x } from "./es6ImportNamedImportNoNamedExports_0";
1415
~
15-
!!! error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
16+
!!! error TS2460: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is exported as 'a'.
17+
!!! related TS2728 tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts:1:5: 'a' is declared here.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/b.ts(1,15): error TS2460: Module '"./a"' declares 'bar' locally, but it is exported as 'baz'.
2+
3+
4+
==== tests/cases/compiler/a.ts (0 errors) ====
5+
declare function foo(): any
6+
declare function bar(): any;
7+
export { foo, bar as baz };
8+
9+
==== tests/cases/compiler/b.ts (1 errors) ====
10+
import { foo, bar } from "./a";
11+
~~~
12+
!!! error TS2460: Module '"./a"' declares 'bar' locally, but it is exported as 'baz'.
13+
!!! related TS2728 tests/cases/compiler/a.ts:2:18: 'bar' is declared here.
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/importNonExportedMember.ts] ////
2+
3+
//// [a.ts]
4+
declare function foo(): any
5+
declare function bar(): any;
6+
export { foo, bar as baz };
7+
8+
//// [b.ts]
9+
import { foo, bar } from "./a";
10+
11+
12+
//// [a.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
//// [b.js]
16+
"use strict";
17+
exports.__esModule = true;
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/a.ts ===
2+
declare function foo(): any
3+
>foo : Symbol(foo, Decl(a.ts, 0, 0))
4+
5+
declare function bar(): any;
6+
>bar : Symbol(bar, Decl(a.ts, 0, 27))
7+
8+
export { foo, bar as baz };
9+
>foo : Symbol(foo, Decl(a.ts, 2, 8))
10+
>bar : Symbol(bar, Decl(a.ts, 0, 27))
11+
>baz : Symbol(baz, Decl(a.ts, 2, 13))
12+
13+
=== tests/cases/compiler/b.ts ===
14+
import { foo, bar } from "./a";
15+
>foo : Symbol(foo, Decl(b.ts, 0, 8))
16+
>bar : Symbol(bar, Decl(b.ts, 0, 13))
17+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
=== tests/cases/compiler/a.ts ===
2+
declare function foo(): any
3+
>foo : () => any
4+
5+
declare function bar(): any;
6+
>bar : () => any
7+
8+
export { foo, bar as baz };
9+
>foo : () => any
10+
>bar : () => any
11+
>baz : () => any
12+
13+
=== tests/cases/compiler/b.ts ===
14+
import { foo, bar } from "./a";
15+
>foo : () => any
16+
>bar : any
17+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
tests/cases/compiler/b.ts(1,10): error TS2459: Module '"./a"' declares 'bar' locally, but it is not exported.
2+
3+
4+
==== tests/cases/compiler/a.ts (0 errors) ====
5+
declare function foo(): any
6+
declare function bar(): any;
7+
export { foo };
8+
9+
==== tests/cases/compiler/b.ts (1 errors) ====
10+
import { bar } from "./a";
11+
~~~
12+
!!! error TS2459: Module '"./a"' declares 'bar' locally, but it is not exported.
13+
!!! related TS2728 tests/cases/compiler/a.ts:2:18: 'bar' is declared here.
14+
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
//// [tests/cases/compiler/importNonExportedMember1.ts] ////
2+
3+
//// [a.ts]
4+
declare function foo(): any
5+
declare function bar(): any;
6+
export { foo };
7+
8+
//// [b.ts]
9+
import { bar } from "./a";
10+
11+
12+
//// [a.js]
13+
"use strict";
14+
exports.__esModule = true;
15+
//// [b.js]
16+
"use strict";
17+
exports.__esModule = true;
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/a.ts ===
2+
declare function foo(): any
3+
>foo : Symbol(foo, Decl(a.ts, 0, 0))
4+
5+
declare function bar(): any;
6+
>bar : Symbol(bar, Decl(a.ts, 0, 27))
7+
8+
export { foo };
9+
>foo : Symbol(foo, Decl(a.ts, 2, 8))
10+
11+
=== tests/cases/compiler/b.ts ===
12+
import { bar } from "./a";
13+
>bar : Symbol(bar, Decl(b.ts, 0, 8))
14+
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
=== tests/cases/compiler/a.ts ===
2+
declare function foo(): any
3+
>foo : () => any
4+
5+
declare function bar(): any;
6+
>bar : () => any
7+
8+
export { foo };
9+
>foo : () => any
10+
11+
=== tests/cases/compiler/b.ts ===
12+
import { bar } from "./a";
13+
>bar : any
14+
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @filename: a.ts
2+
declare function foo(): any
3+
declare function bar(): any;
4+
export { foo, bar as baz };
5+
6+
// @filename: b.ts
7+
import { foo, bar } from "./a";
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// @filename: a.ts
2+
declare function foo(): any
3+
declare function bar(): any;
4+
export { foo };
5+
6+
// @filename: b.ts
7+
import { bar } from "./a";

0 commit comments

Comments
 (0)