Skip to content

Commit 312a5f9

Browse files
committed
Change message for import *-ing an export= based on module type. Update tests/baselines
1 parent dae64e2 commit 312a5f9

13 files changed

+193
-20
lines changed

src/compiler/checker.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2281,9 +2281,15 @@ namespace ts {
22812281
// combine other declarations with the module or variable (e.g. a class/module, function/module, interface/variable).
22822282
function resolveESModuleSymbol(moduleSymbol: Symbol | undefined, referencingLocation: Node, dontResolveAlias: boolean): Symbol | undefined {
22832283
const symbol = resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias);
2284+
22842285
if (!dontResolveAlias && symbol) {
22852286
if (!(symbol.flags & (SymbolFlags.Module | SymbolFlags.Variable)) && !getDeclarationOfKind(symbol, SyntaxKind.SourceFile)) {
2286-
error(referencingLocation, Diagnostics.ECMAScript_imports_can_only_reference_an_export_declaration_with_the_esModuleInterop_flag_enabled_and_by_using_default_imports);
2287+
const compilerOptionName = moduleKind >= ModuleKind.ES2015
2288+
? "allowSyntheticDefaultImports"
2289+
: "esModuleInterop";
2290+
2291+
error(referencingLocation, Diagnostics.When_writing_ECMAScript_imports_callable_export_style_modules_can_only_be_imported_by_turning_on_the_0_flag_and_using_a_default_import, compilerOptionName);
2292+
22872293
return symbol;
22882294
}
22892295

src/compiler/diagnosticMessages.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1744,7 +1744,7 @@
17441744
"category": "Error",
17451745
"code": 2496
17461746
},
1747-
"ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.": {
1747+
"When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the '{0}' flag and using a default import.": {
17481748
"category": "Error",
17491749
"code": 2497
17501750
},

tests/baselines/reference/es6ExportEqualsInterop.errors.txt

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,24 +11,24 @@ tests/cases/compiler/main.ts(33,8): error TS1192: Module '"function"' has no def
1111
tests/cases/compiler/main.ts(34,8): error TS1192: Module '"function-module"' has no default export.
1212
tests/cases/compiler/main.ts(35,8): error TS1192: Module '"class"' has no default export.
1313
tests/cases/compiler/main.ts(36,8): error TS1192: Module '"class-module"' has no default export.
14-
tests/cases/compiler/main.ts(39,21): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
15-
tests/cases/compiler/main.ts(45,21): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
16-
tests/cases/compiler/main.ts(47,21): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
14+
tests/cases/compiler/main.ts(39,21): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
15+
tests/cases/compiler/main.ts(45,21): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
16+
tests/cases/compiler/main.ts(47,21): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
1717
tests/cases/compiler/main.ts(50,1): error TS2693: 'y1' only refers to a type, but is being used as a value here.
1818
tests/cases/compiler/main.ts(56,4): error TS2339: Property 'a' does not exist on type '() => any'.
1919
tests/cases/compiler/main.ts(58,4): error TS2339: Property 'a' does not exist on type 'typeof Foo'.
2020
tests/cases/compiler/main.ts(62,10): error TS2305: Module '"interface"' has no exported member 'a'.
21-
tests/cases/compiler/main.ts(62,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
21+
tests/cases/compiler/main.ts(62,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
2222
tests/cases/compiler/main.ts(68,10): error TS2305: Module '"function"' has no exported member 'a'.
23-
tests/cases/compiler/main.ts(68,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
23+
tests/cases/compiler/main.ts(68,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
2424
tests/cases/compiler/main.ts(70,10): error TS2305: Module '"class"' has no exported member 'a'.
25-
tests/cases/compiler/main.ts(70,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
25+
tests/cases/compiler/main.ts(70,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
2626
tests/cases/compiler/main.ts(85,10): error TS2305: Module '"interface"' has no exported member 'a'.
27-
tests/cases/compiler/main.ts(85,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
27+
tests/cases/compiler/main.ts(85,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
2828
tests/cases/compiler/main.ts(91,10): error TS2305: Module '"function"' has no exported member 'a'.
29-
tests/cases/compiler/main.ts(91,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
29+
tests/cases/compiler/main.ts(91,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
3030
tests/cases/compiler/main.ts(93,10): error TS2305: Module '"class"' has no exported member 'a'.
31-
tests/cases/compiler/main.ts(93,25): error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
31+
tests/cases/compiler/main.ts(93,25): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
3232
tests/cases/compiler/main.ts(97,15): error TS2498: Module '"interface"' uses 'export =' and cannot be used with 'export *'.
3333
tests/cases/compiler/main.ts(98,15): error TS2498: Module '"variable"' uses 'export =' and cannot be used with 'export *'.
3434
tests/cases/compiler/main.ts(99,15): error TS2498: Module '"interface-variable"' uses 'export =' and cannot be used with 'export *'.
@@ -108,19 +108,19 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
108108
// namespace import
109109
import * as y1 from "interface";
110110
~~~~~~~~~~~
111-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
111+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
112112
import * as y2 from "variable";
113113
import * as y3 from "interface-variable";
114114
import * as y4 from "module";
115115
import * as y5 from "interface-module";
116116
import * as y6 from "variable-module";
117117
import * as y7 from "function";
118118
~~~~~~~~~~
119-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
119+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
120120
import * as y8 from "function-module";
121121
import * as y9 from "class";
122122
~~~~~~~
123-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
123+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
124124
import * as y0 from "class-module";
125125

126126
y1.a;
@@ -145,7 +145,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
145145
~
146146
!!! error TS2305: Module '"interface"' has no exported member 'a'.
147147
~~~~~~~~~~~
148-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
148+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
149149
import { a as a2 } from "variable";
150150
import { a as a3 } from "interface-variable";
151151
import { a as a4 } from "module";
@@ -155,13 +155,13 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
155155
~
156156
!!! error TS2305: Module '"function"' has no exported member 'a'.
157157
~~~~~~~~~~
158-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
158+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
159159
import { a as a8 } from "function-module";
160160
import { a as a9 } from "class";
161161
~
162162
!!! error TS2305: Module '"class"' has no exported member 'a'.
163163
~~~~~~~
164-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
164+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
165165
import { a as a0 } from "class-module";
166166

167167
a1;
@@ -180,7 +180,7 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
180180
~
181181
!!! error TS2305: Module '"interface"' has no exported member 'a'.
182182
~~~~~~~~~~~
183-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
183+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
184184
export { a as a2 } from "variable";
185185
export { a as a3 } from "interface-variable";
186186
export { a as a4 } from "module";
@@ -190,13 +190,13 @@ tests/cases/compiler/main.ts(106,15): error TS2498: Module '"class-module"' uses
190190
~
191191
!!! error TS2305: Module '"function"' has no exported member 'a'.
192192
~~~~~~~~~~
193-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
193+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
194194
export { a as a8 } from "function-module";
195195
export { a as a9 } from "class";
196196
~
197197
!!! error TS2305: Module '"class"' has no exported member 'a'.
198198
~~~~~~~
199-
!!! error TS2497: ECMAScript imports can only reference an 'export =' declaration with the 'esModuleInterop' flag enabled and by using default imports.
199+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
200200
export { a as a0 } from "class-module";
201201

202202
// export-star
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
tests/cases/compiler/main.ts(1,20): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
2+
3+
4+
==== tests/cases/compiler/a.ts (0 errors) ====
5+
class a { }
6+
export = a;
7+
8+
==== tests/cases/compiler/main.ts (1 errors) ====
9+
import * as a from "./a";
10+
~~~~~
11+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'esModuleInterop' flag and using a default import.
12+
a;
13+
14+
15+
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
//// [tests/cases/compiler/es6ImportEqualsExportModuleCommonJsError.ts] ////
2+
3+
//// [a.ts]
4+
class a { }
5+
export = a;
6+
7+
//// [main.ts]
8+
import * as a from "./a";
9+
a;
10+
11+
12+
13+
14+
//// [a.js]
15+
"use strict";
16+
var a = /** @class */ (function () {
17+
function a() {
18+
}
19+
return a;
20+
}());
21+
module.exports = a;
22+
//// [main.js]
23+
"use strict";
24+
exports.__esModule = true;
25+
var a = require("./a");
26+
a;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/a.ts ===
2+
class a { }
3+
>a : Symbol(a, Decl(a.ts, 0, 0))
4+
5+
export = a;
6+
>a : Symbol(a, Decl(a.ts, 0, 0))
7+
8+
=== tests/cases/compiler/main.ts ===
9+
import * as a from "./a";
10+
>a : Symbol(a, Decl(main.ts, 0, 6))
11+
12+
a;
13+
>a : Symbol(a, Decl(main.ts, 0, 6))
14+
15+
16+
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/a.ts ===
2+
class a { }
3+
>a : a
4+
5+
export = a;
6+
>a : a
7+
8+
=== tests/cases/compiler/main.ts ===
9+
import * as a from "./a";
10+
>a : typeof a
11+
12+
a;
13+
>a : typeof a
14+
15+
16+
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
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.
2+
tests/cases/compiler/main.ts(1,20): error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'allowSyntheticDefaultImports' flag and using a default import.
3+
4+
5+
==== tests/cases/compiler/a.ts (1 errors) ====
6+
class a { }
7+
export = a;
8+
~~~~~~~~~~~
9+
!!! error TS1203: Export assignment cannot be used when targeting ECMAScript modules. Consider using 'export default' or another module format instead.
10+
11+
==== tests/cases/compiler/main.ts (1 errors) ====
12+
import * as a from "./a";
13+
~~~~~
14+
!!! error TS2497: When writing ECMAScript imports, callable 'export ='-style modules can only be imported by turning on the 'allowSyntheticDefaultImports' flag and using a default import.
15+
a;
16+
17+
18+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
//// [tests/cases/compiler/es6ImportEqualsExportModuleEs2015Error.ts] ////
2+
3+
//// [a.ts]
4+
class a { }
5+
export = a;
6+
7+
//// [main.ts]
8+
import * as a from "./a";
9+
a;
10+
11+
12+
13+
14+
//// [a.js]
15+
var a = /** @class */ (function () {
16+
function a() {
17+
}
18+
return a;
19+
}());
20+
//// [main.js]
21+
import * as a from "./a";
22+
a;
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
=== tests/cases/compiler/a.ts ===
2+
class a { }
3+
>a : Symbol(a, Decl(a.ts, 0, 0))
4+
5+
export = a;
6+
>a : Symbol(a, Decl(a.ts, 0, 0))
7+
8+
=== tests/cases/compiler/main.ts ===
9+
import * as a from "./a";
10+
>a : Symbol(a, Decl(main.ts, 0, 6))
11+
12+
a;
13+
>a : Symbol(a, Decl(main.ts, 0, 6))
14+
15+
16+

0 commit comments

Comments
 (0)