fix(dart_libs): add _dart suffix only for reserved lib names.

Closes #871
This commit is contained in:
Rado Kirov 2015-03-11 18:07:29 -07:00
parent 74795eec29
commit ce29862e2f
2 changed files with 26 additions and 8 deletions

View File

@ -516,7 +516,19 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
}
toString() {
return "library " + this.libName + "_dart;\n" + super.toString();
return "library " + this._transformLibName(this.libName) + ";\n" + super.toString();
}
_transformLibName(libName) {
var parts = libName.split('.');
for (var part of parts) {
if (DART_RESERVED_WORDS.indexOf(part) != -1) {
return libName + '_dart';
}
}
return libName;
}
}
// see: https://www.dartlang.org/docs/dart-up-and-running/ch02.html for a full list.
const DART_RESERVED_WORDS = ['if', 'switch'];

View File

@ -27,7 +27,7 @@ describe('transpile to dart', function(){
"var s1:string = \"${a}\";" +
"var s2:string = '\\${a}';" +
"var s3:string = '$a';");
expect(result.js).toBe("library test_dart;\n" +
expect(result.js).toBe("library test;\n" +
"num a = 1;\n" +
"String s1 = \"\\${a}\";\n" +
"String s2 = '\\${a}';\n" +
@ -39,7 +39,7 @@ describe('transpile to dart', function(){
"var a:number = 1;" +
"var s1:string = `$a`;" +
"var s2:string = `\\$a`;");
expect(result.js).toBe("library test_dart;\n" +
expect(result.js).toBe("library test;\n" +
"num a = 1;\n" +
"String s1 = '''\\$a''';\n" +
"String s2 = '''\\$a''';\n");
@ -49,7 +49,7 @@ describe('transpile to dart', function(){
var result = compiler.compile(options, "test.js",
"var a:number = 1;" +
"var s1:string = `${a}`;");
expect(result.js).toBe("library test_dart;\n" +
expect(result.js).toBe("library test;\n" +
"num a = 1;\n" +
"String s1 = '''${a}''';\n");
});
@ -60,25 +60,31 @@ describe('transpile to dart', function(){
it('should support types without generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List = [];");
expect(result.js).toBe("library test_dart;\nList a = [];\n");
expect(result.js).toBe("library test;\nList a = [];\n");
});
it('should support one level generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<string> = [];");
expect(result.js).toBe("library test_dart;\nList<String> a = [];\n");
expect(result.js).toBe("library test;\nList<String> a = [];\n");
});
it('should support multiple one level generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<A,B> = [];");
expect(result.js).toBe("library test_dart;\nList<A, B> a = [];\n");
expect(result.js).toBe("library test;\nList<A, B> a = [];\n");
});
it('should support nested generics', function() {
var result = compiler.compile(options, "test.js",
"var a:List<A<B>> = [];");
expect(result.js).toBe("library test_dart;\nList<A<B>> a = [];\n");
expect(result.js).toBe("library test;\nList<A<B>> a = [];\n");
});
it('should add dart suffix to reserved words', function() {
var result = compiler.compile(options, "project/if.js",
"var a;");
expect(result.js).toBe("library project.if_dart;\nvar a;\n");
});
});
});