From b091fa57efcddbe9169e3358482e3ec80531ea0a Mon Sep 17 00:00:00 2001 From: Mohamed Hegazy Date: Thu, 12 Mar 2015 10:11:37 -0700 Subject: [PATCH] Emit export function declaration in es6 format Conflicts: src/compiler/emitter.ts --- src/compiler/emitter.ts | 5 +- .../reference/es6ModuleFunctionDeclaration.js | 63 ++++++++++++++++ .../es6ModuleFunctionDeclaration.types | 71 +++++++++++++++++++ .../compiler/es6ModuleFunctionDeclaration.ts | 29 ++++++++ 4 files changed, 167 insertions(+), 1 deletion(-) create mode 100644 tests/baselines/reference/es6ModuleFunctionDeclaration.js create mode 100644 tests/baselines/reference/es6ModuleFunctionDeclaration.types create mode 100644 tests/cases/compiler/es6ModuleFunctionDeclaration.ts diff --git a/src/compiler/emitter.ts b/src/compiler/emitter.ts index 7c367f2f845..011a09ee64e 100644 --- a/src/compiler/emitter.ts +++ b/src/compiler/emitter.ts @@ -4344,6 +4344,9 @@ module ts { // For targeting below es6, emit functions-like declaration including arrow function using function keyword. // When targeting ES6, emit arrow function natively in ES6 by omitting function keyword and using fat arrow instead if (!shouldEmitAsArrowFunction(node)) { + if (isES6ModuleMemberDeclaration(node)) { + write("export "); + } write("function "); } @@ -4420,7 +4423,7 @@ module ts { emitExpressionFunctionBody(node, node.body); } - if (node.flags & NodeFlags.Export && !(node.flags & NodeFlags.Default)) { + if (node.flags & NodeFlags.Export && !(node.flags & NodeFlags.Default) && !isES6ModuleMemberDeclaration(node)) { writeLine(); emitStart(node); emitModuleMemberName(node); diff --git a/tests/baselines/reference/es6ModuleFunctionDeclaration.js b/tests/baselines/reference/es6ModuleFunctionDeclaration.js new file mode 100644 index 00000000000..4c1fc33647c --- /dev/null +++ b/tests/baselines/reference/es6ModuleFunctionDeclaration.js @@ -0,0 +1,63 @@ +//// [es6ModuleFunctionDeclaration.ts] +export function foo() { +} +function foo2() { +} +foo(); +foo2(); + +export module m1 { + export function foo3() { + } + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); +} +module m2 { + export function foo3() { + } + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); + m1.foo3(); +} + +//// [es6ModuleFunctionDeclaration.js] +export function foo() { +} +function foo2() { +} +foo(); +foo2(); +var m1; +(function (m1) { + function foo3() { + } + m1.foo3 = foo3; + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); +})(m1 || (m1 = {})); +export { m1 }; +var m2; +(function (m2) { + function foo3() { + } + m2.foo3 = foo3; + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); + m1.foo3(); +})(m2 || (m2 = {})); diff --git a/tests/baselines/reference/es6ModuleFunctionDeclaration.types b/tests/baselines/reference/es6ModuleFunctionDeclaration.types new file mode 100644 index 00000000000..b1252c1e8bc --- /dev/null +++ b/tests/baselines/reference/es6ModuleFunctionDeclaration.types @@ -0,0 +1,71 @@ +=== tests/cases/compiler/es6ModuleFunctionDeclaration.ts === +export function foo() { +>foo : () => void +} +function foo2() { +>foo2 : () => void +} +foo(); +>foo() : void +>foo : () => void + +foo2(); +>foo2() : void +>foo2 : () => void + +export module m1 { +>m1 : typeof m1 + + export function foo3() { +>foo3 : () => void + } + function foo4() { +>foo4 : () => void + } + foo(); +>foo() : void +>foo : () => void + + foo2(); +>foo2() : void +>foo2 : () => void + + foo3(); +>foo3() : void +>foo3 : () => void + + foo4(); +>foo4() : void +>foo4 : () => void +} +module m2 { +>m2 : typeof m2 + + export function foo3() { +>foo3 : () => void + } + function foo4() { +>foo4 : () => void + } + foo(); +>foo() : void +>foo : () => void + + foo2(); +>foo2() : void +>foo2 : () => void + + foo3(); +>foo3() : void +>foo3 : () => void + + foo4(); +>foo4() : void +>foo4 : () => void + + m1.foo3(); +>m1.foo3() : void +>m1.foo3 : () => void +>m1 : typeof m1 +>foo3 : () => void +} diff --git a/tests/cases/compiler/es6ModuleFunctionDeclaration.ts b/tests/cases/compiler/es6ModuleFunctionDeclaration.ts new file mode 100644 index 00000000000..82eebb1580e --- /dev/null +++ b/tests/cases/compiler/es6ModuleFunctionDeclaration.ts @@ -0,0 +1,29 @@ +// @target: ES6 +export function foo() { +} +function foo2() { +} +foo(); +foo2(); + +export module m1 { + export function foo3() { + } + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); +} +module m2 { + export function foo3() { + } + function foo4() { + } + foo(); + foo2(); + foo3(); + foo4(); + m1.foo3(); +} \ No newline at end of file