fix(transpiler/dart): re-exporting only some bindings

```
export {Foo, Bar} from ‘./foo’;
==>
export ‘./foo’ show Foo, Bar;
```
This commit is contained in:
Vojta Jina 2014-11-10 10:40:07 -08:00
parent 7a70f8f92d
commit c5153175b6
4 changed files with 45 additions and 6 deletions

View File

@ -0,0 +1,3 @@
export var Bar1 = 'BAR1';
export var Bar2 = 'BAR2';
export function Bar3() {}

View File

@ -1 +1,2 @@
export * from './foo';
export {Bar1, Bar2} from './bar';

View File

@ -1,4 +1,4 @@
import {describe, it, expect} from 'test_lib/test_lib';
import {describe, it, expect, IS_DARTIUM} from 'test_lib/test_lib';
import {Foo, Bar} from './foo';
// TODO: Does not work, as dart does not support renaming imports
@ -21,6 +21,18 @@ export function main() {
expect(exportModule.Foo).toBe('FOO');
expect(exportModule.Bar).toBe('BAR');
expect(exportModule.Bar1).toBe('BAR1');
expect(exportModule.Bar2).toBe('BAR2');
// Make sure Bar3 is not re-exported.
expect(function() {
exportModule.Bar3();
}).toThrowError(IS_DARTIUM ?
// Dart
"No top-level method 'exportModule.Bar3' declared.":
// JavaScript
'undefined is not a function'
);
expect(Type).toBeTruthy();
});

View File

@ -25,6 +25,7 @@ import {
import {ParseTreeWriter as JavaScriptParseTreeWriter, ObjectLiteralExpression} from 'traceur/src/outputgeneration/ParseTreeWriter';
import {ImportedBinding, BindingIdentifier} from 'traceur/src/syntax/trees/ParseTrees';
import {IdentifierToken} from 'traceur/src/syntax/IdentifierToken';
import {EXPORT_STAR} from 'traceur/src/syntax/trees/ParseTreeType';
export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
constructor(moduleName, outputPath) {
@ -275,12 +276,34 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
// EXPORTS
visitExportDeclaration(tree) {
if (tree.declaration.moduleSpecifier) {
this.write_('export');
this.writeSpace_();
this.visitModuleSpecifier(tree.declaration.moduleSpecifier);
this.write_(SEMI_COLON);
if (tree.declaration.specifierSet.type === EXPORT_STAR) {
// export * from './foo'
// ===>
// export './foo';
this.write_('export');
this.writeSpace_();
this.visitModuleSpecifier(tree.declaration.moduleSpecifier);
this.write_(SEMI_COLON);
} else {
// export {Foo, Bar} from './foo'
// ===>
// export './foo' show Foo, Bar;
this.write_('export');
this.writeSpace_();
this.visitModuleSpecifier(tree.declaration.moduleSpecifier);
this.writeSpace_();
this.write_('show');
this.writeSpace_();
this.writeList_(tree.declaration.specifierSet.specifiers, COMMA, false);
this.write_(SEMI_COLON);
}
} else {
// remove "export"
// Just remove the "export" keyword.
// export var x = true;
// export class Foo {}
// ===>
// var x = true;
// class Foo {}
this.writeAnnotations_(tree.annotations);
this.visitAny(tree.declaration);
}