diff --git a/internal/checker/checker.go b/internal/checker/checker.go index 47610ff765..3d0d2c60be 100644 --- a/internal/checker/checker.go +++ b/internal/checker/checker.go @@ -14746,8 +14746,10 @@ func (c *Checker) resolveESModuleSymbol(moduleSymbol *ast.Symbol, referencingLoc symbol := c.resolveExternalModuleSymbol(moduleSymbol, dontResolveAlias) if !dontResolveAlias && symbol != nil { if !suppressInteropError && symbol.Flags&(ast.SymbolFlagsModule|ast.SymbolFlagsVariable) == 0 && ast.GetDeclarationOfKind(symbol, ast.KindSourceFile) == nil { - compilerOptionName := core.IfElse(c.moduleKind >= core.ModuleKindES2015, "allowSyntheticDefaultImports", "esModuleInterop") - c.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) + if ast.GetDeclarationOfKind(symbol, ast.KindJSExportAssignment) == nil { + compilerOptionName := core.IfElse(c.moduleKind >= core.ModuleKindES2015, "allowSyntheticDefaultImports", "esModuleInterop") + c.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 } referenceParent := referencingLocation.Parent diff --git a/testdata/baselines/reference/compiler/commonJsModule.symbols b/testdata/baselines/reference/compiler/commonJsModule.symbols new file mode 100644 index 0000000000..d638107133 --- /dev/null +++ b/testdata/baselines/reference/compiler/commonJsModule.symbols @@ -0,0 +1,26 @@ +//// [tests/cases/compiler/commonJsModule.ts] //// + +=== shared.vars.js === +const foo = ['bar', 'baz']; +>foo : Symbol(foo, Decl(shared.vars.js, 0, 5)) + +module.exports = { +>module.exports : Symbol(export=, Decl(shared.vars.js, 0, 27)) +>module : Symbol(module.exports) +>exports : Symbol(export=, Decl(shared.vars.js, 0, 27)) + + foo, +>foo : Symbol(foo, Decl(shared.vars.js, 2, 18)) + +}; + +=== index.ts === +import { foo } from "./shared.vars"; +>foo : Symbol(foo, Decl(index.ts, 0, 8)) + +console.log(foo); +>console.log : Symbol(log, Decl(lib.dom.d.ts, --, --)) +>console : Symbol(console, Decl(lib.dom.d.ts, --, --)) +>log : Symbol(log, Decl(lib.dom.d.ts, --, --)) +>foo : Symbol(foo, Decl(index.ts, 0, 8)) + diff --git a/testdata/baselines/reference/compiler/commonJsModule.types b/testdata/baselines/reference/compiler/commonJsModule.types new file mode 100644 index 0000000000..a8f1f09ca2 --- /dev/null +++ b/testdata/baselines/reference/compiler/commonJsModule.types @@ -0,0 +1,32 @@ +//// [tests/cases/compiler/commonJsModule.ts] //// + +=== shared.vars.js === +const foo = ['bar', 'baz']; +>foo : string[] +>['bar', 'baz'] : string[] +>'bar' : "bar" +>'baz' : "baz" + +module.exports = { +>module.exports = { foo,} : { foo: string[]; } +>module.exports : { foo: string[]; } +>module : { "export=": { foo: string[]; }; } +>exports : { foo: string[]; } +>{ foo,} : { foo: string[]; } + + foo, +>foo : string[] + +}; + +=== index.ts === +import { foo } from "./shared.vars"; +>foo : string[] + +console.log(foo); +>console.log(foo) : void +>console.log : (...data: any[]) => void +>console : Console +>log : (...data: any[]) => void +>foo : string[] + diff --git a/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt b/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt index b210254502..e974eed6ea 100644 --- a/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt +++ b/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt @@ -1,4 +1,3 @@ -/index.ts(1,23): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. /index.ts(3,16): error TS2671: Cannot augment module './test' because it resolves to a non-module entity. /index.ts(11,10): error TS2749: 'Abcde' refers to a value, but is being used as a type here. Did you mean 'typeof Abcde'? @@ -13,10 +12,8 @@ Abcde }; -==== /index.ts (3 errors) ==== +==== /index.ts (2 errors) ==== import { Abcde } from "./test"; - ~~~~~~~~ -!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. declare module "./test" { ~~~~~~~~ diff --git a/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt b/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt index 4a8d07e7d0..b4c688f682 100644 --- a/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt +++ b/testdata/baselines/reference/submodule/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt @@ -1,4 +1,3 @@ -/index.ts(1,19): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. /index.ts(3,16): error TS2671: Cannot augment module './test' because it resolves to a non-module entity. /index.ts(7,3): error TS2339: Property 'toFixed' does not exist on type 'string'. @@ -8,10 +7,8 @@ a: "ok" }; -==== /index.ts (3 errors) ==== +==== /index.ts (2 errors) ==== import { a } from "./test"; - ~~~~~~~~ -!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. declare module "./test" { ~~~~~~~~ diff --git a/testdata/baselines/reference/submodule/conformance/conflictingCommonJSES2015Exports.errors.txt b/testdata/baselines/reference/submodule/conformance/conflictingCommonJSES2015Exports.errors.txt index 57cc9ec89f..c01a40ff2f 100644 --- a/testdata/baselines/reference/submodule/conformance/conflictingCommonJSES2015Exports.errors.txt +++ b/testdata/baselines/reference/submodule/conformance/conflictingCommonJSES2015Exports.errors.txt @@ -1,5 +1,4 @@ bug24934.js(2,1): error TS2309: An export assignment cannot be used in a module with other exported elements. -use.js(1,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. ==== bug24934.js (1 errors) ==== @@ -7,9 +6,7 @@ use.js(1,21): error TS2497: This module can only be referenced with ECMAScript i module.exports = { abc }; ~~~~~~~~~~~~~~~~~~~~~~~~ !!! error TS2309: An export assignment cannot be used in a module with other exported elements. -==== use.js (1 errors) ==== +==== use.js (0 errors) ==== import { abc } from './bug24934'; - ~~~~~~~~~~~~ -!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. abc(1, 2, 3); \ No newline at end of file diff --git a/testdata/baselines/reference/submodule/conformance/reExportJsFromTs.errors.txt b/testdata/baselines/reference/submodule/conformance/reExportJsFromTs.errors.txt deleted file mode 100644 index 40aeeccd69..0000000000 --- a/testdata/baselines/reference/submodule/conformance/reExportJsFromTs.errors.txt +++ /dev/null @@ -1,13 +0,0 @@ -/src/constants.ts(1,30): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. - - -==== /lib/constants.js (0 errors) ==== - module.exports = { - str: 'x', - }; - -==== /src/constants.ts (1 errors) ==== - import * as tsConstants from "../lib/constants"; - ~~~~~~~~~~~~~~~~~~ -!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. - export { tsConstants }; \ No newline at end of file diff --git a/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt.diff b/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt.diff index 4f27d9dc88..b039959a17 100644 --- a/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt.diff +++ b/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation.errors.txt.diff @@ -2,21 +2,18 @@ +++ new.jsExportMemberMergedWithModuleAugmentation.errors.txt @@= skipped -0, +0 lines =@@ -/index.ts(11,7): error TS2741: Property 'x' is missing in type '{ b: string; }' but required in type 'Abcde'. -+/index.ts(1,23): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. +/index.ts(3,16): error TS2671: Cannot augment module './test' because it resolves to a non-module entity. +/index.ts(11,10): error TS2749: 'Abcde' refers to a value, but is being used as a type here. Did you mean 'typeof Abcde'? ==== /test.js (0 errors) ==== -@@= skipped -10, +12 lines =@@ +@@= skipped -10, +11 lines =@@ Abcde }; -==== /index.ts (1 errors) ==== -+==== /index.ts (3 errors) ==== ++==== /index.ts (2 errors) ==== import { Abcde } from "./test"; -+ ~~~~~~~~ -+!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. declare module "./test" { + ~~~~~~~~ @@ -24,7 +21,7 @@ interface Abcde { b: string } } -@@= skipped -12, +16 lines =@@ +@@= skipped -12, +14 lines =@@ // Bug: the type meaning from /test.js does not // propagate through the object literal export. const x: Abcde = { b: "" }; diff --git a/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt.diff b/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt.diff index 0d0a3d51ef..a7005abc39 100644 --- a/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt.diff +++ b/testdata/baselines/reference/submoduleAccepted/compiler/jsExportMemberMergedWithModuleAugmentation2.errors.txt.diff @@ -2,7 +2,6 @@ +++ new.jsExportMemberMergedWithModuleAugmentation2.errors.txt @@= skipped -0, +0 lines =@@ -/index.ts(4,16): error TS2300: Duplicate identifier 'a'. -+/index.ts(1,19): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. +/index.ts(3,16): error TS2671: Cannot augment module './test' because it resolves to a non-module entity. /index.ts(7,3): error TS2339: Property 'toFixed' does not exist on type 'string'. -/test.js(2,3): error TS2300: Duplicate identifier 'a'. @@ -19,11 +18,8 @@ -!!! related TS6203 /index.ts:4:16: 'a' was also declared here. }; --==== /index.ts (2 errors) ==== -+==== /index.ts (3 errors) ==== + ==== /index.ts (2 errors) ==== import { a } from "./test"; -+ ~~~~~~~~ -+!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. declare module "./test" { + ~~~~~~~~ diff --git a/testdata/baselines/reference/submoduleAccepted/conformance/conflictingCommonJSES2015Exports.errors.txt.diff b/testdata/baselines/reference/submoduleAccepted/conformance/conflictingCommonJSES2015Exports.errors.txt.diff index 2d20070de6..eccafa819c 100644 --- a/testdata/baselines/reference/submoduleAccepted/conformance/conflictingCommonJSES2015Exports.errors.txt.diff +++ b/testdata/baselines/reference/submoduleAccepted/conformance/conflictingCommonJSES2015Exports.errors.txt.diff @@ -3,7 +3,6 @@ @@= skipped -0, +0 lines =@@ -bug24934.js(2,1): error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. +bug24934.js(2,1): error TS2309: An export assignment cannot be used in a module with other exported elements. -+use.js(1,21): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. ==== bug24934.js (1 errors) ==== @@ -11,12 +10,8 @@ module.exports = { abc }; - ~~~~~~ -!!! error TS2580: Cannot find name 'module'. Do you need to install type definitions for node? Try `npm i --save-dev @types/node`. --==== use.js (0 errors) ==== + ~~~~~~~~~~~~~~~~~~~~~~~~ +!!! error TS2309: An export assignment cannot be used in a module with other exported elements. -+==== use.js (1 errors) ==== + ==== use.js (0 errors) ==== import { abc } from './bug24934'; -+ ~~~~~~~~~~~~ -+!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. - abc(1, 2, 3); - \ No newline at end of file + abc(1, 2, 3); \ No newline at end of file diff --git a/testdata/baselines/reference/submoduleAccepted/conformance/reExportJsFromTs.errors.txt.diff b/testdata/baselines/reference/submoduleAccepted/conformance/reExportJsFromTs.errors.txt.diff deleted file mode 100644 index 0070c9a26c..0000000000 --- a/testdata/baselines/reference/submoduleAccepted/conformance/reExportJsFromTs.errors.txt.diff +++ /dev/null @@ -1,17 +0,0 @@ ---- old.reExportJsFromTs.errors.txt -+++ new.reExportJsFromTs.errors.txt -@@= skipped -0, +0 lines =@@ -- -+/src/constants.ts(1,30): error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. -+ -+ -+==== /lib/constants.js (0 errors) ==== -+ module.exports = { -+ str: 'x', -+ }; -+ -+==== /src/constants.ts (1 errors) ==== -+ import * as tsConstants from "../lib/constants"; -+ ~~~~~~~~~~~~~~~~~~ -+!!! error TS2497: This module can only be referenced with ECMAScript imports/exports by turning on the 'esModuleInterop' flag and referencing its default export. -+ export { tsConstants }; \ No newline at end of file diff --git a/testdata/tests/cases/compiler/commonJsModule.ts b/testdata/tests/cases/compiler/commonJsModule.ts new file mode 100644 index 0000000000..a93d343088 --- /dev/null +++ b/testdata/tests/cases/compiler/commonJsModule.ts @@ -0,0 +1,15 @@ +// @allowJs: true +// @noEmit: true +// @esModuleInterop: true +// @module: commonjs +// @Filename: shared.vars.js +const foo = ['bar', 'baz']; + +module.exports = { + foo, +}; + +// @Filename: index.ts +import { foo } from "./shared.vars"; + +console.log(foo); \ No newline at end of file