| 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 103 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 114 arguments.keys.where((name) => !holeNames.contains(name)).join(", "); | 114 arguments.keys.where((name) => !holeNames.contains(name)).join(", "); |
| 115 throw "Template arguments has unused mappings: $unusedNames"; | 115 throw "Template arguments has unused mappings: $unusedNames"; |
| 116 } | 116 } |
| 117 if (!holeNames.every((String name) => arguments.containsKey(name))) { | 117 if (!holeNames.every((String name) => arguments.containsKey(name))) { |
| 118 String notFound = | 118 String notFound = |
| 119 holeNames.where((name) => !arguments.containsKey(name)).join(", "); | 119 holeNames.where((name) => !arguments.containsKey(name)).join(", "); |
| 120 throw "Template arguments is missing mappings for: $notFound"; | 120 throw "Template arguments is missing mappings for: $notFound"; |
| 121 } | 121 } |
| 122 return instantiator(arguments); | 122 return instantiator(arguments); |
| 123 } | 123 } |
| 124 | |
| 125 /// Like [instantiate] but works with free variables. | |
| 126 Node safeCreate(Map arguments) { | |
| 127 if (holeNames.isEmpty) return ast; | |
| 128 return instantiator(new Map.fromIterable(holeNames, value: (name) { | |
| 129 var a = arguments[name]; | |
| 130 return a != null ? a : new InterpolatedExpression(name); | |
| 131 })); | |
| 132 } | |
| 133 } | 124 } |
| 134 | 125 |
| 135 /** | 126 /** |
| 136 * An Instantiator is a Function that generates a JS AST tree or List of | 127 * An Instantiator is a Function that generates a JS AST tree or List of |
| 137 * trees. [arguments] is a List for positional templates, or Map for | 128 * trees. [arguments] is a List for positional templates, or Map for |
| 138 * named templates. | 129 * named templates. |
| 139 */ | 130 */ |
| 140 typedef Instantiator(var arguments); | 131 typedef Instantiator(var arguments); |
| 141 | 132 |
| 142 /** | 133 /** |
| (...skipping 771 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 914 if (count != before) containsInterpolatedNode.add(node); | 905 if (count != before) containsInterpolatedNode.add(node); |
| 915 return null; | 906 return null; |
| 916 } | 907 } |
| 917 | 908 |
| 918 visitInterpolatedNode(InterpolatedNode node) { | 909 visitInterpolatedNode(InterpolatedNode node) { |
| 919 containsInterpolatedNode.add(node); | 910 containsInterpolatedNode.add(node); |
| 920 if (node.isNamed) holeNames.add(node.nameOrPosition); | 911 if (node.isNamed) holeNames.add(node.nameOrPosition); |
| 921 ++count; | 912 ++count; |
| 922 } | 913 } |
| 923 } | 914 } |
| OLD | NEW |