8000 Fixed default import from export equals · prmdeveloper/TypeScript@3ebf0fc · GitHub
[go: up one dir, main page]

Skip to content

Commit 3ebf0fc

Browse files
committed
Fixed default import from export equals
(cherry picked from commit c4a10cf)
1 parent b760fc0 commit 3ebf0fc

File tree

8 files changed

+117
-4
lines changed

8 files changed

+117
-4
lines changed

src/compiler/binder.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -278,7 +278,7 @@ namespace ts {
278278
function declareSymbol(symbolTable: SymbolTable, parent: Symbol, node: Declaration, includes: SymbolFlags, excludes: SymbolFlags): Symbol {
279279
Debug.assert(!hasDynamicName(node));
280280

281-
const isJsModuleExport = node.kind === SyntaxKind.BinaryExpression ? getSpecialPropertyAssignmentKind(node) === SpecialPropertyAssignmentKind.ModuleExports : false;
281+
const isJsModuleExport = getSpecialPropertyAssignmentKind(node) === SpecialPropertyAssignmentKind.ModuleExports;
282282
const isDefaultExport = node.flags & NodeFlags.Default;
283283

284284
// The exported symbol for an export default function/class node is always named "default"

src/compiler/checker.ts

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -892,8 +892,12 @@ namespace ts {
892892

893893
function getTargetOfImportClause(node: ImportClause): Symbol {
894894
const moduleSymbol = resolveExternalModuleName(node, (<ImportDeclaration>node.parent).moduleSpecifier);
895+
895896
if (moduleSymbol) {
896-
const exportDefaultSymbol = resolveSymbol(moduleSymbol.exports["default"]);
897+
const exportDefaultSymbol = moduleSymbol.exports["export="] ?
898+
getPropertyOfType(getTypeOfSymbol(moduleSymbol.exports["export="]), "default") :
899+
resolveSymbol(moduleSymbol.exports["default"]);
900+
897901
if (!exportDefaultSymbol && !allowSyntheticDefaultImports) {
898902
error(node.name, Diagnostics.Module_0_has_no_default_export, symbolToString(moduleSymbol));
899903
}
@@ -967,8 +971,7 @@ namespace ts {
967971
let symbolFromVariable: Symbol;
968972
// First check if module was specified with "export=". If so, get the member from the resolved type
969973
if (moduleSymbol && moduleSymbol.exports && moduleSymbol.exports["export="]) {
970-
const members = (getTypeOfSymbol(targetSymbol) as ResolvedType).members;
971-
symbolFromVariable = members && members[name.text];
974+
symbolFromVariable = getPropertyOfType(getTypeOfSymbol(targetSymbol), name.text);
972975
}
973976
else {
974977
symbolFromVariable = getPropertyOfVariable(targetSymbol, name.text);

src/compiler/utilities.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1105,6 +1105,9 @@ namespace ts {
11051105
/// Given a BinaryExpression, returns SpecialPropertyAssignmentKind for the various kinds of property
11061106
/// assignments we treat as special in the binder
11071107
export function getSpecialPropertyAssignmentKind(expression: Node): SpecialPropertyAssignmentKind {
1108+
if (!isInJavaScriptFile(expression)) {
1109+
return SpecialPropertyAssignmentKind.None;
1110+
}
11081111
if (expression.kind !== SyntaxKind.BinaryExpression) {
11091112
return SpecialPropertyAssignmentKind.None;
11101113
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//// [tests/cases/compiler/exportEqualsDefaultProperty.ts] ////
2+
3+
//// [exp.ts]
4+
5+
var x = {
6+
"greeting": "hello, world",
7+
"default": 42
8+
};
9+
10+
export = x
11+
12+
//// [imp.ts]
13+
import foo from "./exp";
14+
foo.toExponential(2);
15+
16+
17+
//// [exp.js]
18+
"use strict";
19+
var x = {
20+
"greeting": "hello, world",
21+
"default": 42
22+
};
23+
module.exports = x;
24+
//// [imp.js]
25+
"use strict";
26+
var exp_1 = require("./exp");
27+
exp_1["default"].toExponential(2);
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
=== tests/cases/compiler/exp.ts ===
2+
3+
var x = {
4+
>x : Symbol(x, Decl(exp.ts, 1, 3))
5+
6+
"greeting": "hello, world",
7+
"default": 42
8+
};
9+
10+
export = x
11+
>x : Symbol(x, Decl(exp.ts, 1, 3))
12+
13+
=== tests/cases/compiler/imp.ts ===
14+
import foo from "./exp";
15+
>foo : Symbol(foo, Decl(imp.ts, 0, 6))
16+
17+
foo.toExponential(2);
18+
>foo.toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, --, --))
19+
>foo : Symbol(foo, Decl(imp.ts, 0, 6))
20+
>toExponential : Symbol(Number.toExponential, Decl(lib.d.ts, --, --))
21+
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
=== tests/cases/compiler/exp.ts ===
2+
3+
var x = {
4+
>x : { "greeting": string; "default": number; }
5+
>{ "greeting": "hello, world", "default": 42} : { "greeting": string; "default": number; }
6+
7+
"greeting": "hello, world",
8+
>"hello, world" : string
9+
10+
"default": 42
11+
>42 : number
12+
13+
};
14+
15+
export = x
16+
>x : { "greeting": string; "default": number; }
17+
18+
=== tests/cases/compiler/imp.ts ===
19+
import foo from "./exp";
20+
>foo : number
21+
22+
foo.toExponential(2);
23+
>foo.toExponential(2) : string
24+
>foo.toExponential : (fractionDigits?: number) => string
25+
>foo : number
26+
>toExponential : (fractionDigits?: number) => string
27+
>2 : number
28+
Lines changed: 12 additions & 0 deletions
F9D1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
2+
// @Filename: exp.ts
3+
var x = {
4+
"greeting": "hello, world",
5+
"default": 42
6+
};
7+
8+
export = x
9+
10+
// @Filename: imp.ts
11+
import foo from "./exp";
12+
foo.toExponential(2);

tests/cases/fourslash/javascriptModules22.ts

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,28 @@
55
//// function foo() { return {a: "hello, world"}; }
66
//// module.exports = foo();
77

8+
// @Filename: mod2.js
9+
//// var x = {name: 'test'};
10+
//// (function createExport(obj){
11+
//// module.exports = {
12+
//// "default": x,
13+
//// "sausages": {eggs: 2}
14+
//// };
15+
//// })();
16+
817
// @Filename: app.js
918
//// import {a} from "./mod"
19+
//// import def, {sausages} from "./mod2"
1020
//// a./**/
1121

1222
goTo.marker();
1323
verify.completionListContains('toString');
24+
25+
edit.backspace(2);
26+
edit.insert("def.");
27+
verify.completionListContains("name");
28+
29+
edit.insert("name;\nsausages.");
30+
verify.completionListContains("eggs");
31+
edit.insert("eggs;");
32+
verify.numberOfErrorsInCurrentFile(0);

0 commit comments

Comments
 (0)
0