Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Emit export declarations in place #4259

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 31 additions & 62 deletions src/compiler/emitter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3090,23 +3090,28 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
}

function emitExportMemberAssignments(name: Identifier) {
if (compilerOptions.module === ModuleKind.System) {
return;
}

if (!exportEquals && exportSpecifiers && hasProperty(exportSpecifiers, name.text)) {
for (let specifier of exportSpecifiers[name.text]) {
writeLine();
function emitExportMemberAssignments(specifier: ExportSpecifier) {
if (resolver.isValueAliasDeclaration(specifier)) {
writeLine();
emitStart(specifier);
if (compilerOptions.module === ModuleKind.System) {
write(`${exportFunctionForFile}("`);
emit(specifier.name);
write(`", `);
emitExpressionIdentifier(specifier.propertyName || specifier.name);
write(")");
}
else {
emitStart(specifier.name);
emitContainingModuleName(specifier);
write(".");
emitNodeWithoutSourceMap(specifier.name);
emitEnd(specifier.name);
write(" = ");
emitExpressionIdentifier(name);
write(";");
emitExpressionIdentifier(specifier.propertyName || specifier.name);
}
emitEnd(specifier);
write(";");
}
}

Expand Down Expand Up @@ -3406,19 +3411,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
}

function emitExportVariableAssignments(node: VariableDeclaration | BindingElement) {
if (node.kind === SyntaxKind.OmittedExpression) {
return;
}
let name = node.name;
if (name.kind === SyntaxKind.Identifier) {
emitExportMemberAssignments(<Identifier>name);
}
else if (isBindingPattern(name)) {
forEach((<BindingPattern>name).elements, emitExportVariableAssignments);
}
}

function getCombinedFlagsForIdentifier(node: Identifier): NodeFlags {
if (!node.parent || (node.parent.kind !== SyntaxKind.VariableDeclaration && node.parent.kind !== SyntaxKind.BindingElement)) {
return 0;
Expand Down Expand Up @@ -3457,9 +3449,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(";");
}
}
if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile) {
forEach(node.declarationList.declarations, emitExportVariableAssignments);
}
}

function shouldEmitLeadingAndTrailingCommentsForVariableStatement(node: VariableStatement) {
Expand Down Expand Up @@ -3666,9 +3655,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}

emitSignatureAndBody(node);
if (languageVersion < ScriptTarget.ES6 && node.kind === SyntaxKind.FunctionDeclaration && node.parent === currentSourceFile && node.name) {
emitExportMemberAssignments((<FunctionDeclaration>node).name);
}
if (node.kind !== SyntaxKind.MethodDeclaration && node.kind !== SyntaxKind.MethodSignature) {
emitTrailingComments(node);
}
Expand Down Expand Up @@ -4588,10 +4574,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
if (node.kind === SyntaxKind.ClassDeclaration) {
emitExportMemberAssignment(<ClassDeclaration>node);
}

if (languageVersion < ScriptTarget.ES6 && node.parent === currentSourceFile && node.name) {
emitExportMemberAssignments(node.name);
}
}

function emitClassMemberPrefix(node: ClassLikeDeclaration, member: Node) {
Expand Down Expand Up @@ -5188,7 +5170,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitDeclarationName(node);
write(");");
}
emitExportMemberAssignments(node.name);
}
}

Expand Down Expand Up @@ -5309,7 +5290,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
emitDeclarationName(node);
write(");");
}
emitExportMemberAssignments(<Identifier>node.name);
}
}

Expand Down Expand Up @@ -5340,13 +5320,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
return node.kind === SyntaxKind.ImportDeclaration && (<ImportDeclaration>node).importClause && !!(<ImportDeclaration>node).importClause.name;
}

function emitExportImportAssignments(node: Node) {
if (isAliasSymbolDeclaration(node) && resolver.isValueAliasDeclaration(node)) {
emitExportMemberAssignments(<Identifier>(<Declaration>node).name);
}
forEachChild(node, emitExportImportAssignments);
}

function emitImportDeclaration(node: ImportDeclaration) {
if (languageVersion < ScriptTarget.ES6) {
return emitExternalImportDeclaration(node);
Expand Down Expand Up @@ -5432,7 +5405,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
write(";");
emitEnd(node);
emitExportImportAssignments(node);
emitTrailingComments(node);
}
else {
Expand All @@ -5450,7 +5422,6 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
write(getGeneratedNameForNode(<ImportDeclaration>node));
write(";");
}
emitExportImportAssignments(node);
}
}
}
Expand Down Expand Up @@ -5505,13 +5476,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi

write(";");
emitEnd(node);
emitExportImportAssignments(node);
emitTrailingComments(node);
}
}

function emitExportDeclaration(node: ExportDeclaration) {
Debug.assert(compilerOptions.module !== ModuleKind.System);
Debug.assert(compilerOptions.module !== ModuleKind.System || !!node.exportClause);

if (languageVersion < ScriptTarget.ES6) {
if (node.moduleSpecifier && (!node.exportClause || resolver.isValueAliasDeclaration(node))) {
Expand Down Expand Up @@ -5556,6 +5526,10 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
}
emitEnd(node);
}
else if (node.parent === currentSourceFile && !node.moduleSpecifier && node.exportClause) {
// export { x, y, ... }
forEach(node.exportClause.elements, emitExportMemberAssignments);
}
}
else {
if (!node.exportClause || resolver.isValueAliasDeclaration(node)) {
Expand Down Expand Up @@ -5674,7 +5648,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
externalImports.push(<ExportDeclaration>node);
}
}
else {
else if (compilerOptions.module === ModuleKind.System) {
// export { x, y }
for (let specifier of (<ExportDeclaration>node).exportClause.elements) {
let name = (specifier.propertyName || specifier.name).text;
Expand Down Expand Up @@ -5824,7 +5798,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi

for (let element of exportDecl.exportClause.elements) {
// write name of indirectly exported entry, i.e. 'export {x} from ...'
writeExportedName(element.name || element.propertyName);
writeExportedName(element.name);
}
}

Expand Down Expand Up @@ -6152,12 +6126,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
// fall-through
case SyntaxKind.ImportEqualsDeclaration:
Debug.assert(importVariableName !== "");

writeLine();
// save import into the local
write(`${importVariableName} = ${parameterName};`);
writeLine();
break;

case SyntaxKind.ExportDeclaration:
Debug.assert(importVariableName !== "");

Expand Down Expand Up @@ -6218,31 +6192,26 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, Promi
for (let i = startIndex; i < node.statements.length; ++i) {
let statement = node.statements[i];
switch (statement.kind) {
// - function declarations are not emitted because they were already hoisted
// - import declarations are not emitted since they are already handled in setters
// - export declarations with module specifiers are not emitted since they were already written in setters
// - export declarations without module specifiers are emitted preserving the order
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportDeclaration:
continue;
case SyntaxKind.ExportDeclaration:
if (!(<ExportDeclaration>statement).moduleSpecifier) {
for (let element of (<ExportDeclaration>statement).exportClause.elements) {
// write call to exporter function for every export specifier in exports list
emitExportSpecifierInSystemModule(element);
}
break;
}
// - function declarations are not emitted because they were already hoisted
// - import declarations are not emitted since they are already handled in setters
case SyntaxKind.FunctionDeclaration:
case SyntaxKind.ImportDeclaration:
continue;
case SyntaxKind.ImportEqualsDeclaration:
if (!isInternalModuleImportEqualsDeclaration(statement)) {
// - import equals declarations that import external modules are not emitted
continue;
}
// fall-though for import declarations that import internal modules
default:
writeLine();
emit(statement);
}
}
writeLine();
emit(statement);
}
decreaseIndent();
writeLine();
Expand Down
10 changes: 10 additions & 0 deletions tests/baselines/reference/amdReexportAmbientVar.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//// [amdReexportAmbientVar.ts]
/// <amd-dependency path="text!extern_view.html" name="view"/>
declare var view: string;
export {view};
export function test() { }

//// [amdReexportAmbientVar.js]
exports.view = view;
function test() { }
exports.test = test;
11 changes: 11 additions & 0 deletions tests/baselines/reference/amdReexportAmbientVar.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/amdReexportAmbientVar.ts ===
/// <amd-dependency path="text!extern_view.html" name="view"/>
declare var view: string;
>view : Symbol(view, Decl(amdReexportAmbientVar.ts, 1, 11))

export {view};
>view : Symbol(view, Decl(amdReexportAmbientVar.ts, 2, 8))

export function test() { }
>test : Symbol(test, Decl(amdReexportAmbientVar.ts, 2, 14))

11 changes: 11 additions & 0 deletions tests/baselines/reference/amdReexportAmbientVar.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/amdReexportAmbientVar.ts ===
/// <amd-dependency path="text!extern_view.html" name="view"/>
declare var view: string;
>view : string

export {view};
>view : string

export function test() { }
>test : () => void

6 changes: 3 additions & 3 deletions tests/baselines/reference/es6ExportClauseInEs5.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ var c = (function () {
}
return c;
})();
exports.c = c;
exports.c2 = c;
var m;
(function (m) {
m.x = 10;
})(m || (m = {}));
exports.instantiatedModule = m;
var x = 10;
exports.c = c;
exports.c2 = c;
exports.instantiatedModule = m;
exports.x = x;


Expand Down
16 changes: 16 additions & 0 deletions tests/baselines/reference/exportClauseEmit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//// [exportClauseEmit.ts]
var str = "Hello";
// Change str
str = "Hello World!!!";
export {str};

//// [exportClauseEmit.js]
var str = "Hello";
// Change str
str = "Hello World!!!";
exports.str = str;


//// [exportClauseEmit.d.ts]
declare var str: string;
export { str };
11 changes: 11 additions & 0 deletions tests/baselines/reference/exportClauseEmit.symbols
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
=== tests/cases/compiler/exportClauseEmit.ts ===
var str = "Hello";
>str : Symbol(str, Decl(exportClauseEmit.ts, 0, 3))

// Change str
str = "Hello World!!!";
>str : Symbol(str, Decl(exportClauseEmit.ts, 0, 3))

export {str};
>str : Symbol(str, Decl(exportClauseEmit.ts, 3, 8))

14 changes: 14 additions & 0 deletions tests/baselines/reference/exportClauseEmit.types
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
=== tests/cases/compiler/exportClauseEmit.ts ===
var str = "Hello";
>str : string
>"Hello" : string

// Change str
str = "Hello World!!!";
>str = "Hello World!!!" : string
>str : string
>"Hello World!!!" : string

export {str};
>str : string

1 change: 1 addition & 0 deletions tests/baselines/reference/exportSpecifierForAGlobal.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export function f() {


//// [b.js]
exports.X = X;
function f() {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This export wasn't there previously; correct?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the issue is reported in #4004

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah, sorry about that :)

var x;
return x;
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/exportStar-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,10 @@ define(["require", "exports"], function (require, exports) {
//// [t3.js]
define(["require", "exports"], function (require, exports) {
var x = "x";
exports.x = x;
var y = "y";
exports.y = y;
var z = "z";
exports.x = x;
exports.y = y;
exports.z = z;
});
//// [t4.js]
Expand Down
4 changes: 2 additions & 2 deletions tests/baselines/reference/exportStar.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,10 +39,10 @@ function foo() { }
exports.foo = foo;
//// [t3.js]
var x = "x";
exports.x = x;
var y = "y";
exports.y = y;
var z = "z";
exports.x = x;
exports.y = y;
exports.z = z;
//// [t4.js]
function __export(m) {
Expand Down
10 changes: 5 additions & 5 deletions tests/baselines/reference/exportsAndImports1-amd.js
Original file line number Diff line number Diff line change
Expand Up @@ -37,27 +37,27 @@ export { v, f, C, I, E, D, M, N, T, a };
//// [t1.js]
define(["require", "exports"], function (require, exports) {
var v = 1;
exports.v = v;
function f() { }
exports.f = f;
var C = (function () {
function C() {
}
return C;
})();
exports.C = C;
var E;
(function (E) {
E[E["A"] = 0] = "A";
E[E["B"] = 1] = "B";
E[E["C"] = 2] = "C";
})(E || (E = {}));
exports.E = E;
var M;
(function (M) {
})(M || (M = {}));
exports.M = M;
var a = M.x;
exports.v = v;
exports.f = f;
exports.C = C;
exports.E = E;
exports.M = M;
exports.a = a;
});
//// [t2.js]
Expand Down
Loading