Skip to content

Commit 865f67e

Browse files
committed
Merge pull request #2197 from Microsoft/exportDefault
Complete support for ES6 modules
2 parents b228787 + 5c56684 commit 865f67e

File tree

1,411 files changed

+16268
-34973
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

1,411 files changed

+16268
-34973
lines changed

src/compiler/binder.ts

Lines changed: 33 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,13 @@ module ts {
120120
return "__new";
121121
case SyntaxKind.IndexSignature:
122122
return "__index";
123+
case SyntaxKind.ExportDeclaration:
124+
return "__export";
125+
case SyntaxKind.ExportAssignment:
126+
return "default";
127+
case SyntaxKind.FunctionDeclaration:
128+
case SyntaxKind.ClassDeclaration:
129+
return node.flags & NodeFlags.Default ? "default" : undefined;
123130
}
124131
}
125132

@@ -130,7 +137,9 @@ module ts {
130137
function declareSymbol(symbols: SymbolTable, parent: Symbol, node: Declaration, includes: SymbolFlags, excludes: SymbolFlags): Symbol {
131138
Debug.assert(!hasDynamicName(node));
132139

133-
var name = getDeclarationName(node);
140+
// The exported symbol for an export default function/class node is always named "default"
141+
var name = node.flags & NodeFlags.Default && parent ? "default" : getDeclarationName(node);
142+
134143
if (name !== undefined) {
135144
var symbol = hasProperty(symbols, name) ? symbols[name] : (symbols[name] = createSymbol(0, name));
136145
if (symbol.flags & excludes) {
@@ -145,9 +154,9 @@ module ts {
145154
: Diagnostics.Duplicate_identifier_0;
146155

147156
forEach(symbol.declarations, declaration => {
148-
file.bindDiagnostics.push(createDiagnosticForNode(declaration.name, message, getDisplayName(declaration)));
157+
file.bindDiagnostics.push(createDiagnosticForNode(declaration.name || declaration, message, getDisplayName(declaration)));
149158
});
150-
file.bindDiagnostics.push(createDiagnosticForNode(node.name, message, getDisplayName(node)));
159+
file.bindDiagnostics.push(createDiagnosticForNode(node.name || node, message, getDisplayName(node)));
151160

152161
symbol = createSymbol(0, name);
153162
}
@@ -188,7 +197,7 @@ module ts {
188197

189198
function declareModuleMember(node: Declaration, symbolKind: SymbolFlags, symbolExcludes: SymbolFlags) {
190199
var hasExportModifier = getCombinedNodeFlags(node) & NodeFlags.Export;
191-
if (symbolKind & SymbolFlags.Import) {
200+
if (symbolKind & SymbolFlags.Alias) {
192201
if (node.kind === SyntaxKind.ExportSpecifier || (node.kind === SyntaxKind.ImportEqualsDeclaration && hasExportModifier)) {
193202
declareSymbol(container.symbol.exports, container.symbol, node, symbolKind, symbolExcludes);
194203
}
@@ -324,13 +333,6 @@ module ts {
324333
}
325334
}
326335

327-
function bindExportDeclaration(node: ExportDeclaration) {
328-
if (!node.exportClause) {
329-
((<ExportContainer>container).exportStars || ((<ExportContainer>container).exportStars = [])).push(node);
330-
}
331-
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
332-
}
333-
334336
function bindFunctionOrConstructorType(node: SignatureDeclaration) {
335337
// For a given function symbol "<...>(...) => T" we want to generate a symbol identical
336338
// to the one we would get for: { <...>(...): T }
@@ -484,19 +486,34 @@ module ts {
484486
case SyntaxKind.NamespaceImport:
485487
case SyntaxKind.ImportSpecifier:
486488
case SyntaxKind.ExportSpecifier:
487-
bindDeclaration(<Declaration>node, SymbolFlags.Import, SymbolFlags.ImportExcludes, /*isBlockScopeContainer*/ false);
488-
break;
489-
case SyntaxKind.ExportDeclaration:
490-
bindExportDeclaration(<ExportDeclaration>node);
489+
bindDeclaration(<Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes, /*isBlockScopeContainer*/ false);
491490
break;
492491
case SyntaxKind.ImportClause:
493492
if ((<ImportClause>node).name) {
494-
bindDeclaration(<Declaration>node, SymbolFlags.Import, SymbolFlags.ImportExcludes, /*isBlockScopeContainer*/ false);
493+
bindDeclaration(<Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes, /*isBlockScopeContainer*/ false);
495494
}
496495
else {
497496
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
498497
}
499498
break;
499+
case SyntaxKind.ExportDeclaration:
500+
if (!(<ExportDeclaration>node).exportClause) {
501+
// All export * declarations are collected in an __export symbol
502+
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.ExportStar, 0);
503+
}
504+
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
505+
break;
506+
case SyntaxKind.ExportAssignment:
507+
if ((<ExportAssignment>node).expression.kind === SyntaxKind.Identifier) {
508+
// An export default clause with an identifier exports all meanings of that identifier
509+
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Alias, SymbolFlags.AliasExcludes);
510+
}
511+
else {
512+
// An export default clause with an expression exports a value
513+
declareSymbol(container.symbol.exports, container.symbol, <Declaration>node, SymbolFlags.Property, SymbolFlags.PropertyExcludes);
514+
}
515+
bindChildren(node, 0, /*isBlockScopeContainer*/ false);
516+
break;
500517
case SyntaxKind.SourceFile:
501518
if (isExternalModule(<SourceFile>node)) {
502519
bindAnonymousDeclaration(<SourceFile>node, SymbolFlags.ValueModule, '"' + removeFileExtension((<SourceFile>node).fileName) + '"', /*isBlockScopeContainer*/ true);

0 commit comments

Comments
 (0)