Skip to content

Commit 93c18f5

Browse files
committed
fix(build): Escape dollar signs in dart-transpiled string literals
Escape dollar signs in string literals - dart should not interpolate them. Closes angular#509
1 parent 9f6b6cc commit 93c18f5

File tree

2 files changed

+41
-12
lines changed

2 files changed

+41
-12
lines changed

tools/transpiler/src/outputgeneration/DartParseTreeWriter.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,9 +99,15 @@ export class DartParseTreeWriter extends JavaScriptParseTreeWriter {
9999
}
100100

101101
visitTemplateLiteralPortion(tree) {
102-
this.writeRaw_(tree.value.toString().replace(/('|")/g, "\\$&"));
102+
this.writeRaw_(tree.value.toString()
103+
.replace(/('|")/g, "\\$&")
104+
.replace(/([^\\])\$/g, "$1\\\$")
105+
.replace(/^\$/, '\\\$'));
103106
}
104107

108+
visitLiteralExpression(tree) {
109+
this.write_(('' + tree.literalToken).replace(/([^\\])\$/g, "$1\\\$"));
110+
}
105111

106112
// FUNCTIONS
107113
// - remove the "function" keyword

tools/transpiler/unittest/transpilertests.js

Lines changed: 34 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,38 @@ var OPTIONS = {
1313
describe('transpile to dart', function(){
1414

1515
// https://github.com/angular/angular/issues/509
16-
it('should not interpolate inside old quotes', function(){
17-
var result = compiler.compile(OPTIONS, "test.js",
18-
"var a = 1;" +
19-
"var s1 = '${a}';" +
20-
"var s2 = `${a}`;");
21-
expect(result.js).toBe("library test;\n" +
22-
"var a = 1;\n" +
23-
// FIXME: this should escape the interpolation with backslash to fix the issue
24-
"var s1 = '${a}';\n" +
25-
"var s2 = '''${a}''';\n");
26-
})
16+
describe('string interpolation', function() {
17+
it('should not interpolate inside old quotes', function(){
18+
var result = compiler.compile(OPTIONS, "test.js",
19+
"var a:number = 1;" +
20+
"var s1:string = \"${a}\";" +
21+
"var s2:string = '\\${a}';" +
22+
"var s3:string = '$a';");
23+
expect(result.js).toBe("library test;\n" +
24+
"num a = 1;\n" +
25+
"String s1 = \"\\${a}\";\n" +
26+
"String s2 = '\\${a}';\n" +
27+
"String s3 = '\\$a';\n");
28+
});
29+
30+
it('should not interpolate without curly braces', function() {
31+
var result = compiler.compile(OPTIONS, "test.js",
32+
"var a:number = 1;" +
33+
"var s1:string = `$a`;" +
34+
"var s2:string = `\\$a`;");
35+
expect(result.js).toBe("library test;\n" +
36+
"num a = 1;\n" +
37+
"String s1 = '''\\$a''';\n" +
38+
"String s2 = '''\\$a''';\n");
39+
});
40+
41+
it('should interpolate inside template quotes', function() {
42+
var result = compiler.compile(OPTIONS, "test.js",
43+
"var a:number = 1;" +
44+
"var s1:string = `${a}`;");
45+
expect(result.js).toBe("library test;\n" +
46+
"num a = 1;\n" +
47+
"String s1 = '''${a}''';\n");
48+
});
49+
});
2750
});

0 commit comments

Comments
 (0)