Skip to content

Commit

Permalink
Ensure export= symbol from JavaScript always has a valueDeclaration (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
andrewbranch committed Oct 21, 2019
1 parent e8782ae commit 1d3ecc0
Show file tree
Hide file tree
Showing 5 changed files with 66 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/compiler/binder.ts
Expand Up @@ -2690,7 +2690,8 @@ namespace ts {
const flags = exportAssignmentIsAlias(node)
? SymbolFlags.Alias // An export= with an EntityNameExpression or a ClassExpression exports all meanings of that identifier or class
: SymbolFlags.Property | SymbolFlags.ExportValue | SymbolFlags.ValueModule;
declareSymbol(file.symbol.exports!, file.symbol, node, flags | SymbolFlags.Assignment, SymbolFlags.None);
const symbol = declareSymbol(file.symbol.exports!, file.symbol, node, flags | SymbolFlags.Assignment, SymbolFlags.None);
setValueDeclaration(symbol, node);
}

function bindThisPropertyAssignment(node: BindablePropertyAssignmentExpression | PropertyAccessExpression | LiteralLikeElementAccessExpression) {
Expand Down
@@ -0,0 +1,16 @@
/b.js(1,8): error TS1259: Module '"/a"' can only be default-imported using the 'esModuleInterop' flag


==== /a.js (0 errors) ====
// https://github.com/microsoft/TypeScript/issues/34481


const alias = {};
module.exports = alias;

==== /b.js (1 errors) ====
import a from "./a";
~
!!! error TS1259: Module '"/a"' can only be default-imported using the 'esModuleInterop' flag
!!! related TS2594 /a.js:5:1: This module is declared with using 'export =', and can only be used with a default import when using the 'esModuleInterop' flag.

17 changes: 17 additions & 0 deletions tests/baselines/reference/javascriptImportDefaultBadExport.symbols
@@ -0,0 +1,17 @@
=== /a.js ===
// https://github.com/microsoft/TypeScript/issues/34481


const alias = {};
>alias : Symbol(alias, Decl(a.js, 3, 5))

module.exports = alias;
>module.exports : Symbol("/a", Decl(a.js, 0, 0))
>module : Symbol(export=, Decl(a.js, 3, 17))
>exports : Symbol(export=, Decl(a.js, 3, 17))
>alias : Symbol(alias, Decl(a.js, 3, 5))

=== /b.js ===
import a from "./a";
>a : Symbol(a, Decl(b.js, 0, 6))

19 changes: 19 additions & 0 deletions tests/baselines/reference/javascriptImportDefaultBadExport.types
@@ -0,0 +1,19 @@
=== /a.js ===
// https://github.com/microsoft/TypeScript/issues/34481


const alias = {};
>alias : {}
>{} : {}

module.exports = alias;
>module.exports = alias : {}
>module.exports : {}
>module : { "/a": {}; }
>exports : {}
>alias : {}

=== /b.js ===
import a from "./a";
>a : any

12 changes: 12 additions & 0 deletions tests/cases/compiler/javascriptImportDefaultBadExport.ts
@@ -0,0 +1,12 @@
// https://github.com/microsoft/TypeScript/issues/34481

// @allowJs: true
// @checkJs: true
// @noEmit: true

// @Filename: /a.js
const alias = {};
module.exports = alias;

// @Filename: /b.js
import a from "./a";

0 comments on commit 1d3ecc0

Please sign in to comment.