Skip to content

Commit 826650b

Browse files
committed
Clean up the compatibility helper js a bit
1 parent a9fe01b commit 826650b

File tree

1 file changed

+24
-251
lines changed

1 file changed

+24
-251
lines changed
Lines changed: 24 additions & 251 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,7 @@
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-
151
class DBEntry {
162
constructor(library_name, library_version, test_status, notes) {
173
this.name = library_name;
18-
this.version = Utilities.normalize_version(library_version);
4+
this.version = library_version.replace(/^v(\d.*)/, '$1').replace(/ and /g, ", ");
195
this.test_status = parseInt(test_status);
206
this._notes = notes;
217
}
@@ -60,259 +46,46 @@ class DB {
6046
this.language = language;
6147

6248
const lines = db_contents.split('\n');
49+
let any_versions = {}
6350

64-
for (let l in lines) {
65-
if (!lines[l]) {
51+
for (const line of lines) {
52+
if (!line) {
6653
continue;
6754
}
68-
let [name, version, test_status, ...notes] = lines[l].split(',');
55+
let [name, version, test_status, ...notes] = line.split(',');
6956
const entry = new DBEntry(name, version, test_status, notes.join(','));
7057

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];
7363
}
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+
}
7471

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)) {
7875
if (previous_entry.is_test_percentage() && previous_entry.has_no_test_results()) {
7976
previous_entry.notes = entry.notes;
8077
} else {
8178
previous_entry.notes = entry.notes + " " + previous_entry.notes;
8279
}
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-
}
10180
}
81+
return previous_entry;
10282
} 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;
11984
}
12085
}
12186

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(/<[^>]+>/, ""));
17989
}
180-
181-
while (segments.length < 2) {
182-
segments.push(0);
183-
}
184-
185-
return `~> ${segments.join('.')}`
18690
}
18791
}
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

Comments
 (0)