diff --git a/src/compiler/checker.ts b/src/compiler/checker.ts index 41f4d8bef68..7d369317b7e 100644 --- a/src/compiler/checker.ts +++ b/src/compiler/checker.ts @@ -2408,11 +2408,18 @@ namespace ts { // combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable). function resolveESModuleSymbol(moduleSymbol: Symbol | undefined, referencingLocation: Node, dontResolveAlias: boolean): Symbol | undefined { const symbol = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias); + if (!dontResolveAlias && symbol) { if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable)) && !getDeclarationOfKind(symbol, SyntaxKind.SourceFile)) { - error(referencingLocation, Diagnostics.Module_0_resolves_to_a_non_module_entity_and_cannot_be_imported_using_this_construct, symbolToString(moduleSymbol!)); + const compilerOptionName = moduleKind >= ModuleKind.ES2015 + ? "allowSyntheticDefaultImports" + : "esModuleInterop"; + + error(referencingLocation, Diagnostics.This_module_can_only_be_referenced_with_ECMAScript_imports_Slashexports_by_turning_on_the_0_flag_and_referencing_its_default_export, compilerOptionName); + return symbol; } + if (compilerOptions.esModuleInterop) { const referenceParent = referencingLocation.parent; if ( diff --git a/src/compiler/diagnosticMessages.json b/src/compiler/diagnosticMessages.json index 57f4c4c5611..9a918507dc4 100644 --- a/src/compiler/diagnosticMessages.json +++ b/src/compiler/diagnosticMessages.json @@ -1776,7 +1776,7 @@ "category": "Error", "code": 2496 }, - "Module '{0}' resolves to a non-module entity and cannot be imported using this construct.": { + "This module can only be referenced with ECMAScript imports/exports by turning on the '{0}' flag and referencing its default export.": { "category": "Error", "code": 2497 }, diff --git a/tests/baselines/reference/es6ExportEqualsInterop.errors.txt b/tests/baselines/reference/es6ExportEqualsInterop.errors.txt index 4675136d081..5b8e83b3cca 100644 --- a/tests/baselines/reference/es6ExportEqualsInterop.errors.txt +++ b/tests/baselines/reference/es6ExportEqualsInterop.errors.txt @@ -11,24 +11,24 @@ tests/cases/compiler/main.ts(33,8): error TS1192: Module '"function"' has no def tests/cases/compiler/main.ts(34,8): error TS1192: Module '"function-module"' has no default export. tests/cases/compiler/main.ts(35,8): error TS1192: Module '"class"' has no default export. tests/cases/compiler/main.ts(36,8): error TS1192: Module '"class-module"' has no default export. -tests/cases/compiler/main.ts(39,21): error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. -tests/cases/compiler/main.ts(45,21): error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. -tests/cases/compiler/main.ts(47,21): error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(39,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. +tests/cases/compiler/main.ts(45,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. +tests/cases/compiler/main.ts(47,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(50,1): error TS2693: 'y1' only refers to a type, but is being used as a value here. tests/cases/compiler/main.ts(56,4): error TS2339: Property 'a' does not exist on type '() => any'. tests/cases/compiler/main.ts(58,4): error TS2339: Property 'a' does not exist on type 'typeof Foo'. tests/cases/compiler/main.ts(62,10): error TS2305: Module '"interface"' has no exported member 'a'. -tests/cases/compiler/main.ts(62,25): error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(62,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(68,10): error TS2305: Module '"function"' has no exported member 'a'. -tests/cases/compiler/main.ts(68,25): error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(68,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(70,10): error TS2305: Module '"class"' has no exported member 'a'. -tests/cases/compiler/main.ts(70,25): error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(70,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(85,10): error TS2305: Module '"interface"' has no exported member 'a'. -tests/cases/compiler/main.ts(85,25): error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(85,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(91,10): error TS2305: Module '"function"' has no exported member 'a'. -tests/cases/compiler/main.ts(91,25): error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(91,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(93,10): error TS2305: Module '"class"' has no exported member 'a'. -tests/cases/compiler/main.ts(93,25): error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +tests/cases/compiler/main.ts(93,25): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. tests/cases/compiler/main.ts(97,15): error TS2498: Module '"interface"' uses 'export =' and cannot be used with 'export *'. tests/cases/compiler/main.ts(98,15): error TS2498: Module '"variable"' uses 'export =' and cannot be used with 'export *'. tests/cases/compiler/main.ts(99,15): error TS2498: Module '"interface-variable"' uses 'export =' and cannot be used with 'export *'. @@ -108,7 +108,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses // namespace import import * as y1 from "interface"; ~~~~~~~~~~~ -!!! error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import * as y2 from "variable"; import * as y3 from "interface-variable"; import * as y4 from "module"; @@ -116,11 +116,11 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses import * as y6 from "variable-module"; import * as y7 from "function"; ~~~~~~~~~~ -!!! error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import * as y8 from "function-module"; import * as y9 from "class"; ~~~~~~~ -!!! error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import * as y0 from "class-module"; y1.a; @@ -145,7 +145,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses ~ !!! error TS2305: Module '"interface"' has no exported member 'a'. ~~~~~~~~~~~ -!!! error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import { a as a2 } from "variable"; import { a as a3 } from "interface-variable"; import { a as a4 } from "module"; @@ -155,13 +155,13 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses ~ !!! error TS2305: Module '"function"' has no exported member 'a'. ~~~~~~~~~~ -!!! error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import { a as a8 } from "function-module"; import { a as a9 } from "class"; ~ !!! error TS2305: Module '"class"' has no exported member 'a'. ~~~~~~~ -!!! error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. import { a as a0 } from "class-module"; a1; @@ -180,7 +180,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses ~ !!! error TS2305: Module '"interface"' has no exported member 'a'. ~~~~~~~~~~~ -!!! error TS2497: Module '"interface"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. export { a as a2 } from "variable"; export { a as a3 } from "interface-variable"; export { a as a4 } from "module"; @@ -190,13 +190,13 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses ~ !!! error TS2305: Module '"function"' has no exported member 'a'. ~~~~~~~~~~ -!!! error TS2497: Module '"function"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. export { a as a8 } from "function-module"; export { a as a9 } from "class"; ~ !!! error TS2305: Module '"class"' has no exported member 'a'. ~~~~~~~ -!!! error TS2497: Module '"class"' resolves to a non-module entity and cannot be imported using this construct. +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. export { a as a0 } from "class-module"; // export-star diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.errors.txt b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.errors.txt new file mode 100644 index 00000000000..3d8301d3ca1 --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.errors.txt @@ -0,0 +1,15 @@ +tests/cases/compiler/main.ts(1,20): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. + + +==== tests/cases/compiler/a.ts (0 errors) ==== + class a { } + export = a; + +==== tests/cases/compiler/main.ts (1 errors) ==== + import * as a from "./a"; + ~~~~~ +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. + a; + + + \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.js b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.js new file mode 100644 index 00000000000..37f127721ce --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.js @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/es6ImportEqualsExportModuleCommonJsError.ts] //// + +//// [a.ts] +class a { } +export = a; + +//// [main.ts] +import * as a from "./a"; +a; + + + + +//// [a.js] +"use strict"; +var a = /** @class */ (function () { + function a() { + } + return a; +}()); +module.exports = a; +//// [main.js] +"use strict"; +exports.__esModule = true; +var a = require("./a"); +a; diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.symbols b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.symbols new file mode 100644 index 00000000000..071f2fb5725 --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.symbols @@ -0,0 +1,16 @@ +=== tests/cases/compiler/a.ts === +class a { } +>a : Symbol(a, Decl(a.ts, 0, 0)) + +export = a; +>a : Symbol(a, Decl(a.ts, 0, 0)) + +=== tests/cases/compiler/main.ts === +import * as a from "./a"; +>a : Symbol(a, Decl(main.ts, 0, 6)) + +a; +>a : Symbol(a, Decl(main.ts, 0, 6)) + + + diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.types b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.types new file mode 100644 index 00000000000..652d7d6241b --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleCommonJsError.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/a.ts === +class a { } +>a : a + +export = a; +>a : a + +=== tests/cases/compiler/main.ts === +import * as a from "./a"; +>a : typeof a + +a; +>a : typeof a + + + diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.errors.txt b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.errors.txt new file mode 100644 index 00000000000..94c05e068f5 --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.errors.txt @@ -0,0 +1,18 @@ +tests/cases/compiler/a.ts(2,1): error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. +tests/cases/compiler/main.ts(1,20): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export. + + +==== tests/cases/compiler/a.ts (1 errors) ==== + class a { } + export = a; + ~~~~~~~~~~~ +!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead. + +==== tests/cases/compiler/main.ts (1 errors) ==== + import * as a from "./a"; + ~~~~~ +!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'allowSyntheticDefaultImports' flag and referencing its default export. + a; + + + \ No newline at end of file diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js new file mode 100644 index 00000000000..603f158946e --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.js @@ -0,0 +1,22 @@ +//// [tests/cases/compiler/es6ImportEqualsExportModuleEs2015Error.ts] //// + +//// [a.ts] +class a { } +export = a; + +//// [main.ts] +import * as a from "./a"; +a; + + + + +//// [a.js] +var a = /** @class */ (function () { + function a() { + } + return a; +}()); +//// [main.js] +import * as a from "./a"; +a; diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.symbols b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.symbols new file mode 100644 index 00000000000..071f2fb5725 --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.symbols @@ -0,0 +1,16 @@ +=== tests/cases/compiler/a.ts === +class a { } +>a : Symbol(a, Decl(a.ts, 0, 0)) + +export = a; +>a : Symbol(a, Decl(a.ts, 0, 0)) + +=== tests/cases/compiler/main.ts === +import * as a from "./a"; +>a : Symbol(a, Decl(main.ts, 0, 6)) + +a; +>a : Symbol(a, Decl(main.ts, 0, 6)) + + + diff --git a/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.types b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.types new file mode 100644 index 00000000000..652d7d6241b --- /dev/null +++ b/tests/baselines/reference/es6ImportEqualsExportModuleEs2015Error.types @@ -0,0 +1,16 @@ +=== tests/cases/compiler/a.ts === +class a { } +>a : a + +export = a; +>a : a + +=== tests/cases/compiler/main.ts === +import * as a from "./a"; +>a : typeof a + +a; +>a : typeof a + + + diff --git a/tests/cases/compiler/es6ImportEqualsExportModuleCommonJsError.ts b/tests/cases/compiler/es6ImportEqualsExportModuleCommonJsError.ts new file mode 100644 index 00000000000..577e606c249 --- /dev/null +++ b/tests/cases/compiler/es6ImportEqualsExportModuleCommonJsError.ts @@ -0,0 +1,11 @@ +// @module: commonjs + +// @filename: a.ts +class a { } +export = a; + +// @filename: main.ts +import * as a from "./a"; +a; + + diff --git a/tests/cases/compiler/es6ImportEqualsExportModuleEs2015Error.ts b/tests/cases/compiler/es6ImportEqualsExportModuleEs2015Error.ts new file mode 100644 index 00000000000..a5505b0e90e --- /dev/null +++ b/tests/cases/compiler/es6ImportEqualsExportModuleEs2015Error.ts @@ -0,0 +1,11 @@ +// @module: es2015 + +// @filename: a.ts +class a { } +export = a; + +// @filename: main.ts +import * as a from "./a"; +a; + +