OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 /** | 5 /** |
6 * A script to assist in documenting the difference between the dart:html API | 6 * A script to assist in documenting the difference between the dart:html API |
7 * and the old DOM API. | 7 * and the old DOM API. |
8 */ | 8 */ |
9 #library('html_diff'); | 9 #library('html_diff'); |
10 | 10 |
11 #import('dart:coreimpl'); | 11 #import('dart:coreimpl'); |
12 #import('dart:io'); | 12 #import('dart:io'); |
13 | 13 |
14 #import('../../pkg/dartdoc/dartdoc.dart'); | 14 #import('../../pkg/dartdoc/dartdoc.dart'); |
15 #import('../../pkg/dartdoc/mirrors/mirrors.dart'); | 15 #import('../../pkg/dartdoc/mirrors/mirrors.dart'); |
16 #import('../../pkg/dartdoc/mirrors/mirrors_util.dart'); | 16 #import('../../pkg/dartdoc/mirrors/mirrors_util.dart'); |
17 | 17 |
18 final HTML_LIBRARY_NAME = 'dart:html'; | 18 final HTML_LIBRARY_NAME = 'dart:html'; |
19 final DOM_LIBRARY_NAME = 'dart:dom_deprecated'; | |
20 | 19 |
21 /** | 20 /** |
22 * A class for computing a many-to-many mapping between the types and | 21 * A class for computing a many-to-many mapping between the types and |
23 * members in `dart:dom_deprecated` and `dart:html`. This mapping is | 22 * members in `dart:html` and the MDN DOM types. This mapping is |
24 * based on two indicators: | 23 * based on two indicators: |
25 * | 24 * |
26 * 1. Auto-detected wrappers. Most `dart:html` types correspond | 25 * 1. Auto-detected wrappers. Most `dart:html` types correspond |
27 * straightforwardly to a single `dart:dom_deprecated` type, and | 26 * straightforwardly to a single `@domName` type, and |
28 * have the same name. In addition, most `dart:html` methods | 27 * have the same name. In addition, most `dart:html` methods |
29 * just call a single `dart:dom_deprecated` method. This class | 28 * just call a single `@domName` method. This class |
30 * detects these simple correspondences automatically. | 29 * detects these simple correspondences automatically. |
31 * | 30 * |
32 * 2. Manual annotations. When it's not clear which | 31 * 2. Manual annotations. When it's not clear which |
33 * `dart:dom_deprecated` items a given `dart:html` item | 32 * `@domName` items a given `dart:html` item |
34 * corresponds to, the `dart:html` item can be annotated in the | 33 * corresponds to, the `dart:html` item can be annotated in the |
35 * documentation comments using the `@domName` annotation. | 34 * documentation comments using the `@domName` annotation. |
36 * | 35 * |
37 * The `@domName` annotations for types and members are of the form | 36 * The `@domName` annotations for types and members are of the form |
38 * `@domName NAME(, NAME)*`, where the `NAME`s refer to the | 37 * `@domName NAME(, NAME)*`, where the `NAME`s refer to the |
39 * `dart:dom_deprecated` types/members that correspond to the | 38 * `@domName` types/members that correspond to the |
40 * annotated `dart:html` type/member. `NAME`s on member annotations | 39 * annotated `dart:html` type/member. `NAME`s on member annotations |
41 * can refer to either fully-qualified member names (e.g. | 40 * can refer to either fully-qualified member names (e.g. |
42 * `Document.createElement`) or unqualified member names | 41 * `Document.createElement`) or unqualified member names |
43 * (e.g. `createElement`). Unqualified member names are assumed to | 42 * (e.g. `createElement`). Unqualified member names are assumed to |
44 * refer to members of one of the corresponding `dart:dom_deprecated` | 43 * refer to members of one of the corresponding `@domName` |
45 * types. | 44 * types. |
46 */ | 45 */ |
47 class HtmlDiff { | 46 class HtmlDiff { |
48 /** A map from `dart:dom_deprecated` members to corresponding | 47 /** |
49 * `dart:html` members. */ | 48 * A map from `@domName` members to corresponding |
50 final Map<MemberMirror, Set<MemberMirror>> domToHtml; | 49 * `dart:html` members. |
50 */ | |
51 final Map<String, Set<MemberMirror>> domToHtml; | |
eub
2012/09/05 21:28:11
Can you note why we use MemberMirrors just in this
eub
2012/09/05 21:28:11
BTW, hm, is it true that domToHtml is consumed onl
Emily Fortuna
2012/09/05 21:57:26
Good catch. This was some ... vestigial code from
| |
51 | 52 |
52 /** A map from `dart:html` members to corresponding | 53 /** |
53 * `dart:dom_deprecated` members. | 54 * A map from `dart:html` members to the corresponding fully qualified |
54 * TODO(johnniwinther): We use qualified names as keys, since mirrors | 55 * `@domName` member(s). |
55 * (currently) are not equal between different mirror systems. | |
56 */ | 56 */ |
57 final Map<String, Set<MemberMirror>> htmlToDom; | 57 final Map<String, Set<String>> htmlToDom; |
58 | 58 |
59 /** A map from `dart:dom_deprecated` types to corresponding | 59 /** A map from `dart:html` types to corresponding `@domName` types. */ |
60 * `dart:html` types. | 60 final Map<String, Set<String>> htmlTypesToDom; |
61 * TODO(johnniwinther): We use qualified names as keys, since mirrors | |
62 * (currently) are not equal between different mirror systems. | |
63 */ | |
64 final Map<String, Set<InterfaceMirror>> domTypesToHtml; | |
65 | |
66 /** A map from `dart:html` types to corresponding | |
67 * `dart:dom_deprecated` types. | |
68 * TODO(johnniwinther): We use qualified names as keys, since mirrors | |
69 * (currently) are not equal between different mirror systems. | |
70 */ | |
71 final Map<String, Set<InterfaceMirror>> htmlTypesToDom; | |
72 | 61 |
73 final CommentMap comments; | 62 final CommentMap comments; |
74 | 63 |
75 /** If true, then print warning messages. */ | 64 /** If true, then print warning messages. */ |
76 final bool _printWarnings; | 65 final bool _printWarnings; |
77 | 66 |
78 static Compilation _compilation; | 67 static Compilation _compilation; |
79 static MirrorSystem _mirrors; | 68 static MirrorSystem _mirrors; |
80 static LibraryMirror dom; | 69 static LibraryMirror dom; |
81 | 70 |
82 /** | 71 /** |
83 * Perform static initialization of [world]. This should be run before | 72 * Perform static initialization of [world]. This should be run before |
84 * calling [HtmlDiff.run]. | 73 * calling [HtmlDiff.run]. |
85 */ | 74 */ |
86 static void initialize(Path libDir) { | 75 static void initialize(Path libDir) { |
87 _compilation = new Compilation.library( | 76 _compilation = new Compilation.library( |
88 const <Path>[ | 77 const <Path>[const Path(HTML_LIBRARY_NAME)], libDir); |
89 const Path(DOM_LIBRARY_NAME), | |
90 const Path(HTML_LIBRARY_NAME) | |
91 ], libDir); | |
92 _mirrors = _compilation.mirrors; | 78 _mirrors = _compilation.mirrors; |
93 | |
94 // Find 'dart:dom_deprecated' by its library tag 'dom'. | |
95 dom = findMirror(_mirrors.libraries, DOM_LIBRARY_NAME); | |
96 } | 79 } |
97 | 80 |
98 HtmlDiff([bool printWarnings = false]) : | 81 HtmlDiff([bool printWarnings = false]) : |
99 _printWarnings = printWarnings, | 82 _printWarnings = printWarnings, |
100 domToHtml = new Map<MemberMirror, Set<MemberMirror>>(), | 83 domToHtml = new Map<String, Set<MemberMirror>>(), |
101 htmlToDom = new Map<String, Set<MemberMirror>>(), | 84 htmlToDom = new Map<String, Set<String>>(), |
102 domTypesToHtml = new Map<String, Set<InterfaceMirror>>(), | 85 htmlTypesToDom = new Map<String, Set<String>>(), |
103 htmlTypesToDom = new Map<String, Set<InterfaceMirror>>(), | |
104 comments = new CommentMap(); | 86 comments = new CommentMap(); |
105 | 87 |
106 void warn(String s) { | 88 void warn(String s) { |
107 if (_printWarnings) { | 89 if (_printWarnings) { |
108 print('Warning: $s'); | 90 print('Warning: $s'); |
109 } | 91 } |
110 } | 92 } |
111 | 93 |
112 /** | 94 /** |
113 * Computes the `dart:dom_deprecated` to `dart:html` mapping, and | 95 * Computes the `@domName` to `dart:html` mapping, and |
114 * places it in [domToHtml], [htmlToDom], [domTypesToHtml], and | 96 * places it in [domToHtml], [htmlToDom], and |
115 * [htmlTypesToDom]. Before this is run, Frog should be initialized | 97 * [htmlTypesToDom]. Before this is run, dart2js should be initialized |
116 * (via [parseOptions] and [initializeWorld]) and | 98 * (via [parseOptions] and [initializeWorld]) and |
117 * [HtmlDiff.initialize] should be called. | 99 * [HtmlDiff.initialize] should be called. |
118 */ | 100 */ |
119 void run() { | 101 void run() { |
120 LibraryMirror htmlLib = findMirror(_mirrors.libraries, HTML_LIBRARY_NAME); | 102 LibraryMirror htmlLib = findMirror(_mirrors.libraries, HTML_LIBRARY_NAME); |
121 if (htmlLib === null) { | 103 if (htmlLib === null) { |
122 warn('Could not find $HTML_LIBRARY_NAME'); | 104 warn('Could not find $HTML_LIBRARY_NAME'); |
123 return; | 105 return; |
124 } | 106 } |
125 for (InterfaceMirror htmlType in htmlLib.types.getValues()) { | 107 for (InterfaceMirror htmlType in htmlLib.types.getValues()) { |
126 final domTypes = htmlToDomTypes(htmlType); | 108 final domTypes = htmlToDomTypes(htmlType); |
127 if (domTypes.isEmpty()) continue; | 109 if (domTypes.isEmpty()) continue; |
128 | 110 |
129 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName, | 111 htmlTypesToDom.putIfAbsent(htmlType.qualifiedName, |
130 () => new Set()).addAll(domTypes); | 112 () => new Set()).addAll(domTypes); |
131 domTypes.forEach((t) => | |
132 domTypesToHtml.putIfAbsent(t.qualifiedName, | |
133 () => new Set()).add(htmlType)); | |
134 | 113 |
135 htmlType.declaredMembers.forEach( | 114 htmlType.declaredMembers.forEach( |
136 (_, m) => _addMemberDiff(m, domTypes)); | 115 (_, m) => _addMemberDiff(m, domTypes)); |
137 } | 116 } |
138 } | 117 } |
139 | 118 |
140 /** | 119 /** |
141 * Records the `dart:dom_deprecated` to `dart:html` mapping for | 120 * Records the `@domName` to `dart:html` mapping for |
142 * [implMember] (from `dart:html`). [domTypes] are the | 121 * [htmlMember] (from `dart:html`). [domTypes] are the |
143 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s | 122 * `@domName` type values that correspond to [htmlMember]'s |
144 * defining [Type]. | 123 * defining type. |
145 */ | 124 */ |
146 void _addMemberDiff(MemberMirror htmlMember, List<TypeMirror> domTypes) { | 125 void _addMemberDiff(MemberMirror htmlMember, List<String> domTypes) { |
147 var domMembers = htmlToDomMembers(htmlMember, domTypes); | 126 var domMembers = htmlToDomMembers(htmlMember, domTypes); |
148 if (htmlMember == null && !domMembers.isEmpty()) { | 127 if (htmlMember == null && !domMembers.isEmpty()) { |
149 warn('$HTML_LIBRARY_NAME member ' | 128 warn('$HTML_LIBRARY_NAME member ' |
150 '${htmlMember.surroundingDeclaration.simpleName}.' | 129 '${htmlMember.surroundingDeclaration.simpleName}.' |
151 '${htmlMember.simpleName} has no corresponding ' | 130 '${htmlMember.simpleName} has no corresponding ' |
152 '$HTML_LIBRARY_NAME member.'); | 131 '$HTML_LIBRARY_NAME member.'); |
153 } | 132 } |
154 | 133 |
155 if (htmlMember == null) return; | 134 if (htmlMember == null) return; |
156 if (!domMembers.isEmpty()) { | 135 if (!domMembers.isEmpty()) { |
157 htmlToDom[htmlMember.qualifiedName] = domMembers; | 136 htmlToDom[htmlMember.qualifiedName] = domMembers; |
158 } | 137 } |
159 domMembers.forEach((m) => | 138 domMembers.forEach((m) => |
160 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); | 139 domToHtml.putIfAbsent(m, () => new Set()).add(htmlMember)); |
161 } | 140 } |
162 | 141 |
163 /** | 142 /** |
164 * Returns the `dart:dom_deprecated` [Type]s that correspond to | 143 * Returns the `@domName` type values that correspond to |
165 * [htmlType] from `dart:html`. This can be the empty list if no | 144 * [htmlType] from `dart:html`. This can be the empty list if no |
166 * correspondence is found. | 145 * correspondence is found. |
167 */ | 146 */ |
168 List<InterfaceMirror> htmlToDomTypes(InterfaceMirror htmlType) { | 147 List<String> htmlToDomTypes(InterfaceMirror htmlType) { |
169 if (htmlType.simpleName == null) return []; | 148 if (htmlType.simpleName == null) return []; |
170 final tags = _getTags(comments.find(htmlType.location)); | 149 final tags = _getTags(comments.find(htmlType.location)); |
171 if (tags.containsKey('domName')) { | 150 if (tags.containsKey('domName')) { |
172 var domNames = <String>[]; | 151 var domNames = <String>[]; |
173 for (var s in tags['domName'].split(',')) { | 152 for (var s in tags['domName'].split(',')) { |
174 domNames.add(s.trim()); | 153 domNames.add(s.trim()); |
175 } | 154 } |
176 if (domNames.length == 1 && domNames[0] == 'none') return []; | 155 if (domNames.length == 1 && domNames[0] == 'none') return <String>[]; |
177 var domTypes = <InterfaceMirror>[]; | 156 return domNames; |
178 for (var domName in domNames) { | |
179 final domType = findMirror(dom.types, domName); | |
180 if (domType == null) { | |
181 warn('no $DOM_LIBRARY_NAME type named $domName'); | |
182 } else { | |
183 domTypes.add(domType); | |
184 } | |
185 } | |
186 return domTypes; | |
187 } | 157 } |
188 return <InterfaceMirror>[]; | 158 return <String>[]; |
189 } | 159 } |
190 | 160 |
191 /** | 161 /** |
192 * Returns the `dart:dom_deprecated` [Member]s that correspond to | 162 * Returns the `@domName` member values that correspond to |
193 * [htmlMember] from `dart:html`. This can be the empty set if no | 163 * [htmlMember] from `dart:html`. This can be the empty set if no |
194 * correspondence is found. [domTypes] are the | 164 * correspondence is found. [domTypes] are the |
195 * `dart:dom_deprecated` [Type]s that correspond to [implMember]'s | 165 * `@domName` type values that correspond to [htmlMember]'s |
196 * defining [Type]. | 166 * defining type. |
197 */ | 167 */ |
198 Set<MemberMirror> htmlToDomMembers(MemberMirror htmlMember, | 168 Set<String> htmlToDomMembers(MemberMirror htmlMember, List<String> domTypes) { |
199 List<InterfaceMirror> domTypes) { | |
200 if (htmlMember.isPrivate) return new Set(); | 169 if (htmlMember.isPrivate) return new Set(); |
201 final tags = _getTags(comments.find(htmlMember.location)); | 170 final tags = _getTags(comments.find(htmlMember.location)); |
202 if (tags.containsKey('domName')) { | 171 if (tags.containsKey('domName')) { |
203 var domNames = <String>[]; | 172 var domNames = <String>[]; |
204 for (var s in tags['domName'].split(',')) { | 173 for (var s in tags['domName'].split(',')) { |
205 domNames.add(s.trim()); | 174 domNames.add(s.trim()); |
206 } | 175 } |
207 if (domNames.length == 1 && domNames[0] == 'none') return new Set(); | 176 if (domNames.length == 1 && domNames[0] == 'none') return new Set(); |
208 final members = new Set(); | 177 final members = new Set(); |
209 domNames.forEach((name) { | 178 domNames.forEach((name) { |
210 var nameMembers = _membersFromName(name, domTypes); | 179 var nameMembers = _membersFromName(name, domTypes); |
211 if (nameMembers.isEmpty()) { | 180 if (nameMembers.isEmpty()) { |
212 if (name.contains('.')) { | 181 if (name.contains('.')) { |
213 warn('no member $name'); | 182 warn('no member $name'); |
214 } else { | 183 } else { |
215 final options = <String>[]; | 184 final options = <String>[]; |
216 for (var t in domTypes) { | 185 for (var t in domTypes) { |
217 options.add('${t.simpleName}.${name}'); | 186 options.add('$t.$name'); |
218 } | 187 } |
219 Strings.join(options, ' or '); | 188 Strings.join(options, ' or '); |
220 warn('no member $options'); | 189 warn('no member $options'); |
221 } | 190 } |
222 } | 191 } |
223 members.addAll(nameMembers); | 192 members.addAll(nameMembers); |
224 }); | 193 }); |
225 return members; | 194 return members; |
226 } | 195 } |
227 | 196 |
228 return new Set(); | 197 return new Set(); |
229 } | 198 } |
230 | 199 |
231 /** | 200 /** |
232 * Returns the `dart:dom_deprecated` [Member]s that are indicated by | 201 * Returns the `@domName` strings that are indicated by |
233 * [name]. [name] can be either an unqualified member name | 202 * [name]. [name] can be either an unqualified member name |
234 * (e.g. `createElement`), in which case it's treated as the name of | 203 * (e.g. `createElement`), in which case it's treated as the name of |
235 * a member of one of [defaultTypes], or a fully-qualified member | 204 * a member of one of [defaultTypes], or a fully-qualified member |
236 * name (e.g. `Document.createElement`), in which case it's looked | 205 * name (e.g. `Document.createElement`), in which case it's treated as a |
237 * up in `dart:dom_deprecated` and [defaultTypes] is ignored. | 206 * member of the @domName element (`Document` in this case). |
238 */ | 207 */ |
239 Set<MemberMirror> _membersFromName(String name, | 208 Set<String> _membersFromName(String name, List<String> defaultTypes) { |
240 List<InterfaceMirror> defaultTypes) { | |
241 if (!name.contains('.', 0)) { | 209 if (!name.contains('.', 0)) { |
242 if (defaultTypes.isEmpty()) { | 210 if (defaultTypes.isEmpty()) { |
243 warn('no default type for ${name}'); | 211 warn('no default type for $name'); |
244 return new Set(); | 212 return new Set(); |
245 } | 213 } |
246 final members = new Set<MemberMirror>(); | 214 final members = new Set<String>(); |
247 defaultTypes.forEach((t) { | 215 defaultTypes.forEach((t) { members.add('$t.$name'); }); |
248 MemberMirror member = findMirror(t.declaredMembers, name); | |
249 if (member !== null) { | |
250 members.add(member); | |
251 } | |
252 }); | |
253 return members; | 216 return members; |
254 } | 217 } |
255 | 218 |
256 final splitName = name.split('.'); | 219 if (name.split('.').length != 2) { |
257 if (splitName.length != 2) { | |
258 warn('invalid member name ${name}'); | 220 warn('invalid member name ${name}'); |
259 return new Set(); | 221 return new Set(); |
260 } | 222 } |
261 | 223 return new Set.from([name]); |
262 var typeName = splitName[0]; | |
263 | |
264 InterfaceMirror type = findMirror(dom.types, typeName); | |
265 if (type == null) return new Set(); | |
266 | |
267 MemberMirror member = findMirror(type.declaredMembers, splitName[1]); | |
268 if (member == null) return new Set(); | |
269 | |
270 return new Set.from([member]); | |
271 } | 224 } |
272 | 225 |
273 /** | 226 /** |
274 * Extracts a [Map] from tag names to values from [comment], which is parsed | 227 * Extracts a [Map] from tag names to values from [comment], which is parsed |
275 * from a Dart source file via dartdoc. Tags are of the form `@NAME VALUE`, | 228 * from a Dart source file via dartdoc. Tags are of the form `@NAME VALUE`, |
276 * where `NAME` is alphabetic and `VALUE` can contain any character other than | 229 * where `NAME` is alphabetic and `VALUE` can contain any character other than |
277 * `;`. Multiple tags can be separated by semicolons. | 230 * `;`. Multiple tags can be separated by semicolons. |
278 * | 231 * |
279 * At time of writing, the only tag that's used is `@domName`. | 232 * At time of writing, the only tag that's used is `@domName`. |
280 */ | 233 */ |
281 Map<String, String> _getTags(String comment) { | 234 Map<String, String> _getTags(String comment) { |
282 if (comment == null) return const <String, String>{}; | 235 if (comment == null) return const <String, String>{}; |
283 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); | 236 final re = const RegExp("@([a-zA-Z]+) ([^;]+)(?:;|\$)"); |
284 final tags = <String, String>{}; | 237 final tags = <String, String>{}; |
285 for (var m in re.allMatches(comment.trim())) { | 238 for (var m in re.allMatches(comment.trim())) { |
286 tags[m[1]] = m[2]; | 239 tags[m[1]] = m[2]; |
287 } | 240 } |
288 return tags; | 241 return tags; |
289 } | 242 } |
290 } | 243 } |
OLD | NEW |