| 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 library docgen.model_helpers; | 5 library docgen.model_helpers; |
| 6 | 6 |
| 7 import 'dart:collection'; | 7 import 'dart:collection'; |
| 8 | 8 |
| 9 import '../exports/dart2js_mirrors.dart' as dart2js_mirrors; | 9 import '../exports/dart2js_mirrors.dart' as dart2js_mirrors; |
| 10 import '../exports/mirrors_util.dart' as dart2js_util; | 10 import '../exports/mirrors_util.dart' as dart2js_util; |
| (...skipping 10 matching lines...) Expand all Loading... |
| 21 import 'method.dart'; | 21 import 'method.dart'; |
| 22 import 'parameter.dart'; | 22 import 'parameter.dart'; |
| 23 import 'variable.dart'; | 23 import 'variable.dart'; |
| 24 | 24 |
| 25 String getLibraryDocName(LibraryMirror mirror) => | 25 String getLibraryDocName(LibraryMirror mirror) => |
| 26 dart2js_util.qualifiedNameOf(mirror).replaceAll('.', '-'); | 26 dart2js_util.qualifiedNameOf(mirror).replaceAll('.', '-'); |
| 27 | 27 |
| 28 /// Expand the method map [mapToExpand] into a more detailed map that | 28 /// Expand the method map [mapToExpand] into a more detailed map that |
| 29 /// separates out setters, getters, constructors, operators, and methods. | 29 /// separates out setters, getters, constructors, operators, and methods. |
| 30 Map expandMethodMap(Map<String, Method> mapToExpand) => { | 30 Map expandMethodMap(Map<String, Method> mapToExpand) => { |
| 31 'setters': recurseMap(filterMap(mapToExpand, | 31 'setters': recurseMap(_filterMap(mapToExpand, |
| 32 (key, val) => val.mirror.isSetter)), | 32 (key, val) => val.mirror.isSetter)), |
| 33 'getters': recurseMap(filterMap(mapToExpand, | 33 'getters': recurseMap(_filterMap(mapToExpand, |
| 34 (key, val) => val.mirror.isGetter)), | 34 (key, val) => val.mirror.isGetter)), |
| 35 'constructors': recurseMap(filterMap(mapToExpand, | 35 'constructors': recurseMap(_filterMap(mapToExpand, |
| 36 (key, val) => val.mirror.isConstructor)), | 36 (key, val) => val.mirror.isConstructor)), |
| 37 'operators': recurseMap(filterMap(mapToExpand, | 37 'operators': recurseMap(_filterMap(mapToExpand, |
| 38 (key, val) => val.mirror.isOperator)), | 38 (key, val) => val.mirror.isOperator)), |
| 39 'methods': recurseMap(filterMap(mapToExpand, | 39 'methods': recurseMap(_filterMap(mapToExpand, |
| 40 (key, val) => val.mirror.isRegularMethod && !val.mirror.isOperator)) | 40 (key, val) => val.mirror.isRegularMethod && !val.mirror.isOperator)) |
| 41 }; | 41 }; |
| 42 | 42 |
| 43 String getDefaultValue(ParameterMirror mirror) { | 43 String getDefaultValue(ParameterMirror mirror) { |
| 44 if (!mirror.hasDefaultValue) return null; | 44 if (!mirror.hasDefaultValue) return null; |
| 45 return getDefaultValueFromConstMirror(mirror.defaultValue); | 45 return getDefaultValueFromConstMirror(mirror.defaultValue); |
| 46 } | 46 } |
| 47 | 47 |
| 48 String getDefaultValueFromConstMirror( | 48 String getDefaultValueFromConstMirror( |
| 49 dart2js_mirrors.Dart2JsConstantMirror valueMirror) { | 49 dart2js_mirrors.Dart2JsConstantMirror valueMirror) { |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 98 return (mirror.isPrivate || _isLibraryPrivate(mirror.owner) || | 98 return (mirror.isPrivate || _isLibraryPrivate(mirror.owner) || |
| 99 mirror.isNameSynthetic); | 99 mirror.isNameSynthetic); |
| 100 } else { | 100 } else { |
| 101 return (mirror.isPrivate || isHidden(mirror.owner) || | 101 return (mirror.isPrivate || isHidden(mirror.owner) || |
| 102 mirror.isNameSynthetic); | 102 mirror.isNameSynthetic); |
| 103 } | 103 } |
| 104 } | 104 } |
| 105 | 105 |
| 106 /// Transforms the map by calling toMap on each value in it. | 106 /// Transforms the map by calling toMap on each value in it. |
| 107 Map recurseMap(Map inputMap) { | 107 Map recurseMap(Map inputMap) { |
| 108 var outputMap = new SplayTreeMap(); | 108 var outputMap = new LinkedHashMap(); |
| 109 inputMap.forEach((key, value) { | 109 inputMap.forEach((key, value) { |
| 110 if (value is Map) { | 110 if (value is Map) { |
| 111 outputMap[key] = recurseMap(value); | 111 outputMap[key] = recurseMap(value); |
| 112 } else { | 112 } else { |
| 113 outputMap[key] = value.toMap(); | 113 outputMap[key] = value.toMap(); |
| 114 } | 114 } |
| 115 }); | 115 }); |
| 116 return outputMap; | 116 return outputMap; |
| 117 } | 117 } |
| 118 | 118 |
| 119 Map filterMap(Map map, Function test) { | |
| 120 var exported = new Map(); | |
| 121 map.forEach((key, value) { | |
| 122 if (test(key, value)) exported[key] = value; | |
| 123 }); | |
| 124 return exported; | |
| 125 } | |
| 126 | |
| 127 /// Read a pubspec and return the library name given a [LibraryMirror]. | 119 /// Read a pubspec and return the library name given a [LibraryMirror]. |
| 128 String getPackageName(LibraryMirror mirror) { | 120 String getPackageName(LibraryMirror mirror) { |
| 129 if (mirror.uri.scheme != 'file') return ''; | 121 if (mirror.uri.scheme != 'file') return ''; |
| 130 var rootdir = getPackageDirectory(mirror); | 122 var rootdir = getPackageDirectory(mirror); |
| 131 if (rootdir == null) return ''; | 123 if (rootdir == null) return ''; |
| 132 return packageNameFor(rootdir); | 124 return packageNameFor(rootdir); |
| 133 } | 125 } |
| 134 | 126 |
| 135 | 127 |
| 136 /// Helper that maps [mirrors] to their simple name in map. | 128 /// Helper that maps [mirrors] to their simple name in map. |
| 137 Map addAll(Map map, Iterable<DeclarationMirror> mirrors) { | 129 Map addAll(Map map, Iterable<DeclarationMirror> mirrors) { |
| 138 for (var mirror in mirrors) { | 130 for (var mirror in mirrors) { |
| 139 map[dart2js_util.nameOf(mirror)] = mirror; | 131 map[dart2js_util.nameOf(mirror)] = mirror; |
| 140 } | 132 } |
| 141 return map; | 133 return map; |
| 142 } | 134 } |
| 143 | 135 |
| 144 /// For the given library determine what items (if any) are exported. | 136 /// For the given library determine what items (if any) are exported. |
| 145 /// | 137 /// |
| 146 /// Returns a Map with three keys: "classes", "methods", and "variables" the | 138 /// Returns a Map with three keys: "classes", "methods", and "variables" the |
| 147 /// values of which point to a map of exported name identifiers with values | 139 /// values of which point to a map of exported name identifiers with values |
| 148 /// corresponding to the actual DeclarationMirror. | 140 /// corresponding to the actual DeclarationMirror. |
| 149 Map<String, Map<String, DeclarationMirror>> calcExportedItems( | 141 Map<String, Map<String, DeclarationMirror>> calcExportedItems( |
| 150 LibrarySourceMirror library) { | 142 LibrarySourceMirror library) { |
| 151 var exports = {}; | 143 var exports = {}; |
| 152 exports['classes'] = {}; | 144 exports['classes'] = new SplayTreeMap(); |
| 153 exports['methods'] = {}; | 145 exports['methods'] = new SplayTreeMap(); |
| 154 exports['variables'] = {}; | 146 exports['variables'] = new SplayTreeMap(); |
| 155 | 147 |
| 156 // Determine the classes, variables and methods that are exported for a | 148 // Determine the classes, variables and methods that are exported for a |
| 157 // specific dependency. | 149 // specific dependency. |
| 158 void _populateExports(LibraryDependencyMirror export, bool showExport) { | 150 void _populateExports(LibraryDependencyMirror export, bool showExport) { |
| 159 var transitiveExports = calcExportedItems(export.targetLibrary); | 151 var transitiveExports = calcExportedItems(export.targetLibrary); |
| 160 exports['classes'].addAll(transitiveExports['classes']); | 152 exports['classes'].addAll(transitiveExports['classes']); |
| 161 exports['methods'].addAll(transitiveExports['methods']); | 153 exports['methods'].addAll(transitiveExports['methods']); |
| 162 exports['variables'].addAll(transitiveExports['variables']); | 154 exports['variables'].addAll(transitiveExports['variables']); |
| 163 // If there is a show in the export, add only the show items to the | 155 // If there is a show in the export, add only the show items to the |
| 164 // library. Ex: "export foo show bar" | 156 // library. Ex: "export foo show bar" |
| (...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 return exports; | 205 return exports; |
| 214 } | 206 } |
| 215 | 207 |
| 216 | 208 |
| 217 /// Returns a map of [Variable] objects constructed from [mirrorMap]. | 209 /// Returns a map of [Variable] objects constructed from [mirrorMap]. |
| 218 /// The optional parameter [containingLibrary] is contains data for variables | 210 /// The optional parameter [containingLibrary] is contains data for variables |
| 219 /// defined at the top level of a library (potentially for exporting | 211 /// defined at the top level of a library (potentially for exporting |
| 220 /// purposes). | 212 /// purposes). |
| 221 Map<String, Variable> createVariables(Iterable<VariableMirror> mirrors, | 213 Map<String, Variable> createVariables(Iterable<VariableMirror> mirrors, |
| 222 Indexable owner) { | 214 Indexable owner) { |
| 223 var data = {}; | 215 var data = new SplayTreeMap<String, Variable>(); |
| 224 // TODO(janicejl): When map to map feature is created, replace the below | 216 // TODO(janicejl): When map to map feature is created, replace the below |
| 225 // with a filter. Issue(#9590). | 217 // with a filter. Issue(#9590). |
| 226 mirrors.forEach((dart2js_mirrors.Dart2JsFieldMirror mirror) { | 218 mirrors.forEach((dart2js_mirrors.Dart2JsFieldMirror mirror) { |
| 227 if (includePrivateMembers || !isHidden(mirror)) { | 219 if (includePrivateMembers || !isHidden(mirror)) { |
| 228 var mirrorName = dart2js_util.nameOf(mirror); | 220 var mirrorName = dart2js_util.nameOf(mirror); |
| 229 data[mirrorName] = new Variable(mirrorName, mirror, owner); | 221 data[mirrorName] = new Variable(mirrorName, mirror, owner); |
| 230 } | 222 } |
| 231 }); | 223 }); |
| 232 return data; | 224 return data; |
| 233 } | 225 } |
| 234 | 226 |
| 235 /// Returns a map of [Method] objects constructed from [mirrorMap]. | 227 /// Returns a map of [Method] objects constructed from [mirrorMap]. |
| 236 /// The optional parameter [containingLibrary] is contains data for variables | 228 /// The optional parameter [containingLibrary] is contains data for variables |
| 237 /// defined at the top level of a library (potentially for exporting | 229 /// defined at the top level of a library (potentially for exporting |
| 238 /// purposes). | 230 /// purposes). |
| 239 Map<String, Method> createMethods(Iterable<MethodMirror> mirrors, | 231 Map<String, Method> createMethods(Iterable<MethodMirror> mirrors, |
| 240 Indexable owner) { | 232 Indexable owner) { |
| 241 var group = new Map<String, Method>(); | 233 var group = new SplayTreeMap<String, Method>(); |
| 242 mirrors.forEach((MethodMirror mirror) { | 234 mirrors.forEach((MethodMirror mirror) { |
| 243 if (includePrivateMembers || !mirror.isPrivate) { | 235 if (includePrivateMembers || !mirror.isPrivate) { |
| 244 group[dart2js_util.nameOf(mirror)] = new Method(mirror, owner); | 236 group[dart2js_util.nameOf(mirror)] = new Method(mirror, owner); |
| 245 } | 237 } |
| 246 }); | 238 }); |
| 247 return group; | 239 return group; |
| 248 } | 240 } |
| 249 | 241 |
| 250 /// Returns a map of [Parameter] objects constructed from [mirrorList]. | 242 /// Returns a map of [Parameter] objects constructed from [mirrorList]. |
| 251 Map<String, Parameter> createParameters(List<ParameterMirror> mirrorList, | 243 Map<String, Parameter> createParameters(List<ParameterMirror> mirrorList, |
| 252 Indexable owner) { | 244 Indexable owner) { |
| 253 var data = {}; | 245 var data = {}; |
| 254 mirrorList.forEach((ParameterMirror mirror) { | 246 mirrorList.forEach((ParameterMirror mirror) { |
| 255 data[dart2js_util.nameOf(mirror)] = | 247 data[dart2js_util.nameOf(mirror)] = |
| 256 new Parameter(mirror, owner.owningLibrary); | 248 new Parameter(mirror, owner.owningLibrary); |
| 257 }); | 249 }); |
| 258 return data; | 250 return data; |
| 259 } | 251 } |
| 260 | 252 |
| 261 /// Returns a map of [Generic] objects constructed from the class mirror. | 253 /// Returns a map of [Generic] objects constructed from the class mirror. |
| 262 Map<String, Generic> createGenerics(TypeMirror mirror) { | 254 Map<String, Generic> createGenerics(TypeMirror mirror) { |
| 263 return new Map.fromIterable(mirror.typeVariables, | 255 return new Map.fromIterable(mirror.typeVariables, |
| 264 key: (e) => dart2js_util.nameOf(e), | 256 key: (e) => dart2js_util.nameOf(e), |
| 265 value: (e) => new Generic(e)); | 257 value: (e) => new Generic(e)); |
| 266 } | 258 } |
| 267 | 259 |
| 260 Map _filterMap(Map map, bool test(k, v)) { |
| 261 var exported = new SplayTreeMap(); |
| 262 map.forEach((key, value) { |
| 263 if (test(key, value)) exported[key] = value; |
| 264 }); |
| 265 return exported; |
| 266 } |
| 267 |
| 268 /// Annotations that we do not display in the viewer. | 268 /// Annotations that we do not display in the viewer. |
| 269 const List<String> _SKIPPED_ANNOTATIONS = const [ | 269 const List<String> _SKIPPED_ANNOTATIONS = const [ |
| 270 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', | 270 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', |
| 271 '_js_helper.Returns' | 271 '_js_helper.Returns' |
| 272 ]; | 272 ]; |
| 273 | 273 |
| 274 /// Returns true if a library name starts with an underscore, and false | 274 /// Returns true if a library name starts with an underscore, and false |
| 275 /// otherwise. | 275 /// otherwise. |
| 276 /// | 276 /// |
| 277 /// An example that starts with _ is _js_helper. | 277 /// An example that starts with _ is _js_helper. |
| 278 /// An example that contains ._ is dart._collection.dev | 278 /// An example that contains ._ is dart._collection.dev |
| 279 bool _isLibraryPrivate(dart2js_mirrors.Dart2JsLibraryMirror mirror) { | 279 bool _isLibraryPrivate(dart2js_mirrors.Dart2JsLibraryMirror mirror) { |
| 280 // This method is needed because LibraryMirror.isPrivate returns `false` all | 280 // This method is needed because LibraryMirror.isPrivate returns `false` all |
| 281 // the time. | 281 // the time. |
| 282 var sdkLibrary = LIBRARIES[dart2js_util.nameOf(mirror)]; | 282 var sdkLibrary = LIBRARIES[dart2js_util.nameOf(mirror)]; |
| 283 if (sdkLibrary != null) { | 283 if (sdkLibrary != null) { |
| 284 return !sdkLibrary.documented; | 284 return !sdkLibrary.documented; |
| 285 } else if (dart2js_util.nameOf(mirror).startsWith('_') || dart2js_util.nameOf( | 285 } else if (dart2js_util.nameOf(mirror).startsWith('_') || dart2js_util.nameOf( |
| 286 mirror).contains('._')) { | 286 mirror).contains('._')) { |
| 287 return true; | 287 return true; |
| 288 } | 288 } |
| 289 return false; | 289 return false; |
| 290 } | 290 } |
| OLD | NEW |