From ce29862e2fc882ed4f39e8fbd9b1b8021941e085 Mon Sep 17 00:00:00 2001 From: Rado Kirov Date: Wed, 11 Mar 2015 18:07:29 -0700 Subject: [PATCH] fix(dart_libs): add _dart suffix only for reserved lib names. Closes #871 --- .../outputgeneration/DartParseTreeWriter.js | 14 ++++++++++++- tools/transpiler/unittest/transpilertests.js | 20 ++++++++++++------- 2 files changed, 26 insertions(+), 8 deletions(-) diff --git a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js index 17e51a51ba..95d5abab60 100644 --- a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js +++ b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js @@ -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']; diff --git a/tools/transpiler/unittest/transpilertests.js b/tools/transpiler/unittest/transpilertests.js index fa95e1f895..52d8dd91d7 100644 --- a/tools/transpiler/unittest/transpilertests.js +++ b/tools/transpiler/unittest/transpilertests.js @@ -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 = [];"); - expect(result.js).toBe("library test_dart;\nList a = [];\n"); + expect(result.js).toBe("library test;\nList a = [];\n"); }); it('should support multiple one level 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 nested 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 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"); }); }); });