Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(115)

Side by Side Diff: pkg/docgen/lib/src/model_helpers.dart

Issue 222223004: pkg/docgen: continue the refactoring (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « no previous file | pkg/docgen/lib/src/models.dart » ('j') | pkg/docgen/lib/src/models.dart » ('J')
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 '../../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mi rrors.dart' 7 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/dart2js_mi rrors.dart'
8 as dart2js_mirrors; 8 as dart2js_mirrors;
9 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_ut il.dart' 9 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/mirrors_ut il.dart'
10 as dart2js_util; 10 as dart2js_util;
11 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/source_mir rors.dart'; 11 import '../../../../sdk/lib/_internal/compiler/implementation/mirrors/source_mir rors.dart';
12 import '../../../../sdk/lib/_internal/libraries.dart'; 12 import '../../../../sdk/lib/_internal/libraries.dart';
13 13
14 import 'library_helpers.dart' show includePrivateMembers;
14 import 'models.dart'; 15 import 'models.dart';
15 import 'package_helpers.dart'; 16 import 'package_helpers.dart';
16 17
17 /// Returns a list of meta annotations assocated with a mirror. 18 /// Returns a list of meta annotations assocated with a mirror.
18 List<Annotation> createAnnotations(DeclarationMirror mirror, Library 19 List<Annotation> createAnnotations(DeclarationMirror mirror,
19 owningLibrary) { 20 Library owningLibrary) {
20 var annotationMirrors = mirror.metadata.where((e) => e is 21 var annotationMirrors = mirror.metadata
21 dart2js_mirrors.Dart2JsConstructedConstantMirror); 22 .where((e) => e is dart2js_mirrors.Dart2JsConstructedConstantMirror);
22 var annotations = []; 23 var annotations = [];
23 annotationMirrors.forEach((annotation) { 24 annotationMirrors.forEach((annotation) {
24 var docgenAnnotation = new Annotation(annotation, owningLibrary); 25 var docgenAnnotation = new Annotation(annotation, owningLibrary);
25 if (!_SKIPPED_ANNOTATIONS.contains(dart2js_util.qualifiedNameOf( 26 if (!_SKIPPED_ANNOTATIONS.contains(dart2js_util.qualifiedNameOf(
26 docgenAnnotation.mirror))) { 27 docgenAnnotation.mirror))) {
27 annotations.add(docgenAnnotation); 28 annotations.add(docgenAnnotation);
28 } 29 }
29 }); 30 });
30 return annotations; 31 return annotations;
31 } 32 }
(...skipping 34 matching lines...) Expand 10 before | Expand all | Expand 10 after
66 } 67 }
67 68
68 /// Read a pubspec and return the library name given a [LibraryMirror]. 69 /// Read a pubspec and return the library name given a [LibraryMirror].
69 String getPackageName(LibraryMirror mirror) { 70 String getPackageName(LibraryMirror mirror) {
70 if (mirror.uri.scheme != 'file') return ''; 71 if (mirror.uri.scheme != 'file') return '';
71 var rootdir = getPackageDirectory(mirror); 72 var rootdir = getPackageDirectory(mirror);
72 if (rootdir == null) return ''; 73 if (rootdir == null) return '';
73 return packageNameFor(rootdir); 74 return packageNameFor(rootdir);
74 } 75 }
75 76
77
78 /// Helper that maps [mirrors] to their simple name in map.
79 Map addAll(Map map, Iterable<DeclarationMirror> mirrors) {
80 for (var mirror in mirrors) {
81 map[dart2js_util.nameOf(mirror)] = mirror;
82 }
83 return map;
84 }
85
86 /// For the given library determine what items (if any) are exported.
87 ///
88 /// Returns a Map with three keys: "classes", "methods", and "variables" the
89 /// values of which point to a map of exported name identifiers with values
90 /// corresponding to the actual DeclarationMirror.
91 Map<String, Map<String, DeclarationMirror>> calcExportedItems(
92 LibrarySourceMirror library) {
93 var exports = {};
94 exports['classes'] = {};
95 exports['methods'] = {};
96 exports['variables'] = {};
97
98 // Determine the classes, variables and methods that are exported for a
99 // specific dependency.
100 void _populateExports(LibraryDependencyMirror export, bool showExport) {
101 if (!showExport) {
102 // Add all items, and then remove the hidden ones.
103 // Ex: "export foo hide bar"
104 addAll(exports['classes'],
105 dart2js_util.typesOf(export.targetLibrary.declarations));
106 addAll(exports['methods'],
107 export.targetLibrary.declarations.values.where(
108 (mirror) => mirror is MethodMirror));
109 addAll(exports['variables'],
110 dart2js_util.variablesOf(export.targetLibrary.declarations));
111 }
112 for (CombinatorMirror combinator in export.combinators) {
113 for (String identifier in combinator.identifiers) {
114 var librarySourceMirror =
115 export.targetLibrary as DeclarationSourceMirror;
116 var declaration = librarySourceMirror.lookupInScope(identifier);
117 if (declaration == null) {
118 // Technically this should be a bug, but some of our packages
119 // (such as the polymer package) are curently broken in this
120 // way, so we just produce a warning.
121 print('Warning identifier $identifier not found in library '
122 '${dart2js_util.qualifiedNameOf(export.targetLibrary)}');
123 } else {
124 var subMap = exports['classes'];
125 if (declaration is MethodMirror) {
126 subMap = exports['methods'];
127 } else if (declaration is VariableMirror) {
128 subMap = exports['variables'];
129 }
130 if (showExport) {
131 subMap[identifier] = declaration;
132 } else {
133 subMap.remove(identifier);
134 }
135 }
136 }
137 }
138 }
139
140 Iterable<LibraryDependencyMirror> exportList =
141 library.libraryDependencies.where((lib) => lib.isExport);
142 for (LibraryDependencyMirror export in exportList) {
143 // If there is a show in the export, add only the show items to the
144 // library. Ex: "export foo show bar"
145 // Otherwise, add all items, and then remove the hidden ones.
146 // Ex: "export foo hide bar"
147 _populateExports(export,
148 export.combinators.any((combinator) => combinator.isShow));
149 }
150 return exports;
151 }
152
153
154 /// Returns a map of [Variable] objects constructed from [mirrorMap].
155 /// The optional parameter [containingLibrary] is contains data for variables
156 /// defined at the top level of a library (potentially for exporting
157 /// purposes).
158 Map<String, Variable> createVariables(Iterable<VariableMirror> mirrors,
159 Indexable owner) {
160 var data = {};
161 // TODO(janicejl): When map to map feature is created, replace the below
162 // with a filter. Issue(#9590).
163 mirrors.forEach((dart2js_mirrors.Dart2JsFieldMirror mirror) {
164 if (includePrivateMembers || !isHidden(mirror)) {
165 var mirrorName = dart2js_util.nameOf(mirror);
166 data[mirrorName] = new Variable(mirrorName, mirror, owner);
167 }
168 });
169 return data;
170 }
171
172 /// Returns a map of [Method] objects constructed from [mirrorMap].
173 /// The optional parameter [containingLibrary] is contains data for variables
174 /// defined at the top level of a library (potentially for exporting
175 /// purposes).
176 Map<String, Method> createMethods(Iterable<MethodMirror> mirrors,
177 Indexable owner) {
178 var group = new Map<String, Method>();
179 mirrors.forEach((MethodMirror mirror) {
180 if (includePrivateMembers || !mirror.isPrivate) {
181 group[dart2js_util.nameOf(mirror)] = new Method(mirror, owner);
182 }
183 });
184 return group;
185 }
186
187 /// Returns a map of [Parameter] objects constructed from [mirrorList].
188 Map<String, Parameter> createParameters(List<ParameterMirror> mirrorList,
189 Indexable owner) {
190 var data = {};
191 mirrorList.forEach((ParameterMirror mirror) {
192 data[dart2js_util.nameOf(mirror)] =
193 new Parameter(mirror, owner.owningLibrary);
194 });
195 return data;
196 }
197
198 /// Returns a map of [Generic] objects constructed from the class mirror.
199 Map<String, Generic> createGenerics(TypeMirror mirror) {
200 return new Map.fromIterable(mirror.typeVariables,
201 key: (e) => dart2js_util.nameOf(e),
202 value: (e) => new Generic(e));
203 }
204
76 /// Annotations that we do not display in the viewer. 205 /// Annotations that we do not display in the viewer.
77 const List<String> _SKIPPED_ANNOTATIONS = const [ 206 const List<String> _SKIPPED_ANNOTATIONS = const [
78 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates', 207 'metadata.DocsEditable', '_js_helper.JSName', '_js_helper.Creates',
79 '_js_helper.Returns' 208 '_js_helper.Returns'
80 ]; 209 ];
81 210
82 /// Returns true if a library name starts with an underscore, and false 211 /// Returns true if a library name starts with an underscore, and false
83 /// otherwise. 212 /// otherwise.
84 /// 213 ///
85 /// An example that starts with _ is _js_helper. 214 /// An example that starts with _ is _js_helper.
86 /// An example that contains ._ is dart._collection.dev 215 /// An example that contains ._ is dart._collection.dev
87 bool _isLibraryPrivate(dart2js_mirrors.Dart2JsLibraryMirror mirror) { 216 bool _isLibraryPrivate(dart2js_mirrors.Dart2JsLibraryMirror mirror) {
88 // This method is needed because LibraryMirror.isPrivate returns `false` all 217 // This method is needed because LibraryMirror.isPrivate returns `false` all
89 // the time. 218 // the time.
90 var sdkLibrary = LIBRARIES[dart2js_util.nameOf(mirror)]; 219 var sdkLibrary = LIBRARIES[dart2js_util.nameOf(mirror)];
91 if (sdkLibrary != null) { 220 if (sdkLibrary != null) {
92 return !sdkLibrary.documented; 221 return !sdkLibrary.documented;
93 } else if (dart2js_util.nameOf(mirror).startsWith('_') || dart2js_util.nameOf( 222 } else if (dart2js_util.nameOf(mirror).startsWith('_') || dart2js_util.nameOf(
94 mirror).contains('._')) { 223 mirror).contains('._')) {
95 return true; 224 return true;
96 } 225 }
97 return false; 226 return false;
98 } 227 }
OLDNEW
« no previous file with comments | « no previous file | pkg/docgen/lib/src/models.dart » ('j') | pkg/docgen/lib/src/models.dart » ('J')

Powered by Google App Engine
This is Rietveld 408576698