Skip to content

Commit 1142ec3

Browse files
committed
fixup! refactor: migrate from TypeScript to JavaScript
1 parent 8900976 commit 1142ec3

35 files changed

+809
-359
lines changed

lint/common/overlap.js

Lines changed: 22 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,23 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
4+
/**
5+
* @import { BrowserName, SupportStatement, SimpleSupportStatement } from '../../types/types.js'
6+
* @import { Logger } from '../utils.js'
7+
*/
8+
39
import chalk from 'chalk-template';
410
import { compareVersions } from 'compare-versions';
511

612
import { createStatementGroupKey } from '../utils.js';
713
import compareStatements from '../../scripts/lib/compare-statements.js';
814
/**
915
* Groups statements by group key.
10-
* @param data The support statements to group.
11-
* @returns the statement groups
16+
* @param {SimpleSupportStatement[]} data The support statements to group.
17+
* @returns {Map<string, SimpleSupportStatement[]>} the statement groups
1218
*/
1319
const groupByStatementKey = (data) => {
20+
/** @type {Map<string, SimpleSupportStatement[]>} */
1421
const groups = new Map();
1522
for (const support of data) {
1623
const key = createStatementGroupKey(support);
@@ -25,10 +32,11 @@ const groupByStatementKey = (data) => {
2532
};
2633
/**
2734
* Formats a support statement as a simplified JSON-like version range.
28-
* @param support The statement to format
29-
* @returns The formatted range
35+
* @param {SimpleSupportStatement} support The statement to format
36+
* @returns {string} The formatted range
3037
*/
3138
const formatRange = (support) => {
39+
/** @type {string[]} */
3240
const result = [];
3341
if (support.version_added) {
3442
result.push(`added: ${support.version_added}`);
@@ -40,12 +48,12 @@ const formatRange = (support) => {
4048
};
4149
/**
4250
* Process data and check to make sure there aren't support statements whose version ranges overlap.
43-
* @param data The data to test
44-
* @param browser The name of the browser
51+
* @param {SupportStatement} data The data to test
52+
* @param {BrowserName} browser The name of the browser
4553
* @param {object} options The check options
46-
* @param [options.logger] The logger to output errors to
47-
* @param [options.fix] Whether the statements should be fixed (if possible)
48-
* @returns the data (with fixes, if specified)
54+
* @param {Logger} [options.logger] The logger to output errors to
55+
* @param {boolean} [options.fix] Whether the statements should be fixed (if possible)
56+
* @returns {SupportStatement} the data (with fixes, if specified)
4957
*/
5058
export const checkOverlap = (data, browser, { logger, fix = false }) => {
5159
if (!Array.isArray(data)) {
@@ -57,8 +65,8 @@ export const checkOverlap = (data, browser, { logger, fix = false }) => {
5765
for (const [groupKey, groupData] of groups.entries()) {
5866
const statements = groupData.slice().sort(compareStatements).reverse();
5967
for (let i = 0; i < statements.length - 1; i++) {
60-
const current = statements.at(i);
61-
const next = statements.at(i + 1);
68+
const current = /** @type {SimpleSupportStatement} */ (statements.at(i));
69+
const next = /** @type {SimpleSupportStatement} */ (statements.at(i + 1));
6270
if (!statementsOverlap(current, next)) {
6371
continue;
6472
}
@@ -85,9 +93,9 @@ export const checkOverlap = (data, browser, { logger, fix = false }) => {
8593
};
8694
/**
8795
* Checks if the support statements overlap in terms of their version ranges.
88-
* @param current the current statement.
89-
* @param next the chronologically following statement.
90-
* @returns Whether the support statements overlap.
96+
* @param {SimpleSupportStatement} current the current statement.
97+
* @param {SimpleSupportStatement} next the chronologically following statement.
98+
* @returns {boolean} Whether the support statements overlap.
9199
*/
92100
const statementsOverlap = (current, next) => {
93101
if (typeof current.version_removed === 'string') {

lint/fixer/browser-order.js

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,47 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
4+
/**
5+
* @import { BrowserName, CompatStatement, SupportBlock } from '../../types/types.js'
6+
*/
7+
38
/**
49
* Return a new "support_block" object whose first-level properties
510
* (browser names) have been ordered according to Array.prototype.sort,
611
* and so will be stringified in that order as well. This relies on
712
* guaranteed "own" property ordering, which is insertion order for
813
* non-integer keys (which is our case).
9-
* @param key The key of the object
10-
* @param value The value of the key
11-
* @returns Value with sorting applied
14+
* @param {string} key The key of the object
15+
* @param {CompatStatement} value The value of the key
16+
* @returns {CompatStatement} Value with sorting applied
1217
*/
1318
export const orderSupportBlock = (key, value) => {
1419
if (key === '__compat') {
15-
value.support = Object.keys(value.support)
20+
value.support = /** @type {BrowserName[]} */ (Object.keys(value.support))
1621
.sort()
17-
.reduce((result, key) => {
18-
result[key] = value.support[key];
19-
return result;
20-
}, {});
22+
.reduce(
23+
/**
24+
* Accumulate browser support in sorted order
25+
* @param {SupportBlock} result - Accumulator object
26+
* @param {BrowserName} key - Browser name
27+
* @returns {SupportBlock} Updated accumulator
28+
*/
29+
(result, key) => {
30+
result[key] = value.support[key];
31+
return result;
32+
},
33+
/** @type {SupportBlock} */ ({}),
34+
);
2135
}
2236
return value;
2337
};
2438
/**
2539
* Perform a fix of the browser order of a __compat.support block within
2640
* all the data in a specified file. The function will then automatically
2741
* write any needed changes back into the file.
28-
* @param filename The path to the file to fix in-place
29-
* @param actual The current content of the file
30-
* @returns expected content of the file
42+
* @param {string} filename The path to the file to fix in-place
43+
* @param {string} actual The current content of the file
44+
* @returns {string} expected content of the file
3145
*/
3246
const fixBrowserOrder = (filename, actual) => {
3347
if (filename.includes('/browsers/')) {

lint/fixer/common-errors.js

Lines changed: 20 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,38 +1,48 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
4+
/**
5+
* @import { CompatStatement, SimpleSupportStatement } from '../../types/types.js'
6+
*/
7+
38
import { walk } from '../../utils/index.js';
9+
410
/**
511
* Fixes common errors in CompatStatements.
612
*
713
* - Replaces `browser: { version_added: "mirror" }` with `browser: "mirror"`
814
* - Wraps `browser: false` with `browser: `{ version_added: false }`
9-
* @param compat The compat statement to fix
15+
* @param {CompatStatement} compat The compat statement to fix
16+
* @returns {void}
1017
*/
1118
export const fixCommonErrorsInCompatStatement = (compat) => {
1219
for (const browser of Object.keys(compat.support)) {
13-
if (compat.support[browser] === false) {
14-
compat.support[browser] = {
20+
const browserName =
21+
/** @type {import('../../types/types.js').BrowserName} */ (browser);
22+
const supportStatement = /** @type {any} */ (compat.support[browserName]);
23+
if (supportStatement === false) {
24+
compat.support[browserName] = {
1525
version_added: false,
1626
};
1727
} else if (
18-
typeof compat.support[browser] === 'object' &&
19-
JSON.stringify(compat.support[browser]) === '{"version_added":"mirror"}'
28+
typeof supportStatement === 'object' &&
29+
JSON.stringify(supportStatement) === '{"version_added":"mirror"}'
2030
) {
21-
compat.support[browser] = 'mirror';
31+
/** @type {any} */ (compat.support)[browserName] = 'mirror';
2232
}
2333
if (
2434
browser == 'ie' &&
25-
JSON.stringify(compat.support[browser]) === '{"version_added":false}'
35+
JSON.stringify(supportStatement) === '{"version_added":false}'
2636
) {
2737
Reflect.deleteProperty(compat.support, browser);
2838
}
2939
}
3040
};
3141
/**
3242
* Update compat data to 'mirror' if the statement matches mirroring
33-
* @param filename The name of the file to fix
34-
* @param actual The current content of the file
35-
* @returns expected content of the file
43+
* @param {string} filename The name of the file to fix
44+
* @param {string} actual The current content of the file
45+
* @returns {string} expected content of the file
3646
*/
3747
const fixCommonErrors = (filename, actual) => {
3848
if (filename.includes('/browsers/')) {

lint/fixer/descriptions.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
34
import testDescriptions, { processData } from '../linter/test-descriptions.js';
45
import walk from '../../utils/walk.js';
6+
57
/**
68
* Fixes issues with descriptions
7-
* @param filename The filename containing compatibility info
8-
* @param actual The current content of the file
9-
* @returns expected content of the file
9+
* @param {string} filename The filename containing compatibility info
10+
* @param {string} actual The current content of the file
11+
* @returns {string} expected content of the file
1012
*/
1113
const fixDescriptions = (filename, actual) => {
1214
if (filename.includes('/browsers/')) {

lint/fixer/feature-order.js

Lines changed: 25 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,33 +1,48 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
4+
/**
5+
* @import { Identifier } from '../../types/types.js'
6+
*/
7+
38
import compareFeatures from '../../scripts/lib/compare-features.js';
9+
410
/**
511
* Return a new feature object whose first-level properties have been
612
* ordered according to Array.prototype.sort, and so will be
713
* stringified in that order as well. This relies on guaranteed "own"
814
* property ordering, which is insertion order for non-integer keys
915
* (which is our case).
10-
* @param _ The key in the object
11-
* @param value The value of the key
12-
* @returns The new value
16+
* @param {string} _ The key in the object
17+
* @param {Identifier} value The value of the key
18+
* @returns {Identifier} The new value
1319
*/
1420
export const orderFeatures = (_, value) => {
1521
if (value instanceof Object && '__compat' in value) {
1622
value = Object.keys(value)
1723
.sort(compareFeatures)
18-
.reduce((result, key) => {
19-
result[key] = value[key];
20-
return result;
21-
}, {});
24+
.reduce(
25+
/**
26+
* Accumulate features in sorted order
27+
* @param {Identifier} result - Accumulator object
28+
* @param {string} key - Feature key
29+
* @returns {Identifier} Updated accumulator
30+
*/
31+
(result, key) => {
32+
result[key] = value[key];
33+
return result;
34+
},
35+
/** @type {Identifier} */ ({}),
36+
);
2237
}
2338
return value;
2439
};
2540
/**
2641
* Perform a fix of feature order within all the data in a specified file.
2742
* The function will then automatically write any needed changes back into the file.
28-
* @param filename The filename to perform fix upon
29-
* @param actual The current content of the file
30-
* @returns expected content of the file
43+
* @param {string} filename The filename to perform fix upon
44+
* @param {string} actual The current content of the file
45+
* @returns {string} expected content of the file
3146
*/
3247
const fixFeatureOrder = (filename, actual) => {
3348
if (filename.includes('/browsers/')) {

lint/fixer/flags.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,20 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
4+
/**
5+
* @import { BrowserName, SupportStatement, SimpleSupportStatement } from '../../types/types.js'
6+
*/
7+
38
import testFlags, {
49
isIrrelevantFlagData,
510
getBasicSupportStatement,
611
} from '../linter/test-flags.js';
712
import walk from '../../utils/walk.js';
13+
814
/**
915
* Removes irrelevant flags from the compatibility data
10-
* @param supportData The compatibility statement to test
11-
* @returns The compatibility statement with all of the flags removed
16+
* @param {SupportStatement} supportData The compatibility statement to test
17+
* @returns {SupportStatement} The compatibility statement with all of the flags removed
1218
*/
1319
export const removeIrrelevantFlags = (supportData) => {
1420
if (typeof supportData === 'string') {
@@ -20,6 +26,7 @@ export const removeIrrelevantFlags = (supportData) => {
2026
}
2127
return supportData;
2228
}
29+
/** @type {SimpleSupportStatement[]} */
2330
const result = [];
2431
const basicSupport = getBasicSupportStatement(supportData);
2532
for (const statement of supportData) {
@@ -33,13 +40,15 @@ export const removeIrrelevantFlags = (supportData) => {
3340
if (result.length == 1) {
3441
return result[0];
3542
}
36-
return result;
43+
return /** @type {[SimpleSupportStatement, SimpleSupportStatement, ...SimpleSupportStatement[]]} */ (
44+
result
45+
);
3746
};
3847
/**
3948
* Removes irrelevant flags from the compatibility data of a specified file
40-
* @param filename The filename containing compatibility info
41-
* @param actual The current content of the file
42-
* @returns expected content of the file
49+
* @param {string} filename The filename containing compatibility info
50+
* @param {string} actual The current content of the file
51+
* @returns {string} expected content of the file
4352
*/
4453
const fixFlags = (filename, actual) => {
4554
if (filename.includes('/browsers/')) {
@@ -51,8 +60,11 @@ const fixFlags = (filename, actual) => {
5160
if (testFlags.exceptions?.includes(feature.path)) {
5261
continue;
5362
}
54-
for (const [browser, supportData] of Object.entries(
55-
feature.compat.support,
63+
for (const [
64+
browser,
65+
supportData,
66+
] of /** @type {[BrowserName, SupportStatement][]} */ (
67+
Object.entries(feature.compat.support)
5668
)) {
5769
feature.data.__compat.support[browser] =
5870
removeIrrelevantFlags(supportData);

lint/fixer/links.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
34
import { IS_WINDOWS } from '../utils.js';
45
import { processData } from '../linter/test-links.js';
6+
57
/**
68
* Fix issues with links throughout the BCD files
7-
* @param filename The name of the file to fix
8-
* @param actual The current content of the file
9-
* @returns expected content of the file
9+
* @param {string} filename The name of the file to fix
10+
* @param {string} actual The current content of the file
11+
* @returns {Promise<string>} expected content of the file
1012
*/
1113
const fixLinks = async (filename, actual) => {
1214
if (filename.includes('/browsers/')) {

lint/fixer/mdn-urls.js

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,14 @@
11
/* This file is a part of @mdn/browser-compat-data
22
* See LICENSE file for more information. */
3+
34
import testMDNURLs, { processData } from '../linter/test-mdn-urls.js';
45
import walk from '../../utils/walk.js';
6+
57
/**
68
* Fixes issues with MDN URLs
7-
* @param filename The filename containing compatibility info
8-
* @param actual The current content of the file
9-
* @returns expected content of the file
9+
* @param {string} filename The filename containing compatibility info
10+
* @param {string} actual The current content of the file
11+
* @returns {Promise<string>} expected content of the file
1012
*/
1113
const fixMDNURLs = async (filename, actual) => {
1214
if (filename.includes('/browsers/')) {

0 commit comments

Comments
 (0)