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

Side by Side 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 unified diff | Download patch
« no previous file with comments | « lib/runtime/dart/core.js ('k') | pubspec.yaml » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 2 // for details. All rights reserved. Use of this source code is governed by a
3 // BSD-style license that can be found in the LICENSE file. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 // Utilities for building JS ASTs at runtime. Contains a builder class 5 // Utilities for building JS ASTs at runtime. Contains a builder class
6 // and a parser that parses part of the language. 6 // and a parser that parses part of the language.
7 7
8 part of js_ast; 8 part of js_ast;
9 9
10 10
(...skipping 281 matching lines...) Expand 10 before | Expand all | Expand 10 after
292 292
293 Template statementTemplateYielding(Node ast) { 293 Template statementTemplateYielding(Node ast) {
294 return new Template.withStatementResult(ast); 294 return new Template.withStatementResult(ast);
295 } 295 }
296 296
297 /// Creates a literal js string from [value]. 297 /// Creates a literal js string from [value].
298 LiteralString escapedString(String value, [String quote = '"']) { 298 LiteralString escapedString(String value, [String quote = '"']) {
299 // Start by escaping the backslashes. 299 // Start by escaping the backslashes.
300 String escaped = value.replaceAll('\\', '\\\\'); 300 String escaped = value.replaceAll('\\', '\\\\');
301 301
302
303 // Replace $ in template strings:
304 // http://www.ecma-international.org/ecma-262/6.0/#sec-template-literal-lexi cal-components
305 var quoteReplace = quote == '`' ? r'`$' : quote;
306
302 // http://www.ecma-international.org/ecma-262/6.0/#sec-literals-string-liter als 307 // http://www.ecma-international.org/ecma-262/6.0/#sec-literals-string-liter als
303 // > All code points may appear literally in a string literal except for the 308 // > All code points may appear literally in a string literal except for the
304 // > closing quote code points, U+005C (REVERSE SOLIDUS), 309 // > closing quote code points, U+005C (REVERSE SOLIDUS),
305 // > U+000D (CARRIAGE RETURN), U+2028 (LINE SEPARATOR), 310 // > U+000D (CARRIAGE RETURN), U+2028 (LINE SEPARATOR),
306 // > U+2029 (PARAGRAPH SEPARATOR), and U+000A (LINE FEED). 311 // > U+2029 (PARAGRAPH SEPARATOR), and U+000A (LINE FEED).
307 var re = new RegExp('\n|\r|$quote|\b|\f|\t|\v|\u2028|\u2029'); 312 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.
308 escaped = escaped.replaceAllMapped(re, (m) { 313 escaped = escaped.replaceAllMapped(re, (m) {
309 switch (m.group(0)) { 314 switch (m.group(0)) {
310 case "\n" : return r"\n"; 315 case "\n" : return r"\n";
311 case "\r" : return r"\r"; 316 case "\r" : return r"\r";
312 case "\u2028": return r"\u2028"; 317 case "\u2028": return r"\u2028";
313 case "\u2029": return r"\u2029"; 318 case "\u2029": return r"\u2029";
314 // Quotes are only replaced if they conflict with the containing quote 319 // Quotes and $ are only replaced if they conflict with the containing
315 case '"': return r'\"'; 320 // quote, see regex above.
316 case "'": return r"\'"; 321 case '"': return r'\"';
317 case "`": return r"\`"; 322 case "'": return r"\'";
323 case "`": return r"\`";
324 case r"$": return r"\$";
318 // TODO(jmesserly): these don't need to be escaped for correctness, 325 // TODO(jmesserly): these don't need to be escaped for correctness,
319 // but they are conventionally escaped. 326 // but they are conventionally escaped.
320 case "\b" : return r"\b"; 327 case "\b": return r"\b";
321 case "\t" : return r"\t"; 328 case "\t": return r"\t";
322 case "\f" : return r"\f"; 329 case "\f": return r"\f";
323 case "\v" : return r"\v"; 330 case "\v": return r"\v";
324 } 331 }
325 }); 332 });
326 LiteralString result = new LiteralString('$quote$escaped$quote'); 333 LiteralString result = new LiteralString('$quote$escaped$quote');
327 // We don't escape quotes of a different style under the assumption that the 334 // We don't escape quotes of a different style under the assumption that the
328 // string is wrapped into quotes. Verify that assumption. 335 // string is wrapped into quotes. Verify that assumption.
329 assert(result.value.codeUnitAt(0) == quote.codeUnitAt(0)); 336 assert(result.value.codeUnitAt(0) == quote.codeUnitAt(0));
330 return result; 337 return result;
331 } 338 }
332 339
333 /// Creates a literal js string from [value]. 340 /// Creates a literal js string from [value].
(...skipping 1242 matching lines...) Expand 10 before | Expand all | Expand 10 after
1576 expectCategory(RSQUARE); 1583 expectCategory(RSQUARE);
1577 return expr; 1584 return expr;
1578 } else if (acceptCategory(HASH)) { 1585 } else if (acceptCategory(HASH)) {
1579 return parseInterpolatedExpression(); 1586 return parseInterpolatedExpression();
1580 } else { 1587 } else {
1581 error('Expected property name'); 1588 error('Expected property name');
1582 return null; 1589 return null;
1583 } 1590 }
1584 } 1591 }
1585 } 1592 }
OLDNEW
« 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