Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 import 'dart:collection' show HashMap, HashSet; | 5 import 'dart:collection' show HashMap, HashSet; |
| 6 import 'dart:math' show min, max; | 6 import 'dart:math' show min, max; |
| 7 | 7 |
| 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; | 8 import 'package:analyzer/analyzer.dart' hide ConstantEvaluator; |
| 9 import 'package:analyzer/dart/ast/ast.dart'; | 9 import 'package:analyzer/dart/ast/ast.dart'; |
| 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; | 10 import 'package:analyzer/dart/ast/token.dart' show Token, TokenType; |
| (...skipping 4170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4181 | 4181 |
| 4182 @override | 4182 @override |
| 4183 JS.LiteralString visitSimpleStringLiteral(SimpleStringLiteral node) => | 4183 JS.LiteralString visitSimpleStringLiteral(SimpleStringLiteral node) => |
| 4184 js.escapedString(node.value, node.isSingleQuoted ? "'" : '"'); | 4184 js.escapedString(node.value, node.isSingleQuoted ? "'" : '"'); |
| 4185 | 4185 |
| 4186 @override | 4186 @override |
| 4187 JS.Expression visitAdjacentStrings(AdjacentStrings node) => | 4187 JS.Expression visitAdjacentStrings(AdjacentStrings node) => |
| 4188 _visitListToBinary(node.strings, '+'); | 4188 _visitListToBinary(node.strings, '+'); |
| 4189 | 4189 |
| 4190 @override | 4190 @override |
| 4191 JS.TemplateString visitStringInterpolation(StringInterpolation node) { | 4191 JS.Expression visitStringInterpolation(StringInterpolation node) { |
| 4192 // Assuming we implement toString() on our objects, we can avoid calling it | 4192 var parts = []; |
| 4193 // in most cases. Builtin types may differ though. We could handle this with | 4193 bool needsTag = false; |
| 4194 // a tagged template. | 4194 for (var interpolatePart in node.elements) { |
| 4195 return new JS.TemplateString(_visitList(node.elements)); | 4195 if (!needsTag && interpolatePart is InterpolationExpression) { |
| 4196 // If we have something that could be native or null, we need to use | |
| 4197 // dart.str | |
| 4198 var expr = interpolatePart.expression; | |
| 4199 var type = getStaticType(expr); | |
| 4200 | |
| 4201 // JS template strings will call .toString() by default. This works for | |
| 4202 // Dart types if we know they are not null or native. But if we have | |
| 4203 // null, or a native type like List, we need to go through a runtime | |
| 4204 // helper that will give the correct Dart behavior. | |
| 4205 needsTag = isNullable(expr) || | |
| 4206 type.isDynamic || | |
| 4207 type.isObject || | |
| 4208 (_extensionTypes.hasNativeSubtype(type) && | |
| 4209 type != types.stringType); | |
| 4210 } | |
| 4211 | |
| 4212 parts.add(_visit(interpolatePart)); | |
| 4213 } | |
| 4214 | |
| 4215 // Optimization: '$thing' is a common way to call toString. | |
|
sra1
2016/05/17 23:47:58
It is not a synonym.
'$thing' will throw if toStr
Jennifer Messerly
2016/05/18 00:23:42
Doh. If we want to support that then all readabili
| |
| 4216 if (parts.length == 3 && parts.first == '' && parts.last == '') { | |
| 4217 var code = needsTag ? 'dart.toString(#)' : '#.toString()'; | |
| 4218 return js.call(code, parts[1]); | |
| 4219 } | |
| 4220 | |
| 4221 var templateStr = new JS.TemplateString(parts); | |
| 4222 if (needsTag) { | |
| 4223 return new JS.TaggedTemplate(js.call('dart.str'), templateStr); | |
| 4224 } | |
| 4225 return templateStr; | |
| 4196 } | 4226 } |
| 4197 | 4227 |
| 4198 @override | 4228 @override |
| 4199 String visitInterpolationString(InterpolationString node) { | 4229 String visitInterpolationString(InterpolationString node) { |
| 4200 // TODO(jmesserly): this call adds quotes, and then we strip them off. | 4230 // TODO(jmesserly): this call adds quotes, and then we strip them off. |
| 4201 var str = js.escapedString(node.value, '`').value; | 4231 var str = js.escapedString(node.value, '`').value; |
| 4202 return str.substring(1, str.length - 1); | 4232 return str.substring(1, str.length - 1); |
| 4203 } | 4233 } |
| 4204 | 4234 |
| 4205 @override | 4235 @override |
| (...skipping 288 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 4494 } | 4524 } |
| 4495 | 4525 |
| 4496 bool isLibraryPrefix(Expression node) => | 4526 bool isLibraryPrefix(Expression node) => |
| 4497 node is SimpleIdentifier && node.staticElement is PrefixElement; | 4527 node is SimpleIdentifier && node.staticElement is PrefixElement; |
| 4498 | 4528 |
| 4499 LibraryElement _getLibrary(AnalysisContext c, String uri) => | 4529 LibraryElement _getLibrary(AnalysisContext c, String uri) => |
| 4500 c.computeLibraryElement(c.sourceFactory.forUri(uri)); | 4530 c.computeLibraryElement(c.sourceFactory.forUri(uri)); |
| 4501 | 4531 |
| 4502 bool _isDartRuntime(LibraryElement l) => | 4532 bool _isDartRuntime(LibraryElement l) => |
| 4503 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; | 4533 l.isInSdk && l.source.uri.toString() == 'dart:_runtime'; |
| OLD | NEW |