Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(204)

Unified Diff: lib/src/js/builder.dart

Issue 1426243002: fix #379, escape of $ in template strings (Closed) Base URL: git@github.com:dart-lang/dev_compiler.git@master
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « lib/runtime/dart/core.js ('k') | pubspec.yaml » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: lib/src/js/builder.dart
diff --git a/lib/src/js/builder.dart b/lib/src/js/builder.dart
index affcffea42abfd1fce7a66b1907516ee87968bb1..93d5c67711641631f5b5a4fbdcf9e1d675ca7c59 100644
--- a/lib/src/js/builder.dart
+++ b/lib/src/js/builder.dart
@@ -299,28 +299,35 @@ class JsBuilder {
// Start by escaping the backslashes.
String escaped = value.replaceAll('\\', '\\\\');
+
+ // Replace $ in template strings:
+ // http://www.ecma-international.org/ecma-262/6.0/#sec-template-literal-lexical-components
+ var quoteReplace = quote == '`' ? r'`$' : quote;
+
// http://www.ecma-international.org/ecma-262/6.0/#sec-literals-string-literals
// > All code points may appear literally in a string literal except for the
// > closing quote code points, U+005C (REVERSE SOLIDUS),
// > U+000D (CARRIAGE RETURN), U+2028 (LINE SEPARATOR),
// > U+2029 (PARAGRAPH SEPARATOR), and U+000A (LINE FEED).
- var re = new RegExp('\n|\r|$quote|\b|\f|\t|\v|\u2028|\u2029');
+ var re = new RegExp('[\n\r$quoteReplace\b\f\t\v\u2028\u2029]');
Jennifer Messerly 2015/11/02 18:26:33 not sure why this wasn't a character class before.
escaped = escaped.replaceAllMapped(re, (m) {
switch (m.group(0)) {
case "\n" : return r"\n";
case "\r" : return r"\r";
case "\u2028": return r"\u2028";
case "\u2029": return r"\u2029";
- // Quotes are only replaced if they conflict with the containing quote
- case '"': return r'\"';
- case "'": return r"\'";
- case "`": return r"\`";
+ // Quotes and $ are only replaced if they conflict with the containing
+ // quote, see regex above.
+ case '"': return r'\"';
+ case "'": return r"\'";
+ case "`": return r"\`";
+ case r"$": return r"\$";
// TODO(jmesserly): these don't need to be escaped for correctness,
// but they are conventionally escaped.
- case "\b" : return r"\b";
- case "\t" : return r"\t";
- case "\f" : return r"\f";
- case "\v" : return r"\v";
+ case "\b": return r"\b";
+ case "\t": return r"\t";
+ case "\f": return r"\f";
+ case "\v": return r"\v";
}
});
LiteralString result = new LiteralString('$quote$escaped$quote');
« no previous file with comments | « lib/runtime/dart/core.js ('k') | pubspec.yaml » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698