mirror of
https://github.com/vale981/tridactyl
synced 2025-03-04 09:01:39 -05:00
Add custom eslint rule detecting uses of potentially unsupported APIs
This commit is contained in:
parent
2b233aa20a
commit
5a143edc4c
3 changed files with 44 additions and 1 deletions
|
@ -34,6 +34,7 @@ module.exports = {
|
||||||
"sonarjs"
|
"sonarjs"
|
||||||
],
|
],
|
||||||
"rules": {
|
"rules": {
|
||||||
|
"unsupported-apis": "warn",
|
||||||
"sonarjs/cognitive-complexity": "off", //"error",
|
"sonarjs/cognitive-complexity": "off", //"error",
|
||||||
"sonarjs/no-duplicate-string": "off",
|
"sonarjs/no-duplicate-string": "off",
|
||||||
"sonarjs/no-unused-collection": "off", //"error", // There seems to be a bug with this rule - exported collections are assumed unused
|
"sonarjs/no-unused-collection": "off", //"error", // There seems to be a bug with this rule - exported collections are assumed unused
|
||||||
|
|
|
@ -6,4 +6,4 @@ else
|
||||||
echo "Warning: shellcheck is not installed, skipping shell scripts"
|
echo "Warning: shellcheck is not installed, skipping shell scripts"
|
||||||
fi
|
fi
|
||||||
yarn run lint
|
yarn run lint
|
||||||
"$(yarn bin)/eslint" --ext .ts .
|
"$(yarn bin)/eslint" --rulesdir custom-eslint-rules --ext .ts .
|
||||||
|
|
42
custom-eslint-rules/unsupported-apis.js
Normal file
42
custom-eslint-rules/unsupported-apis.js
Normal file
|
@ -0,0 +1,42 @@
|
||||||
|
const bcd = require('@mdn/browser-compat-data');
|
||||||
|
const api = bcd.webextensions.api;
|
||||||
|
const supported_browsers = ["firefox", "chrome"];
|
||||||
|
|
||||||
|
function detectBrowserUsage(context, node) {
|
||||||
|
let localApi = api;
|
||||||
|
let fullName = [];
|
||||||
|
while (node.type == "MemberExpression" && node.property.name in localApi) {
|
||||||
|
const n = node;
|
||||||
|
node = node.parent;
|
||||||
|
fullName.push(n.property.name);
|
||||||
|
localApi = localApi[n.property.name];
|
||||||
|
if (!localApi.__compat) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
let support = localApi.__compat.support;
|
||||||
|
for (let browser of supported_browsers) {
|
||||||
|
if (support[browser].version_added === false) {
|
||||||
|
context.report({
|
||||||
|
node: n,
|
||||||
|
messageId: "unsupportedApis",
|
||||||
|
data: {
|
||||||
|
name: browser,
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
module.exports = {
|
||||||
|
meta: {
|
||||||
|
messages: {
|
||||||
|
unsupportedApis: "Unsupported on '{{ name }}'"
|
||||||
|
}
|
||||||
|
},
|
||||||
|
create(context) {
|
||||||
|
return {
|
||||||
|
'MemberExpression[object.name="browser"]': (n) => detectBrowserUsage(context, n),
|
||||||
|
};
|
||||||
|
}
|
||||||
|
};
|
Loading…
Add table
Reference in a new issue