From 93c18f5396fabd284038fc45c25f5b3120793830 Mon Sep 17 00:00:00 2001 From: Alex Eagle Date: Wed, 4 Feb 2015 16:40:04 -0800 Subject: [PATCH] fix(build): Escape dollar signs in dart-transpiled string literals Escape dollar signs in string literals - dart should not interpolate them. Closes #509 --- .../outputgeneration/DartParseTreeWriter.js | 8 +++- tools/transpiler/unittest/transpilertests.js | 45 ++++++++++++++----- 2 files changed, 41 insertions(+), 12 deletions(-) diff --git a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js index 609cde6b57..d2a8a8cfde 100644 --- a/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js +++ b/tools/transpiler/src/outputgeneration/DartParseTreeWriter.js @@ -99,9 +99,15 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter { } visitTemplateLiteralPortion(tree) { - this.writeRaw_(tree.value.toString().replace(/('|")/g, "\\$&")); + this.writeRaw_(tree.value.toString() + .replace(/('|")/g, "\\$&") + .replace(/([^\\])\$/g, "$1\\\$") + .replace(/^\$/, '\\\$')); } + visitLiteralExpression(tree) { + this.write_(('' + tree.literalToken).replace(/([^\\])\$/g, "$1\\\$")); + } // FUNCTIONS // - remove the "function" keyword diff --git a/tools/transpiler/unittest/transpilertests.js b/tools/transpiler/unittest/transpilertests.js index 4316dcc204..d8b8a972ec 100644 --- a/tools/transpiler/unittest/transpilertests.js +++ b/tools/transpiler/unittest/transpilertests.js @@ -13,15 +13,38 @@ var OPTIONS = { describe('transpile to dart', function(){ // https://github.com/angular/angular/issues/509 - it('should not interpolate inside old quotes', function(){ - var result = compiler.compile(OPTIONS, "test.js", - "var a = 1;" + - "var s1 = '${a}';" + - "var s2 = `${a}`;"); - expect(result.js).toBe("library test;\n" + - "var a = 1;\n" + - // FIXME: this should escape the interpolation with backslash to fix the issue - "var s1 = '${a}';\n" + - "var s2 = '''${a}''';\n"); - }) + describe('string interpolation', function() { + it('should not interpolate inside old quotes', function(){ + var result = compiler.compile(OPTIONS, "test.js", + "var a:number = 1;" + + "var s1:string = \"${a}\";" + + "var s2:string = '\\${a}';" + + "var s3:string = '$a';"); + expect(result.js).toBe("library test;\n" + + "num a = 1;\n" + + "String s1 = \"\\${a}\";\n" + + "String s2 = '\\${a}';\n" + + "String s3 = '\\$a';\n"); + }); + + it('should not interpolate without curly braces', function() { + var result = compiler.compile(OPTIONS, "test.js", + "var a:number = 1;" + + "var s1:string = `$a`;" + + "var s2:string = `\\$a`;"); + expect(result.js).toBe("library test;\n" + + "num a = 1;\n" + + "String s1 = '''\\$a''';\n" + + "String s2 = '''\\$a''';\n"); + }); + + it('should interpolate inside template quotes', function() { + var result = compiler.compile(OPTIONS, "test.js", + "var a:number = 1;" + + "var s1:string = `${a}`;"); + expect(result.js).toBe("library test;\n" + + "num a = 1;\n" + + "String s1 = '''${a}''';\n"); + }); + }); });