OLD | NEW |
| (Empty) |
1 library util; | |
2 | |
3 import 'dart:io'; | |
4 | |
5 Map<String, Map> _allProps; | |
6 | |
7 Map<String, Map> get allProps { | |
8 if (_allProps == null) { | |
9 // Database of expected property names for each type in WebKit. | |
10 _allProps = parse.parse( | |
11 new File('data/dartIdl.json').readAsStringSync()); | |
12 } | |
13 return _allProps; | |
14 } | |
15 | |
16 Set<String> matchedTypes; | |
17 | |
18 /** Returns whether the type has any member matching the specified name. */ | |
19 bool hasAny(String type, String prop) { | |
20 final data = allProps[type]; | |
21 return data['properties'].containsKey(prop) || | |
22 data['methods'].containsKey(prop) || | |
23 data['constants'].containsKey(prop); | |
24 } | |
25 | |
26 /** | |
27 * Return the members from an [entry] as Map of member names to member | |
28 * objects. | |
29 */ | |
30 Map getMembersMap(Map entry) { | |
31 List<Map> rawMembers = entry["members"]; | |
32 final members = {}; | |
33 for (final entry in rawMembers) { | |
34 members[entry['name']] = entry; | |
35 } | |
36 return members; | |
37 } | |
38 | |
39 /** | |
40 * Score entries using similarity heuristics calculated from the observed and | |
41 * expected list of members. We could be much less naive and penalize spurious | |
42 * methods, prefer entries with class level comments, etc. This method is | |
43 * needed becase we extract entries for each of the top search results for | |
44 * each class name and rely on these scores to determine which entry was | |
45 * best. Typically all scores but one will be zero. Multiple pages have | |
46 * non-zero scores when MDN has multiple pages on the same class or pages on | |
47 * similar classes (e.g. HTMLElement and Element), or pages on Mozilla | |
48 * specific classes that are similar to DOM classes (Console). | |
49 */ | |
50 num scoreEntry(Map entry, String type) { | |
51 num score = 0; | |
52 // TODO(jacobr): consider removing skipped entries completely instead of | |
53 // just giving them lower scores. | |
54 if (!entry.containsKey('skipped')) { | |
55 score++; | |
56 } | |
57 if (entry.containsKey("members")) { | |
58 Map members = getMembersMap(entry); | |
59 for (String name in members.keys) { | |
60 if (hasAny(type, name)) { | |
61 score++; | |
62 } | |
63 } | |
64 } | |
65 return score; | |
66 } | |
67 | |
68 /** | |
69 * Given a list of candidates for the documentation for a type, find the one | |
70 * that is the best. | |
71 */ | |
72 Map pickBestEntry(List entries, String type) { | |
73 num bestScore = -1; | |
74 Map bestEntry; | |
75 for (Map entry in entries) { | |
76 if (entry != null) { | |
77 num score = scoreEntry(entry, type); | |
78 if (score > bestScore) { | |
79 bestScore = score; | |
80 bestEntry = entry; | |
81 } | |
82 } | |
83 } | |
84 return bestEntry; | |
85 } | |
86 | |
87 /** | |
88 * Helper for sync creation of a whole file from a string. | |
89 */ | |
90 void writeFileSync(String filename, String data) { | |
91 File f = new File(filename); | |
92 RandomAccessFile raf = f.openSync(FileMode.WRITE); | |
93 raf.writeStringSync(data); | |
94 raf.closeSync(); | |
95 } | |
OLD | NEW |