feat(29624): better errors for non-exported types (#36187)

This commit is contained in:
Alexander T 2020-01-21 23:03:22 +02:00 committed by Daniel Rosenwasser
parent 342f4c0b54
commit 38eccbab2a
13 changed files with 175 additions and 6 deletions

View File

@ -2355,7 +2355,7 @@ namespace ts {
}
}
else {
if (moduleSymbol.exports && moduleSymbol.exports.has(InternalSymbolName.Default)) {
if (moduleSymbol.exports?.has(InternalSymbolName.Default)) {
error(
name,
Diagnostics.Module_0_has_no_exported_member_1_Did_you_mean_to_use_import_1_from_0_instead,
@ -2364,7 +2364,7 @@ namespace ts {
);
}
else {
error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
reportNonExportedMember(name, declarationName, moduleSymbol, moduleName);
}
}
}
@ -2373,6 +2373,27 @@ namespace ts {
}
}
function reportNonExportedMember(name: Identifier, declarationName: string, moduleSymbol: Symbol, moduleName: string): void {
const localSymbol = moduleSymbol.valueDeclaration.locals?.get(name.escapedText);
const exports = moduleSymbol.exports;
if (localSymbol) {
const exportedSymbol = exports && !exports.has(InternalSymbolName.ExportEquals)
? find(symbolsToArray(exports), symbol => !!getSymbolIfSameReference(symbol, localSymbol))
: undefined;
const diagnostic = exportedSymbol
? error(name, Diagnostics.Module_0_declares_1_locally_but_it_is_exported_as_2, moduleName, declarationName, symbolToString(exportedSymbol))
: error(name, Diagnostics.Module_0_declares_1_locally_but_it_is_not_exported, moduleName, declarationName);
addRelatedInfo(diagnostic,
createDiagnosticForNode(localSymbol.valueDeclaration, Diagnostics._0_is_declared_here, declarationName)
);
}
else {
error(name, Diagnostics.Module_0_has_no_exported_member_1, moduleName, declarationName);
}
}
function getTargetOfImportSpecifier(node: ImportSpecifier, dontResolveAlias: boolean): Symbol | undefined {
const resolved = getExternalModuleMember(node.parent.parent.parent, node, dontResolveAlias);
if (resolved && node.parent.parent.isTypeOnly) {

View File

@ -1764,6 +1764,14 @@
"category": "Error",
"code": 2458
},
"Module '{0}' declares '{1}' locally, but it is not exported.": {
"category": "Error",
"code": 2459
},
"Module '{0}' declares '{1}' locally, but it is exported as '{2}'.": {
"category": "Error",
"code": 2460
},
"Type '{0}' is not an array type.": {
"category": "Error",
"code": 2461

View File

@ -1,5 +1,5 @@
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(1,10): error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(1,10): error TS2459: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is not exported.
tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2459: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is not exported.
==== tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts (0 errors) ====
@ -9,7 +9,9 @@ tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts(2,10): error TS2305
==== tests/cases/compiler/es6ImportNamedImportNoNamedExports_1.ts (2 errors) ====
import { a } from "./es6ImportNamedImportNoNamedExports_0";
~
!!! error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
!!! error TS2459: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is not exported.
!!! related TS2728 tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts:1:5: 'a' is declared here.
import { a as x } from "./es6ImportNamedImportNoNamedExports_0";
~
!!! error TS2305: Module '"./es6ImportNamedImportNoNamedExports_0"' has no exported member 'a'.
!!! error TS2459: Module '"./es6ImportNamedImportNoNamedExports_0"' declares 'a' locally, but it is not exported.
!!! related TS2728 tests/cases/compiler/es6ImportNamedImportNoNamedExports_0.ts:1:5: 'a' is declared here.

View File

@ -0,0 +1,14 @@
tests/cases/compiler/b.ts(1,15): error TS2460: Module '"./a"' declares 'bar' locally, but it is exported as 'baz'.
==== tests/cases/compiler/a.ts (0 errors) ====
declare function foo(): any
declare function bar(): any;
export { foo, bar as baz };
==== tests/cases/compiler/b.ts (1 errors) ====
import { foo, bar } from "./a";
~~~
!!! error TS2460: Module '"./a"' declares 'bar' locally, but it is exported as 'baz'.
!!! related TS2728 tests/cases/compiler/a.ts:2:18: 'bar' is declared here.

View File

@ -0,0 +1,17 @@
//// [tests/cases/compiler/importNonExportedMember.ts] ////
//// [a.ts]
declare function foo(): any
declare function bar(): any;
export { foo, bar as baz };
//// [b.ts]
import { foo, bar } from "./a";
//// [a.js]
"use strict";
exports.__esModule = true;
//// [b.js]
"use strict";
exports.__esModule = true;

View File

@ -0,0 +1,17 @@
=== tests/cases/compiler/a.ts ===
declare function foo(): any
>foo : Symbol(foo, Decl(a.ts, 0, 0))
declare function bar(): any;
>bar : Symbol(bar, Decl(a.ts, 0, 27))
export { foo, bar as baz };
>foo : Symbol(foo, Decl(a.ts, 2, 8))
>bar : Symbol(bar, Decl(a.ts, 0, 27))
>baz : Symbol(baz, Decl(a.ts, 2, 13))
=== tests/cases/compiler/b.ts ===
import { foo, bar } from "./a";
>foo : Symbol(foo, Decl(b.ts, 0, 8))
>bar : Symbol(bar, Decl(b.ts, 0, 13))

View File

@ -0,0 +1,17 @@
=== tests/cases/compiler/a.ts ===
declare function foo(): any
>foo : () => any
declare function bar(): any;
>bar : () => any
export { foo, bar as baz };
>foo : () => any
>bar : () => any
>baz : () => any
=== tests/cases/compiler/b.ts ===
import { foo, bar } from "./a";
>foo : () => any
>bar : any

View File

@ -0,0 +1,14 @@
tests/cases/compiler/b.ts(1,10): error TS2459: Module '"./a"' declares 'bar' locally, but it is not exported.
==== tests/cases/compiler/a.ts (0 errors) ====
declare function foo(): any
declare function bar(): any;
export { foo };
==== tests/cases/compiler/b.ts (1 errors) ====
import { bar } from "./a";
~~~
!!! error TS2459: Module '"./a"' declares 'bar' locally, but it is not exported.
!!! related TS2728 tests/cases/compiler/a.ts:2:18: 'bar' is declared here.

View File

@ -0,0 +1,17 @@
//// [tests/cases/compiler/importNonExportedMember1.ts] ////
//// [a.ts]
declare function foo(): any
declare function bar(): any;
export { foo };
//// [b.ts]
import { bar } from "./a";
//// [a.js]
"use strict";
exports.__esModule = true;
//// [b.js]
"use strict";
exports.__esModule = true;

View File

@ -0,0 +1,14 @@
=== tests/cases/compiler/a.ts ===
declare function foo(): any
>foo : Symbol(foo, Decl(a.ts, 0, 0))
declare function bar(): any;
>bar : Symbol(bar, Decl(a.ts, 0, 27))
export { foo };
>foo : Symbol(foo, Decl(a.ts, 2, 8))
=== tests/cases/compiler/b.ts ===
import { bar } from "./a";
>bar : Symbol(bar, Decl(b.ts, 0, 8))

View File

@ -0,0 +1,14 @@
=== tests/cases/compiler/a.ts ===
declare function foo(): any
>foo : () => any
declare function bar(): any;
>bar : () => any
export { foo };
>foo : () => any
=== tests/cases/compiler/b.ts ===
import { bar } from "./a";
>bar : any

View File

@ -0,0 +1,7 @@
// @filename: a.ts
declare function foo(): any
declare function bar(): any;
export { foo, bar as baz };
// @filename: b.ts
import { foo, bar } from "./a";

View File

@ -0,0 +1,7 @@
// @filename: a.ts
declare function foo(): any
declare function bar(): any;
export { foo };
// @filename: b.ts
import { bar } from "./a";