-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathoverlap.js
More file actions
143 lines (122 loc) · 4.32 KB
/
overlap.js
File metadata and controls
143 lines (122 loc) · 4.32 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
/* This file is a part of @mdn/browser-compat-data
* See LICENSE file for more information. */
import { styleText } from 'node:util';
import { compareVersions } from 'compare-versions';
import { createStatementGroupKey } from '../utils.js';
import compareStatements from '../../scripts/lib/compare-statements.js';
/** @import {Logger} from '../utils.js' */
/** @import {BrowserName, InternalSimpleSupportStatement, InternalSupportStatement} from '../../types/index.js' */
/**
* Groups statements by group key.
* @param {InternalSimpleSupportStatement[]} data The support statements to group.
* @returns {Map<string, InternalSimpleSupportStatement[]>} the statement groups
*/
const groupByStatementKey = (data) => {
/** @type {Map<string, InternalSimpleSupportStatement[]>} */
const groups = new Map();
for (const support of data) {
const key = createStatementGroupKey(support);
const group = groups.get(key);
if (group) {
group.push(support);
} else {
groups.set(key, [support]);
}
}
return groups;
};
/**
* Formats a support statement as a simplified JSON-like version range.
* @param {InternalSimpleSupportStatement} support The statement to format
* @returns {string} The formatted range
*/
const formatRange = (support) => {
/** @type {string[]} */
const result = [];
if (support.version_added) {
result.push(`added: ${support.version_added}`);
}
if (support.version_removed) {
result.push(`removed: ${support.version_removed}`);
}
return `{ ${result.join(', ')} }`;
};
/**
* Process data and check to make sure there aren't support statements whose version ranges overlap.
* @param {InternalSupportStatement} data The data to test
* @param {BrowserName} browser The name of the browser
* @param {object} options The check options
* @param {Logger} [options.logger] The logger to output errors to
* @param {boolean} [options.fix] Whether the statements should be fixed (if possible)
* @returns {InternalSupportStatement} the data (with fixes, if specified)
*/
export const checkOverlap = (data, browser, { logger, fix = false }) => {
if (!Array.isArray(data)) {
// If there's only one statement, skip since this is a linter for multiple statements
return data;
}
const filteredData = data.filter((support) => !support.flags);
const groups = groupByStatementKey(filteredData);
for (const [groupKey, groupData] of groups.entries()) {
const statements = groupData.slice().sort(compareStatements).reverse();
for (let i = 0; i < statements.length - 1; i++) {
const current = /** @type {InternalSimpleSupportStatement} */ (
statements.at(i)
);
const next = /** @type {InternalSimpleSupportStatement} */ (
statements.at(i + 1)
);
if (!statementsOverlap(current, next)) {
continue;
}
let fixed = false;
if (fix) {
if (
typeof current.version_removed === 'undefined' &&
next.version_added !== false &&
next.version_added !== 'preview'
) {
current.version_removed = next.version_added;
fixed = true;
}
}
if (!fixed && logger) {
logger.error(
`${styleText('bold', browser)} statements overlap for ${styleText('bold', groupKey)}: ` +
`[${formatRange(next)}, ${formatRange(current)}]`,
);
}
}
}
return data;
};
/**
* Checks if the support statements overlap in terms of their version ranges.
* @param {InternalSimpleSupportStatement} current the current statement.
* @param {InternalSimpleSupportStatement} next the chronologically following statement.
* @returns {boolean} Whether the support statements overlap.
*/
const statementsOverlap = (current, next) => {
if (typeof current.version_removed === 'string') {
// If previous has no removed version, we always have an overlap.
if (next.version_added === 'preview') {
// Feature got re-introduced.
return false;
}
if (
typeof next.version_added === 'string' &&
compareVersions(current.version_removed, next.version_added) <= 0
) {
// No overlap.
return false;
}
} else if (
next.version_added === 'preview' &&
current.partial_implementation === true
) {
// Stable has partial support.
// Preview has full support.
return false;
}
return true;
};