|
1 |
| -class RequestedModule { |
2 |
| - constructor(name, version) { |
3 |
| - this.name = name; |
4 |
| - this.version = Utilities.normalize_version(version); |
5 |
| - } |
6 |
| -} |
7 |
| - |
8 |
| -class ModuleListing { |
9 |
| - constructor(name, contents) { |
10 |
| - this.name = name; |
11 |
| - this.contents = contents; |
12 |
| - } |
13 |
| -} |
14 |
| - |
15 | 1 | class DBEntry {
|
16 | 2 | constructor(library_name, library_version, test_status, notes) {
|
17 | 3 | this.name = library_name;
|
18 |
| - this.version = Utilities.normalize_version(library_version); |
| 4 | + this.version = library_version.replace(/^v(\d.*)/, '$1').replace(/ and /g, ", "); |
19 | 5 | this.test_status = parseInt(test_status);
|
20 | 6 | this._notes = notes;
|
21 | 7 | }
|
@@ -60,259 +46,46 @@ class DB {
|
60 | 46 | this.language = language;
|
61 | 47 |
|
62 | 48 | const lines = db_contents.split('\n');
|
| 49 | + let any_versions = {} |
63 | 50 |
|
64 |
| - for (let l in lines) { |
65 |
| - if (!lines[l]) { |
| 51 | + for (const line of lines) { |
| 52 | + if (!line) { |
66 | 53 | continue;
|
67 | 54 | }
|
68 |
| - let [name, version, test_status, ...notes] = lines[l].split(','); |
| 55 | + let [name, version, test_status, ...notes] = line.split(','); |
69 | 56 | const entry = new DBEntry(name, version, test_status, notes.join(','));
|
70 | 57 |
|
71 |
| - if (!(entry.name in this.db)) { |
72 |
| - this.db[entry.name] = {}; |
| 58 | + this.db[entry.name] ||= {}; |
| 59 | + this.db[entry.name][entry.version] = merge_entries(entry, this.db[entry.name][entry.version]); |
| 60 | + |
| 61 | + if (entry.version == "any") { |
| 62 | + any_versions[entry.name] = this.db[entry.name][entry.version]; |
73 | 63 | }
|
| 64 | + } |
| 65 | + |
| 66 | + for (const name in any_versions) { |
| 67 | + for (const version in this.db[name]) { |
| 68 | + this.db[name][version] = merge_entries(any_versions[name], this.db[name][version]); |
| 69 | + } |
| 70 | + } |
74 | 71 |
|
75 |
| - for (const v of [entry.version, Utilities.approximate_recommendation(entry.version)]) { |
76 |
| - let previous_entry = this.db[entry.name][v]; |
77 |
| - if (previous_entry && !previous_entry.notes.includes(entry.notes)) { |
| 72 | + function merge_entries(entry, previous_entry) { |
| 73 | + if (previous_entry) { |
| 74 | + if (!notes_overlap(previous_entry.notes, entry.notes)) { |
78 | 75 | if (previous_entry.is_test_percentage() && previous_entry.has_no_test_results()) {
|
79 | 76 | previous_entry.notes = entry.notes;
|
80 | 77 | } else {
|
81 | 78 | previous_entry.notes = entry.notes + " " + previous_entry.notes;
|
82 | 79 | }
|
83 |
| - } else { |
84 |
| - this.db[entry.name][v] = entry; |
85 |
| - } |
86 |
| - } |
87 |
| - } |
88 |
| - } |
89 |
| - |
90 |
| - lookup(requested_name, requested_version, print_missing) { |
91 |
| - const ret = []; |
92 |
| - |
93 |
| - if (requested_name in this.db) { |
94 |
| - if (requested_version === undefined) { |
95 |
| - const versions = this.db[requested_name]; |
96 |
| - for (let version in versions) { |
97 |
| - if (!version.startsWith('~')) { |
98 |
| - const entry = versions[version]; |
99 |
| - ret.push([entry.name, version, entry.test_status, entry.notes]); |
100 |
| - } |
101 | 80 | }
|
| 81 | + return previous_entry; |
102 | 82 | } else {
|
103 |
| - if (requested_version in this.db[requested_name]) { |
104 |
| - const entry = this.db[requested_name][requested_version]; |
105 |
| - ret.push([entry.name, entry.version, entry.test_status, entry.notes]); |
106 |
| - } else { |
107 |
| - const semver_match = Utilities.approximate_recommendation(requested_version); |
108 |
| - if (semver_match in this.db[requested_name]) { |
109 |
| - const entry = this.db[requested_name][semver_match]; |
110 |
| - ret.push([entry.name, entry.version, entry.test_status, entry.notes]); |
111 |
| - } else { |
112 |
| - ret.push([requested_name, requested_version, 'unknown', undefined]); |
113 |
| - } |
114 |
| - } |
115 |
| - } |
116 |
| - } else { |
117 |
| - if (print_missing) { |
118 |
| - ret.push([requested_name, '*', 'library not yet tested', undefined]); |
| 83 | + return entry; |
119 | 84 | }
|
120 | 85 | }
|
121 | 86 |
|
122 |
| - // In the event of multiple versions for a module, sort the results from highest version to lowest. |
123 |
| - ret.sort(function(a, b) { |
124 |
| - return b[1].localeCompare(a[1]); |
125 |
| - }); |
126 |
| - |
127 |
| - return ret; |
128 |
| - } |
129 |
| - |
130 |
| - lookup_module(module, print_missing) { |
131 |
| - return this.lookup(module.name, module.version, print_missing); |
132 |
| - } |
133 |
| - |
134 |
| -} |
135 |
| - |
136 |
| -class Utilities { |
137 |
| - static normalize_version(version) { |
138 |
| - if (version === undefined) { |
139 |
| - return undefined; |
140 |
| - } else { |
141 |
| - return version.replace(/^v(\d.*)/, '$1').replace(/ and /g, ", "); |
142 |
| - } |
143 |
| - } |
144 |
| - |
145 |
| - static version_segments(version) { |
146 |
| - if (version === undefined) { |
147 |
| - return undefined; |
148 |
| - } |
149 |
| - |
150 |
| - const string_parts = version.match(/[0-9]+|[a-z]+/ig); |
151 |
| - |
152 |
| - // The result of the regexp match will be an array of strings. We would like to be |
153 |
| - // able to perform numeric comparisons on the parts corresponding to integer values, |
154 |
| - // so convert them here. |
155 |
| - return string_parts.map(function(e) { |
156 |
| - let x = parseInt(e); |
157 |
| - |
158 |
| - if (isNaN(x)) { |
159 |
| - return e; |
160 |
| - } else { |
161 |
| - return x; |
162 |
| - } |
163 |
| - }); |
164 |
| - } |
165 |
| - |
166 |
| - static approximate_recommendation(version) { |
167 |
| - if (version === undefined) { |
168 |
| - return undefined; |
169 |
| - } |
170 |
| - |
171 |
| - const segments = this.version_segments(version); |
172 |
| - |
173 |
| - while (segments.some(function(e) { typeof(e) === 'string' })) { |
174 |
| - segments.pop(); |
175 |
| - } |
176 |
| - |
177 |
| - while (segments.length > 2) { |
178 |
| - segments.pop(); |
| 87 | + function notes_overlap(notes1, notes2) { |
| 88 | + return notes1.replace(/<[^>]+>/, "").includes(notes2.replace(/<[^>]+>/, "")); |
179 | 89 | }
|
180 |
| - |
181 |
| - while (segments.length < 2) { |
182 |
| - segments.push(0); |
183 |
| - } |
184 |
| - |
185 |
| - return `~> ${segments.join('.')}` |
186 | 90 | }
|
187 | 91 | }
|
188 |
| - |
189 |
| -class DependencyFileProcessor { |
190 |
| - static handle_gemfile(db, contents) { |
191 |
| - if (db.language !== 'ruby') { |
192 |
| - return []; |
193 |
| - } |
194 |
| - |
195 |
| - const r = /\n (\S+?) \((.+?)\)/g; |
196 |
| - let match; |
197 |
| - const queries = []; |
198 |
| - |
199 |
| - while (match = r.exec(contents)) { |
200 |
| - queries.push(match.slice(1, 3)); |
201 |
| - } |
202 |
| - |
203 |
| - queries.sort(function(a, b) { |
204 |
| - return a[0].localeCompare(b[0]); |
205 |
| - }); |
206 |
| - |
207 |
| - let results = []; |
208 |
| - for (let [name, version] of queries) { |
209 |
| - results = results.concat(db.lookup(name, version, true)); |
210 |
| - } |
211 |
| - |
212 |
| - return results; |
213 |
| - } |
214 |
| - |
215 |
| - static handle_package_json(db, contents) { |
216 |
| - if (db.language !== 'js') { |
217 |
| - return []; |
218 |
| - } |
219 |
| - |
220 |
| - const json = JSON.parse(contents); |
221 |
| - let queries = []; |
222 |
| - |
223 |
| - for (let name in json['dependencies']) { |
224 |
| - queries.push([name, undefined]); |
225 |
| - } |
226 |
| - |
227 |
| - let results = []; |
228 |
| - for (let [name, version] of queries) { |
229 |
| - results = results.concat(db.lookup(name, version, true)); |
230 |
| - } |
231 |
| - |
232 |
| - return results; |
233 |
| - } |
234 |
| - |
235 |
| - static handle_package_lock(db, contents) { |
236 |
| - if (db.language !== 'js') { |
237 |
| - return []; |
238 |
| - } |
239 |
| - |
240 |
| - const json = JSON.parse(contents); |
241 |
| - let queries = [[json['name'], json['version']]]; |
242 |
| - |
243 |
| - for (let name in json['dependencies']) { |
244 |
| - let metadata = json['dependencies'][name]; |
245 |
| - |
246 |
| - if (metadata['dev'] || metadata['optional']) { |
247 |
| - continue; |
248 |
| - } |
249 |
| - |
250 |
| - queries.push([name, metadata['version']]); |
251 |
| - } |
252 |
| - |
253 |
| - let results = []; |
254 |
| - for (let [name, version] of queries) { |
255 |
| - results = results.concat(db.lookup(name, version, true)); |
256 |
| - } |
257 |
| - |
258 |
| - return results; |
259 |
| - } |
260 |
| - |
261 |
| - static handle_yarn_lock(db, contents) { |
262 |
| - if (db.language != 'js') { |
263 |
| - return []; |
264 |
| - } |
265 |
| - |
266 |
| - const r = /\n(\w.*?)@.+?:\s+?version "(.+?)"/g; |
267 |
| - let match; |
268 |
| - const queries = []; |
269 |
| - |
270 |
| - while (match = r.exec(contents)) { |
271 |
| - queries.push(match.slice(1, 3)); |
272 |
| - } |
273 |
| - |
274 |
| - queries.sort(function(a, b) { |
275 |
| - return a[0].localeCompare(b[0]); |
276 |
| - }); |
277 |
| - |
278 |
| - let results = []; |
279 |
| - for (let [name, version] of queries) { |
280 |
| - results = results.concat(db.lookup(name, version, true)); |
281 |
| - } |
282 |
| - |
283 |
| - return results; |
284 |
| - } |
285 |
| - |
286 |
| - static handle_packrat_lock(db, contents) { |
287 |
| - if (db.language != 'r') { |
288 |
| - return []; |
289 |
| - } |
290 |
| - |
291 |
| - const r = /\nPackage: (\w+)\nSource: (\w+)\nVersion: (.+?)\n/g; |
292 |
| - let match; |
293 |
| - const queries = []; |
294 |
| - |
295 |
| - while (match = r.exec(contents)) { |
296 |
| - queries.push(match.slice(1, 4)); |
297 |
| - } |
298 |
| - |
299 |
| - queries.sort(function(a, b) { |
300 |
| - return a[0].localeCompare(b[0]); |
301 |
| - }); |
302 |
| - |
303 |
| - let results = []; |
304 |
| - for (let [name, repository, version] of queries) { |
305 |
| - results = results.concat(db.lookup(name, version, true)); |
306 |
| - } |
307 |
| - |
308 |
| - return results; |
309 |
| - } |
310 |
| -} |
311 |
| - |
312 |
| -if (typeof module !== 'undefined') { |
313 |
| - module.exports.DB = DB; |
314 |
| - module.exports.DependencyFileProcessor = DependencyFileProcessor; |
315 |
| - module.exports.ModuleListing = ModuleListing; |
316 |
| - module.exports.RequestedModule = RequestedModule; |
317 |
| - module.exports.Utilities = Utilities; |
318 |
| -} |
0 commit comments