| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 part of js_ast; | 5 part of js_ast; |
| 6 | 6 |
| 7 class TemplateManager { | 7 class TemplateManager { |
| 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); | 8 Map<String, Template> expressionTemplates = new Map<String, Template>(); |
| 9 Map<String, Template> statementTemplates = new Map<String, Template>(); | 9 Map<String, Template> statementTemplates = new Map<String, Template>(); |
| 10 | 10 |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 111 arguments.keys.where((name) => !holeNames.contains(name)).join(", "); | 111 arguments.keys.where((name) => !holeNames.contains(name)).join(", "); |
| 112 throw "Template arguments has unused mappings: $unusedNames"; | 112 throw "Template arguments has unused mappings: $unusedNames"; |
| 113 } | 113 } |
| 114 if (!holeNames.every((String name) => arguments.containsKey(name))) { | 114 if (!holeNames.every((String name) => arguments.containsKey(name))) { |
| 115 String notFound = | 115 String notFound = |
| 116 holeNames.where((name) => !arguments.containsKey(name)).join(", "); | 116 holeNames.where((name) => !arguments.containsKey(name)).join(", "); |
| 117 throw "Template arguments is missing mappings for: $notFound"; | 117 throw "Template arguments is missing mappings for: $notFound"; |
| 118 } | 118 } |
| 119 return instantiator(arguments); | 119 return instantiator(arguments); |
| 120 } | 120 } |
| 121 |
| 122 /// Like [instantiate] but works with free variables. |
| 123 Node safeCreate(Map arguments) { |
| 124 if (holeNames.isEmpty) return ast; |
| 125 return instantiator(new Map.fromIterable(holeNames, value: (name) { |
| 126 var a = arguments[name]; |
| 127 return a != null ? a : new InterpolatedExpression(name); |
| 128 })); |
| 129 } |
| 121 } | 130 } |
| 122 | 131 |
| 123 /** | 132 /** |
| 124 * An Instantiator is a Function that generates a JS AST tree or List of | 133 * An Instantiator is a Function that generates a JS AST tree or List of |
| 125 * trees. [arguments] is a List for positional templates, or Map for | 134 * trees. [arguments] is a List for positional templates, or Map for |
| 126 * named templates. | 135 * named templates. |
| 127 */ | 136 */ |
| 128 typedef Instantiator(var arguments); | 137 typedef Instantiator(var arguments); |
| 129 | 138 |
| 130 | 139 |
| (...skipping 700 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 831 if (count != before) containsInterpolatedNode.add(node); | 840 if (count != before) containsInterpolatedNode.add(node); |
| 832 return null; | 841 return null; |
| 833 } | 842 } |
| 834 | 843 |
| 835 visitInterpolatedNode(InterpolatedNode node) { | 844 visitInterpolatedNode(InterpolatedNode node) { |
| 836 containsInterpolatedNode.add(node); | 845 containsInterpolatedNode.add(node); |
| 837 if (node.isNamed) holeNames.add(node.nameOrPosition); | 846 if (node.isNamed) holeNames.add(node.nameOrPosition); |
| 838 ++count; | 847 ++count; |
| 839 } | 848 } |
| 840 } | 849 } |
| OLD | NEW |