Skip to content

Commit

Permalink
throw an error when no default export present (microsoft#35815)
Browse files Browse the repository at this point in the history
  • Loading branch information
a-tarasyuk authored and weswigham committed Jan 14, 2020
1 parent 91ffa1c commit daf786e
Show file tree
Hide file tree
Showing 44 changed files with 548 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/compiler/checker.ts
Expand Up @@ -2322,13 +2322,18 @@ namespace ts {
else {
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.escapedText);
}

// if symbolFromVariable is export - get its final target
symbolFromVariable = resolveSymbol(symbolFromVariable, dontResolveAlias);

let symbolFromModule = getExportOfModule(targetSymbol, name.escapedText, dontResolveAlias);
// If the export member we're looking for is default, and there is no real default but allowSyntheticDefaultImports is on, return the entire module as the default
if (!symbolFromModule && allowSyntheticDefaultImports && name.escapedText === InternalSymbolName.Default) {
symbolFromModule = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
if (symbolFromModule === undefined && name.escapedText === InternalSymbolName.Default) {
const file = find(moduleSymbol.declarations, isSourceFile);
if (canHaveSyntheticDefault(file, moduleSymbol, dontResolveAlias)) {
symbolFromModule = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) || resolveSymbol(moduleSymbol, dontResolveAlias);
}
}

const symbol = symbolFromModule && symbolFromVariable && symbolFromModule !== symbolFromVariable ?
combineValueAndTypeSymbols(symbolFromVariable, symbolFromModule) :
symbolFromModule || symbolFromVariable;
Expand Down
11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault.errors.txt
@@ -0,0 +1,11 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.ts (0 errors) ====
export const b = null;

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.
20 changes: 20 additions & 0 deletions tests/baselines/reference/reexportMissingDefault.js
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/reexportMissingDefault.ts] ////

//// [b.ts]
export const b = null;

//// [a.ts]
export { b } from "./b";
export { default } from "./b";

//// [b.js]
"use strict";
exports.__esModule = true;
exports.b = null;
//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.b = b_1.b;
var b_2 = require("./b");
exports["default"] = b_2["default"];
11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault.symbols
@@ -0,0 +1,11 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : Symbol(b, Decl(b.ts, 0, 12))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default } from "./b";
>default : Symbol(default, Decl(a.ts, 1, 8))

12 changes: 12 additions & 0 deletions tests/baselines/reference/reexportMissingDefault.types
@@ -0,0 +1,12 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : any
>null : null

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : any

export { default } from "./b";
>default : any

12 changes: 12 additions & 0 deletions tests/baselines/reference/reexportMissingDefault1.errors.txt
@@ -0,0 +1,12 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.ts (0 errors) ====
export const b = null;

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.

21 changes: 21 additions & 0 deletions tests/baselines/reference/reexportMissingDefault1.js
@@ -0,0 +1,21 @@
//// [tests/cases/compiler/reexportMissingDefault1.ts] ////

//// [b.ts]
export const b = null;

//// [a.ts]
export { b } from "./b";
export { default } from "./b";


//// [b.js]
"use strict";
exports.__esModule = true;
exports.b = null;
//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.b = b_1.b;
var b_2 = require("./b");
exports["default"] = b_2["default"];
11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault1.symbols
@@ -0,0 +1,11 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : Symbol(b, Decl(b.ts, 0, 12))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default } from "./b";
>default : Symbol(default, Decl(a.ts, 1, 8))

12 changes: 12 additions & 0 deletions tests/baselines/reference/reexportMissingDefault1.types
@@ -0,0 +1,12 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : any
>null : null

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : any

export { default } from "./b";
>default : any

11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault2.errors.txt
@@ -0,0 +1,11 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.ts (0 errors) ====
export const b = null;

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.
20 changes: 20 additions & 0 deletions tests/baselines/reference/reexportMissingDefault2.js
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/reexportMissingDefault2.ts] ////

//// [b.ts]
export const b = null;

//// [a.ts]
export { b } from "./b";
export { default } from "./b";

//// [b.js]
"use strict";
exports.__esModule = true;
exports.b = null;
//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.b = b_1.b;
var b_2 = require("./b");
exports["default"] = b_2["default"];
11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault2.symbols
@@ -0,0 +1,11 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : Symbol(b, Decl(b.ts, 0, 12))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default } from "./b";
>default : Symbol(default, Decl(a.ts, 1, 8))

12 changes: 12 additions & 0 deletions tests/baselines/reference/reexportMissingDefault2.types
@@ -0,0 +1,12 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : any
>null : null

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : any

export { default } from "./b";
>default : any

11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault3.errors.txt
@@ -0,0 +1,11 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.ts (0 errors) ====
export const b = null;

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default as a } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.
20 changes: 20 additions & 0 deletions tests/baselines/reference/reexportMissingDefault3.js
@@ -0,0 +1,20 @@
//// [tests/cases/compiler/reexportMissingDefault3.ts] ////

//// [b.ts]
export const b = null;

//// [a.ts]
export { b } from "./b";
export { default as a } from "./b";

//// [b.js]
"use strict";
exports.__esModule = true;
exports.b = null;
//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.b = b_1.b;
var b_2 = require("./b");
exports.a = b_2["default"];
11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault3.symbols
@@ -0,0 +1,11 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : Symbol(b, Decl(b.ts, 0, 12))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default as a } from "./b";
>a : Symbol(a, Decl(a.ts, 1, 8))

13 changes: 13 additions & 0 deletions tests/baselines/reference/reexportMissingDefault3.types
@@ -0,0 +1,13 @@
=== tests/cases/compiler/b.ts ===
export const b = null;
>b : any
>null : null

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : any

export { default as a } from "./b";
>default : any
>a : any

12 changes: 12 additions & 0 deletions tests/baselines/reference/reexportMissingDefault4.errors.txt
@@ -0,0 +1,12 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.d.ts (0 errors) ====
declare var b: number;
export { b };

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.
17 changes: 17 additions & 0 deletions tests/baselines/reference/reexportMissingDefault4.js
@@ -0,0 +1,17 @@
//// [tests/cases/compiler/reexportMissingDefault4.ts] ////

//// [b.d.ts]
declare var b: number;
export { b };

//// [a.ts]
export { b } from "./b";
export { default } from "./b";

//// [a.js]
"use strict";
exports.__esModule = true;
var b_1 = require("./b");
exports.b = b_1.b;
var b_2 = require("./b");
exports["default"] = b_2["default"];
14 changes: 14 additions & 0 deletions tests/baselines/reference/reexportMissingDefault4.symbols
@@ -0,0 +1,14 @@
=== tests/cases/compiler/b.d.ts ===
declare var b: number;
>b : Symbol(b, Decl(b.d.ts, 0, 11))

export { b };
>b : Symbol(b, Decl(b.d.ts, 1, 8))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default } from "./b";
>default : Symbol(default, Decl(a.ts, 1, 8))

14 changes: 14 additions & 0 deletions tests/baselines/reference/reexportMissingDefault4.types
@@ -0,0 +1,14 @@
=== tests/cases/compiler/b.d.ts ===
declare var b: number;
>b : number

export { b };
>b : number

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : number

export { default } from "./b";
>default : any

29 changes: 29 additions & 0 deletions tests/baselines/reference/reexportMissingDefault5.js
@@ -0,0 +1,29 @@
//// [tests/cases/compiler/reexportMissingDefault5.ts] ////

//// [b.d.ts]
declare var b: number;
export { b };

//// [a.ts]
export { b } from "./b";
export { default as Foo } from "./b";

//// [a.js]
System.register(["./b"], function (exports_1, context_1) {
"use strict";
var __moduleName = context_1 && context_1.id;
return {
setters: [
function (b_1_1) {
exports_1({
"b": b_1_1["b"]
});
exports_1({
"Foo": b_1_1["default"]
});
}
],
execute: function () {
}
};
});
15 changes: 15 additions & 0 deletions tests/baselines/reference/reexportMissingDefault5.symbols
@@ -0,0 +1,15 @@
=== tests/cases/compiler/b.d.ts ===
declare var b: number;
>b : Symbol(b, Decl(b.d.ts, 0, 11))

export { b };
>b : Symbol(b, Decl(b.d.ts, 1, 8))

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : Symbol(b, Decl(a.ts, 0, 8))

export { default as Foo } from "./b";
>default : Symbol("tests/cases/compiler/b", Decl(b.d.ts, 0, 0))
>Foo : Symbol(Foo, Decl(a.ts, 1, 8))

15 changes: 15 additions & 0 deletions tests/baselines/reference/reexportMissingDefault5.types
@@ -0,0 +1,15 @@
=== tests/cases/compiler/b.d.ts ===
declare var b: number;
>b : number

export { b };
>b : number

=== tests/cases/compiler/a.ts ===
export { b } from "./b";
>b : number

export { default as Foo } from "./b";
>default : typeof import("tests/cases/compiler/b")
>Foo : typeof import("tests/cases/compiler/b")

11 changes: 11 additions & 0 deletions tests/baselines/reference/reexportMissingDefault6.errors.txt
@@ -0,0 +1,11 @@
tests/cases/compiler/a.ts(2,10): error TS2305: Module '"./b"' has no exported member 'default'.


==== tests/cases/compiler/b.ts (0 errors) ====
export const b = null;

==== tests/cases/compiler/a.ts (1 errors) ====
export { b } from "./b";
export { default } from "./b";
~~~~~~~
!!! error TS2305: Module '"./b"' has no exported member 'default'.

0 comments on commit daf786e

Please sign in to comment.