Skip to content

Commit ce29862

Browse files
committed
fix(dart_libs): add _dart suffix only for reserved lib names.
Closes angular#871
1 parent 74795ee commit ce29862

File tree

2 files changed

+26
-8
lines changed

2 files changed

+26
-8
lines changed

tools/transpiler/src/outputgeneration/DartParseTreeWriter.js

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,19 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
516516
}
517517

518518
toString() {
519-
return "library " + this.libName + "_dart;\n" + super.toString();
519+
return "library " + this._transformLibName(this.libName) + ";\n" + super.toString();
520+
}
521+
522+
_transformLibName(libName) {
523+
var parts = libName.split('.');
524+
for (var part of parts) {
525+
if (DART_RESERVED_WORDS.indexOf(part) != -1) {
526+
return libName + '_dart';
527+
}
528+
}
529+
return libName;
520530
}
521531
}
522532

533+
// see: https://www.dartlang.org/docs/dart-up-and-running/ch02.html for a full list.
534+
const DART_RESERVED_WORDS = ['if', 'switch'];

tools/transpiler/unittest/transpilertests.js

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ describe('transpile to dart', function(){
2727
"var s1:string = \"${a}\";" +
2828
"var s2:string = '\\${a}';" +
2929
"var s3:string = '$a';");
30-
expect(result.js).toBe("library test_dart;\n" +
30+
expect(result.js).toBe("library test;\n" +
3131
"num a = 1;\n" +
3232
"String s1 = \"\\${a}\";\n" +
3333
"String s2 = '\\${a}';\n" +
@@ -39,7 +39,7 @@ describe('transpile to dart', function(){
3939
"var a:number = 1;" +
4040
"var s1:string = `$a`;" +
4141
"var s2:string = `\\$a`;");
42-
expect(result.js).toBe("library test_dart;\n" +
42+
expect(result.js).toBe("library test;\n" +
4343
"num a = 1;\n" +
4444
"String s1 = '''\\$a''';\n" +
4545
"String s2 = '''\\$a''';\n");
@@ -49,7 +49,7 @@ describe('transpile to dart', function(){
4949
var result = compiler.compile(options, "test.js",
5050
"var a:number = 1;" +
5151
"var s1:string = `${a}`;");
52-
expect(result.js).toBe("library test_dart;\n" +
52+
expect(result.js).toBe("library test;\n" +
5353
"num a = 1;\n" +
5454
"String s1 = '''${a}''';\n");
5555
});
@@ -60,25 +60,31 @@ describe('transpile to dart', function(){
6060
it('should support types without generics', function() {
6161
var result = compiler.compile(options, "test.js",
6262
"var a:List = [];");
63-
expect(result.js).toBe("library test_dart;\nList a = [];\n");
63+
expect(result.js).toBe("library test;\nList a = [];\n");
6464
});
6565

6666
it('should support one level generics', function() {
6767
var result = compiler.compile(options, "test.js",
6868
"var a:List<string> = [];");
69-
expect(result.js).toBe("library test_dart;\nList<String> a = [];\n");
69+
expect(result.js).toBe("library test;\nList<String> a = [];\n");
7070
});
7171

7272
it('should support multiple one level generics', function() {
7373
var result = compiler.compile(options, "test.js",
7474
"var a:List<A,B> = [];");
75-
expect(result.js).toBe("library test_dart;\nList<A, B> a = [];\n");
75+
expect(result.js).toBe("library test;\nList<A, B> a = [];\n");
7676
});
7777

7878
it('should support nested generics', function() {
7979
var result = compiler.compile(options, "test.js",
8080
"var a:List<A<B>> = [];");
81-
expect(result.js).toBe("library test_dart;\nList<A<B>> a = [];\n");
81+
expect(result.js).toBe("library test;\nList<A<B>> a = [];\n");
82+
});
83+
84+
it('should add dart suffix to reserved words', function() {
85+
var result = compiler.compile(options, "project/if.js",
86+
"var a;");
87+
expect(result.js).toBe("library project.if_dart;\nvar a;\n");
8288
});
8389
});
8490
});

0 commit comments

Comments
 (0)