зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1729460 - Extend ESLint rule mozilla/use-services to cover defineLazyServiceGetter(s). r=mossop
Differential Revision: https://phabricator.services.mozilla.com/D124839
This commit is contained in:
Родитель
14aedbb785
Коммит
bd9ada1626
|
@ -13,7 +13,7 @@ module.exports = {
|
|||
},
|
||||
parser: "espree",
|
||||
parserOptions: {
|
||||
ecmaVersion: 10,
|
||||
ecmaVersion: 12,
|
||||
},
|
||||
|
||||
rules: {
|
||||
|
|
|
@ -21,9 +21,51 @@ module.exports = {
|
|||
create(context) {
|
||||
return {
|
||||
CallExpression(node) {
|
||||
if (!node.callee || !node.callee.property) {
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
node.callee.property.type == "Identifier" &&
|
||||
node.callee.property.name == "defineLazyServiceGetter" &&
|
||||
node.arguments.length == 4 &&
|
||||
node.arguments[3].type == "Literal" &&
|
||||
node.arguments[3].value in servicesInterfaceMap
|
||||
) {
|
||||
let serviceName = servicesInterfaceMap[node.arguments[3].value];
|
||||
|
||||
context.report(
|
||||
node,
|
||||
`Use Services.${serviceName} rather than defineLazyServiceGetter.`
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
node.callee.property.type == "Identifier" &&
|
||||
node.callee.property.name == "defineLazyServiceGetters" &&
|
||||
node.arguments.length == 2 &&
|
||||
node.arguments[1].type == "ObjectExpression"
|
||||
) {
|
||||
for (let property of node.arguments[1].properties) {
|
||||
if (
|
||||
property.value.type == "ArrayExpression" &&
|
||||
property.value.elements.length == 2 &&
|
||||
property.value.elements[1].value in servicesInterfaceMap
|
||||
) {
|
||||
let serviceName =
|
||||
servicesInterfaceMap[property.value.elements[1].value];
|
||||
|
||||
context.report(
|
||||
property.value,
|
||||
`Use Services.${serviceName} rather than defineLazyServiceGetters.`
|
||||
);
|
||||
}
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
!node.callee ||
|
||||
!node.callee.property ||
|
||||
node.callee.property.type != "Identifier" ||
|
||||
node.callee.property.name != "getService" ||
|
||||
node.arguments.length != 1 ||
|
||||
|
|
|
@ -16,9 +16,9 @@ const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
|
|||
// Tests
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
function invalidCode(code, name) {
|
||||
let message = `Use Services.${name} rather than getService().`;
|
||||
return { code, errors: [{ message, type: "CallExpression" }] };
|
||||
function invalidCode(code, name, replaces, type = "CallExpression") {
|
||||
let message = `Use Services.${name} rather than ${replaces}.`;
|
||||
return { code, errors: [{ message, type }] };
|
||||
}
|
||||
|
||||
ruleTester.run("use-services", rule, {
|
||||
|
@ -30,11 +30,31 @@ ruleTester.run("use-services", rule, {
|
|||
invalid: [
|
||||
invalidCode(
|
||||
'Cc["@mozilla.org/appshell/window-mediator;1"].getService(Ci.nsIWindowMediator);',
|
||||
"wm"
|
||||
"wm",
|
||||
"getService()"
|
||||
),
|
||||
invalidCode(
|
||||
'Components.classes["@mozilla.org/toolkit/app-startup;1"].getService(Components.interfaces.nsIAppStartup);',
|
||||
"startup"
|
||||
"startup",
|
||||
"getService()"
|
||||
),
|
||||
invalidCode(
|
||||
`XPCOMUtils.defineLazyServiceGetters(this, {
|
||||
uuidGen: ["@mozilla.org/uuid-generator;1", "nsIUUIDGenerator"],
|
||||
});`,
|
||||
"uuid",
|
||||
"defineLazyServiceGetters",
|
||||
"ArrayExpression"
|
||||
),
|
||||
invalidCode(
|
||||
`XPCOMUtils.defineLazyServiceGetter(
|
||||
this,
|
||||
"gELS",
|
||||
"@mozilla.org/eventlistenerservice;1",
|
||||
"nsIEventListenerService"
|
||||
);`,
|
||||
"els",
|
||||
"defineLazyServiceGetter"
|
||||
),
|
||||
],
|
||||
});
|
||||
|
|
Загрузка…
Ссылка в новой задаче