зеркало из https://github.com/mozilla/gecko-dev.git
Bug 1766228 - Add an ESLint rule to ensure that member property accesses of Services are valid. r=Gijs
Differential Revision: https://phabricator.services.mozilla.com/D149393
This commit is contained in:
Родитель
b813d15395
Коммит
8ca4d49d65
|
@ -12,6 +12,8 @@
|
|||
// This fixture is probably doing too much and should be reduced to the minimum
|
||||
// needed to pass the tests.
|
||||
|
||||
/* eslint-disable mozilla/valid-services */
|
||||
|
||||
// Some constants from nsIPrefBranch.idl.
|
||||
const PREF_INVALID = 0;
|
||||
const PREF_STRING = 32;
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
valid-services
|
||||
==============
|
||||
|
||||
Ensures that accesses of the ``Services`` object are valid.
|
||||
|
||||
Examples of incorrect code for this rule:
|
||||
-----------------------------------------
|
||||
|
||||
Assuming ``foo`` is not defined within Services.
|
||||
|
||||
.. code-block:: js
|
||||
|
||||
Services.foo.fn();
|
||||
|
||||
Examples of correct code for this rule:
|
||||
---------------------------------------
|
||||
|
||||
Assuming ``bar`` is defined within Services.
|
||||
|
||||
.. code-block:: js
|
||||
|
||||
Services.bar.fn();
|
|
@ -179,6 +179,7 @@ module.exports = {
|
|||
"mozilla/use-ownerGlobal": "error",
|
||||
"mozilla/use-returnValue": "error",
|
||||
"mozilla/use-services": "error",
|
||||
"mozilla/valid-services": "error",
|
||||
|
||||
// Use [] instead of Array()
|
||||
"no-array-constructor": "error",
|
||||
|
|
|
@ -73,6 +73,7 @@ module.exports = {
|
|||
"use-isInstance": require("./rules/use-isInstance"),
|
||||
"use-returnValue": require("../lib/rules/use-returnValue"),
|
||||
"use-services": require("../lib/rules/use-services"),
|
||||
"valid-services": require("../lib/rules/valid-services"),
|
||||
"var-only-at-top-level": require("../lib/rules/var-only-at-top-level"),
|
||||
},
|
||||
};
|
||||
|
|
|
@ -0,0 +1,59 @@
|
|||
/**
|
||||
* @fileoverview Require use of Services.* rather than getService.
|
||||
*
|
||||
* This Source Code Form is subject to the terms of the Mozilla Public
|
||||
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
||||
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
|
||||
*/
|
||||
|
||||
"use strict";
|
||||
const helpers = require("../helpers");
|
||||
|
||||
module.exports = {
|
||||
meta: {
|
||||
docs: {
|
||||
url:
|
||||
"https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/use-services.html",
|
||||
},
|
||||
type: "problem",
|
||||
},
|
||||
|
||||
create(context) {
|
||||
let servicesInterfaceMap = helpers.servicesData;
|
||||
let serviceAliases = new Set([
|
||||
...Object.values(servicesInterfaceMap),
|
||||
// This is defined only for Android, so most builds won't pick it up.
|
||||
"androidBridge",
|
||||
// These are defined without interfaces and hence are not in the services map.
|
||||
"cpmm",
|
||||
"crashmanager",
|
||||
"mm",
|
||||
"ppmm",
|
||||
// The new xulStore also does not have an interface.
|
||||
"xulStore",
|
||||
]);
|
||||
return {
|
||||
MemberExpression(node) {
|
||||
if (node.computed || node.object.type !== "Identifier") {
|
||||
return;
|
||||
}
|
||||
|
||||
let alias;
|
||||
if (node.object.name == "Services") {
|
||||
alias = node.property.name;
|
||||
} else if (
|
||||
node.property.name == "Services" &&
|
||||
node.parent.type == "MemberExpression"
|
||||
) {
|
||||
alias = node.parent.property.name;
|
||||
} else {
|
||||
return;
|
||||
}
|
||||
|
||||
if (!serviceAliases.has(alias)) {
|
||||
context.report(node, `Unknown Services member property ${alias}`);
|
||||
}
|
||||
},
|
||||
};
|
||||
},
|
||||
};
|
|
@ -0,0 +1,33 @@
|
|||
/* Any copyright is dedicated to the Public Domain.
|
||||
* http://creativecommons.org/publicdomain/zero/1.0/ */
|
||||
|
||||
"use strict";
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Requirements
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
var rule = require("../lib/rules/valid-services");
|
||||
var RuleTester = require("eslint").RuleTester;
|
||||
|
||||
const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 6 } });
|
||||
|
||||
// ------------------------------------------------------------------------------
|
||||
// Tests
|
||||
// ------------------------------------------------------------------------------
|
||||
|
||||
function invalidCode(code, name) {
|
||||
let message = `Unknown Services member property ${name}`;
|
||||
return { code, errors: [{ message }] };
|
||||
}
|
||||
|
||||
ruleTester.run("valid-services", rule, {
|
||||
valid: ["Services.crashmanager", "lazy.Services.crashmanager"],
|
||||
invalid: [
|
||||
invalidCode("Services.foo", "foo"),
|
||||
invalidCode("Services.foo()", "foo"),
|
||||
invalidCode("lazy.Services.foo", "foo"),
|
||||
invalidCode("Services.foo.bar()", "foo"),
|
||||
invalidCode("lazy.Services.foo.bar()", "foo"),
|
||||
],
|
||||
});
|
Загрузка…
Ссылка в новой задаче