| OLD | NEW |
| 1 // Copyright (c) 2017, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2017, 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 'package:analyzer/src/codegen/tools.dart'; | 5 import 'package:analyzer/src/codegen/tools.dart'; |
| 6 import 'package:front_end/src/codegen/tools.dart'; | 6 import 'package:front_end/src/codegen/tools.dart'; |
| 7 | 7 |
| 8 import 'api.dart'; | 8 import 'api.dart'; |
| 9 import 'codegen_dart.dart'; | 9 import 'codegen_dart.dart'; |
| 10 import 'from_html.dart'; | 10 import 'from_html.dart'; |
| (...skipping 106 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 117 * Generate a constant for each of the fields in the given [type], where the | 117 * Generate a constant for each of the fields in the given [type], where the |
| 118 * name of each constant will be composed from the [parentName] and the name | 118 * name of each constant will be composed from the [parentName] and the name |
| 119 * of the field. | 119 * of the field. |
| 120 */ | 120 */ |
| 121 void _addFieldConstants(String parentName, TypeObject type) { | 121 void _addFieldConstants(String parentName, TypeObject type) { |
| 122 if (type == null) { | 122 if (type == null) { |
| 123 return; | 123 return; |
| 124 } | 124 } |
| 125 type.fields.forEach((TypeObjectField field) { | 125 type.fields.forEach((TypeObjectField field) { |
| 126 String name = field.name; | 126 String name = field.name; |
| 127 String fieldConstantName = parentName + '_' + name.toUpperCase(); | 127 List<String> components = <String>[]; |
| 128 components.add(parentName); |
| 129 components.addAll(_split(name)); |
| 130 String fieldConstantName = _fromComponents(components); |
| 128 constants.add(new _Constant(fieldConstantName, "'$name'")); | 131 constants.add(new _Constant(fieldConstantName, "'$name'")); |
| 129 }); | 132 }); |
| 130 } | 133 } |
| 131 | 134 |
| 132 /** | 135 /** |
| 136 * Return a name generated by converting each of the given [components] to an |
| 137 * uppercase equivalent, then joining them with underscores. |
| 138 */ |
| 139 String _fromComponents(List<String> components) => |
| 140 components.map((String component) => component.toUpperCase()).join('_'); |
| 141 |
| 142 /** |
| 133 * Generate a name from the [domainName], [kind] and [name] components. | 143 * Generate a name from the [domainName], [kind] and [name] components. |
| 134 */ | 144 */ |
| 135 String _generateName(String domainName, String kind, String name) { | 145 String _generateName(String domainName, String kind, String name) { |
| 136 List<String> components = <String>[]; | 146 List<String> components = <String>[]; |
| 137 components.addAll(_split(domainName)); | 147 components.addAll(_split(domainName)); |
| 138 components.add(kind); | 148 components.add(kind); |
| 139 components.addAll(_split(name)); | 149 components.addAll(_split(name)); |
| 140 return components | 150 return _fromComponents(components); |
| 141 .map((String component) => component.toUpperCase()) | |
| 142 .join('_'); | |
| 143 } | 151 } |
| 144 | 152 |
| 145 /** | 153 /** |
| 146 * Return the components of the given [string] that are indicated by an upper | 154 * Return the components of the given [string] that are indicated by an upper |
| 147 * case letter. | 155 * case letter. |
| 148 */ | 156 */ |
| 149 Iterable<String> _split(String first) { | 157 Iterable<String> _split(String first) { |
| 150 RegExp regExp = new RegExp('[A-Z]'); | 158 RegExp regExp = new RegExp('[A-Z]'); |
| 151 List<String> components = <String>[]; | 159 List<String> components = <String>[]; |
| 152 int start = 1; | 160 int start = 1; |
| 153 int index = first.indexOf(regExp, start); | 161 int index = first.indexOf(regExp, start); |
| 154 while (index >= 0) { | 162 while (index >= 0) { |
| 155 components.add(first.substring(start - 1, index)); | 163 components.add(first.substring(start - 1, index)); |
| 156 start = index + 1; | 164 start = index + 1; |
| 157 index = first.indexOf(regExp, start); | 165 index = first.indexOf(regExp, start); |
| 158 } | 166 } |
| 159 components.add(first.substring(start - 1)); | 167 components.add(first.substring(start - 1)); |
| 160 return components; | 168 return components; |
| 161 } | 169 } |
| 162 } | 170 } |
| OLD | NEW |