eslint(unsupported-apis): improve checking as outlined in #4792

This commit is contained in:
glacambre 2023-10-28 18:44:00 +02:00
parent 5635b91dee
commit 5f860f36de

View file

@ -1,15 +1,21 @@
const bcd = require('@mdn/browser-compat-data'); const bcd = require('@mdn/browser-compat-data');
const api = bcd.webextensions.api; const api = bcd.webextensions.api;
const supported_browsers = ["firefox", "chrome"]; const supported_browsers = ["firefox", "chrome"];
const minimalSupportedFirefoxVersion = 114;
function propertyNameOrValue(n) {
return (n.property.type == "Literal" ? n.property.value : n.property.name)
}
function detectBrowserUsage(context, node) { function detectBrowserUsage(context, node) {
let localApi = api; let localApi = api;
let fullName = []; let fullName = [];
while (node.type == "MemberExpression" && node.property.name in localApi) { while (node.type == "MemberExpression" && propertyNameOrValue(node) in localApi) {
const n = node; const n = node;
node = node.parent; node = node.parent;
fullName.push(n.property.name); let name = propertyNameOrValue(n);
localApi = localApi[n.property.name]; fullName.push(name);
localApi = localApi[name];
if (!localApi.__compat) { if (!localApi.__compat) {
continue; continue;
} }
@ -21,8 +27,21 @@ function detectBrowserUsage(context, node) {
messageId: "unsupportedApis", messageId: "unsupportedApis",
data: { data: {
name: browser, name: browser,
api: fullName.join("."),
} }
}); });
} else {
const version = Number(support[browser].version_added);
if (!isNaN(version) && version > minimalSupportedFirefoxVersion) {
context.report({
node: n,
messageId: "apiTooRecent",
data: {
api: fullName.join("."),
version: minimalSupportedFirefoxVersion
}
});
}
} }
} }
} }
@ -31,12 +50,14 @@ function detectBrowserUsage(context, node) {
module.exports = { module.exports = {
meta: { meta: {
messages: { messages: {
unsupportedApis: "Unsupported on '{{ name }}'" unsupportedApis: "{{ api }} unsupported on '{{ name }}'",
apiTooRecent: "{{ api }} is not supported on firefox {{ version }}",
} }
}, },
create(context) { create(context) {
return { return {
'MemberExpression[object.name="browser"]': (n) => detectBrowserUsage(context, n), 'MemberExpression[object.name="browser"]': (n) => detectBrowserUsage(context, n),
'MemberExpression[object.name="browserBg"]': (n) => detectBrowserUsage(context, n),
}; };
} }
}; };