fix(build): Escape dollar signs in dart-transpiled string literals

Escape dollar signs in string literals - dart should not interpolate them.
Closes #509
This commit is contained in:
Alex Eagle 2015-02-04 16:40:04 -08:00
parent 9f6b6cc50c
commit 93c18f5396
2 changed files with 41 additions and 12 deletions

View File

@ -99,9 +99,15 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
} }
visitTemplateLiteralPortion(tree) { 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 // FUNCTIONS
// - remove the "function" keyword // - remove the "function" keyword

View File

@ -13,15 +13,38 @@ var OPTIONS = {
describe('transpile to dart', function(){ describe('transpile to dart', function(){
// https://github.com/angular/angular/issues/509 // https://github.com/angular/angular/issues/509
describe('string interpolation', function() {
it('should not interpolate inside old quotes', function(){ it('should not interpolate inside old quotes', function(){
var result = compiler.compile(OPTIONS, "test.js", var result = compiler.compile(OPTIONS, "test.js",
"var a = 1;" + "var a:number = 1;" +
"var s1 = '${a}';" + "var s1:string = \"${a}\";" +
"var s2 = `${a}`;"); "var s2:string = '\\${a}';" +
"var s3:string = '$a';");
expect(result.js).toBe("library test;\n" + expect(result.js).toBe("library test;\n" +
"var a = 1;\n" + "num a = 1;\n" +
// FIXME: this should escape the interpolation with backslash to fix the issue "String s1 = \"\\${a}\";\n" +
"var s1 = '${a}';\n" + "String s2 = '\\${a}';\n" +
"var 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");
});
});
}); });